hikyuu2/hikyuu_pywrap/trade_manage/_TradeManager.cpp

342 lines
14 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 <boost/python.hpp>
#include <hikyuu/trade_manage/build_in.h>
#include "../_Parameter.h"
#include "../pickle_support.h"
using namespace boost::python;
using namespace hku;
FundsRecord (TradeManagerBase::*getFunds_1)(KQuery::KType) const = &TradeManagerBase::getFunds;
FundsRecord (TradeManagerBase::*getFunds_2)(const Datetime&,
KQuery::KType) = &TradeManagerBase::getFunds;
PriceList (TradeManagerBase::*getTMFundsCurve_1)(const DatetimeList&,
KQuery::KType) = &TradeManagerBase::getFundsCurve;
PriceList (TradeManagerBase::*getTMFundsCurve_2)() = &TradeManagerBase::getFundsCurve;
PriceList (TradeManagerBase::*getTMProfitCurve_1)(const DatetimeList&, KQuery::KType ktype) =
&TradeManagerBase::getProfitCurve;
PriceList (TradeManagerBase::*getTMProfitCurve_2)() = &TradeManagerBase::getProfitCurve;
TradeCostPtr (TradeManagerBase::*get_costFunc)() const = &TradeManagerBase::costFunc;
void (TradeManagerBase::*set_costFunc)(const TradeCostPtr&) = &TradeManagerBase::costFunc;
const string& (TradeManagerBase::*tm_get_name)() const = &TradeManagerBase::name;
void (TradeManagerBase::*tm_set_name)(const string&) = &TradeManagerBase::name;
TradeRecordList (TradeManagerBase::*_getTradeList_1)() const = &TradeManagerBase::getTradeList;
TradeRecordList (TradeManagerBase::*_getTradeList_2)(const Datetime&, const Datetime&) const =
&TradeManagerBase::getTradeList;
void export_TradeManager() {
class_<TradeManagerBase>(
"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命令序列)",
// init<const Datetime&, price_t, const TradeCostPtr&, const string&>())
init<>())
.def("__str__", &TradeManagerBase::str)
.def("__repr__", &TradeManagerBase::str)
.add_property("name", make_function(tm_get_name, return_value_policy<copy_const_reference>()),
tm_set_name, "名称")
.add_property("init_cash", &TradeManagerBase::initCash, "(只读)初始资金")
.add_property("current_cash", &TradeManagerBase::currentCash, "(只读)当前资金")
.add_property("init_datetime", &TradeManagerBase::initDatetime, "(只读)账户建立日期")
.add_property("first_datetime", &TradeManagerBase::firstDatetime,
"(只读)第一笔买入交易发生日期,如未发生交易返回 Datetime>()")
.add_property("last_datetime", &TradeManagerBase::lastDatetime,
"(只读)最后一笔交易日期,注意和交易类型无关,如未发生交易返回账户建立日期")
.add_property("precision", &TradeManagerBase::precision,
"只读价格精度同公共参数“precision”")
.add_property("cost_func", get_costFunc, set_costFunc, "交易成本算法")
.add_property("broker_last_datetime", &TradeManagerBase::getBrokerLastDatetime,
&TradeManagerBase::setBrokerLastDatetime,
R"(实际开始订单代理操作的时刻。
TradeManager会在执行买入//TradeManager会在历史时刻就执行买入/// brokeLastDatetime )")
.def("getParam", &TradeManagerBase::getParam<boost::any>, R"(get_param(self, name)
:param str name:
:return:
:raises out_of_range: )")
.def("set_param", &TradeManagerBase::setParam<object>, 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("getMarginRate", &TradeManager::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("getShortStockNumber", &TradeManager::getShortStockNumber)
.def("get_hold_num", &TradeManagerBase::getHoldNumber, R"(get_hold_num(self, datetime, stock)
:param Datetime datetime:
:param Stock stock:
:rtype: int)")
//.def("getShortHoldNumber", &TradeManager::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, stock)
PositionRecord()
: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("getBorrowCashCost", &TradeManager::getBorrowCashCost)
//.def("getReturnCashCost", &TradeManager::getReturnCashCost)
//.def("getBorrowStockCost", &TradeManager::getBorrowStockCost)
//.def("getReturnStockCost", &TradeManager::getReturnStockCost)
.def("cash", &TradeManagerBase::cash, (arg("datetime"), 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, (arg("ktype") = KQuery::DAY))
.def("get_funds", getFunds_2, (arg("datetime"), arg("ktype") = KQuery::DAY),
R"(get_funds(self, [datetime, ktype = Query.DAY])
:param Datetime datetime:
:param Query.KType ktype: K线类型
:rtype: FundsRecord)")
//.def("getFunds", getFunds_1, (arg("ktype") = KQuery::DAY))
//.def("getFunds", getFunds_2, (arg("datetime"), arg("ktype") = KQuery::DAY))
.def("get_funds_curve", getTMFundsCurve_1, (arg("dates"), 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_funds_curve", getTMFundsCurve_2,
R"(get_funds_curve(self)
线
:return:
:rtype: PriceList)")
.def("get_profit_curve", getTMProfitCurve_1, (arg("dates"), 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_curve", getTMProfitCurve_2,
R"(get_profit_curve(self)
线线
:return: 线
: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("checkinStock", &TradeManager::checkinStock)
//.def("checkoutStock", &TradeManager::checkoutStock)
//.def("borrowCash", &TradeManager::borrowCash)
//.def("returnCash", &TradeManager::returnCash)
//.def("borrowStock", &TradeManager::borrowStock)
//.def("returnStock", &TradeManager::returnStock)
.def(
"buy", &TradeManagerBase::buy,
(arg("datetime"), arg("stock"), arg("real_price"), arg("num"), arg("stoploss") = 0.0,
arg("goal_price") = 0.0, arg("plan_price") = 0.0, 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)")
// buy_overload(args("datetime", "stock", "realPrice",
//"num", "stoploss", "goalPrice", "planPrice","part")))
.def(
"sell", &TradeManagerBase::sell,
(arg("datetime"), arg("stock"), arg("real_price"), arg("num") = MAX_DOUBLE,
arg("stoploss") = 0.0, arg("goal_price") = 0.0, arg("plan_price") = 0.0,
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)")
// sell_overload(args("datetime", "stock", "realPrice", "num", "stoploss", "goalPrice",
// "planPrice", "part")))
//.def("buyShort", &TradeManager::buyShort, buyShort_overload())
//.def("sellShort", &TradeManager::sellShort, sellShort_overload())
.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", &TradeManager::updateWithWeight, R"(update_with_weight(self, date)
:param Datetime date: )")
#if HKU_PYTHON_SUPPORT_PICKLE
.def_pickle(name_init_pickle_suite<TradeManagerBase>())
#endif
;
register_ptr_to_python<TradeManagerPtr>();
}