2017-07-06 01:55:09 +08:00
|
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- coding: utf8 -*-
|
|
|
|
|
# cp936
|
2017-09-26 07:07:56 +08:00
|
|
|
|
#
|
|
|
|
|
# The MIT License (MIT)
|
|
|
|
|
#
|
|
|
|
|
# Copyright (c) 2010-2017 fasiondog
|
|
|
|
|
#
|
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
|
#
|
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
|
#
|
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
# SOFTWARE.
|
2017-07-06 01:55:09 +08:00
|
|
|
|
|
|
|
|
|
#===============================================================================
|
2017-09-26 07:07:56 +08:00
|
|
|
|
# History:
|
|
|
|
|
# 1. 20170704, Added by fasiondog
|
2017-07-06 01:55:09 +08:00
|
|
|
|
#===============================================================================
|
|
|
|
|
|
2020-06-25 15:59:37 +08:00
|
|
|
|
from hikyuu import OrderBrokerBase
|
2018-05-14 02:31:12 +08:00
|
|
|
|
from hikyuu import Datetime
|
2017-10-03 00:02:38 +08:00
|
|
|
|
|
2020-06-25 15:59:37 +08:00
|
|
|
|
|
2017-07-06 01:55:09 +08:00
|
|
|
|
class OrderBrokerWrap(OrderBrokerBase):
|
2017-10-03 00:02:38 +08:00
|
|
|
|
"""订单代理包装类,用户可以参考自定义自己的订单代理,加入额外的处理
|
2018-05-14 02:31:12 +08:00
|
|
|
|
包装只有买卖操作参数只有(code, price, num)的交易接口类
|
2017-07-06 01:55:09 +08:00
|
|
|
|
"""
|
|
|
|
|
def __init__(self, broker, real=True, slip=0.03):
|
2017-10-03 00:02:38 +08:00
|
|
|
|
"""
|
|
|
|
|
订单代理包装类,用户可以参考自定义自己的订单代理,加入额外的处理
|
|
|
|
|
|
|
|
|
|
:param bool real: 下单前是否重新实时获取实时分笔数据
|
|
|
|
|
:param float slip: 如果当前的卖一价格和指示买入的价格绝对差值不超过slip则下单,
|
|
|
|
|
否则忽略; 对卖出操作无效,立即以当前价卖出
|
|
|
|
|
"""
|
2017-07-06 01:55:09 +08:00
|
|
|
|
super(OrderBrokerWrap, self).__init__()
|
|
|
|
|
self._broker = broker
|
|
|
|
|
self._real = real
|
2020-06-25 15:59:37 +08:00
|
|
|
|
self._slip = slip
|
|
|
|
|
|
2018-05-14 02:31:12 +08:00
|
|
|
|
def _buy(self, datetime, market, code, price, num):
|
2017-10-03 00:02:38 +08:00
|
|
|
|
"""实现 OrderBrokerBase 的 _buy 接口"""
|
2017-07-06 01:55:09 +08:00
|
|
|
|
if self._real:
|
|
|
|
|
import tushare as ts
|
|
|
|
|
df = ts.get_realtime_quotes(code)
|
|
|
|
|
new_price = float(df.ix[0]['ask'])
|
|
|
|
|
if (abs(new_price - price) <= self._slip):
|
|
|
|
|
self._broker.buy(code, new_price, num)
|
|
|
|
|
else:
|
|
|
|
|
print("out of slip, not buy!!!!!!!!!!")
|
2018-05-14 02:31:12 +08:00
|
|
|
|
return Datetime.now()
|
2017-07-06 01:55:09 +08:00
|
|
|
|
else:
|
|
|
|
|
self._broker.buy(code, price, num)
|
2018-05-14 02:31:12 +08:00
|
|
|
|
return datetime
|
2020-06-25 15:59:37 +08:00
|
|
|
|
|
2018-05-14 02:31:12 +08:00
|
|
|
|
def _sell(self, datetime, market, code, price, num):
|
2017-10-03 00:02:38 +08:00
|
|
|
|
"""实现 OrderBrokerBase 的 _sell 接口"""
|
2017-07-06 01:55:09 +08:00
|
|
|
|
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)
|
2018-05-14 02:31:12 +08:00
|
|
|
|
return Datetime.now()
|
2017-07-06 01:55:09 +08:00
|
|
|
|
else:
|
|
|
|
|
self._broker.sell(code, price, num)
|
2018-05-14 02:31:12 +08:00
|
|
|
|
return datetime
|
2017-10-03 00:02:38 +08:00
|
|
|
|
|
2020-06-25 15:59:37 +08:00
|
|
|
|
|
2017-07-06 01:55:09 +08:00
|
|
|
|
class TestOrderBroker:
|
2017-10-03 00:02:38 +08:00
|
|
|
|
"""用于测试的订单代理,仅在执行买入/卖出时打印信息"""
|
2017-07-06 01:55:09 +08:00
|
|
|
|
def __init__(self):
|
|
|
|
|
pass
|
2020-06-25 15:59:37 +08:00
|
|
|
|
|
2017-07-06 01:55:09 +08:00
|
|
|
|
def buy(self, code, price, num):
|
|
|
|
|
print("买入:%s %.3f %i" % (code, price, num))
|
2020-06-25 15:59:37 +08:00
|
|
|
|
|
2017-07-06 01:55:09 +08:00
|
|
|
|
def sell(self, code, price, num):
|
|
|
|
|
print("卖出:%s %.3f %i" % (code, price, num))
|
2020-06-25 15:59:37 +08:00
|
|
|
|
|
|
|
|
|
|
2017-10-03 00:02:38 +08:00
|
|
|
|
def crtOB(broker, real=True, slip=0.03):
|
2017-07-06 01:55:09 +08:00
|
|
|
|
"""
|
|
|
|
|
快速生成订单代理包装对象
|
2017-10-03 00:02:38 +08:00
|
|
|
|
|
2017-07-06 01:55:09 +08:00
|
|
|
|
:param broker: 订单代理示例,必须拥有buy和sell方法,并且参数为 code, price, num
|
|
|
|
|
:param bool real: 下单前是否重新实时获取实时分笔数据
|
|
|
|
|
:param float slip: 如果当前的卖一价格和指示买入的价格绝对差值不超过slip则下单,
|
|
|
|
|
否则忽略; 对卖出操作无效,立即以当前价卖出
|
|
|
|
|
"""
|
|
|
|
|
return OrderBrokerWrap(broker, real, slip)
|