abeplus策略,感觉不对,没有及时平仓

老师好

   最近看了经典课程讲座aberratioon,

参照这写了abeplus程序,跑出来不对劲,突破开仓库,盈利了收盘跌破了,怎么没有平仓。请指导指导,K线图如下,

 

程序如下

//---

Params
   Numeric length(80);          //周期
   Numeric  lots(1);        
Vars

   Series<numeric>  avgout(0);   //均线1
   Series<numeric>  avgout2(0);   //均线2
   Series<numeric>  avgout3(0);  //均线3
   Series<numeric>  avgout4(0);  //均线4
   Series<numeric>  avgout5(0);  //均线5
   
   Series<numeric>  myvolav(0);
   Series<numeric>  lps(0);
   Series<numeric>   sps(0);
   Series<numeric>    ps(0);
   Series<numeric>    crit(0);
   
   Series<numeric>    leprice(0);
   Series<numeric>    seprice(0);
   Series<numeric>    sigma(0);
   
   Events
     OnBar(ArrayRef<Integer> indexs)
     { //+过滤开盘集合竞价函数
         
         //均线指标计算   +
         Avgout = Average(close,length);                         //均线1  80
         Avgout2 = Average(close,length-intpart( length/8));  //均线2     70
         Avgout3 = Average(close,length-intpart( length/8)*2);  //均线3   60
         Avgout4 = Average(close,length-intpart( length/8)*3); //均线4    50
         Avgout5 = Average(close,length-intpart( length/8)*4); //均线5    40
         PlotNumeric( "Avgout",Avgout);PlotNumeric( "Avgout2",Avgout2);PlotNumeric( "Avgout3",Avgout3);PlotNumeric( "Avgout4",Avgout4);PlotNumeric( "Avgout5",Avgout5);
         
       sigma = (StandardDev( close,length)*2);  //标准差
       myvolav=( sigma+sigma[1]+sigma[2])/3;    //最近三天标准差的平均值
       leprice=avgout+sigma;                    //上轨道  均线+标准差
       seprice= avgout-sigma;                   //下轨道  均线-标准差
        PlotNumeric( "leprice",leprice);   PlotNumeric( "seprice",seprice);     
         
//突破一倍标准差,进行建仓。限制条件中,有一条是必须跌回了最长期的均线后才能开仓,即lps=0.       
if( MarketPosition< 1 And Close[1] > leprice[1] and sigma[1] > myvolav[1] And lps==0 )   //非多仓,收盘价大于上轨 and 标准差>近3个标准差平均值,     
{
    ps=Close[1]+sigma[1];
    crit=1;
    Buy(lots,Open) ;    //开多仓
    
    }
    
if( MarketPosition > -1 And Close[1] < seprice[1] And sigma[1] > myvolav[1] And sps==0 ) //非空仓,收盘价小于上轨 and 标准差>近3个标准差平均值,
{
    SellShort( lots ,Open) ;  //开空仓
    ps=Close[1]-sigma[1];
    crit=1;
    
}     

// 更新盈利级别,按照sigma递增
  If(MarketPosition ==1 And Close[1] > ps )    
  {
      crit=crit+1;                     //盈利一次,crit就加1
      ps=Close[1]+sigma[1];            //同时ps也加标准差
      
      }
      
      If( MarketPosition==-1 And Close[1] < ps)
      {
          crit=crit+1;
          ps=Close[1]-sigma[1];
      
      }
                 
//跌破均线,进行平仓。盈利越多,跌破的均线周期越短         
 if(MarketPosition==1 And close[1] < avgout[1] And crit==1 )   //盈利1个标准差,跌破均线就平仓
  {
      Sell(0,Open) ;
      lps=0;                    //跌破均线,lps=0,开关
          
    }
    
  If( MarketPosition ==1 And Close[1] < avgout2[1] And crit ==2 )   //盈利2个标准差,跌破均线就平仓
  {
      Sell(0,Open) ;
      lps=1;
      
  }
    
  If( MarketPosition ==1 And Close[1] < avgout3[1] And crit ==3  )  // //盈利3个标准差,跌破均线就平仓
  {
      Sell(0,Open) ;
      lps=1;
      
      }
          
  If( MarketPosition ==1 And Close[1] < avgout4[1] And crit ==4  )  // //盈利4个标准差,跌破均线就平仓
  {
      Sell(0,Open) ;
      lps=1;
      
      }            
                  
 If( MarketPosition ==1 And Close[1] < avgout5[1] And crit ==5  )   // //盈利5个标准差,跌破均线就平仓
  {
      Sell(0,Open) ;
      lps=1;
      
      }                     
                          
  If( MarketPosition ==1 And crit ==6  )   //盈利6个标准差,跌破均线就平仓
  {
      Sell(0,Open) ;
      lps=1;
      
      }                            
                                  
      //空仓时,                                
  If( MarketPosition ==-1 And Close[1] > Avgout[1] And crit ==1  )
  {
      BuyToCover(0,Open) ;
      sps=1;
      
      } 
                                          
 If( MarketPosition ==-1 And Close[1] > Avgout2[1] And crit ==2  )
  {
      BuyToCover(0,Open) ;
      sps=1;
      
      }                                                                                   
                                                                                                                            
 If( MarketPosition ==-1 And Close[1] > Avgout3[1] And crit ==3  )
  {
      BuyToCover(0,Open) ;
      sps=1;
      
      }                                                                                                                                                                     
                                                                                                                                                                                                              
 If( MarketPosition ==-1 And Close[1] > Avgout4[1] And crit ==4  )
  {
      BuyToCover(0,Open) ;
      sps=1;
      
      }                                                                                                                                                                                                                                                       
                                                                                                                                                                                                                                                                                                
 If( MarketPosition ==-1 And Close[1] > Avgout5[1] And crit ==5  )
  {
      BuyToCover(0,Open) ;
      sps=1;
      
      }                                                                                                                                                                                                                                                                                                                                                                                  
                                                        
  if(MarketPosition ==-1 And crit ==6 )        //盈利6个标准差就直接平仓
  {
      BuyToCover(0,Open) ;
      sps=1;
      
      }
      
      
 
  //只有当跌破最长期的均线后,才能准备下一次的开仓
  If( sps ==1 And close[1] > Avgout[1])  sps=0;    //收盘价大于80均线,sps=0,开空仓库条件
  
   If( lps ==1 And close[1] < Avgout[1])  lps=0;   //收盘价小于80均线,lps=0,开多仓库条件
      
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                       
         }
   
   
   
   
   
   

 

基础数据读取的及时性
交易套利合约,满足平仓条件策略没有平仓且信号消失
简语言有没有开仓平仓的策略案例?
策略图表信号不对
感觉是个bug
划线错误(图中白色均线位置感觉不对,是哪里出错了?请老师看看)
模拟交易价格是按照策略,,但是信号不对
老师好,图形加载程序怎么感觉没有执行啊
换月仓位不对
关于开仓策略与平仓策略对接

先定位到哪根K线不对,然后按代码逐行看

学会调试

可以针对这个例子,在周四视频讲座讲讲这个吧