双均线策略

程序理念:1、使用5、10周期均线,金叉开多单,死叉平仓。止损设置为开仓当根K线的前5根k线的最低点;2、出现盘中金叉,收盘未金叉的情况,次根k线平仓;出现盘中死叉、收盘未死叉的情况,次根K线开仓;3、突破近5根k线的最高点且无持仓时,买入开仓;4、自开仓以来存在大于开仓当根K线最高点的价位,且出现死叉时,平仓。

使用时,出现一下情况,1、设置的止损未起作用,仍然出现横盘阶段反复开平、磨损的情况;2、添加上述第4条那个条件后,复盘时,K线不再显示买入、卖出的箭头以及表示盈亏的虚线,是否不起作用了。

源代码如下(程序理念那个第4条暂未添加):

请问该如何优化?谢谢!

“把双均线策略改写成A函数报单”的意义是什么呢?
如何运用Spyder运行官网的双均线策略进行历史回测?
双均线策略自动下单问题
【回测异常】基于双均线策略做多入场点异常,求优化方案
双均线策略修改后为啥不能开仓呢?
双均线策略开仓点提前的问题
我想在双均线策略里,加一个振幅在一定宽度内不做的程序语言
请老师帮我在双均线策略中,加上以ATR数值为止损的程序段
双均线策略,如何通过数学计算来表达金叉或死叉,老师给写一下
问问策略预警问题


略微增加调试代码

不是可以止损的吗

        PlotNumeric("ProtectStopPrice", ProtectStopPrice);
        
        If(MarketPosition == 1 And BarsSinceEntry > 0 And Low <= ProtectStopPrice)
        {
            Sell(PostionLots,  ProtectStopPrice);
            PlotAuto("zhi", "止损",close);
        }


然后请针对你描述的所有问题,举例

比如你认为 “设置的止损未起作用”

请给出什么品种,几分几秒,图上哪里没止损

1、止损设置为开仓当根K线的前5根k线的最低点;

代码里体现不出来你固定了止损位开仓当根,只能看出来你随时在更新为前五根低点的0.99

如果你要固定,要写在开仓代码下面

2、你用Opint1变量来过滤假金叉死叉,这个变量的数据你确定是这么算的吗?画出来是这样

玫粉色这根

谢谢,请问如果要设置开仓价位当根K线,前5根K线最低点下2个基点,该怎么设置?



 Params

   Numeric EFast(5);       //短均线周期参数

   Numeric ESlow(10);      //长均线周期参数

   Numeric StopLength(5); //止损统计周期数

   

   Numeric PostionLots(1);//头寸

   

Vars

   Series<Numeric> MAEFast;    //短均线

   Series<Numeric> MAESlow;    //长均线    

   Series<Numeric> Opint1;     // 逆解交叉点值

   Series<Numeric> AvgTR;                    

   Numeric ProtectStopPrice;  //保护性止损价

   Numeric TrailStopPrice;    //跟踪性止损价    

   Series<Numeric> MyEntryPrice;  // 开仓价格  

