"""
同步持仓 非净头寸发单(逻辑部分的代码)
"""
for market_vt_symbol, bar in bars.items():
# strategy position
strategy_position = TQZSymbolOperator.tqz_get_strategy_position(
market_vt_symbol=market_vt_symbol,
strategy_data=self.strategy_position_dictionary
)
# real position
real_position = self.tqz_get_real_position(
market_vt_symbol=market_vt_symbol,
direction=Direction.NET
)
if real_position is not strategy_position:
self.real_pos[market_vt_symbol] = real_position
print("vt_symbol:" + market_vt_symbol, end=" ")
print("bar.datetime:" + str(bar.datetime), end=" ")
if strategy_position == 0:
if real_position > 0:
lot = TQZPositionData.tqz_risk_control(lot=real_position)
print("平多 " + str(lot) + " 手")
# self.sell(vt_symbol=market_vt_symbol, price=bar.close_price, volume=lot)
elif real_position < 0:
lot = TQZPositionData.tqz_risk_control(lot=abs(real_position))
print("平空 " + str(lot) + " 手")
# self.cover(vt_symbol=market_vt_symbol, price=bar.close_price, volume=lot)
elif real_position == 0:
print("不做处理")
pass
elif strategy_position > 0:
if real_position > 0:
if strategy_position > real_position:
lot = TQZPositionData.tqz_risk_control(lot=(strategy_position - real_position))
print("开多 " + str(lot) + " 手")
# self.buy(vt_symbol=market_vt_symbol, price=bar.close_price, volume=lot)
elif strategy_position < real_position:
lot = TQZPositionData.tqz_risk_control(lot=(real_position - strategy_position))
print("平多 " + str(lot) + " 手")
# self.sell(vt_symbol=market_vt_symbol, price=bar.close_price, volume=lot)
elif strategy_position == real_position:
print("不做处理")
pass
elif real_position < 0:
lot_sell = TQZPositionData.tqz_risk_control(lot=abs(real_position))
print("平空 " + str(lot_sell) + " 手 |", end=" ")
# self.sell(vt_symbol=market_vt_symbol, price=bar.close_price, volume=lot_sell)
lot_buy = TQZPositionData.tqz_risk_control(lot=strategy_position)
print("开多 " + str(lot_buy) + " 手")
# self.buy(vt_symbol=market_vt_symbol, price=bar.close_price, volume=lot_buy)
elif real_position == 0:
lot = TQZPositionData.tqz_risk_control(lot=strategy_position)
print("开多 " + str(lot) + " 手")
# self.buy(vt_symbol=market_vt_symbol, price=bar.close_price, volume=lot)
elif strategy_position < 0:
if real_position > 0:
lot_buy = TQZPositionData.tqz_risk_control(lot=real_position)
print("平多 " + str(lot_buy) + " 手 |", end=" ")
# self.sell(vt_symbol=market_vt_symbol, price=bar.close_price, volume=lot_buy)
lot_sell = TQZPositionData.tqz_risk_control(lot=abs(strategy_position))
print("开空 " + str(lot_sell) + " 手")
# self.short(vt_symbol=market_vt_symbol, price=bar.close_price, volume=lot_sell)
elif real_position < 0:
if abs(strategy_position) > abs(real_position):
lot = TQZPositionData.tqz_risk_control(lot=abs(strategy_position) - abs(real_position))
print("开空 " + str(lot) + " 手")
# self.short(vt_symbol=market_vt_symbol, price=bar.close_price, volume=lot)
elif abs(strategy_position) < abs(real_position):
lot = TQZPositionData.tqz_risk_control(lot=abs(real_position) - abs(strategy_position))
print("平空 " + str(lot) + " 手")
# self.cover(vt_symbol=market_vt_symbol, price=bar.close_price, volume=lot)
elif strategy_position == real_position:
print("不做处理")
pass
elif real_position == 0:
lot = TQZPositionData.tqz_risk_control(lot=abs(strategy_position))
print("开空 " + str(lot) + " 手")
# self.short(vt_symbol=market_vt_symbol, price=bar.close_price, volume=lot)
"""
同步持仓 净头寸发单(逻辑部分的代码)
"""
for market_vt_symbol, bar in bars.items():
# strategy position
strategy_position = TQZSymbolOperator.tqz_get_strategy_position(
market_vt_symbol=market_vt_symbol,
strategy_data=self.strategy_position_dictionary
)
# real position
real_position_net = self.tqz_get_real_position(
market_vt_symbol=market_vt_symbol,
direction=Direction.NET
)
real_position_buy = self.tqz_get_real_position(
market_vt_symbol=market_vt_symbol,
direction=Direction.LONG
)
real_position_sell = self.tqz_get_real_position(
market_vt_symbol=market_vt_symbol,
direction=Direction.SHORT
)
if (real_position_net is not strategy_position) or (real_position_buy != 0 and real_position_sell != 0):
self.real_pos[market_vt_symbol] = real_position_net
self.write_log(str(market_vt_symbol) + "_strategy_position=" + str(strategy_position) + "|realposition=" + str(real_position_net))
print("vt_symbol:" + market_vt_symbol, end=" ")
if strategy_position == 0: # 空仓状态
if real_position_buy != 0:
real_position_buy = TQZPositionData.tqz_risk_control(lot=abs(real_position_buy))
print(f"平多 {str(real_position_buy)} 手")
# self.sell(vt_symbol=market_vt_symbol, price=bar.close_price, volume=real_position_buy)
pass
if real_position_sell != 0:
real_position_sell = TQZPositionData.tqz_risk_control(lot=abs(real_position_sell))
print(f"平空 {str(real_position_sell)} 手")
# self.cover(vt_symbol=market_vt_symbol, price=bar.close_price, volume=real_position_sell)
pass
elif strategy_position > 0: # 多单
if real_position_sell != 0:
real_position_sell = TQZPositionData.tqz_risk_control(lot=abs(real_position_sell))
print(f"平空 {str(real_position_sell)} 手")
# self.cover(vt_symbol=market_vt_symbol, price=bar.close_price, volume=real_position_sell)
pass
if abs(real_position_buy) > abs(strategy_position):
real_position_buy = TQZPositionData.tqz_risk_control(lot=abs(real_position_buy) - abs(strategy_position))
print(f"平多 {str(real_position_buy)} 手")
# self.sell(vt_symbol=market_vt_symbol, price=bar.close_price, volume=real_position_buy)
pass
elif abs(real_position_buy) < abs(strategy_position):
real_position_buy = TQZPositionData.tqz_risk_control(lot=abs(strategy_position) - abs(real_position_buy))
print(f"开多 {str(real_position_buy)} 手")
# self.buy(vt_symbol=market_vt_symbol, price=bar.close_price, volume=real_position_buy)
pass
elif abs(real_position_buy) == abs(strategy_position):
print("正常 不处理")
pass
elif strategy_position < 0: # 空单
if real_position_buy != 0:
real_position_buy = TQZPositionData.tqz_risk_control(lot=abs(real_position_buy))
print(f"平多 {str(real_position_buy)} 手")
# self.sell(vt_symbol=market_vt_symbol, price=bar.close_price, volume=real_position_buy)
pass
if abs(real_position_sell) > abs(strategy_position):
real_position_sell = TQZPositionData.tqz_risk_control(lot=abs(real_position_sell) - abs(strategy_position))
print(f"开空 {str(real_position_sell)} 手")
# self.short(vt_symbol=market_vt_symbol, price=bar.close_price, volume=real_position_sell)
pass
elif abs(real_position_sell) < abs(strategy_position):
real_position_sell = TQZPositionData.tqz_risk_control(lot=abs(strategy_position) - abs(real_position_sell))
print(f"开空 {str(real_position_sell)} 手")
# self.short(vt_symbol=market_vt_symbol, price=bar.close_price, volume=real_position_sell)
pass
elif abs(real_position_sell) == abs(strategy_position):
print("正常 不处理")
pass