• 简单均线策略


    1. 开仓条件

    开多条件

    (前一短均<=当前长均 and 当前短均>当前长均)  or  (前一短均<当前长均 && 当前短均>=当前长均)

     

    开空条件

    (前一短均>=当前长均 and 当前短均<当前长均)  or  (前一短均>当前长均 and 当前短均<=当前长均)

     

    2. 平仓条件

    平多条件

      (前一 短均线 >= 当前长均线 and 当前短均线 < 当前长均线)  or  (前一短均>当前长均 and 当前短均<=当前长均)

    平空条件

      (前一短均>=当前长均 and 当前短均<当前长均)  or  (前一短均>当前长均 and 当前短均<=当前长均)

     

    1. 当前周期内,只允许做一次某个方向的单 
    //[EA]简单的均线穿越
    //
    
    
    //---------------------------------------------------------------------+
    extern int    短均周期=3;
    extern int    长均周期=13;
    extern int    短均均模=0;
    extern int    长均均模=3;
    extern int    短均价类=0;
    extern int    长均价类=4;
    extern int    短均平移=0;
    extern int    长均平移=0;
    extern double 手数 = 0.01;
    extern int    滑点 = 0;
    
    int New_Bar;
    int 最后时间;
    int 开仓定位;
    int 关仓定位;
    int 单总数;
    double 当前短均;
    double 前一短均;
    double 当前长均;
    double 前一长均;
    int 买单判断; 
    int 卖单判断; 
    
    //------------------------------主函数---------------------------------+
    
    int start()  
       {
       买单判断=0;
       卖单判断=0;
       double 下单现价;
       int 开仓单=0;                                         // 未用
       int 单总数=OrdersTotal(); 
       for(int i=单总数-1; i>=0; i--)
          {
          if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true) 
             {
             if(OrderType()==OP_BUY)
                {
                买单判断=1;                                //没用
                if(均线穿越子()==1)
                   {
                   下单现价=MarketInfo(Symbol(),MODE_BID); 
                   OrderClose(OrderTicket(),
                              OrderLots(),
                              下单现价,
                              滑点,
                              CLR_NONE); //平买
                   }
                }
             if(OrderType()==OP_SELL) 
                {
                卖单判断=1;                               //没用
                if(均线穿越子()==2)
                   {
                   下单现价=MarketInfo(Symbol(),MODE_ASK);
                   OrderClose(OrderTicket(),
                              OrderLots(),
                              下单现价,
                              滑点,
                              CLR_NONE); //平卖
                   }
                }
             }
          }
       
       New_Bar=0;                                                // 将棒 赋为0  
       if (最后时间 != Time[0])
          {
          New_Bar= 1;                                            // 将棒赋为 1
          最后时间 = Time[0];
          } 
       
       当前短均= iMA(NULL,0, 短均周期, 短均平移, 获均线类型子(短均均模), 获均线价格类型子(短均价类), 0);
       前一短均= iMA(NULL,0, 短均周期, 短均平移, 获均线类型子(短均均模), 获均线价格类型子(短均价类), 1);
       当前长均= iMA(NULL,0, 长均周期, 长均平移, 获均线类型子(长均均模), 获均线价格类型子(长均价类), 0);
       前一长均= iMA(NULL,0, 长均周期, 长均平移, 获均线类型子(长均均模), 获均线价格类型子(长均价类), 1);
       
       if (开仓前检查穿越定位子()==1 && New_Bar==1)
          {
          开买子();
          }      
       if (开仓前检查穿越定位子()==2 && New_Bar==1)
          {
          开卖子();
          }    
       return;
       }
         
    //----------------------------穿越位置开 子函数()----------------------+
    
    int 开仓前检查穿越定位子()
       {
       开仓定位=0;
       if ((前一短均<=当前长均 && 当前短均>当前长均) || (前一短均<当前长均 && 当前短均>=当前长均))
          {
          开仓定位=1;
          }                  
       if ((前一短均>=当前长均 && 当前短均<当前长均) || (前一短均>当前长均 && 当前短均<=当前长均))
          {
          开仓定位=2;
          }             
       return(开仓定位);
       }
       
    //------------------------------均线穿越 子函数()----------------------+
    
    int 均线穿越子()
       {
       关仓定位=0; 
       if ((前一短均>=当前长均 && 当前短均<当前长均) || (前一短均>当前长均 && 当前短均<=当前长均))
          {
          关仓定位=1;
          }                  
       if ((前一短均<=当前长均 && 当前短均>当前长均) || (前一短均<当前长均 && 当前短均>=当前长均))
          {
          关仓定位=2;
          } 
                      
       return(关仓定位);
       }
       
    //--------------------------开买单 子函数()----------------------------+
    
    int 开买子()                                                   // 注意函数类型!
       {
       if (单总数==1)
          {
          OrderSelect(0, SELECT_BY_POS,MODE_TRADES);
          if (OrderType()==OP_BUY)  return;                        // 如果类型是买单时,返回,即不再开买
          }
       OrderSend(Symbol(),
                 OP_BUY,
                 手数, 
                 Ask, 
                 滑点, 
                 0, 
                 0, 
                 "Buy: 均线穿越", 
                 1, 
                 0, 
                 CLR_NONE);
       return;                                                     // 未写返回值
       }
       
    //---------------------------开卖单子函数()----------------------------+
    
    int 开卖子() 
       {
       if (单总数==1)
          {
          OrderSelect(0, SELECT_BY_POS,MODE_TRADES);
          if (OrderType()==OP_SELL)  return;
          }
       OrderSend(Symbol(),
                 OP_SELL, 
                 手数, 
                 Bid, 
                 滑点, 
                 0, 
                 0, 
                 "Sell: 均线穿越", 
                 2, 
                 0,
                 CLR_NONE);
       return; 
       }
       
    //----------------------均线模式选择 子函数()--------------------------+
    
    int 获均线类型子(int 均模)
       {
          switch(均模)
            {
             case 0: return(0);
             case 1: return(1);
             case 2: return(2);
             case 3: return(3);
            }
       }
       
    //--------------------均线使用的价格模式选择 子函数()-------------------+
    
    int 获均线价格类型子(int 使用价格)
       {
          switch(使用价格)
            {
             case 0: return(PRICE_CLOSE);       
             case 1: return(PRICE_OPEN);
             case 2: return(PRICE_HIGH);
             case 3: return(PRICE_LOW);
             case 4: return(PRICE_MEDIAN);
             case 5: return(PRICE_TYPICAL);
             case 6: return(PRICE_WEIGHTED);
            }
       }
    //----------------------------------------------------------------------+
  • 相关阅读:
    题解 CF171G 【Mysterious numbers
    题解 P1157 【组合的输出】
    题解 P3955 【图书管理员】
    题解 P2036 【Perket】
    题解 CF837A 【Text Volume】
    题解 CF791A 【Bear and Big Brother】
    题解 CF747A 【Display Size】
    题解 P1332 【血色先锋队】
    题解 P2660 【zzc 种田】
    题解 P4470 【[BJWC2018]售票】
  • 原文地址:https://www.cnblogs.com/Dreamxin/p/10913562.html
Copyright © 2020-2023  润新知