OnBar里实时止损后仓位没更新


// 全局变量

Vars

   // ===== 结构价格 =====

   Series<Numeric> StructPrice1(0);

   Series<Numeric> StructPrice2(0);


   // ===== 止损价格 =====

   Series<Numeric> StopLossLong(0);

   Series<Numeric> StopLossShort(0);


   // ===== 当前bar状态 =====

   Numeric ExitLongThisBar(0);

   Numeric ExitShortThisBar(0);


   // ===== 全局状态 =====

   Global Bool PendingOpen(False);

   Global Bool PendingClose(False);


   // ===== 日志 =====

   String LogPath("D:\\strategy_log.txt");


// 主事件


Events

// Bar收盘逻辑(信号层)


OnBarClose(ArrayRef<Integer> indexes)

{

   // bar初始化

   ExitLongThisBar = 0;

   ExitShortThisBar = 0;


   // 时间过滤

   If(交易时间过滤)

   {

       // 数据长度保护


       If(BarCount() < 最小bar数量)

           Return;

       // 更新结构价格

       If(满足某结构)

       {

           StructPrice2 = StructPrice1;

           StructPrice1 = 某价格;

       }


       // 多头平仓逻辑

       If(MarketPosition == 1

           And 满足多头离场条件)

       {

           Sell(0, Close);

           ExitLongThisBar = 1;

           StopLossLong = 0;

           FileAppend(LogPath,

               "多头平仓日志");

       }


       // 空头平仓逻辑

       If(MarketPosition == -1

           And 满足空头离场条件)

       {

           BuyToCover(0, Close);

           ExitShortThisBar = 1;

           StopLossShort = 0;

           FileAppend(LogPath,

               "空头平仓日志");

       }


       // 开多逻辑

       If(MarketPosition == 0

           And 满足开多条件)

       {

           Buy(1, Close);

           StopLossLong = 某止损价;

           FileAppend(LogPath,

               "开多日志");

       }


       // 开空逻辑

       If(MarketPosition == 0

           And 满足开空条件)

       {

           SellShort(1, Close);

           StopLossShort = 某止损价;

           FileAppend(LogPath,

               "开空日志");

       }

   }


// Tick逻辑(实时风控层)

OnBar(ArrayRef<Integer> indexes)

{

   // tick级收盘强平

   If(进入tick收盘区间)

   {

       If(MarketPosition == 1)

       {

           Sell(0, Close);

       }

       If(MarketPosition == -1)

       {

           BuyToCover(0, Close);

       }

   }


   // 多头实时止损

   If(MarketPosition == 1

       And 满足多头实时止损)

   {

       Sell(0, Close);

       ExitLongThisBar = 1;

       StopLossLong = 0;

   }


   // 空头实时止损

   If(MarketPosition == -1

       And 满足空头实时止损)

   {

       BuyToCover(0, Close);

       ExitShortThisBar = 1;

       StopLossShort = 0;

   }


   // 多头结构破坏止损

   If(MarketPosition == 1

       And 满足结构破坏)

   {

       Sell(0, Close);

       ExitLongThisBar = 1;

       StopLossLong = 0;

   }


   // 空头结构破坏止损

   If(MarketPosition == -1

       And 满足结构破坏)

   {

       BuyToCover(0, Close);

       ExitShortThisBar = 1;

       StopLossShort = 0;

   }

}


这是我的整体的架构,在OnbarClose里面满足条件用close开仓和平仓,在Onbar里面也用close实时止损。现在碰到个问题,当Onbar里面止损之后仓位不会实时更新,导致在OnbarClose里面读取到仓位还不是0就不会开仓。请问这个问题有没有好的办法。

还有我一直碰到信号闪烁导致不成交,现在不知道是在OnbarClose还是Onbar在里面出现的,要怎么优化。

请问老师问有好的建议吗?








实时止损问题
实时止损问题
开仓K实时开仓,并同K实时止损
请教:实时行情下MarketPosition不能实时更新
实际仓位少于要平仓的数量,结果没平仓成功,这种代码怎么写?
明明有开仓信号,结果成交结果里没有,委托没成交里也没有
Onbar OnTick谁先更新的问题?
请教如何在事件onbar域里判断当前的数据源是实时数据还是历史回测数据
开仓后如何设置止损线,及如何移动止损线?
仓位异常