mirror of
https://gitee.com/fasiondog/hikyuu.git
synced 2024-12-02 20:08:26 +08:00
1、更改OrderBroker接口,错误的使用了本地机器的时间;
2、Python中的TradeManager的buy、sell方法支持python的参数风格 3、build.bat里遗漏拷贝mysqlclient的动态库
This commit is contained in:
parent
7492d2ffe0
commit
92d83c27e4
@ -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
|
||||
|
@ -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<Datetime>();
|
||||
}
|
||||
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<Datetime>();
|
||||
}
|
||||
return Datetime::now();
|
||||
return tradetime;
|
||||
}
|
||||
|
||||
} /* namespace hku */
|
||||
|
@ -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;
|
||||
|
@ -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<OrderBrokerPtr>::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<OrderBrokerPtr>::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<OrderBrokerPtr>::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<OrderBrokerPtr>::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;
|
||||
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -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())
|
||||
|
||||
|
@ -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:
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user