hikyuu2/hikyuu/trade_manage/broker.py

102 lines
4.1 KiB
C++
Raw Normal View History

#!/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-09-26 07:07:56 +08:00
# History:
# 1. 20170704, Added by fasiondog
#===============================================================================
2020-06-25 15:59:37 +08:00
from hikyuu import OrderBrokerBase
from hikyuu import Datetime
2017-10-03 00:02:38 +08:00
2020-06-25 15:59:37 +08:00
class OrderBrokerWrap(OrderBrokerBase):
2017-10-03 00:02:38 +08:00
"""订单代理包装类,用户可以参考自定义自己的订单代理,加入额外的处理
(code, price, num)
"""
def __init__(self, broker, real=True, slip=0.03):
2017-10-03 00:02:38 +08:00
"""
:param bool real:
:param float slip: slip则下单
;
"""
super(OrderBrokerWrap, self).__init__()
self._broker = broker
self._real = real
2020-06-25 15:59:37 +08:00
self._slip = slip
def _buy(self, datetime, market, code, price, num):
2017-10-03 00:02:38 +08:00
"""实现 OrderBrokerBase 的 _buy 接口"""
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!!!!!!!!!!")
return Datetime.now()
else:
self._broker.buy(code, price, num)
return datetime
2020-06-25 15:59:37 +08:00
def _sell(self, datetime, market, code, price, num):
2017-10-03 00:02:38 +08:00
"""实现 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
2017-10-03 00:02:38 +08:00
2020-06-25 15:59:37 +08:00
class TestOrderBroker:
2017-10-03 00:02:38 +08:00
"""用于测试的订单代理,仅在执行买入/卖出时打印信息"""
def __init__(self):
pass
2020-06-25 15:59:37 +08:00
def buy(self, code, price, num):
print("买入:%s %.3f %i" % (code, price, num))
2020-06-25 15:59:37 +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-10-03 00:02:38 +08:00
:param broker: buy和sell方法 code, price, num
:param bool real:
:param float slip: slip则下单
;
"""
return OrderBrokerWrap(broker, real, slip)