[增强] BarData扩充字段interval_num,修正部分bug

This commit is contained in:
msincenselee 2020-04-09 14:43:10 +08:00
parent 16f9c6641b
commit 1e95f7bd76
7 changed files with 22 additions and 12 deletions

View File

@ -283,7 +283,7 @@ class AlgoEngine(BaseEngine):
ret = self.stop_algo(req.orderid) ret = self.stop_algo(req.orderid)
if ret: if ret:
order.cancelTime = datetime.now().strftime('%H:%M:%S.%f') order.cancel_time = datetime.now().strftime('%H:%M:%S.%f')
order.status = Status.CANCELLED order.status = Status.CANCELLED
event1 = Event(type=EVENT_ORDER, data=order) event1 = Event(type=EVENT_ORDER, data=order)
self.event_engine.put(event1) self.event_engine.put(event1)

View File

@ -354,7 +354,7 @@ class BackTestingEngine(object):
return self.volume_tick.get(vt_symbol, 1) return self.volume_tick.get(vt_symbol, 1)
def set_contract(self, symbol: str, exchange: Exchange, product: Product, name: str, size: int, def set_contract(self, symbol: str, exchange: Exchange, product: Product, name: str, size: int,
price_tick: float, volume_tick: float = 1, margin_rate: float = 0.1): price_tick: float, volume_tick: float = 0.1, margin_rate: float = 0.1):
"""设置合约信息""" """设置合约信息"""
vt_symbol = '.'.join([symbol, exchange.value]) vt_symbol = '.'.join([symbol, exchange.value])
if vt_symbol not in self.contract_dict: if vt_symbol not in self.contract_dict:
@ -560,8 +560,8 @@ class BackTestingEngine(object):
exchange=Exchange(symbol_data.get('exchange', 'LOCAL')), exchange=Exchange(symbol_data.get('exchange', 'LOCAL')),
product=Product(symbol_data.get('product', "期货")), product=Product(symbol_data.get('product', "期货")),
size=symbol_data.get('symbol_size', 10), size=symbol_data.get('symbol_size', 10),
price_tick=symbol_data.get('price_tick', 1), price_tick=symbol_data.get('price_tick', 0.01),
volume_tick=symbol_data.get('min_volume', 1), volume_tick=symbol_data.get('min_volume', 0.1),
margin_rate=margin_rate margin_rate=margin_rate
) )
@ -601,8 +601,8 @@ class BackTestingEngine(object):
def new_bar(self, bar): def new_bar(self, bar):
"""新的K线""" """新的K线"""
self.last_bar.update({bar.vt_symbol: bar}) self.last_bar.update({bar.vt_symbol: bar})
if self.last_dt is None or (bar.datetime and bar.datetime > self.last_dt): if self.last_dt is None or (bar.datetime and bar.datetime > self.last_dt - timedelta(seconds=self.bar_interval_seconds)):
self.last_dt = bar.datetime self.last_dt = bar.datetime + timedelta(seconds=self.bar_interval_seconds)
self.set_price(bar.vt_symbol, bar.close_price) self.set_price(bar.vt_symbol, bar.close_price)
self.cross_stop_order(bar=bar) # 撮合停止单 self.cross_stop_order(bar=bar) # 撮合停止单
self.cross_limit_order(bar=bar) # 先撮合限价单 self.cross_limit_order(bar=bar) # 先撮合限价单
@ -887,7 +887,7 @@ class BackTestingEngine(object):
if register_strategy.strategy_name != strategy.strategy_name: if register_strategy.strategy_name != strategy.strategy_name:
return False return False
order.status = Status.CANCELLED order.status = Status.CANCELLED
order.cancelTime = str(self.last_dt) order.cancel_time = str(self.last_dt)
self.active_limit_orders.pop(vt_orderid, None) self.active_limit_orders.pop(vt_orderid, None)
strategy.on_order(order) strategy.on_order(order)
return True return True
@ -941,7 +941,7 @@ class BackTestingEngine(object):
strategy_cond = strategy.strategy_name == order_strategy.strategy_name strategy_cond = strategy.strategy_name == order_strategy.strategy_name
if offset_cond and symbol_cond and strategy_cond: if offset_cond and symbol_cond and strategy_cond:
self.write_log(u'撤销订单:{},{} {}@{}' self.write_log(u'撤销限价订单:{},{} {}@{}'
.format(vt_orderid, order.direction, order.price, order.volume)) .format(vt_orderid, order.direction, order.price, order.volume))
order.status = Status.CANCELLED order.status = Status.CANCELLED
order.cancel_time = str(self.last_dt) order.cancel_time = str(self.last_dt)

View File

