hikyuu2/hikyuu/trade_manage/broker.py
2020-06-25 15:59:37 +08:00

102 lines
4.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/python
# -*- coding: utf8 -*-
# cp936
#
# 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.
#===============================================================================
# History:
# 1. 20170704, Added by fasiondog
#===============================================================================
from hikyuu import OrderBrokerBase
from hikyuu import Datetime
class OrderBrokerWrap(OrderBrokerBase):
"""订单代理包装类,用户可以参考自定义自己的订单代理,加入额外的处理
(code, price, num)
"""
def __init__(self, broker, real=True, slip=0.03):
"""
:param bool real:
:param float slip: slip则下单
;
"""
super(OrderBrokerWrap, self).__init__()
self._broker = broker
self._real = real
self._slip = slip
def _buy(self, datetime, market, code, price, num):
"""实现 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
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:
"""用于测试的订单代理,仅在执行买入/卖出时打印信息"""
def __init__(self):
pass
def buy(self, code, price, num):
print("买入:%s %.3f %i" % (code, price, num))
def sell(self, code, price, num):
print("卖出:%s %.3f %i" % (code, price, num))
def crtOB(broker, real=True, slip=0.03):
"""
:param broker: buy和sell方法 code, price, num
:param bool real:
:param float slip: slip则下单
;
"""
return OrderBrokerWrap(broker, real, slip)