From 92d83c27e479b5ca8776083d1a1d2d4c308948d3 Mon Sep 17 00:00:00 2001 From: fasiondog Date: Mon, 14 May 2018 02:31:12 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E6=9B=B4=E6=94=B9OrderBroker?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=EF=BC=8C=E9=94=99=E8=AF=AF=E7=9A=84=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E4=BA=86=E6=9C=AC=E5=9C=B0=E6=9C=BA=E5=99=A8=E7=9A=84?= =?UTF-8?q?=E6=97=B6=E9=97=B4=EF=BC=9B=202=E3=80=81Python=E4=B8=AD?= =?UTF-8?q?=E7=9A=84TradeManager=E7=9A=84buy=E3=80=81sell=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E6=94=AF=E6=8C=81python=E7=9A=84=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E9=A3=8E=E6=A0=BC=203=E3=80=81build.bat=E9=87=8C=E9=81=97?= =?UTF-8?q?=E6=BC=8F=E6=8B=B7=E8=B4=9Dmysqlclient=E7=9A=84=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.bat | 1 + libs/hikyuu/trade_manage/OrderBrokerBase.cpp | 18 +++++--- libs/hikyuu/trade_manage/OrderBrokerBase.h | 36 +++++++++++++--- libs/hikyuu/trade_manage/TradeManager.cpp | 42 +++++-------------- .../trade_manage/_OrderBroker.cpp | 8 ++-- .../trade_manage/_TradeManager.cpp | 21 +++++++--- tools/hikyuu/trade_manage/broker.py | 10 ++++- tools/hikyuu/trade_manage/trade.py | 3 ++ 8 files changed, 83 insertions(+), 56 deletions(-) diff --git a/build.bat b/build.bat index 61e06dce..ca0329c5 100644 --- a/build.bat +++ b/build.bat @@ -56,6 +56,7 @@ del .\tools\hikyuu\*.pyd /S copy .\extern-libs\boost\*.dll .\tools\hikyuu copy .\extern-libs\hdf5\dll\*.dll .\tools\hikyuu copy .\extern-libs\log4cplus\dll\*.dll .\tools\hikyuu +copy .\extern-libs\mysql\lib\*.dll .\tools\hikyuu copy .\extern-libs\msvc\*.dll .\tools\hikyuu copy .\build\extern-libs\%BUILD_OUTPUT_PATH%\*.dll .\tools\hikyuu diff --git a/libs/hikyuu/trade_manage/OrderBrokerBase.cpp b/libs/hikyuu/trade_manage/OrderBrokerBase.cpp index aa4a436b..1fa6fc5f 100644 --- a/libs/hikyuu/trade_manage/OrderBrokerBase.cpp +++ b/libs/hikyuu/trade_manage/OrderBrokerBase.cpp @@ -31,22 +31,28 @@ OrderBrokerBase::~OrderBrokerBase() { } -Datetime OrderBrokerBase::buy(const string& code, price_t price, int num) { +Datetime OrderBrokerBase::buy(Datetime datetime, const string& market, + const string& code, price_t price, int num) { + Datetime tradetime; try { - _buy(code, price, num); + tradetime = _buy(datetime, market, code, price, num); } catch (...) { HKU_ERROR("Unknow error in BUY operation!!! [OrderBrokerBase::buy]"); + tradetime = Null(); } - return Datetime::now(); + return tradetime; } -Datetime OrderBrokerBase::sell(const string& code, price_t price, int num) { +Datetime OrderBrokerBase::sell(Datetime datetime, const string& market, + const string& code, price_t price, int num) { + Datetime tradetime; try { - _sell(code, price, num); + tradetime = _sell(datetime, market, code, price, num); } catch (...) { HKU_ERROR("Unknow error in SELL operation!!! [OrderBrokerBase::sell]"); + tradetime = Null(); } - return Datetime::now(); + return tradetime; } } /* namespace hku */ diff --git a/libs/hikyuu/trade_manage/OrderBrokerBase.h b/libs/hikyuu/trade_manage/OrderBrokerBase.h index 73145d23..023ae66f 100644 --- a/libs/hikyuu/trade_manage/OrderBrokerBase.h +++ b/libs/hikyuu/trade_manage/OrderBrokerBase.h @@ -40,37 +40,61 @@ public: /** * 执行买入操作 + * @param datetime 策略指示时间 + * @param market 市场标识 * @param code 证券代码 * @param price 买入价格 * @param num 买入数量 - * @return 操作执行的时刻 + * @return 操作执行的时刻。实盘时,应返回委托单时间或服务器交易时间。 */ - Datetime buy(const string& code, price_t price, int num); + Datetime buy(Datetime datetime, + const string& market, + const string& code, + price_t price, + int num); /** * 执行卖出操作 + * @param datetime 策略指示时间 + * @param market 市场标识 * @param code 证券代码 * @param price 卖出价格 * @param num 卖出数量 - * @return 操作执行的时刻 + * @return 操作执行的时刻。实盘时,应返回委托单时间或服务器交易时间。 */ - Datetime sell(const string& code, price_t price, int num); + Datetime sell(Datetime datetime, + const string& market, + const string& code, + price_t price, int num); /** * 执行实际买入操作 + * @param datetime 策略指示时间 + * @param market 市场标识 * @param code 证券代码 * @param price 买入价格 * @param num 买入数量 + * @return 操作执行的时刻。实盘时,应返回委托单时间或服务器交易时间。 */ - virtual void _buy(const string& code, price_t price, int num) = 0; + virtual Datetime _buy(Datetime datetime, + const string& market, + const string& code, + price_t price, + int num) = 0; /** * 执行实际卖出操作 + * @param datetime 策略指示时间 + * @param market 市场标识 * @param code 证券代码 * @param price 卖出价格 * @param num 卖出数量 + * @return 操作执行的时刻。实盘时,应返回委托单时间或服务器交易时间。 */ - virtual void _sell(const string& code, price_t price, int num) = 0; + virtual Datetime _sell(Datetime datetime, + const string& market, + const string& code, + price_t price, int num) = 0; protected: string m_name; diff --git a/libs/hikyuu/trade_manage/TradeManager.cpp b/libs/hikyuu/trade_manage/TradeManager.cpp index 3306958d..ae9a71f5 100644 --- a/libs/hikyuu/trade_manage/TradeManager.cpp +++ b/libs/hikyuu/trade_manage/TradeManager.cpp @@ -1125,13 +1125,13 @@ TradeRecord TradeManager::buy(const Datetime& datetime, const Stock& stock, } if (result.datetime > m_broker_last_datetime) { - Datetime timestamp; - bd::date result_day = result.datetime.ptime().date(); list::const_iterator broker_iter = m_broker_list.begin(); for(; broker_iter != m_broker_list.end(); ++broker_iter) { - timestamp = (*broker_iter)->buy(stock.code(), planPrice, number); - bt::time_duration x = timestamp.ptime().time_of_day(); - m_broker_last_datetime = Datetime(bt::ptime(result_day, x)); + Datetime realtime = (*broker_iter)->buy(datetime, + stock.market(), + stock.code(), + planPrice, number); + m_broker_last_datetime = realtime; } } @@ -1245,13 +1245,13 @@ TradeRecord TradeManager::sell(const Datetime& datetime, const Stock& stock, } if (result.datetime > m_broker_last_datetime) { - Datetime timestamp; - bd::date result_day = result.datetime.ptime().date(); list::const_iterator broker_iter = m_broker_list.begin(); for(; broker_iter != m_broker_list.end(); ++broker_iter) { - timestamp = (*broker_iter)->sell(stock.code(), planPrice, number); - bt::time_duration x = timestamp.ptime().time_of_day(); - m_broker_last_datetime = Datetime(bt::ptime(result_day, x)); + Datetime realtime = (*broker_iter)->sell(datetime, + stock.market(), + stock.code(), + planPrice, number); + m_broker_last_datetime = realtime; } } @@ -2308,17 +2308,6 @@ bool TradeManager::_add_buy_tr(const TradeRecord& tr) { (tr.realPrice - tr.stoploss) * tr.number * tr.stock.unit(), precision); } - if (tr.datetime > m_broker_last_datetime) { - Datetime timestamp; - bd::date result_day = tr.datetime.ptime().date(); - list::const_iterator broker_iter = m_broker_list.begin(); - for(; broker_iter != m_broker_list.end(); ++broker_iter) { - timestamp = (*broker_iter)->buy(tr.stock.code(), tr.planPrice, tr.number); - bt::time_duration x = timestamp.ptime().time_of_day(); - m_broker_last_datetime = Datetime(bt::ptime(result_day, x)); - } - } - _saveAction(new_tr); return true; @@ -2379,17 +2368,6 @@ bool TradeManager::_add_sell_tr(const TradeRecord& tr) { m_position.erase(tr.stock.id()); } - if (tr.datetime > m_broker_last_datetime) { - Datetime timestamp; - bd::date result_day = tr.datetime.ptime().date(); - list::const_iterator broker_iter = m_broker_list.begin(); - for(; broker_iter != m_broker_list.end(); ++broker_iter) { - timestamp = (*broker_iter)->sell(tr.stock.code(), tr.planPrice, tr.number); - bt::time_duration x = timestamp.ptime().time_of_day(); - m_broker_last_datetime = Datetime(bt::ptime(result_day, x)); - } - } - _saveAction(new_tr); return true; diff --git a/libs/hikyuu_python/trade_manage/_OrderBroker.cpp b/libs/hikyuu_python/trade_manage/_OrderBroker.cpp index 1cca0db2..0edcd405 100644 --- a/libs/hikyuu_python/trade_manage/_OrderBroker.cpp +++ b/libs/hikyuu_python/trade_manage/_OrderBroker.cpp @@ -18,12 +18,12 @@ public: OrderBrokerWrap(): OrderBrokerBase() {} OrderBrokerWrap(const string& name): OrderBrokerBase(name) {} - void _buy(const string& code, price_t price, int num) { - this->get_override("_buy")(code, price, num); + Datetime _buy(Datetime datetime, const string& market, const string& code, price_t price, int num) { + return this->get_override("_buy")(datetime, market, code, price, num); } - void _sell(const string& code, price_t price, int num) { - this->get_override("_sell")(code, price, num); + Datetime _sell(Datetime datetime, const string& market, const string& code, price_t price, int num) { + return this->get_override("_sell")(datetime, market, code, price, num); } }; diff --git a/libs/hikyuu_python/trade_manage/_TradeManager.cpp b/libs/hikyuu_python/trade_manage/_TradeManager.cpp index 1c42f9a8..c525fa35 100644 --- a/libs/hikyuu_python/trade_manage/_TradeManager.cpp +++ b/libs/hikyuu_python/trade_manage/_TradeManager.cpp @@ -131,14 +131,23 @@ void export_TradeManager() { //.def("returnCash", &TradeManager::returnCash) //.def("borrowStock", &TradeManager::borrowStock) //.def("returnStock", &TradeManager::returnStock) - //.def("buy", &TradeManager::buy, buy_overload()) + .def("buy", &TradeManager::buy, - buy_overload(args("datetime", "stock", "realPrice", - "num", "stoploss", "goalPrice", "planPrice","part"))) - //.def("sell", &TradeManager::sell, sell_overload()) + (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"))) + .def("sell", &TradeManager::sell, - sell_overload(args("datetime", "stock", "realPrice", - "num", "stoploss", "goalPrice", "planPrice", "part"))) + (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"))) //.def("buyShort", &TradeManager::buyShort, buyShort_overload()) //.def("sellShort", &TradeManager::sellShort, sellShort_overload()) diff --git a/tools/hikyuu/trade_manage/broker.py b/tools/hikyuu/trade_manage/broker.py index b18856ec..836cec29 100644 --- a/tools/hikyuu/trade_manage/broker.py +++ b/tools/hikyuu/trade_manage/broker.py @@ -30,10 +30,12 @@ #=============================================================================== from hikyuu.trade_manage import OrderBrokerBase +from hikyuu import Datetime class OrderBrokerWrap(OrderBrokerBase): """订单代理包装类,用户可以参考自定义自己的订单代理,加入额外的处理 + 包装只有买卖操作参数只有(code, price, num)的交易接口类 """ def __init__(self, broker, real=True, slip=0.03): @@ -49,7 +51,7 @@ class OrderBrokerWrap(OrderBrokerBase): self._real = real self._slip=slip - def _buy(self, code, price, num): + def _buy(self, datetime, market, code, price, num): """实现 OrderBrokerBase 的 _buy 接口""" if self._real: import tushare as ts @@ -59,18 +61,22 @@ class OrderBrokerWrap(OrderBrokerBase): self._broker.buy(code, new_price, num) else: print("out of slip, not buy!!!!!!!!!!") + return Datetime.now() else: self._broker.buy(code, price, num) + return datetime - def _sell(self, code, price, num): + def _sell(self, datetime, market, code, price, num): """实现 OrderBrokerBase 的 _sell 接口""" if self._real: import tushare as ts df = ts.get_realtime_quotes(code) new_price = float(df.ix[0]['bid']) self._broker.sell(code, new_price, num) + return Datetime.now() else: self._broker.sell(code, price, num) + return datetime class TestOrderBroker: diff --git a/tools/hikyuu/trade_manage/trade.py b/tools/hikyuu/trade_manage/trade.py index a90acd90..158e744c 100644 --- a/tools/hikyuu/trade_manage/trade.py +++ b/tools/hikyuu/trade_manage/trade.py @@ -29,6 +29,9 @@ # 1. 20130213, Added by fasiondog #=============================================================================== +#TradeManager.buy等方法默认参数使用了Part,这里必须先导入,否则报初始化错误 +from hikyuu.trade_sys import system + from ._trade_manage import * from hikyuu import Datetime, Query, QueryByDate, StockManager from hikyuu.util.slice import list_getitem