hikyuu2/tools/hikyuu/interactive/broker.py

65 lines
2.4 KiB
C++
Raw Normal View History

#!/usr/bin/python
# -*- coding: utf8 -*-
# cp936
#===============================================================================
# 作者fasiondog
# 历史120170704, Added by fasiondog
#===============================================================================
from hikyuu.trade_manage import OrderBrokerBase
class OrderBrokerWrap(OrderBrokerBase):
"""
:param bool real:
:param float slip: slip则下单
;
"""
def __init__(self, broker, real=True, slip=0.03):
super(OrderBrokerWrap, self).__init__()
self._broker = broker
self._real = real
self._slip=slip
def _buy(self, code, price, num):
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!!!!!!!!!!")
else:
self._broker.buy(code, price, num)
def _sell(self, code, price, num):
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)
else:
self._broker.sell(code, price, num)
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 crtRB(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)