bug fix,修改为当前价-开仓均价

This commit is contained in:
msincenselee 2019-06-03 14:25:16 +08:00
parent 4b9b03aa3b
commit 63d5524d92
3 changed files with 39 additions and 21 deletions

View File

@ -1056,13 +1056,20 @@ class CtpTdApi(TdApi):
pre_settlement_price = data['PreSettlementPrice'] pre_settlement_price = data['PreSettlementPrice']
# 开仓均价 # 开仓均价
open_cost_price = data['OpenCost'] / (pos.position * size) 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: if pos.direction is DIRECTION_LONG:

View File

@ -1054,6 +1054,7 @@ class CtpTdApi(TdApi):
exchange = self.symbolExchangeDict.get(pos.symbol, EXCHANGE_UNKNOWN) exchange = self.symbolExchangeDict.get(pos.symbol, EXCHANGE_UNKNOWN)
yd_position = data['YdPosition']
# 针对上期所持仓的今昨分条返回(有昨仓、无今仓),读取昨仓数据 # 针对上期所持仓的今昨分条返回(有昨仓、无今仓),读取昨仓数据
if exchange == EXCHANGE_SHFE: if exchange == EXCHANGE_SHFE:
if data['YdPosition'] and not data['TodayPosition']: if data['YdPosition'] and not data['TodayPosition']:
@ -1080,13 +1081,22 @@ class CtpTdApi(TdApi):
pre_settlement_price = data['PreSettlementPrice'] pre_settlement_price = data['PreSettlementPrice']
# 开仓均价 # 开仓均价
open_cost_price = data['OpenCost'] / (pos.position * size) 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: if pos.direction is DIRECTION_LONG:
@ -1106,7 +1116,7 @@ class CtpTdApi(TdApi):
self.posDict.clear() self.posDict.clear()
except Exception as ex: except Exception as ex:
self.gateway.writeError('onRspQryInvestorPosition exception:{}'.format(str(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()))
#---------------------------------------------------------------------- #----------------------------------------------------------------------

View File

@ -26,6 +26,8 @@ class VtGateway(object):
# 所有订阅onBar的都会添加 # 所有订阅onBar的都会添加
self.klines = {} self.klines = {}
self.symbol_price_dict = {}
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
def onTick(self, tick): def onTick(self, tick):
"""市场行情推送""" """市场行情推送"""
@ -34,6 +36,11 @@ class VtGateway(object):
event1.dict_['data'] = tick event1.dict_['data'] = tick
self.eventEngine.put(event1) 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 = Event(type_=EVENT_TICK+tick.vtSymbol)
event2.dict_['data'] = tick event2.dict_['data'] = tick
@ -45,6 +52,8 @@ class VtGateway(object):
event = Event(type_=type) event = Event(type_=type)
event.dict_['data'] = bar event.dict_['data'] = bar
self.eventEngine.put(event) 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): def onTrade(self, trade):
@ -108,13 +117,6 @@ class VtGateway(object):
event1.dict_['data'] = error event1.dict_['data'] = error
self.eventEngine.put(event1) 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): def onLog(self, log):
@ -222,7 +224,6 @@ class VtGateway(object):
if self.logger: if self.logger:
self.logger.error(content) self.logger.error(content)
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
def printDict(self,d): def printDict(self,d):
"""返回dict的字符串类型""" """返回dict的字符串类型"""