From 1e95f7bd764aea35d011c95b19a31b6b2d9e2bff Mon Sep 17 00:00:00 2001 From: msincenselee Date: Thu, 9 Apr 2020 14:43:10 +0800 Subject: [PATCH] =?UTF-8?q?[=E5=A2=9E=E5=BC=BA]=20BarData=E6=89=A9?= =?UTF-8?q?=E5=85=85=E5=AD=97=E6=AE=B5interval=5Fnum=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E6=AD=A3=E9=83=A8=E5=88=86bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/app/algo_trading/engine.py | 2 +- vnpy/app/cta_crypto/back_testing.py | 14 +++++++------- vnpy/app/cta_strategy_pro/back_testing.py | 8 +++++--- vnpy/component/cta_line_bar.py | 4 +++- vnpy/component/cta_policy.py | 1 + vnpy/gateway/binancef/binancef_gateway.py | 4 ++++ vnpy/trader/object.py | 1 + 7 files changed, 22 insertions(+), 12 deletions(-) diff --git a/vnpy/app/algo_trading/engine.py b/vnpy/app/algo_trading/engine.py index 77f110f0..ee7af87b 100644 --- a/vnpy/app/algo_trading/engine.py +++ b/vnpy/app/algo_trading/engine.py @@ -283,7 +283,7 @@ class AlgoEngine(BaseEngine): ret = self.stop_algo(req.orderid) 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 event1 = Event(type=EVENT_ORDER, data=order) self.event_engine.put(event1) diff --git a/vnpy/app/cta_crypto/back_testing.py b/vnpy/app/cta_crypto/back_testing.py index ab2f03a1..134e838b 100644 --- a/vnpy/app/cta_crypto/back_testing.py +++ b/vnpy/app/cta_crypto/back_testing.py @@ -354,7 +354,7 @@ class BackTestingEngine(object): return self.volume_tick.get(vt_symbol, 1) 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]) if vt_symbol not in self.contract_dict: @@ -560,8 +560,8 @@ class BackTestingEngine(object): exchange=Exchange(symbol_data.get('exchange', 'LOCAL')), product=Product(symbol_data.get('product', "期货")), size=symbol_data.get('symbol_size', 10), - price_tick=symbol_data.get('price_tick', 1), - volume_tick=symbol_data.get('min_volume', 1), + price_tick=symbol_data.get('price_tick', 0.01), + volume_tick=symbol_data.get('min_volume', 0.1), margin_rate=margin_rate ) @@ -601,8 +601,8 @@ class BackTestingEngine(object): def new_bar(self, bar): """新的K线""" self.last_bar.update({bar.vt_symbol: bar}) - if self.last_dt is None or (bar.datetime and bar.datetime > self.last_dt): - self.last_dt = bar.datetime + 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 + timedelta(seconds=self.bar_interval_seconds) self.set_price(bar.vt_symbol, bar.close_price) self.cross_stop_order(bar=bar) # 撮合停止单 self.cross_limit_order(bar=bar) # 先撮合限价单 @@ -887,7 +887,7 @@ class BackTestingEngine(object): if register_strategy.strategy_name != strategy.strategy_name: return False 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) strategy.on_order(order) return True @@ -941,7 +941,7 @@ class BackTestingEngine(object): strategy_cond = strategy.strategy_name == order_strategy.strategy_name 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)) order.status = Status.CANCELLED order.cancel_time = str(self.last_dt) diff --git a/vnpy/app/cta_strategy_pro/back_testing.py b/vnpy/app/cta_strategy_pro/back_testing.py index f010e482..7d580e05 100644 --- a/vnpy/app/cta_strategy_pro/back_testing.py +++ b/vnpy/app/cta_strategy_pro/back_testing.py @@ -583,8 +583,8 @@ class BackTestingEngine(object): def new_bar(self, bar): """新的K线""" self.last_bar.update({bar.vt_symbol: bar}) - if self.last_dt is None or (bar.datetime and bar.datetime > self.last_dt): - self.last_dt = bar.datetime + 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 + timedelta(seconds=self.bar_interval_seconds) self.set_price(bar.vt_symbol, bar.close_price) self.cross_stop_order(bar=bar) # 撮合停止单 self.cross_limit_order(bar=bar) # 先撮合限价单 @@ -898,7 +898,7 @@ class BackTestingEngine(object): if register_strategy.strategy_name != strategy.strategy_name: return False 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) strategy.on_order(order) return True @@ -960,6 +960,7 @@ class BackTestingEngine(object): if strategy: strategy.on_order(order) + # 撤销本地停止单 for stop_orderid in list(self.active_stop_orders.keys()): order = self.active_stop_orders.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): """ + 本地停止单撮合 Cross stop order with last bar/tick data. """ vt_symbol = bar.vt_symbol if bar else tick.vt_symbol diff --git a/vnpy/component/cta_line_bar.py b/vnpy/component/cta_line_bar.py index b32d1327..e707b76d 100644 --- a/vnpy/component/cta_line_bar.py +++ b/vnpy/component/cta_line_bar.py @@ -1055,7 +1055,9 @@ class CtaLineBar(object): gateway_name=tick.gateway_name, symbol=tick.symbol, exchange=tick.exchange, - datetime=tick.datetime + datetime=tick.datetime, + interval=self.interval, + interval_num=self.bar_interval ) # 创建新的K线 # 计算K线的整点分钟周期,这里周期最小是1分钟。如果你是采用非整点分钟,例如1.5分钟,请把这段注解掉 if self.minute_interval and self.interval == Interval.SECOND: diff --git a/vnpy/component/cta_policy.py b/vnpy/component/cta_policy.py index 91c9c3a5..7925e355 100644 --- a/vnpy/component/cta_policy.py +++ b/vnpy/component/cta_policy.py @@ -8,6 +8,7 @@ from vnpy.component.base import CtaComponent from vnpy.trader.utility import get_folder_path TNS_STATUS_OBSERVATE = 'observate' +TNS_STATUS_READY = 'ready' TNS_STATUS_ORDERING = 'ordering' TNS_STATUS_OPENED = 'opened' TNS_STATUS_CLOSED = 'closed' diff --git a/vnpy/gateway/binancef/binancef_gateway.py b/vnpy/gateway/binancef/binancef_gateway.py index 3e152b6b..3c2b09ac 100644 --- a/vnpy/gateway/binancef/binancef_gateway.py +++ b/vnpy/gateway/binancef/binancef_gateway.py @@ -38,6 +38,7 @@ from vnpy.trader.object import ( SubscribeRequest, HistoryRequest ) +from vnpy.trader.utility import print_dict from vnpy.trader.event import EVENT_TIMER from vnpy.event import Event, EventEngine @@ -508,11 +509,14 @@ class BinancefRestApi(RestClient): "unrealizedProfit": "-0.44537584", // 持仓未实现盈亏 "walletBalance": "9.19485176" // 账户余额 }""" + # self.gateway.write_log(print_dict(asset)) account = AccountData( accountid=f"{self.gateway_name}_{asset['asset']}", balance=float(asset["marginBalance"]), frozen=float(asset["maintMargin"]), holding_profit=float(asset['unrealizedProfit']), + currency='USDT', + margin=float(asset["initialMargin"]), gateway_name=self.gateway_name ) diff --git a/vnpy/trader/object.py b/vnpy/trader/object.py index 06a11eae..2da0edb4 100644 --- a/vnpy/trader/object.py +++ b/vnpy/trader/object.py @@ -96,6 +96,7 @@ class BarData(BaseData): trading_day: str = "" # '%Y-%m-%d' interval: Interval = None # constant.py Internal 1m, 1h, 1d, 1w .etc + interval_num: int = 1 # 5 for 5m, 5h etc volume: float = 0 open_interest: float = 0 open_price: float = 0