@ -583,8 +583,8 @@ class BackTestingEngine(object):
def new_bar(self, bar): def new_bar(self, bar):
"""新的K线""" """新的K线"""
self.last_bar.update({bar.vt_symbol: bar}) self.last_bar.update({bar.vt_symbol: bar})
if self.last_dt is None or (bar.datetime and bar.datetime > self.last_dt): if self.last_dt is None or (bar.datetime and bar.datetime > self.last_dt - timedelta(seconds=self.bar_interval_seconds)):
self.last_dt = bar.datetime self.last_dt = bar.datetime + timedelta(seconds=self.bar_interval_seconds)
self.set_price(bar.vt_symbol, bar.close_price) self.set_price(bar.vt_symbol, bar.close_price)
self.cross_stop_order(bar=bar) # 撮合停止单 self.cross_stop_order(bar=bar) # 撮合停止单
self.cross_limit_order(bar=bar) # 先撮合限价单 self.cross_limit_order(bar=bar) # 先撮合限价单
@ -898,7 +898,7 @@ class BackTestingEngine(object):
if register_strategy.strategy_name != strategy.strategy_name: if register_strategy.strategy_name != strategy.strategy_name:
return False return False
order.status = Status.CANCELLED order.status = Status.CANCELLED
order.cancelTime = str(self.last_dt) order.cancel_time = str(self.last_dt)
self.active_limit_orders.pop(vt_orderid, None) self.active_limit_orders.pop(vt_orderid, None)
strategy.on_order(order) strategy.on_order(order)
return True return True
@ -960,6 +960,7 @@ class BackTestingEngine(object):
if strategy: if strategy:
strategy.on_order(order) strategy.on_order(order)
# 撤销本地停止单
for stop_orderid in list(self.active_stop_orders.keys()): for stop_orderid in list(self.active_stop_orders.keys()):
order = self.active_stop_orders.get(stop_orderid, None) order = self.active_stop_orders.get(stop_orderid, None)
order_strategy = self.order_strategy_dict.get(stop_orderid, None) order_strategy = self.order_strategy_dict.get(stop_orderid, None)
@ -992,6 +993,7 @@ class BackTestingEngine(object):
def cross_stop_order(self, bar: BarData = None, tick: TickData = None): def cross_stop_order(self, bar: BarData = None, tick: TickData = None):
""" """
本地停止单撮合
Cross stop order with last bar/tick data. Cross stop order with last bar/tick data.
""" """
vt_symbol = bar.vt_symbol if bar else tick.vt_symbol vt_symbol = bar.vt_symbol if bar else tick.vt_symbol

View File

@ -1055,7 +1055,9 @@ class CtaLineBar(object):
gateway_name=tick.gateway_name, gateway_name=tick.gateway_name,
symbol=tick.symbol, symbol=tick.symbol,
exchange=tick.exchange, exchange=tick.exchange,
datetime=tick.datetime datetime=tick.datetime,
interval=self.interval,
interval_num=self.bar_interval
) # 创建新的K线 ) # 创建新的K线
# 计算K线的整点分钟周期这里周期最小是1分钟。如果你是采用非整点分钟例如1.5分钟,请把这段注解掉 # 计算K线的整点分钟周期这里周期最小是1分钟。如果你是采用非整点分钟例如1.5分钟,请把这段注解掉
if self.minute_interval and self.interval == Interval.SECOND: if self.minute_interval and self.interval == Interval.SECOND:

View File

@ -8,6 +8,7 @@ from vnpy.component.base import CtaComponent
from vnpy.trader.utility import get_folder_path from vnpy.trader.utility import get_folder_path
TNS_STATUS_OBSERVATE = 'observate' TNS_STATUS_OBSERVATE = 'observate'
TNS_STATUS_READY = 'ready'
TNS_STATUS_ORDERING = 'ordering' TNS_STATUS_ORDERING = 'ordering'
TNS_STATUS_OPENED = 'opened' TNS_STATUS_OPENED = 'opened'
TNS_STATUS_CLOSED = 'closed' TNS_STATUS_CLOSED = 'closed'

View File

@ -38,6 +38,7 @@ from vnpy.trader.object import (
SubscribeRequest, SubscribeRequest,
HistoryRequest HistoryRequest
) )
from vnpy.trader.utility import print_dict
from vnpy.trader.event import EVENT_TIMER from vnpy.trader.event import EVENT_TIMER
from vnpy.event import Event, EventEngine from vnpy.event import Event, EventEngine
@ -508,11 +509,14 @@ class BinancefRestApi(RestClient):
"unrealizedProfit": "-0.44537584", // 持仓未实现盈亏 "unrealizedProfit": "-0.44537584", // 持仓未实现盈亏
"walletBalance": "9.19485176" // 账户余额 "walletBalance": "9.19485176" // 账户余额
}""" }"""
# self.gateway.write_log(print_dict(asset))
account = AccountData( account = AccountData(
accountid=f"{self.gateway_name}_{asset['asset']}", accountid=f"{self.gateway_name}_{asset['asset']}",
balance=float(asset["marginBalance"]), balance=float(asset["marginBalance"]),
frozen=float(asset["maintMargin"]), frozen=float(asset["maintMargin"]),
holding_profit=float(asset['unrealizedProfit']), holding_profit=float(asset['unrealizedProfit']),
currency='USDT',
margin=float(asset["initialMargin"]),
gateway_name=self.gateway_name gateway_name=self.gateway_name
) )

View File

@ -96,6 +96,7 @@ class BarData(BaseData):
trading_day: str = "" # '%Y-%m-%d' trading_day: str = "" # '%Y-%m-%d'
interval: Interval = None # constant.py Internal 1m, 1h, 1d, 1w .etc interval: Interval = None # constant.py Internal 1m, 1h, 1d, 1w .etc
interval_num: int = 1 # 5 for 5m, 5h etc
volume: float = 0 volume: float = 0
open_interest: float = 0 open_interest: float = 0
open_price: float = 0 open_price: float = 0