Events

   OnBar(ArrayRef<Integer> indexs)

   {

       MAEFast = Average(Close, EFast); //短均线

       MAESlow = Average(Close, ESlow); //长均线

   

       Opint1 = ((ESlow - 1) * Average(Close[1], ESlow - 1) * EFast - (EFast - 1) * Average(Close[1], EFast - 1) * ESlow) / (ESlow - EFast);

       ProtectStopPrice = Lowest(Low[1],StopLength) * 0.99;

       TrailStopPrice = Highest(High[1],StopLength) * 1.01;


       

       If(MarketPosition <> 1 And MAEFast[1]<MAESlow[1] and High >= Opint1)

       {

           Buy(PostionLots, Max(Open, Opint1));  

           MyEntryPrice = Max(Open, Opint1);

                 

       }

       If(MarketPosition<>1 And Low[1]<=Opint1[1] And Close[1]>=Opint1[1] And High >= Opint1)           //过滤假死叉

       {

           Buy(PostionLots,Max(Open,Opint1));

           

       }

      If(MarketPosition == 1 And BarsSinceEntry > 0 And Low <= ProtectStopPrice)

       {

          Sell(PostionLots,  ProtectStopPrice);

       }

       If(MarketPosition == 1 And BarsSinceEntry > 0 And High[1]>=Opint1[1] And Close[1]<=Opint1[1] And Low <= Opint1)         //过滤假金叉

       {  

           Sell(PostionLots, Min(Open, opint1));

           

       }

       If(MarketPosition == 1 And BarsSinceEntry > 0 And MAEFast[1]> MAESlow[1] And Low<Opint1)

   

       {

           Sell(PostionLots,Min(Open,Opint1));

       }

   

           Range[0:DataSourceSize() - 1]

           {

               PlotNumeric("MAEFast", MAEFast);

               PlotNumeric("MAESlow", MAESlow);

           }

   

你贴的3个代码都没有贴全 ,无法运行

是全的啊


1.贴代码用代码模式

2.止损未起作用 ,是怎么个不起作用

3.优化一般指优化参数,不要和改错 改bug混淆



  Params
    Numeric EFast(5);       //短均线周期参数
    Numeric ESlow(10);      //长均线周期参数
    Numeric StopLength(5); //止损统计周期数
    
    Numeric PostionLots(1);//头寸 
    
Vars
    Series<Numeric> MAEFast;    //短均线
    Series<Numeric> MAESlow;    //长均线    
    Series<Numeric> Opint1;     // 逆解交叉点值 
    Series<Numeric> AvgTR;                    
    Numeric ProtectStopPrice;  //保护性止损价
    Numeric TrailStopPrice;    //跟踪性止损价    
    Series<Numeric> MyEntryPrice;  // 开仓价格   
Events
    OnBar(ArrayRef<Integer> indexs)
    {
        MAEFast = Average(Close, EFast); //短均线
        MAESlow = Average(Close, ESlow); //长均线
    
        Opint1 = ((ESlow - 1) * Average(Close[1], ESlow - 1) * EFast - (EFast - 1) * Average(Close[1], EFast - 1) * ESlow) / (ESlow - EFast);
        ProtectStopPrice = Lowest(Low[1],StopLength) * 0.99; 
        TrailStopPrice = Highest(High[1],StopLength) * 1.01;

        
        If(MarketPosition <> 1 And MAEFast[1]<MAESlow[1] and High >= Opint1)
        {
            Buy(PostionLots, Max(Open, Opint1));  
            MyEntryPrice = Max(Open, Opint1); 
                  
        }
        If(MarketPosition<>1 And Low[1]<=Opint1[1] And Close[1]>=Opint1[1] And High >= Opint1)           //过滤假死叉
        {
            Buy(PostionLots,Max(Open,Opint1));
            
        }
       If(MarketPosition == 1 And BarsSinceEntry > 0 And Low <= ProtectStopPrice)
        {
           Sell(PostionLots,  ProtectStopPrice);
        }
        If(MarketPosition == 1 And BarsSinceEntry > 0 And High[1]>=Opint1[1] And Close[1]<=Opint1[1] And Low <= Opint1)         //过滤假金叉
        {  
            Sell(PostionLots, Min(Open, opint1));
            
        }
        If(MarketPosition == 1 And BarsSinceEntry > 0 And MAEFast[1]> MAESlow[1] And Low<Opint1)
    
        {
            Sell(PostionLots,Min(Open,Opint1));
        }
    
            Range[0:DataSourceSize() - 1]
            {
                PlotNumeric("MAEFast", MAEFast);
                PlotNumeric("MAESlow", MAESlow);
            }
    



  Params
    Numeric EFast(5);       //短均线周期参数
    Numeric ESlow(10);      //长均线周期参数
    Numeric StopLength(5); //止损统计周期数
    
    Numeric PostionLots(1);//头寸 
    
Vars
    Series<Numeric> MAEFast;    //短均线
    Series<Numeric> MAESlow;    //长均线    
    Series<Numeric> Opint1;     // 逆解交叉点值 
    Series<Numeric> AvgTR;                    
    Numeric ProtectStopPrice;  //保护性止损价
    Numeric TrailStopPrice;    //跟踪性止损价    
    Series<Numeric> MyEntryPrice;  // 开仓价格   
Events
    OnBar(ArrayRef<Integer> indexs)
    {
        MAEFast = Average(Close, EFast); //短均线
        MAESlow = Average(Close, ESlow); //长均线
    
        Opint1 = ((ESlow - 1) * Average(Close[1], ESlow - 1) * EFast - (EFast - 1) * Average(Close[1], EFast - 1) * ESlow) / (ESlow - EFast);
        ProtectStopPrice = Lowest(Low[1],StopLength) * 0.99; 
        TrailStopPrice = Highest(High[1],StopLength) * 1.01;

        
        If(MarketPosition <> 1 And MAEFast[1]<MAESlow[1] and High >= Opint1)
        {
            Buy(PostionLots, Max(Open, Opint1));  
            MyEntryPrice = Max(Open, Opint1); 
                  
        }
        If(MarketPosition<>1 And Low[1]<=Opint1[1] And Close[1]>=Opint1[1] And High >= Opint1)           //过滤假死叉
        {
            Buy(PostionLots,Max(Open,Opint1));
            
        }
       If(MarketPosition == 1 And BarsSinceEntry > 0 And Low <= ProtectStopPrice)
        {
           Sell(PostionLots,  ProtectStopPrice);
        }
        If(MarketPosition == 1 And BarsSinceEntry > 0 And High[1]>=Opint1[1] And Close[1]<=Opint1[1] And Low <= Opint1)         //过滤假金叉
        {  
            Sell(PostionLots, Min(Open, opint1));
            
        }
        If(MarketPosition == 1 And BarsSinceEntry > 0 And MAEFast[1]> MAESlow[1] And Low<Opint1)
    
        {
            Sell(PostionLots,Min(Open,Opint1));
        }
    
            Range[0:DataSourceSize() - 1]
            {
                PlotNumeric("MAEFast", MAEFast);
                PlotNumeric("MAESlow", MAESlow);
            }
    


设置了sell前5周期最低点,但是体现在k线上,没有平掉


设置了sell前5周期最低点,但是体现在k线上,没有平掉