bug fix,取消当前价计算

This commit is contained in:
msincenselee 2019-06-04 15:43:27 +08:00
parent 435593ef04
commit 3cd1ccc479
2 changed files with 48 additions and 45 deletions

View File

@ -1008,6 +1008,11 @@ class CtpTdApi(TdApi):
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def onRspQryInvestorPosition(self, data, error, n, last): def onRspQryInvestorPosition(self, data, error, n, last):
"""持仓查询回报""" """持仓查询回报"""
if self.gateway.debug:
print('onRspQryInvestorPosition')
print(u'data:\n{}'.format(self.gateway.printDict(data)))
print(u'error:{}'.format(error))
print('n:{},last:{}'.format(n,last))
if not data['InstrumentID']: if not data['InstrumentID']:
return return
@ -1032,47 +1037,49 @@ 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']:
pos.ydPosition = data['Position'] pos.ydPosition = data['Position']
yd_position = data['YdPosition']
else:
yd_position = 0
# 否则基于总持仓和今持仓来计算昨仓数据 # 否则基于总持仓和今持仓来计算昨仓数据
else: else:
pos.ydPosition = data['Position'] - data['TodayPosition'] pos.ydPosition = data['Position'] - data['TodayPosition']
yd_position = data['Position'] - data['TodayPosition']
# 计算成本 # 计算成本
if pos.symbol not in self.symbolSizeDict: if pos.symbol not in self.symbolSizeDict:
return return
size = self.symbolSizeDict[pos.symbol] size = self.symbolSizeDict[pos.symbol]
cost = pos.price * pos.position * size
# 汇总总仓 # 前汇总,总成本
pre_cost = pos.price * pos.position * size
# 仓位累加汇总
pos.position += data['Position'] pos.position += data['Position']
# 计算持仓均价
if pos.position and size: if pos.position and size:
#pos.price = (cost + data['PositionCost']) / (pos.position * size) # 计算持仓均价
pos.price = (cost + data['OpenCost']) / (pos.position * size) pos.price = (pre_cost + data['OpenCost']) / (pos.position * size)
# 上一交易日结算价 # 上一交易日结算价
pre_settlement_price = data['PreSettlementPrice'] pre_settlement_price = data['PreSettlementPrice']
# 开仓均价
open_cost_price = data['OpenCost'] / (pos.position * size)
cur_price = self.gateway.symbol_price_dict.get(pos.vtSymbol, None) # 当前一笔的开仓均价
if cur_price is None: open_cost_price = data['OpenCost'] / (data['Position'] * size)
# 逐笔盈亏 = (上一交易日结算价 - 开仓价)* 持仓数量 * 杠杆 + 当日持仓收益
pre_profit = 0
# 上-交易日收益 = (上一交易日结算价 - 开仓价)* 昨仓持仓数量 * 杠杆
if pos.direction == DIRECTION_LONG: if pos.direction == DIRECTION_LONG:
pre_profit = (pre_settlement_price - open_cost_price) * (yd_position * size) pre_profit = (pre_settlement_price - open_cost_price) * (yd_position * size)
else: else:
pre_profit = (open_cost_price - pre_settlement_price) * (yd_position * size) pre_profit = (open_cost_price - pre_settlement_price) * (yd_position * size)
# 汇总收益:上一收益 + 上-交易日收益 + 当日持仓收益
pos.positionProfit = pos.positionProfit + pre_profit + data['PositionProfit'] 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

@ -1027,7 +1027,7 @@ class CtpTdApi(TdApi):
if self.gateway.debug: if self.gateway.debug:
print('onRspQryInvestorPosition') print('onRspQryInvestorPosition')
print(u'data:{}'.format(data)) print(u'data:\n{}'.format(self.gateway.printDict(data)))
print(u'error:{}'.format(error)) print(u'error:{}'.format(error))
print('n:{},last:{}'.format(n,last)) print('n:{},last:{}'.format(n,last))
@ -1054,49 +1054,48 @@ 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']:
pos.ydPosition = data['Position'] pos.ydPosition = data['Position']
yd_position = data['YdPosition']
else:
yd_position = 0
# 否则基于总持仓和今持仓来计算昨仓数据 # 否则基于总持仓和今持仓来计算昨仓数据
else: else:
pos.ydPosition = data['Position'] - data['TodayPosition'] pos.ydPosition = data['Position'] - data['TodayPosition']
yd_position = data['Position'] - data['TodayPosition']
# 计算成本 # 计算成本
if pos.symbol not in self.symbolSizeDict: if pos.symbol not in self.symbolSizeDict:
return return
size = self.symbolSizeDict[pos.symbol] size = self.symbolSizeDict[pos.symbol]
cost = pos.price * pos.position * size
# 汇总总仓 # 前汇总,总成本
pre_cost = pos.price * pos.position * size
# 仓位累加汇总
pos.position += data['Position'] pos.position += data['Position']
# 计算持仓均价
if pos.position and size: if pos.position and size:
#pos.price = (cost + data['PositionCost']) / (pos.position * size) # 计算持仓均价
pos.price = (cost + data['OpenCost']) / (pos.position * size) pos.price = (pre_cost + data['OpenCost']) / (pos.position * size)
# 上一交易日结算价 # 上一交易日结算价
pre_settlement_price = data['PreSettlementPrice'] pre_settlement_price = data['PreSettlementPrice']
# 开仓均价
open_cost_price = data['OpenCost'] / (pos.position * size)
cur_price = self.gateway.symbol_price_dict.get(pos.vtSymbol, None) # 当前一笔的开仓均价
if cur_price is None: open_cost_price = data['OpenCost'] / (data['Position'] * size)
# 逐笔盈亏 = (上一交易日结算价 - 开仓价)* 昨仓持仓数量 * 杠杆 + 当日持仓收益 pre_profit = 0
# 上-交易日收益 = (上一交易日结算价 - 开仓价)* 昨仓持仓数量 * 杠杆
if pos.direction == DIRECTION_LONG: if pos.direction == DIRECTION_LONG:
pre_profit = (pre_settlement_price - open_cost_price) * (yd_position * size) pre_profit = (pre_settlement_price - open_cost_price) * (yd_position * size)
else: else:
pre_profit = (open_cost_price - pre_settlement_price) * (yd_position * size) pre_profit = (open_cost_price - pre_settlement_price) * (yd_position * size)
# 汇总收益:上一收益 + 上-交易日收益 + 当日持仓收益
pos.positionProfit = pos.positionProfit + pre_profit + data['PositionProfit'] 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:
@ -1107,8 +1106,6 @@ class CtpTdApi(TdApi):
# 查询回报结束 # 查询回报结束
if last: if last:
# 遍历推送 # 遍历推送
if self.gateway.debug:
print(u'最后推送')
for pos in list(self.posDict.values()): for pos in list(self.posDict.values()):
self.gateway.onPosition(pos) self.gateway.onPosition(pos)
@ -1118,7 +1115,6 @@ class CtpTdApi(TdApi):
self.gateway.writeError('onRspQryInvestorPosition exception:{}'.format(str(ex))) self.gateway.writeError('onRspQryInvestorPosition exception:{}'.format(str(ex)))
self.gateway.writeError('trace {}'.format(traceback.format_exc())) self.gateway.writeError('trace {}'.format(traceback.format_exc()))
#---------------------------------------------------------------------- #----------------------------------------------------------------------
def onRspQryTradingAccount(self, data, error, n, last): def onRspQryTradingAccount(self, data, error, n, last):
"""资金账户查询回报""" """资金账户查询回报"""