From 5c80cde61388a7a79eb6d91bb8d2a5f86e25873c Mon Sep 17 00:00:00 2001 From: msincenselee Date: Fri, 8 May 2020 11:33:13 +0800 Subject: [PATCH] [bug fix] --- vnpy/app/cta_stock/template.py | 16 +++++++++++++++- vnpy/gateway/xtp/xtp_gateway.py | 15 +++++++++++---- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/vnpy/app/cta_stock/template.py b/vnpy/app/cta_stock/template.py index 753321d1..4085a18c 100644 --- a/vnpy/app/cta_stock/template.py +++ b/vnpy/app/cta_stock/template.py @@ -462,6 +462,16 @@ class CtaStockTemplate(CtaTemplate): if name in setting: setattr(self, name, setting[name]) + def sync_data(self): + """同步更新数据""" + if not self.backtesting: + self.write_log(u'保存k线缓存数据') + self.save_klines_to_cache() + + if self.inited and self.trading: + self.write_log(u'保存policy数据') + self.policy.save() + def save_klines_to_cache(self, kline_names: list = []): """ 保存K线数据到缓存 @@ -573,7 +583,7 @@ class CtaStockTemplate(CtaTemplate): for lg in long_grids: if len(lg.order_ids) > 0: self.write_log(f'清除委托单:{lg.order_ids}') - [self.cta_engine.cancel_order(vt_orderid) for vt_orderid in lg.order_ids] + [self.cta_engine.cancel_order(self, vt_orderid) for vt_orderid in lg.order_ids] lg.order_ids = [] if lg.open_status: pos = self.get_position(lg.vt_symbol) @@ -1041,6 +1051,10 @@ class CtaStockTemplate(CtaTemplate): return vt_symbol = ordering_grid.vt_symbol cur_price = self.cta_engine.get_price(vt_symbol) + if cur_price is None: + self.write_error(f'暂时不能获取{vt_symbol}最新价格') + return + buy_volume = ordering_grid.volume - ordering_grid.traded_volume min_trade_volume = self.cta_engine.get_volume_tick(vt_symbol) if availiable < buy_volume * cur_price: diff --git a/vnpy/gateway/xtp/xtp_gateway.py b/vnpy/gateway/xtp/xtp_gateway.py index 05175787..5667efd2 100644 --- a/vnpy/gateway/xtp/xtp_gateway.py +++ b/vnpy/gateway/xtp/xtp_gateway.py @@ -517,7 +517,8 @@ class XtpTdApi(TdApi): else: # 股票 direction, offset = DIRECTION_STOCK_XTP2VT[data["side"]] - + insert_time = str(data["insert_time"]) + dt = datetime.strptime(insert_time, '%Y%m%d%H%M%S%f') order = OrderData( symbol=symbol, exchange=MARKET_XTP2VT[data["market"]], @@ -530,7 +531,8 @@ class XtpTdApi(TdApi): volume=data["quantity"], traded=data["qty_traded"], status=STATUS_XTP2VT[data["order_status"]], - time=data["insert_time"], + datetime=dt, + time=dt.strftime('%H:%M:%S'), gateway_name=self.gateway_name ) @@ -545,6 +547,9 @@ class XtpTdApi(TdApi): else: direction, offset = DIRECTION_STOCK_XTP2VT[data["side"]] + trade_time = str(data["trade_time"]) + dt = datetime.strptime(trade_time,'%Y%m%d%H%M%S%f') + trade = TradeData( symbol=symbol, exchange=MARKET_XTP2VT[data["market"]], @@ -555,7 +560,8 @@ class XtpTdApi(TdApi): offset=offset, price=data["price"], volume=data["quantity"], - time=data["trade_time"], + datetime=dt, + time=dt.strftime('%H:%M:%S'), gateway_name=self.gateway_name ) @@ -767,12 +773,13 @@ class XtpTdApi(TdApi): # stock type else: + req.offset = Offset.NONE xtp_req = { "ticker": req.symbol, "market": MARKET_VT2XTP[req.exchange], "price": req.price, "quantity": int(req.volume), - "side": DIRECTION_STOCK_VT2XTP.get((req.direction, Offset.NONE), ""), + "side": DIRECTION_STOCK_VT2XTP.get((req.direction,req.offset), ""), "price_type": ORDERTYPE_VT2XTP[req.type], "business_type": BUSINESS_VT2XTP[req.offset] }