From 63d5524d92fea5561161e2abb310f30e4c86764c Mon Sep 17 00:00:00 2001 From: msincenselee Date: Mon, 3 Jun 2019 14:25:16 +0800 Subject: [PATCH] =?UTF-8?q?bug=20fix=EF=BC=8C=E4=BF=AE=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E4=BB=B7-=E5=BC=80=E4=BB=93=E5=9D=87?= =?UTF-8?q?=E4=BB=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnpy/trader/gateway/ctpGateway/ctpGateway.py | 19 ++++++++++----- .../gateway/ctpseGateway/ctpseGateway.py | 24 +++++++++++++------ vnpy/trader/vtGateway.py | 17 ++++++------- 3 files changed, 39 insertions(+), 21 deletions(-) diff --git a/vnpy/trader/gateway/ctpGateway/ctpGateway.py b/vnpy/trader/gateway/ctpGateway/ctpGateway.py index 6c26526f..3e256c97 100644 --- a/vnpy/trader/gateway/ctpGateway/ctpGateway.py +++ b/vnpy/trader/gateway/ctpGateway/ctpGateway.py @@ -1056,13 +1056,20 @@ class CtpTdApi(TdApi): pre_settlement_price = data['PreSettlementPrice'] # 开仓均价 open_cost_price = data['OpenCost'] / (pos.position * size) - # 逐笔盈亏 = (上一交易日结算价 - 开仓价)* 持仓数量 * 杠杆 + 当日持仓收益 - if pos.direction == DIRECTION_LONG: - pre_profit = (pre_settlement_price - open_cost_price) * (pos.position * size) - else: - pre_profit = (open_cost_price - pre_settlement_price) * (pos.position * size) - pos.positionProfit = pos.positionProfit + pre_profit + data['PositionProfit'] + cur_price = self.gateway.symbol_price_dict.get(pos.vtSymbol, None) + if cur_price is None: + # 逐笔盈亏 = (上一交易日结算价 - 开仓价)* 持仓数量 * 杠杆 + 当日持仓收益 + if pos.direction == DIRECTION_LONG: + pre_profit = (pre_settlement_price - open_cost_price) * (yd_position * size) + else: + pre_profit = (open_cost_price - pre_settlement_price) * (yd_position * size) + pos.positionProfit = pos.positionProfit + pre_profit + data['PositionProfit'] + else: + if pos.direction == DIRECTION_LONG: + pos.positionProfit = (cur_price - open_cost_price) * (pos.position * size) + else: + pos.positionProfit = (open_cost_price - cur_price) * (pos.position * size) # 读取冻结 if pos.direction is DIRECTION_LONG: diff --git a/vnpy/trader/gateway/ctpseGateway/ctpseGateway.py b/vnpy/trader/gateway/ctpseGateway/ctpseGateway.py index bd2aa40b..8679da0a 100644 --- a/vnpy/trader/gateway/ctpseGateway/ctpseGateway.py +++ b/vnpy/trader/gateway/ctpseGateway/ctpseGateway.py @@ -1054,6 +1054,7 @@ class CtpTdApi(TdApi): exchange = self.symbolExchangeDict.get(pos.symbol, EXCHANGE_UNKNOWN) + yd_position = data['YdPosition'] # 针对上期所持仓的今昨分条返回(有昨仓、无今仓),读取昨仓数据 if exchange == EXCHANGE_SHFE: if data['YdPosition'] and not data['TodayPosition']: @@ -1080,13 +1081,22 @@ class CtpTdApi(TdApi): pre_settlement_price = data['PreSettlementPrice'] # 开仓均价 open_cost_price = data['OpenCost'] / (pos.position * size) - # 逐笔盈亏 = (上一交易日结算价 - 开仓价)* 持仓数量 * 杠杆 + 当日持仓收益 - if pos.direction == DIRECTION_LONG: - pre_profit = (pre_settlement_price - open_cost_price) * (pos.position * size) - else: - pre_profit = (open_cost_price - pre_settlement_price) * (pos.position * size) - pos.positionProfit = pos.positionProfit + pre_profit + data['PositionProfit'] + cur_price = self.gateway.symbol_price_dict.get(pos.vtSymbol, None) + if cur_price is None: + + # 逐笔盈亏 = (上一交易日结算价 - 开仓价)* 昨仓持仓数量 * 杠杆 + 当日持仓收益 + if pos.direction == DIRECTION_LONG: + pre_profit = (pre_settlement_price - open_cost_price) * (yd_position * size) + else: + pre_profit = (open_cost_price - pre_settlement_price) * (yd_position * size) + pos.positionProfit = pos.positionProfit + pre_profit + data['PositionProfit'] + else: + # 逐笔盈亏 = (当前价 - 开仓价)* 持仓数量 * 杠杆 + if pos.direction == DIRECTION_LONG: + pos.positionProfit = (cur_price - open_cost_price) * (pos.position * size) + else: + pos.positionProfit = (open_cost_price - cur_price) * (pos.position * size) # 读取冻结 if pos.direction is DIRECTION_LONG: @@ -1106,7 +1116,7 @@ class CtpTdApi(TdApi): self.posDict.clear() except Exception as ex: self.gateway.writeError('onRspQryInvestorPosition exception:{}'.format(str(ex))) - self.gateway.writeError('traceL{}'.format(traceback.format_exc())) + self.gateway.writeError('trace {}'.format(traceback.format_exc())) #---------------------------------------------------------------------- diff --git a/vnpy/trader/vtGateway.py b/vnpy/trader/vtGateway.py index c4f6c9ee..6e7f5764 100644 --- a/vnpy/trader/vtGateway.py +++ b/vnpy/trader/vtGateway.py @@ -26,6 +26,8 @@ class VtGateway(object): # 所有订阅onBar的都会添加 self.klines = {} + self.symbol_price_dict = {} + # ---------------------------------------------------------------------- def onTick(self, tick): """市场行情推送""" @@ -34,6 +36,11 @@ class VtGateway(object): event1.dict_['data'] = tick self.eventEngine.put(event1) + if tick.lastPrice is not None or tick.lastPrice != 0: + self.symbol_price_dict.update({tick.vtSymbol: tick.lastPrice}) + elif tick.askPrice1 is not None and tick.bidPrice1 is not None: + self.symbol_price_dict.update({tick.vtSymbol: (tick.askPrice1 + tick.bidPrice1)/2}) + # 特定合约代码的事件 event2 = Event(type_=EVENT_TICK+tick.vtSymbol) event2.dict_['data'] = tick @@ -45,6 +52,8 @@ class VtGateway(object): event = Event(type_=type) event.dict_['data'] = bar self.eventEngine.put(event) + self.writeLog(u'Onbar Event:{},{},o:{},h:{},l:{},c:{}'.format(bar.vtSymbol, bar.datetime, bar.open, + bar.high, bar.low, bar.close)) # ---------------------------------------------------------------------- def onTrade(self, trade): @@ -108,13 +117,6 @@ class VtGateway(object): event1.dict_['data'] = error self.eventEngine.put(event1) - logMsg = u'{} {}:[{}]:{}'.format(datetime.now(), error.gatewayName, error.errorID,error.errorMsg ) - # 写入本地log日志 - if self.logger: - self.logger.error(logMsg) - print(logMsg,file=sys.stderr) - else: - self.createLogger() # ---------------------------------------------------------------------- def onLog(self, log): @@ -222,7 +224,6 @@ class VtGateway(object): if self.logger: self.logger.error(content) - # ---------------------------------------------------------------------- def printDict(self,d): """返回dict的字符串类型"""