0
点赞
收藏
分享

微信扫一扫

量化交易之vnpy篇 - 几种同步发单模式(中金所股指锁仓模式、最小单边轧差操作模式、双边同步模式,净头寸模式)

全栈学习笔记 2023-03-02 阅读 46


""" 发单逻辑部分 """

def tqz_synchronization_position_cffex_lock_mode(self, market_vt_symbol, now_price, offset_price, strategy_position_net, real_position_net):
"""
synchronization position with lock mode(cffex mode)
"""

vt_orderids = []
buy_price = min(now_price + offset_price, self.vt_symbols_limit_prices[market_vt_symbol].limit_up)
sell_price = max(now_price - offset_price, self.vt_symbols_limit_prices[market_vt_symbol].limit_down)

if strategy_position_net >= 0 and real_position_net >= 0:

if strategy_position_net > real_position_net:

lot = TQZPositionData.tqz_risk_control(lot=strategy_position_net-real_position_net)
print(f'开多 {str(lot)} 手', end=" ")
vt_orderids = self.buy(vt_symbol=market_vt_symbol, price=buy_price, volume=lot, lock=True)
print(f'vt_orderids: {vt_orderids}')

elif strategy_position_net < real_position_net:

lot = TQZPositionData.tqz_risk_control(lot=real_position_net - strategy_position_net)
print(f'平多 {str(lot)} 手', end=" ")
vt_orderids = self.sell(vt_symbol=market_vt_symbol, price=sell_price, volume=lot, lock=True)
print(f'vt_orderids: {vt_orderids}')

elif strategy_position_net is real_position_net:
print(f'净仓相等, 不做处理')

elif strategy_position_net >= 0 and real_position_net <= 0:

lot = TQZPositionData.tqz_risk_control(lot=strategy_position_net-real_position_net)
print(f'开多 {str(lot)} 手', end=" ")
vt_orderids = self.buy(vt_symbol=market_vt_symbol, price=buy_price, volume=lot, lock=True)
print(f'vt_orderids: {vt_orderids}')

elif strategy_position_net <= 0 and real_position_net >= 0:

lot = TQZPositionData.tqz_risk_control(lot=real_position_net - strategy_position_net)
print(f'开空 {str(lot)} 手', end=" ")
vt_orderids = self.short(vt_symbol=market_vt_symbol, price=sell_price, volume=lot, lock=True)
print(f'vt_orderids: {vt_orderids}')

elif strategy_position_net <= 0 and real_position_net <= 0:

if abs(strategy_position_net) > abs(real_position_net):

lot = TQZPositionData.tqz_risk_control(lot=abs(strategy_position_net)-abs(real_position_net))
print(f'开空 {str(lot)} 手', end=" ")
vt_orderids = self.short(vt_symbol=market_vt_symbol, price=sell_price, volume=lot, lock=True)
print(f'vt_orderids: {vt_orderids}')

elif abs(strategy_position_net) < abs(real_position_net):

lot = TQZPositionData.tqz_risk_control(lot=abs(real_position_net) - abs(strategy_position_net))
print(f'平空 {str(lot)} 手', end=" ")
vt_orderids = self.cover(vt_symbol=market_vt_symbol, price=buy_price, volume=lot, lock=True)
print(f'vt_orderids: {vt_orderids}')

elif strategy_position_net is real_position_net:
print(f'净仓相等, 不做处理')

return vt_orderids

def tqz_synchronization_position_min_netting_mode(self, market_vt_symbol, now_price, offset_price, strategy_position_buy, strategy_position_sell, real_position_buy, real_position_sell):
"""
synchronization position in min netting with double direction(buy direction & sell direction) mode.
"""

net_buy_abs = abs(strategy_position_buy - real_position_buy)
net_sell_abs = abs(strategy_position_sell - real_position_sell)
buy_vt_orderids = []
sell_vt_orderids = []

buy_price = min(now_price + offset_price, self.vt_symbols_limit_prices[market_vt_symbol].limit_up)
sell_price = max(now_price - offset_price, self.vt_symbols_limit_prices[market_vt_symbol].limit_down)

interval = " | "
print(market_vt_symbol, end=" ")
if net_buy_abs >= net_sell_abs:

if strategy_position_buy < real_position_buy:
lot = TQZPositionData.tqz_risk_control(lot=real_position_buy - strategy_position_buy)
print(f'平多 {str(lot)} 手', end=" ")
buy_vt_orderids = self.sell(vt_symbol=market_vt_symbol, price=sell_price, volume=lot)
print(f'sell_result: {buy_vt_orderids}', end=interval)

if strategy_position_sell > real_position_sell:
lot = TQZPositionData.tqz_risk_control(lot=strategy_position_sell - real_position_sell)
print(f'开空 {str(lot)} 手', end=" ")
sell_vt_orderids = self.short(vt_symbol=market_vt_symbol, price=sell_price, volume=lot)
print(f'short_result: {sell_vt_orderids}')

else:

if strategy_position_buy > real_position_buy:
lot = TQZPositionData.tqz_risk_control(lot=strategy_position_buy - real_position_buy)
print(f'开多 {str(lot)} 手', end=" ")
buy_vt_orderids = self.buy(vt_symbol=market_vt_symbol, price=buy_price, volume=lot)
print(f'buy_result: {buy_vt_orderids}', end=interval)

