外汇EA
MT4自带的macdsample 解析。
回复:0  浏览:142
  • 楼主admin 圈主
  • 2020-01-01 15:14

//+------------------------------------------------------------------+
//|                                 MACD Sample.mq4 |
//|                Copyright ?2005, metaQuotes Software Corp. |
//|                           http://www.metaquotes.net/ |
//+------------------------------------------------------------------+

extern double TakeProfit = 50;//止盈点数
extern double Lots = 0.1;//开仓交易数
extern double TrailingStop = 30;//移动止损点数
extern double MACDOpenLevel=3;
extern double MACDCloseLevel=2;
extern double MATrendPeriod=26;//均线参数

//+------------------------------------------------------------------+
//|                                             |
//+------------------------------------------------------------------+
int start()
 {
   double MacdCurrent, MacdPrevious, SignalCurrent;
   double SignalPrevious, MaCurrent, MaPrevious;
   int cnt, ticket, total;
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external
// variables (Lots, StopLoss, TakeProfit,
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
   if(Bars 100)
   {
     Print( bars less than 100 //K线不够100根
     return(0);
   }
   if(TakeProfit 10)
   {
     Print( TakeProfit less than 10 //设置的止盈点数小于10
     return(0); // check TakeProfit
   }

 //macd指标
   MacdCurrent=iMACD(NULL,0,5,34,5,PRICE_CLOSE,MODE_MAIN,0);//主线 现值
   MacdPrevious=iMACD(NULL,0,5,34,5,PRICE_CLOSE,MODE_MAIN,1);//主线 前一值
 
   SignalCurrent=iMACD(NULL,0,5,34,5,PRICE_CLOSE,MODE_SIGNAL,0);//信号线 现值
   SignalPrevious=iMACD(NULL,0,15,34,5,PRICE_CLOSE,MODE_SIGNAL,1);//信号线前一值
 
   MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);//均线 现值
   MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);//均线前一直

   total=OrdersTotal();//市场挂单总和
 
   if(total 1) //无仓位就考了开仓
   {
     // no opened orders identified
     if(AccountFreeMargin() (1000*Lots))// 保证金账户余额
     {
       Print( We have no money. Free Margin = , AccountFreeMargin());
       return(0);
     }
     // check for long position (BUY) possibility
     ///开仓买入条件
     bool buyCondit=MacdCurrent 0 MacdCurrent SignalCurrent MacdPrevious SignalPrevious
       MathAbs(MacdCurrent) (MACDOpenLevel*Point) MaCurrent MaPrevious ;
     
     if(buyCondit)
     {
     // 定单编号 ==开仓与挂单 (货币对,头寸方向,仓位,开仓价格,最大滑点,止损 ,盈利,注释,用户指定代码,订单时间,标示的颜色)
       ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point, macd sample ,16384,0,Green);
       if(ticket 0)
       {
       //查看订单 确立是否开仓成功。
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print( BUY order opened : ,OrderOpenPrice());
       }
       else Print( Error opening BUY order : ,GetLastError());
       return(0);
     }
     // check for short position (SELL) possibility
   
     //开空的条件
     bool sellCondit=MacdCurrent 0 MacdCurrent SignalCurrent MacdPrevious SignalPrevious
       MacdCurrent (MACDOpenLevel*Point) MaCurrent MaPrevious;
     
     if(sellCondit)
     {
       ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point, macd sample ,16384,0,Red);
       if(ticket 0)
       {
         if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print( SELL order opened : ,OrderOpenPrice());
       }
       else Print( Error opening SELL order : ,GetLastError());
       return(0);
     }
     return(0);
   }
   // it is important to enter the market correctly,
   // but it is more important to exit it correctly... 
   for(cnt=0;cnt total;cnt++)
   {
   //选择订单
     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
     //有效订单
     if(OrderType() =OP_SELL    // check for opened position
       OrderSymbol()==Symbol()) // check for symbol
     {
     //买入开仓的
       if(OrderType()==OP_BUY)   // long position is opened
       {
         // should it be closed?
         bool buyLiquidated=MacdCurrent 0 MacdCurrent SignalCurrent MacdPrevious SignalPrevious
           MacdCurrent (MACDCloseLevel*Point);
         if(buyLiquidated)
            {
            //平仓操作(定点编号,仓位大小,价格,最高滑点数,标示颜色)
           OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
           return(0); // exit
            }
         // check for trailing stop
         if(TrailingStop 0)
         {            //买价- 订单开仓价 止盈点数
           if(Bid-OrderOpenPrice() Point*TrailingStop)
           {
           //定点止损价 买价-止盈点数
             if(OrderStopLoss() Bid-Point*TrailingStop)
             {
              // 对前面的开仓或挂单进行修改 (订单号 ,价格 ,新止损价格,新盈利价格,有效时间,标示颜色)
               OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
               return(0);
             }
           }
         }
       }
       else // go to short position
       {
         // should it be closed?
         if(MacdCurrent 0 MacdCurrent SignalCurrent
           MacdPrevious SignalPrevious MathAbs(MacdCurrent) (MACDCloseLevel*Point))
         {
           OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
           return(0); // exit
         }
         // check for trailing stop
         if(TrailingStop 0)
         {     //开仓价格- 卖价 止损点数     
           if((OrderOpenPrice()-Ask) (Point*TrailingStop))
           {
           //订单止损 买价+止损点数
             if((OrderStopLoss() (Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
             {
             OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
               return(0);
             }
           }
         }
       }
     }
   }
   return(0);
 }
// the end.