update OrderBroker

This commit is contained in:
fasiondog 2024-04-06 01:24:22 +08:00
parent 56aa21bcf8
commit 8de406c1e9
5 changed files with 31 additions and 27 deletions

View File

@ -35,12 +35,14 @@ Python中的订单代理包装
sys.run(sm['sz000001'], Query(-150))
.. py:class:: OrderBrokerWrap(broker[, real=True, slip=0.03])
.. py:class:: OrderBrokerWrap
订单代理包装类,用户可以参考自定义自己的订单代理,加入额外的处理
:param bool real: 下单前是否重新实时获取实时分笔数据
:param float slip: 如果当前的卖一价格和指示买入的价格绝对差值不超过slip则下单否则忽略; 对卖出操作无效,立即以当前价卖出
.. py:method:: __init__(self, broker, name)
:param broker: python broker 实例
:param str name: 名称
.. py:method:: _buy(self, code, price, num)
@ -59,15 +61,10 @@ Python中的订单代理包装
:param int num: 卖出数量
.. py:function:: crtOB(broker[, real=True, slip=0.03])
.. py:function:: crtOB(broker[, name="NO_NAME"])
快速生成订单代理包装对象
:param broker: 订单代理示例必须拥有buy和sell方法并且参数为 code, price, num
:param bool real: 下单前是否重新实时获取实时分笔数据
:param float slip: 如果当前的卖一价格和指示买入的价格绝对差值不超过slip则下单否则忽略; 对卖出操作无效,立即以当前价卖出
内建的订单代理类
------------------

View File

@ -292,7 +292,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {},
"outputs": [
{
@ -319,8 +319,10 @@
"my_tm = crtTM(init_cash=300000, date=Datetime(201701010000))\n",
"\n",
"#注册实盘交易订单代理\n",
"my_tm.reg_broker(crtOB(TestOrderBroker(), False)) #TestOerderBroker是测试用订单代理对象只打印\n",
"#my_tm.regBroker(crtOB(MailOrderBroker(\"smtp.sina.com\", \"yourmail@sina.com\", \"yourpwd\", \"receivermail@XXX.yy)))\n",
"ob = crtOB(TestOrderBroker())\n",
"my_tm.reg_broker(ob) #TestOerderBroker是测试用订单代理对象只打印\n",
"# 注意pybind 不支持下面这种方式调用,必须先生成实例再传入!!!\n",
"# my_tm.reg_broker(crtOB(TestOrderBroker(), False))\n",
"\n",
"#根据需要修改订单代理最后的时间戳,后续只有大于该时间戳时,订单代理才会实际发出订单指令\n",
"my_tm.broker_last_datetime=Datetime(201701010000)\n",

View File

@ -24,10 +24,10 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#===============================================================================
# ===============================================================================
# History:
# 1. 20170704, Added by fasiondog
#===============================================================================
# ===============================================================================
from hikyuu import OrderBrokerBase
@ -36,16 +36,13 @@ class OrderBrokerWrap(OrderBrokerBase):
"""订单代理包装类,用户可以参考自定义自己的订单代理,加入额外的处理
包装只有买卖操作参数只有(code, price, num)的交易接口类
"""
def __init__(self, broker, slip=0.03):
def __init__(self, broker, name):
"""
订单代理包装类用户可以参考自定义自己的订单代理加入额外的处理
:param float slip: 如果当前的卖一价格和指示买入的价格绝对差值不超过slip则下单
否则忽略; 对卖出操作无效立即以当前价卖出
"""
super(OrderBrokerWrap, self).__init__()
super(OrderBrokerWrap, self).__init__(name)
self._broker = broker
self._slip = slip
def _buy(self, datetime, market, code, price, num):
"""实现 OrderBrokerBase 的 _buy 接口"""
@ -60,6 +57,7 @@ class OrderBrokerWrap(OrderBrokerBase):
class TestOrderBroker:
"""用于测试的订单代理,仅在执行买入/卖出时打印信息"""
def __init__(self):
pass
@ -70,12 +68,12 @@ class TestOrderBroker:
print("卖出:%s %.3f %i" % (code, price, num))
def crtOB(broker, slip=0.03):
def crtOB(broker, name="NO_NAME"):
"""
快速生成订单代理包装对象
:param broker: 订单代理示例必须拥有buy和sell方法并且参数为 code, price, num
:param float slip: 如果当前的卖一价格和指示买入的价格绝对差值不超过slip则下单
否则忽略; 对卖出操作无效立即以当前价卖出
"""
return OrderBrokerWrap(broker, slip)
return OrderBrokerWrap(broker, name)

View File

@ -30,8 +30,11 @@ Datetime OrderBrokerBase::buy(Datetime datetime, const string& market, const str
Datetime tradetime;
try {
tradetime = _buy(datetime, market, code, price, num);
} catch (const std::exception& e) {
HKU_ERROR(e.what());
tradetime = Null<Datetime>();
} catch (...) {
HKU_ERROR("Unknown error in BUY operation!!!");
HKU_ERROR_UNKNOWN;
tradetime = Null<Datetime>();
}
return tradetime;
@ -42,8 +45,11 @@ Datetime OrderBrokerBase::sell(Datetime datetime, const string& market, const st
Datetime tradetime;
try {
tradetime = _sell(datetime, market, code, price, num);
} catch (const std::exception& e) {
HKU_ERROR(e.what());
tradetime = Null<Datetime>();
} catch (...) {
HKU_ERROR("Unknown error in SELL operation!!!");
HKU_ERROR_UNKNOWN;
tradetime = Null<Datetime>();
}
return tradetime;

View File

@ -22,7 +22,8 @@ public:
Datetime _sell(Datetime datetime, const string& market, const string& code, price_t price,
double num) override {
PYBIND11_OVERLOAD_PURE(Datetime, OrderBrokerBase, datetime, market, code, price, num);
PYBIND11_OVERLOAD_PURE(Datetime, OrderBrokerBase, _sell, datetime, market, code, price,
num);
}
};