外汇EA
基于移动平均线的智能交易系统的代码
回复:0  浏览:258
  • 楼主admin 圈主
  • 2020-01-01 15:32
//基于移动平均线的智能交易系统的代码 ,整个程序非常简洁但EA的功能又非常齐全,实现了完全由电脑自动下单和平仓,整个程序只用了一个START()
 //+------------------------------------------------------------------+
 extern double 赢利目标 = 20;
 extern double 止损 = 30;
 extern double 手数 = 2;
 extern double 跟踪止赢 = 50;
 extern int 短均周期 = 5;
 extern int 长均周期 = 60;
 
 //+---------------------------主函数---------------------------------+
 
 int start()
     {
      int cnt,
       编号,
       单总数;
      double 短均,
          长均;
      if (Bars 100)
        {
       Print( bars less than 100
       return (0);
        }
      if (赢利目标 10)
         {
         Print( 赢利目标低于 10
         return (0);
         }
      短均 = iMA(NULL, 0, 短均周期, 0, MODE_EMA, PRICE_CLOSE, 0);
      长均 = iMA(NULL, 0, 长均周期, 0, MODE_EMA, PRICE_CLOSE, 0);
      static int 穿越 = 0;
      穿越 = 穿越子函数(长均, 短均);
      单总数 = OrdersTotal();
      if (单总数 1)
        {
       if (穿越 == 1) // 满足空仓条件,开空仓
         {
           编号 = OrderSend(Symbol(),
                     OP_SELL,
                     手数,
                     Bid,
                     3,
                     Bid + 止损 * Point,
                     Bid - 赢利目标 * Point,
                      EMA_CROSS ,
                     12345,
                     0,
                     Green);
         if (编号 0)
           {
             if (OrderSelect(编号, SELECT_BY_TICKET, MODE_TRADES))
                Print( SELL单开在: , OrderOpenPrice());
           }
           else
             Print( 开卖单出错: , GetLastError());
         return (0);
         }
       if (穿越 == 2) // 满足多仓条件,开多仓
         {
           编号 = OrderSend(Symbol(),
                     OP_BUY,
                     手数,
                     Ask,
                     3,
                     Ask - 止损 * Point,
                     Ask + 赢利目标 * Point,
                      EMA_CROSS ,
                     12345,
                     0,
                     Red);
           if (编号 0)
             {
              if (OrderSelect(编号, SELECT_BY_TICKET, MODE_TRADES))
                Print( BUY单开在: , OrderOpenPrice());
             }
            else
              Print( 开买单出错 : , GetLastError());
           return (0);
           }
       return (0);
       }
       
//---- 跟踪止盈止损
      for (cnt = 0; cnt 单总数; cnt++)
       {
       OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
         if (OrderType() = OP_SELL OrderSymbol() == Symbol())
           {
             if (OrderType() == OP_SELL) // long position is opened
               {
//----跟踪止损
                if (跟踪止赢 0)
                  {
                 if (Bid - OrderOpenPrice() Point * 跟踪止赢)
                   {
                     if (OrderStopLoss() Bid - Point * 跟踪止赢)
                       {
                        OrderModify(OrderTicket(),
                                OrderOpenPrice(),
                                Bid - Point * 跟踪止赢,
                                OrderTakeProfit(),
                                0,
                                Green);
                        return (0);
                       }
                   }
                  }
               }
              else // go to short position
               {
// check for trailing stop
                if (跟踪止赢 0)
                  {
                 if ((OrderOpenPrice() - Ask) (Point * 跟踪止赢))
                   {
                     if ((OrderStopLoss() (Ask + Point * 跟踪止赢)))
                       {
                        OrderModify(OrderTicket(),
                                OrderOpenPrice(),
                                Ask + Point * 跟踪止赢,
                                OrderTakeProfit(),
                                0,
                                Red);
                        return (0);
                        }
                   }
                  }
               }
           }
       }
 //----
      return (0);
      }
     
 //+-----------------------穿越 子函数()------------------------------+
 
 // 移动平均线多空条件判断,
 int 穿越子函数(double 线1, double 线2)
   {
     static int 最后方向 = 0;
     static int 当前方向 = 0;
//首个load不工作, 等待第一次穿越!
     static bool 第一时间 = true;
     if (第一时间 == true)
       {
       第一时间 = false;
       return (0);
       }
 //----因主循环中有单数限制,所以当大于时下单后不会再不停下单
     if (线1 线2)
       当前方向 = 2; //多头市场 上穿做多
     if (线1 线2)
       当前方向 = 1; //空头市场 下穿做空
 //----
     if (当前方向 != 最后方向) //changed
       {
       最后方向 = 当前方向;
       return(最后方向);
       }
     else
       {
       return (0); //方向未改变
       }
   }
 //+--------------------------------------------------------------------+