hikyuu2/hikyuu_pywrap/trade_manage/_TradeManager.cpp
2024-08-15 13:48:22 +08:00

562 lines
23 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* _TradeManager.cpp
*
* Created on: 2013-2-25
* Author: fasiondog
*/
#include <hikyuu/trade_manage/build_in.h>
#include "../pybind_utils.h"
namespace py = pybind11;
using namespace hku;
class PyTradeManagerBase : public TradeManagerBase {
PY_CLONE(PyTradeManagerBase, TradeManagerBase)
public:
using TradeManagerBase::TradeManagerBase;
void _reset() override {
PYBIND11_OVERLOAD(void, TradeManagerBase, _reset, );
}
void updateWithWeight(const Datetime& datetime) override {
PYBIND11_OVERRIDE_NAME(void, TradeManagerBase, "update_with_weight", updateWithWeight,
datetime);
}
double getMarginRate(const Datetime& datetime, const Stock& stock) override {
PYBIND11_OVERRIDE_NAME(double, TradeManagerBase, "get_margin_rate", getMarginRate, datetime,
stock);
}
price_t initCash() const override {
PYBIND11_OVERRIDE_NAME(price_t, TradeManagerBase, "init_cash", initCash);
}
Datetime initDatetime() const override {
PYBIND11_OVERRIDE_NAME(Datetime, TradeManagerBase, "init_datetime", initDatetime);
}
Datetime firstDatetime() const override {
PYBIND11_OVERRIDE_NAME(Datetime, TradeManagerBase, "first_datetime", firstDatetime);
}
Datetime lastDatetime() const override {
PYBIND11_OVERRIDE_NAME(Datetime, TradeManagerBase, "last_datetime", lastDatetime);
}
price_t currentCash() const override {
PYBIND11_OVERRIDE_NAME(price_t, TradeManagerBase, "current_cash", currentCash);
}
price_t currentFrozen() const override {
PYBIND11_OVERRIDE_NAME(price_t, TradeManagerBase, "current_frozen", currentFrozen);
}
price_t cash(const Datetime& datetime, KQuery::KType ktype) override {
PYBIND11_OVERLOAD(price_t, TradeManagerBase, cash, datetime, ktype);
}
bool have(const Stock& stock) const override {
PYBIND11_OVERLOAD(bool, TradeManagerBase, have, stock);
}
bool haveShort(const Stock& stock) const override {
PYBIND11_OVERRIDE_NAME(bool, TradeManagerBase, "have_short", haveShort, stock);
}
size_t getStockNumber() const override {
PYBIND11_OVERRIDE_NAME(size_t, TradeManagerBase, "get_stock_num", getStockNumber, );
}
size_t getShortStockNumber() const override {
PYBIND11_OVERRIDE_NAME(size_t, TradeManagerBase, "get_short_stock_num",
getShortStockNumber, );
}
double getHoldNumber(const Datetime& datetime, const Stock& stock) override {
PYBIND11_OVERRIDE_NAME(double, TradeManagerBase, "get_hold_num", getHoldNumber, datetime,
stock);
}
double getShortHoldNumber(const Datetime& datetime, const Stock& stock) override {
PYBIND11_OVERRIDE_NAME(double, TradeManagerBase, "get_short_hold_num", getShortHoldNumber,
datetime, stock);
}
double getDebtNumber(const Datetime& datetime, const Stock& stock) override {
PYBIND11_OVERRIDE_NAME(double, TradeManagerBase, "get_debt_num", getDebtNumber, datetime,
stock);
}
price_t getDebtCash(const Datetime& datetime) override {
PYBIND11_OVERRIDE_NAME(price_t, TradeManagerBase, "get_debt_cash", getDebtCash, datetime);
}
TradeRecordList getTradeList() const override {
PYBIND11_OVERRIDE_NAME(TradeRecordList, TradeManagerBase, "get_trade_list", getTradeList, );
}
TradeRecordList getTradeList(const Datetime& start, const Datetime& end) const override {
PYBIND11_OVERRIDE_NAME(TradeRecordList, TradeManagerBase, "get_trade_list", getTradeList,
start, end);
}
PositionRecordList getPositionList() const override {
PYBIND11_OVERRIDE_NAME(PositionRecordList, TradeManagerBase, "get_position_list",
getPositionList, );
}
PositionRecordList getHistoryPositionList() const override {
PYBIND11_OVERRIDE_NAME(PositionRecordList, TradeManagerBase, "get_history_position_list",
getHistoryPositionList, );
}
PositionRecordList getShortPositionList() const override {
PYBIND11_OVERRIDE_NAME(PositionRecordList, TradeManagerBase, "get_short_position_list",
getShortPositionList, );
}
PositionRecordList getShortHistoryPositionList() const override {
PYBIND11_OVERRIDE_NAME(PositionRecordList, TradeManagerBase,
"get_short_history_position_list", getShortHistoryPositionList, );
}
PositionRecord getPosition(const Datetime& date, const Stock& stock) override {
PYBIND11_OVERRIDE_NAME(PositionRecord, TradeManagerBase, "get_position", getPosition, date,
stock);
}
PositionRecord getShortPosition(const Stock& stock) const override {
PYBIND11_OVERRIDE_NAME(PositionRecord, TradeManagerBase, "get_short_position",
getShortPosition, stock);
}
BorrowRecordList getBorrowStockList() const override {
PYBIND11_OVERRIDE_NAME(BorrowRecordList, TradeManagerBase, "get_borrow_stock_list",
getBorrowStockList, );
}
bool checkin(const Datetime& datetime, price_t cash) override {
PYBIND11_OVERLOAD(bool, TradeManagerBase, checkin, datetime, cash);
}
bool checkout(const Datetime& datetime, price_t cash) override {
PYBIND11_OVERLOAD(bool, TradeManagerBase, checkout, datetime, cash);
}
bool checkinStock(const Datetime& datetime, const Stock& stock, price_t price,
double number) override {
PYBIND11_OVERRIDE_NAME(bool, TradeManagerBase, "checkin_stock", checkinStock, datetime,
stock, price, number);
}
bool checkoutStock(const Datetime& datetime, const Stock& stock, price_t price,
double number) override {
PYBIND11_OVERRIDE_NAME(bool, TradeManagerBase, "checkout_stock", checkoutStock, datetime,
stock, price, number);
}
TradeRecord buy(const Datetime& datetime, const Stock& stock, price_t realPrice, double number,
price_t stoploss, price_t goalPrice, price_t planPrice,
SystemPart from) override {
PYBIND11_OVERLOAD(TradeRecord, TradeManagerBase, buy, datetime, stock, realPrice, number,
stoploss, goalPrice, planPrice, from);
}
TradeRecord sell(const Datetime& datetime, const Stock& stock, price_t realPrice, double number,
price_t stoploss, price_t goalPrice, price_t planPrice,
SystemPart from) override {
PYBIND11_OVERLOAD(TradeRecord, TradeManagerBase, sell, datetime, stock, realPrice, number,
stoploss, goalPrice, planPrice, from);
}
TradeRecord sellShort(const Datetime& datetime, const Stock& stock, price_t realPrice,
double number, price_t stoploss, price_t goalPrice, price_t planPrice,
SystemPart from) override {
PYBIND11_OVERRIDE_NAME(TradeRecord, TradeManagerBase, "sell_short", sellShort, datetime,
stock, realPrice, number, stoploss, goalPrice, planPrice, from);
}
TradeRecord buyShort(const Datetime& datetime, const Stock& stock, price_t realPrice,
double number, price_t stoploss, price_t goalPrice, price_t planPrice,
SystemPart from) override {
PYBIND11_OVERRIDE_NAME(TradeRecord, TradeManagerBase, "buy_short", buyShort, datetime,
stock, realPrice, number, stoploss, goalPrice, planPrice, from);
}
bool borrowCash(const Datetime& datetime, price_t cash) override {
PYBIND11_OVERRIDE_NAME(bool, TradeManagerBase, "borrow_cash", borrowCash, datetime, cash);
}
bool returnCash(const Datetime& datetime, price_t cash) override {
PYBIND11_OVERRIDE_NAME(bool, TradeManagerBase, "return_cash", returnCash, datetime, cash);
}
bool borrowStock(const Datetime& datetime, const Stock& stock, price_t price,
double number) override {
PYBIND11_OVERRIDE_NAME(bool, TradeManagerBase, "borrow_stock", borrowStock, datetime, stock,
price, number);
}
bool returnStock(const Datetime& datetime, const Stock& stock, price_t price,
double number) override {
PYBIND11_OVERRIDE_NAME(bool, TradeManagerBase, "return_stock", returnStock, datetime, stock,
price, number);
}
FundsRecord getFunds(KQuery::KType ktype) const override {
PYBIND11_OVERRIDE_NAME(FundsRecord, TradeManagerBase, "get_funds", getFunds, ktype);
}
FundsRecord getFunds(const Datetime& datetime, KQuery::KType ktype) override {
PYBIND11_OVERRIDE_NAME(FundsRecord, TradeManagerBase, "get_funds", getFunds, datetime,
ktype);
}
bool addTradeRecord(const TradeRecord& tr) override {
PYBIND11_OVERRIDE_NAME(bool, TradeManagerBase, "add_trade_record", addTradeRecord, tr);
}
string str() const override {
PYBIND11_OVERRIDE_NAME(string, TradeManagerBase, "__str__", str, );
}
void tocsv(const string& path) override {
PYBIND11_OVERLOAD(void, TradeManagerBase, tocsv, path);
}
};
FundsRecord (TradeManagerBase::*getFunds_1)(KQuery::KType) const = &TradeManagerBase::getFunds;
FundsRecord (TradeManagerBase::*getFunds_2)(const Datetime&,
KQuery::KType) = &TradeManagerBase::getFunds;
TradeCostPtr (TradeManagerBase::*get_costFunc)() const = &TradeManagerBase::costFunc;
void (TradeManagerBase::*set_costFunc)(const TradeCostPtr&) = &TradeManagerBase::costFunc;
TradeRecordList (TradeManagerBase::*_getTradeList_1)() const = &TradeManagerBase::getTradeList;
TradeRecordList (TradeManagerBase::*_getTradeList_2)(const Datetime&, const Datetime&) const =
&TradeManagerBase::getTradeList;
void export_TradeManager(py::module& m) {
py::class_<TradeManagerBase, TradeManagerPtr, PyTradeManagerBase>(
m, "TradeManager",
R"(交易管理类,可理解为一个模拟账户进行模拟交易。一般使用 crtTM 创建交易管理实例。
使 crtTM
- reinvest=False (bool) :
- precision=2 (int) :
- support_borrow_cash=False (bool) :
- support_borrow_stock=False (bool) :
- save_action=True (bool) : Python命令序列)")
.def(py::init<>())
.def(py::init<const string&, const TradeCostPtr&>())
.def("__str__", &TradeManagerBase::str)
.def("__repr__", &TradeManagerBase::str)
.def_property("name", py::overload_cast<>(&TradeManagerBase::name, py::const_),
py::overload_cast<const string&>(&TradeManagerBase::name),
py::return_value_policy::copy, "名称")
.def_property_readonly("init_cash", &TradeManagerBase::initCash, "(只读)初始资金")
.def_property_readonly("current_cash", &TradeManagerBase::currentCash, "(只读)当前资金")
.def_property_readonly("init_datetime", &TradeManagerBase::initDatetime,
"(只读)账户建立日期")
.def_property_readonly("first_datetime", &TradeManagerBase::firstDatetime,
"(只读)第一笔买入交易发生日期,如未发生交易返回 Datetime>()")
.def_property_readonly(
"last_datetime", &TradeManagerBase::lastDatetime,
"(只读)最后一笔交易日期,注意和交易类型无关,如未发生交易返回账户建立日期")
.def_property_readonly("precision", &TradeManagerBase::precision,
"只读价格精度同公共参数“precision”")
.def_property("cost_func", get_costFunc, set_costFunc, "交易成本算法")
.def_property("broker_last_datetime", &TradeManagerBase::getBrokerLastDatetime,
&TradeManagerBase::setBrokerLastDatetime,
R"(实际开始订单代理操作的时刻。
TradeManager会在执行买入//TradeManager会在历史时刻就执行买入/// brokeLastDatetime )")
.def("current_frozen", &TradeManagerBase::currentFrozen, R"(frozen(self)
)")
.def("getParam", &TradeManagerBase::getParam<boost::any>, R"(get_param(self, name)
:param str name:
:return:
:raises out_of_range: )")
.def("set_param", &TradeManagerBase::setParam<boost::any>, R"(set_param(self, name, value)
:param str name:
:param value:
:type value: int | bool | float | string | Query | KData | Stock | DatetimeList
:raises logic_error: Unsupported type! )")
.def("have_param", &TradeManagerBase::haveParam, "是否存在指定参数")
.def("reset", &TradeManagerBase::reset, "复位,清空交易、持仓记录")
.def("clone", &TradeManagerBase::clone, "克隆(深复制)实例")
.def("reg_broker", &TradeManagerBase::regBroker, R"(reg_broker(self, broker)
:param OrderBrokerBase broker: )")
.def("clear_broker", &TradeManagerBase::clearBroker, R"(clear_broker(self)
)")
.def("get_margin_rate", &TradeManagerBase::getMarginRate)
.def("have", &TradeManagerBase::have, R"(have(self, stock)
:param Stock stock:
:rtype: bool)")
.def("get_stock_num", &TradeManagerBase::getStockNumber, R"(get_stock_num(self)
:rtype: int)")
.def("get_short_stock_num", &TradeManagerBase::getShortStockNumber)
.def("get_hold_num", &TradeManagerBase::getHoldNumber, R"(get_hold_num(self, datetime, stock)
:param Datetime datetime:
:param Stock stock:
:rtype: int)")
.def("get_short_hold_num", &TradeManagerBase::getShortHoldNumber)
.def("get_trade_list", _getTradeList_1)
.def("get_trade_list", _getTradeList_2, R"(get_trade_list(self[, start, end])
:param Datetime start:
:param Datetime end:
:rtype: TradeRecordList)")
.def("get_position_list", &TradeManagerBase::getPositionList, R"(get_position_list(self)
:rtype: PositionRecordList)")
.def("get_history_position_list", &TradeManagerBase::getHistoryPositionList,
R"(get_history_position_list(self)
:rtype: PositionRecordList)")
.def("get_position", &TradeManagerBase::getPosition, R"(get_position(self, date, stock)
PositionRecord()
:param Datetime date:
:param Stock stock:
:rtype: PositionRecord)")
.def("get_buy_cost", &TradeManagerBase::getBuyCost,
R"(get_buy_cost(self, datetime, stock, price, num)
:param Datetime datetime:
:param Stock stock:
:param float price:
:param float num:
:rtype: CostRecord)")
.def("get_sell_cost", &TradeManagerBase::getSellCost,
R"(get_sell_cost(self, datetime, stock, price, num)
:param Datetime datetime:
:param Stock stock:
:param float price:
:param float num:
:rtype: CostRecord)")
.def("get_borrow_cash_cost", &TradeManagerBase::getBorrowCashCost)
.def("get_return_cash_cost", &TradeManagerBase::getReturnCashCost)
.def("get_borrow_stock_cost", &TradeManagerBase::getBorrowStockCost)
.def("get_return_stock_cost", &TradeManagerBase::getReturnStockCost)
.def("cash", &TradeManagerBase::cash, py::arg("datetime"), py::arg("ktype") = KQuery::DAY,
R"(cash(self, datetime[, ktype=Query.KType.DAY])
:param Datetime datetime:
:param ktype: K线类型
:rtype: float)")
.def("get_funds", getFunds_1, py::arg("ktype") = KQuery::DAY)
.def("get_funds", getFunds_2, py::arg("datetime"), py::arg("ktype") = KQuery::DAY,
R"(get_funds(self, [datetime, ktype = Query.DAY])
:param Datetime datetime:
:param Query.KType ktype: K线类型
:rtype: FundsRecord)")
.def("get_funds_list", &TradeManagerBase::getFundsList, py::arg("dates"),
py::arg("ktype") = KQuery::DAY, R"(get_funds_list(self, dates[, ktype = Query.DAY])
:param Datetime datetime:
:param Query.KType ktype: K线类型
:rtype: FundsList)")
.def("get_funds_curve", &TradeManagerBase::getFundsCurve, py::arg("dates"),
py::arg("ktype") = KQuery::DAY,
R"(get_funds_curve(self, dates[, ktype = Query.DAY])
线
:param DatetimeList dates: 线
:param Query.KType ktype: K线类型
:return:
:rtype: PriceList)")
.def("get_profit_curve", &TradeManagerBase::getProfitCurve, py::arg("dates"),
py::arg("ktype") = KQuery::DAY,
R"(get_profit_curve(self, dates[, ktype = Query.DAY])
线线
:param DatetimeList dates: 线
:param Query.KType ktype: K线类型
:return: 线
:rtype: PriceList)")
.def("get_profit_cum_change_curve", &TradeManagerBase::getProfitCumChangeCurve,
py::arg("dates"), py::arg("ktype") = KQuery::DAY,
R"(get_profit_cum_change_curve(self, dates[, ktype = Query.DAY])
线
:param DatetimeList dates:
:param Query.KType ktype: K线类型
:rtype: PriceList)")
.def("get_base_assets_curve", &TradeManagerBase::getBaseAssetsCurve, py::arg("dates"),
py::arg("ktype") = KQuery::DAY,
R"(get_profit_curve(self, dates[, ktype = Query.DAY])
线
:param DatetimeList dates:
:param Query.KType ktype: K线类型
:rtype: PriceList)")
.def("checkin", &TradeManagerBase::checkin, R"(checkin(self, datetime, cash)
:param Datetime datetime:
:param float cash:
:rtype: TradeRecord)")
.def("checkout", &TradeManagerBase::checkout, R"(checkout(self, datetime, cash)
:param Datetime datetime:
:param float cash:
:rtype: TradeRecord)")
.def("checkin_stock", &TradeManagerBase::checkinStock)
.def("checkout_stock", &TradeManagerBase::checkoutStock)
.def("borrow_cash", &TradeManagerBase::borrowCash)
.def("return_cash", &TradeManagerBase::returnCash)
.def("borrow_stock", &TradeManagerBase::borrowStock)
.def("return_stock", &TradeManagerBase::returnStock)
.def(
"buy", &TradeManagerBase::buy, py::arg("datetime"), py::arg("stock"), py::arg("real_price"),
py::arg("num"), py::arg("stoploss") = 0.0, py::arg("goal_price") = 0.0,
py::arg("plan_price") = 0.0, py::arg("part") = PART_INVALID,
R"(buy(self, datetime, stock, real_price, number[, stoploss=0.0, goal_price=0.0, plan_price=0.0, part=System.INVALID])
:param Datetime datetime:
:param Stock stock:
:param float real_price:
:param float num:
:param float stoploss:
:param float goal_price:
:param float plan_price:
:param SystemPart part:
:rtype: TradeRecord)")
.def(
"sell", &TradeManagerBase::sell, py::arg("datetime"), py::arg("stock"),
py::arg("real_price"), py::arg("num") = MAX_DOUBLE, py::arg("stoploss") = 0.0,
py::arg("goal_price") = 0.0, py::arg("plan_price") = 0.0, py::arg("part") = PART_INVALID,
R"(sell(self, datetime, stock, realPrice[, number=constant.max_double, stoploss=0.0, goal_price=0.0, plan_price=0.0, part=System.INVALID])
:param Datetime datetime:
:param Stock stock:
:param float real_price:
:param float num: constant.max_double
:param float stoploss:
:param float goal_price:
:param float plan_price:
:param SystemPart part:
:rtype: TradeRecord)")
.def("buy_short", &TradeManagerBase::buyShort)
.def("sell_short", &TradeManagerBase::sellShort)
.def("add_trade_record", &TradeManagerBase::addTradeRecord, R"(add_trade_record(self, tr)
:param TradeRecord tr:
:return: True | False
:rtype: bool)")
.def("tocsv", &TradeManagerBase::tocsv, R"(tocsv(self, path)
csv格式输出交易记录线
:param str path: )")
.def("update_with_weight", &TradeManagerBase::updateWithWeight,
R"(update_with_weight(self, date)
:param Datetime date: )")
DEF_PICKLE(TradeManagerPtr);
}