if strategy_position_sell < real_position_sell:
lot = TQZPositionData.tqz_risk_control(lot=real_position_sell - strategy_position_sell)
print(f'平空 {str(lot)} 手', end=" ")
sell_vt_orderids = self.cover(vt_symbol=market_vt_symbol, price=buy_price, volume=lot)
print(f'cover_result: {sell_vt_orderids}')

return list(set(buy_vt_orderids + sell_vt_orderids))

def tqz_synchronization_position_double_direction_mode(self, market_vt_symbol, now_price, offset_price, strategy_position_buy, strategy_position_sell, real_position_buy, real_position_sell):

buy_vt_orderids = []
sell_vt_orderids = []
buy_price = min(now_price + offset_price, self.vt_symbols_limit_prices[market_vt_symbol].limit_up)
sell_price = max(now_price - offset_price, self.vt_symbols_limit_prices[market_vt_symbol].limit_down)

interval = " | "
print(market_vt_symbol, end=" ")
if strategy_position_buy > real_position_buy:

lot = TQZPositionData.tqz_risk_control(lot=strategy_position_buy - real_position_buy)
print(f'开多 {str(lot)} 手', end=" ")
buy_vt_orderids = self.buy(vt_symbol=market_vt_symbol, price=buy_price, volume=lot)
print(f'buy_result: {buy_vt_orderids}', end=interval)

elif strategy_position_buy < real_position_buy:

lot = TQZPositionData.tqz_risk_control(lot=real_position_buy - strategy_position_buy)
print(f'平多 {str(lot)} 手', end=" ")
buy_vt_orderids = self.sell(vt_symbol=market_vt_symbol, price=sell_price, volume=lot)
print(f'sell_result: {buy_vt_orderids}', end=interval)

elif strategy_position_buy is real_position_buy:
print("多单匹配 不处理", end=interval)

if strategy_position_sell > real_position_sell:

lot = TQZPositionData.tqz_risk_control(lot=strategy_position_sell - real_position_sell)
print(f'开空 {str(lot)} 手', end=" ")
sell_vt_orderids = self.short(vt_symbol=market_vt_symbol, price=sell_price, volume=lot)
print(f'short_result: {sell_vt_orderids}')

elif strategy_position_sell < real_position_sell:

lot = TQZPositionData.tqz_risk_control(lot=real_position_sell - strategy_position_sell)
print(f'平空 {str(lot)} 手', end=" ")
sell_vt_orderids = self.cover(vt_symbol=market_vt_symbol, price=buy_price, volume=lot)
print(f'cover_result: {sell_vt_orderids}')

elif strategy_position_sell is real_position_sell:
print("空单匹配 不处理")

return list(set(buy_vt_orderids + sell_vt_orderids))

def tqz_synchronization_position_net_mode(self, market_vt_symbol, now_price, offset_price, strategy_position_buy, strategy_position_sell, real_position_buy, real_position_sell):
buy_vt_orderids = []
sell_vt_orderids = []

if strategy_position_buy > strategy_position_sell:
strategy_position_buy, strategy_position_sell = strategy_position_buy - strategy_position_sell, 0
elif strategy_position_buy < strategy_position_sell:
strategy_position_sell, strategy_position_buy = strategy_position_sell - strategy_position_buy, 0
elif strategy_position_buy is strategy_position_sell:
strategy_position_buy, strategy_position_sell = 0, 0

buy_price = min(now_price + offset_price, self.vt_symbols_limit_prices[market_vt_symbol].limit_up)
sell_price = max(now_price - offset_price, self.vt_symbols_limit_prices[market_vt_symbol].limit_down)

interval = " | "
print(market_vt_symbol, end=" ")
if strategy_position_buy > real_position_buy:

lot = TQZPositionData.tqz_risk_control(lot=strategy_position_buy - real_position_buy)
print(f'开多 {str(lot)} 手', end=" ")
buy_vt_orderids = self.buy(vt_symbol=market_vt_symbol, price=buy_price, volume=lot)
print(f'buy_result: {buy_vt_orderids}', end=interval)

elif strategy_position_buy < real_position_buy:

lot = TQZPositionData.tqz_risk_control(lot=real_position_buy - strategy_position_buy)
print(f'平多 {str(lot)} 手', end=" ")
buy_vt_orderids = self.sell(vt_symbol=market_vt_symbol, price=sell_price, volume=lot)
print(f'sell_result: {buy_vt_orderids}', end=interval)

elif strategy_position_buy is real_position_buy:
print("多单匹配 不处理", end=interval)

if strategy_position_sell > real_position_sell:

lot = TQZPositionData.tqz_risk_control(lot=strategy_position_sell - real_position_sell)
print(f'开空 {str(lot)} 手', end=" ")
sell_vt_orderids = self.short(vt_symbol=market_vt_symbol, price=sell_price, volume=lot)
print(f'short_result: {sell_vt_orderids}')

elif strategy_position_sell < real_position_sell:

lot = TQZPositionData.tqz_risk_control(lot=real_position_sell - strategy_position_sell)
print(f'平空 {str(lot)} 手', end=" ")
sell_vt_orderids = self.cover(vt_symbol=market_vt_symbol, price=buy_price, volume=lot)
print(f'cover_result: {sell_vt_orderids}')

elif strategy_position_sell is real_position_sell:
print("空单匹配 不处理")

return list(set(buy_vt_orderids + sell_vt_orderids))

 

举报

相关推荐

0 条评论