hikyuu2/hikyuu_pywrap/trade_manage/_TradeManager.cpp

168 lines
7.3 KiB
C++
Raw Normal View History

2015-01-07 01:26:14 +08:00
/*
* _TradeManager.cpp
*
* Created on: 2013-2-25
* Author: fasiondog
*/
#include <boost/python.hpp>
2016-04-11 01:27:04 +08:00
#include <hikyuu/trade_manage/build_in.h>
2016-04-03 00:08:31 +08:00
#include "../_Parameter.h"
2015-01-07 01:26:14 +08:00
#include "../pickle_support.h"
using namespace boost::python;
using namespace hku;
#if HKU_PYTHON_SUPPORT_PICKLE
struct TradeManager_pickle_suite : bp::pickle_suite {
static
boost::python::tuple
getinitargs(TradeManager const& w) {
return boost::python::make_tuple(w.initDatetime(), w.initCash(),
w.costFunc(), w.name());
}
static bp::object getstate(const TradeManager& param) {
std::ostringstream os;
OUTPUT_ARCHIVE oa(os);
oa << param;
return bp::str (os.str());
}
static void
setstate(TradeManager& params, bp::object entries) {
bp::str s = bp::extract<bp::str>(entries)();
std::string st = bp::extract<std::string>(s)();
std::istringstream is(st);
INPUT_ARCHIVE ia(is);
ia >> params;
}
};
#endif /* HKU_PYTHON_SUPPORT_PICKLE */
2016-04-08 13:02:08 +08:00
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(cash_overload, cash, 1, 2);
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getFundsCurve_overload, getFundsCurve, 1, 2);
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getProfitCurve_overload, getProfitCurve, 1, 2);
FundsRecord (TradeManager::*getFunds_1)(KQuery::KType) const = &TradeManager::getFunds;
FundsRecord (TradeManager::*getFunds_2)(const Datetime&, KQuery::KType) = &TradeManager::getFunds;
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getFunds_1_overload, TradeManager::getFunds, 0, 1);
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getFunds_2_overload, TradeManager::getFunds, 1, 2);
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(buy_overload, buy, 4, 8);
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(sell_overload, sell, 3, 8);
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(buyShort_overload, buyShort, 3, 8);
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(sellShort_overload, sellShort, 4, 8);
TradeCostPtr (TradeManager::*get_costFunc)() const = &TradeManager::costFunc;
void (TradeManager::*set_costFunc)(const TradeCostPtr&) = &TradeManager::costFunc;
2018-02-19 12:01:27 +08:00
string (TradeManager::*tm_get_name)() const = &TradeManager::name;
void (TradeManager::*tm_set_name)(const string&) = &TradeManager::name;
const TradeRecordList& (TradeManager::*_getTradeList_1)() const = &TradeManager::getTradeList;
TradeRecordList (TradeManager::*_getTradeList_2)(const Datetime&, const Datetime&) const = &TradeManager::getTradeList;
2016-04-08 13:02:08 +08:00
2015-01-07 01:26:14 +08:00
void export_TradeManager() {
//size_t null_size = Null<size_t>();
2016-05-06 02:11:13 +08:00
2015-01-07 01:26:14 +08:00
class_<TradeManager>("TradeManager", init<const Datetime&, price_t,
const TradeCostPtr&, const string&>())
2016-04-03 00:08:31 +08:00
//.def(self_ns::str(self))
.def("__str__", &TradeManager::toString)
2018-02-19 12:01:27 +08:00
.add_property("name", tm_get_name, tm_set_name)
2015-01-07 01:26:14 +08:00
.add_property("initCash", &TradeManager::initCash)
.add_property("currentCash", &TradeManager::currentCash)
2015-01-07 01:26:14 +08:00
.add_property("initDatetime", &TradeManager::initDatetime)
.add_property("firstDatetime", &TradeManager::firstDatetime)
.add_property("lastDatetime", &TradeManager::lastDatetime)
.add_property("reinvest", &TradeManager::reinvest)
.add_property("precision", &TradeManager::precision)
2016-04-08 13:02:08 +08:00
.add_property("costFunc", get_costFunc, set_costFunc)
.add_property("brokeLastDatetime",
&TradeManager::getBrokerLastDatetime,
&TradeManager::setBrokerLastDatetime)
2015-01-07 01:26:14 +08:00
2016-04-03 00:08:31 +08:00
.def("getParam", &TradeManager::getParam<boost::any>)
.def("setParam", &TradeManager::setParam<object>)
2015-01-07 01:26:14 +08:00
.def("reset", &TradeManager::reset)
.def("clone", &TradeManager::clone)
.def("regBroker", &TradeManager::regBroker)
.def("clearBroker", &TradeManager::clearBroker)
2017-10-04 00:34:24 +08:00
//.def("getMarginRate", &TradeManager::getMarginRate)
2015-01-07 01:26:14 +08:00
.def("have", &TradeManager::have)
.def("getStockNumber", &TradeManager::getStockNumber)
2017-10-04 00:34:24 +08:00
//.def("getShortStockNumber", &TradeManager::getShortStockNumber)
2015-01-07 01:26:14 +08:00
.def("getHoldNumber", &TradeManager::getHoldNumber)
2017-10-04 00:34:24 +08:00
//.def("getShortHoldNumber", &TradeManager::getShortHoldNumber)
.def("getTradeList", _getTradeList_1,
2015-01-07 01:26:14 +08:00
return_value_policy<copy_const_reference>())
.def("getTradeList", _getTradeList_2)
2015-01-07 01:26:14 +08:00
.def("getPositionList", &TradeManager::getPositionList)
.def("getHistoryPositionList", &TradeManager::getHistoryPositionList,
return_value_policy<copy_const_reference>())
.def("getPosition", &TradeManager::getPosition)
.def("getBuyCost", &TradeManager::getBuyCost)
.def("getSellCost", &TradeManager::getSellCost)
//.def("getBorrowCashCost", &TradeManager::getBorrowCashCost)
2017-10-04 00:34:24 +08:00
//.def("getReturnCashCost", &TradeManager::getReturnCashCost)
//.def("getBorrowStockCost", &TradeManager::getBorrowStockCost)
//.def("getReturnStockCost", &TradeManager::getReturnStockCost)
2015-01-07 01:26:14 +08:00
.def("cash", &TradeManager::cash, cash_overload())
.def("getFunds", getFunds_1, getFunds_1_overload())
.def("getFunds", getFunds_2, getFunds_2_overload())
2016-05-06 02:11:13 +08:00
//.def("getFunds", getFunds_1, (arg("ktype") = KQuery::DAY))
//.def("getFunds", getFunds_2, (arg("datetime"), arg("ktype") = KQuery::DAY))
2015-01-07 01:26:14 +08:00
.def("getFundsCurve", &TradeManager::getFundsCurve)
.def("getProfitCurve", &TradeManager::getProfitCurve)
.def("checkin", &TradeManager::checkin)
.def("checkout", &TradeManager::checkout)
2017-10-04 00:34:24 +08:00
//.def("checkinStock", &TradeManager::checkinStock)
//.def("checkoutStock", &TradeManager::checkoutStock)
//.def("borrowCash", &TradeManager::borrowCash)
//.def("returnCash", &TradeManager::returnCash)
//.def("borrowStock", &TradeManager::borrowStock)
//.def("returnStock", &TradeManager::returnStock)
2016-05-06 02:11:13 +08:00
.def("buy", &TradeManager::buy,
(arg("datetime"), arg("stock"),
arg("realPrice"), arg("num"), arg("stoploss")=0.0,
arg("goalPrice")=0.0, arg("planPrice")=0.0,
arg("part")=PART_INVALID))
//buy_overload(args("datetime", "stock", "realPrice",
//"num", "stoploss", "goalPrice", "planPrice","part")))
2016-05-06 02:11:13 +08:00
.def("sell", &TradeManager::sell,
2019-03-15 00:48:10 +08:00
// arg("num") 无法支持默认参数为 null_size
//(arg("datetime"), arg("stock"),
//arg("realPrice"), arg("num"), arg("stoploss")=0.0,
//arg("goalPrice")=0.0, arg("planPrice")=0.0,
//arg("part")=PART_INVALID))
sell_overload(args("datetime", "stock", "realPrice",
"num", "stoploss", "goalPrice", "planPrice", "part")))
2017-10-04 00:34:24 +08:00
//.def("buyShort", &TradeManager::buyShort, buyShort_overload())
//.def("sellShort", &TradeManager::sellShort, sellShort_overload())
2015-01-07 01:26:14 +08:00
.def("addTradeRecord", &TradeManager::addTradeRecord)
2015-01-07 01:26:14 +08:00
.def("tocsv", &TradeManager::tocsv)
#if HKU_PYTHON_SUPPORT_PICKLE
.def_pickle(TradeManager_pickle_suite())
#endif
;
register_ptr_to_python<TradeManagerPtr>();
}