量化交易之vn.py篇 - 同步持仓发单逻辑(非净头寸 & 净头寸)

"""
    同步持仓 非净头寸发单(逻辑部分的代码)
"""

        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

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值