Detected Country () : English
EA Double Moving Average Sample - codingforex.com

Welcome to CodingForex.Com!

Knowledge portal where you can learn how to create forex indicators and robot.

Click button below to choose level that suite you:

EA Double Moving Average Sample

Samples [2018-01-04 07:57:42] - by hillzx
Featured Image

Moving average is a popular indicator used by traders. Usually traders use two or three moving averages, then take the intersection (MA Cross) as a trade reference.

In this tutorial we will try how to make EA Moving Average with the following criteria:

  • Brings up information about the MA price on the top left of the screen
  • MA can be modified through the settings tab
  • EA can perform transaction order every MA Cross

 

Quoted from the official link MQL4 regarding the moving average formula, the formula is this way:

 double  iMA(

   string       symbol,           // symbol
   int          timeframe,        // timeframe
   int          ma_period,        // MA averaging period
   int          ma_shift,         // MA shift
   int          ma_method,        // averaging method
   int          applied_price,    // applied price
   int          shift             // shift
   );


There are 4 parameters of MA that we use that is: Period, Shift, Method, Apply To (see picture below):

maset

Next we create a variable extern to accommodate these parameters, we just go to the coding stage:

//+------------------------------------------------------------------+
//| Extern Settings                                  |
//+------------------------------------------------------------------+
extern string info1="Faster Moving Average";
extern ENUM_TIMEFRAMES MA1_TF=PERIOD_CURRENT;
extern int MA1_Period=10;
extern int MA1_Shift=0;
extern ENUM_MA_METHOD MA1_Method=MODE_SMA;
extern ENUM_APPLIED_PRICE MA1_Apply=PRICE_CLOSE;
extern int MA1_Candle=1;

extern string info2="-";
extern string info3="Slower Moving Average";
extern ENUM_TIMEFRAMES MA2_TF=PERIOD_CURRENT;
extern int MA2_Period=200;
extern int MA2_Shift=0;
extern ENUM_MA_METHOD MA2_Method=MODE_SMA;
extern ENUM_APPLIED_PRICE MA2_Apply=PRICE_CLOSE;
extern int MA2_Candle=1;

extern string info4="-";
extern string info5="GUI";
extern int Corner=0;
extern int XPos=0;
extern int YPos=0;
extern color TextColor1=Yellow;
extern color TextColor2=DarkOrange;

extern string info6="-";
extern string info7="Trade";
extern double LotSize=0.01;
extern int TakeProfit=0;
extern int StopLoss=0;
extern int Slippage=5;
extern int MagicNumber=123;



Next, intern settings:

//+------------------------------------------------------------------+
//| Intern Settings                                 |
//+------------------------------------------------------------------+
int oldTime;
double fasterMaCurrent;
double slowerMaCurrent;
double fasterMaBefore;
double slowerMaBefore;
bool isUpTrendCurrent;
bool isUpTrendBefore;

Next, the init code, deinit, and tick:

//+------------------------------------------------------------------+
//| Code Init                              |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
UpdateMAPrice();
WriteText();
oldTime=Time[0];
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
if(oldTime != Time[0] )
{
   oldTime = Time[0];
   UpdateMAPrice();
   WriteText();
} 
  }
//+------------------------------------------------------------------+

Next, create a module for price updates:

double GetPriceMA(ENUM_TIMEFRAMES currentTf,int currentPeriod,int currentShift,ENUM_MA_METHOD currentMethod,ENUM_APPLIED_PRICE currentApply,int currentCandle){
   return iMA(NULL,currentTf,currentPeriod,currentShift,currentMethod,currentApply,currentCandle);
}

void UpdateMAPrice(){
   fasterMaCurrent=GetPriceMA(MA1_TF,MA1_Period,MA1_Shift,MA1_Method,MA1_Apply,MA1_Candle);
   slowerMaCurrent=GetPriceMA(MA2_TF,MA2_Period,MA2_Shift,MA2_Method,MA2_Apply,MA2_Candle);
   if (fasterMaCurrent>slowerMaCurrent){
      isUpTrendCurrent=true;
   } else {
      isUpTrendCurrent=false;
   }
   
   fasterMaBefore=GetPriceMA(MA1_TF,MA1_Period,MA1_Shift,MA1_Method,MA1_Apply,(MA1_Candle+1));
   slowerMaBefore=GetPriceMA(MA2_TF,MA2_Period,MA2_Shift,MA2_Method,MA2_Apply,(MA2_Candle+1));
   if (fasterMaBefore>slowerMaBefore){
      isUpTrendBefore=true;
   } else {
      isUpTrendBefore=false;
   }
}



Next create a module to display posts:

void WriteText(){

   ObjectCreate("TxtHiLLzXMaDoubleCross1",OBJ_LABEL,0,0,0,0,0);
   ObjectSet("TxtHiLLzXMaDoubleCross1",OBJPROP_CORNER,Corner);
   ObjectSet("TxtHiLLzXMaDoubleCross1",OBJPROP_XDISTANCE,XPos );
   ObjectSet("TxtHiLLzXMaDoubleCross1",OBJPROP_YDISTANCE,YPos);
   ObjectSetText("TxtHiLLzXMaDoubleCross1","Current Faster MA Price : " + DoubleToStr(fasterMaCurrent,Digits) ,14,"Arial",TextColor1);
   
   ObjectCreate("TxtHiLLzXMaDoubleCross2",OBJ_LABEL,0,0,0,0,0);
   ObjectSet("TxtHiLLzXMaDoubleCross2",OBJPROP_CORNER,Corner);
   ObjectSet("TxtHiLLzXMaDoubleCross2",OBJPROP_XDISTANCE,XPos );
   ObjectSet("TxtHiLLzXMaDoubleCross2",OBJPROP_YDISTANCE,20+YPos);
   ObjectSetText("TxtHiLLzXMaDoubleCross2","Current Slower MA Price : " + DoubleToStr(slowerMaCurrent,Digits) ,14,"Arial",TextColor1);
   
   ObjectCreate("TxtHiLLzXMaDoubleCross3",OBJ_LABEL,0,0,0,0,0);
   ObjectSet("TxtHiLLzXMaDoubleCross3",OBJPROP_CORNER,Corner);
   ObjectSet("TxtHiLLzXMaDoubleCross3",OBJPROP_XDISTANCE,XPos );
   ObjectSet("TxtHiLLzXMaDoubleCross3",OBJPROP_YDISTANCE,40+YPos);
   ObjectSetText("TxtHiLLzXMaDoubleCross3","Before Faster MA Price : " + DoubleToStr(fasterMaBefore,Digits) ,14,"Arial",TextColor2);
   
   ObjectCreate("TxtHiLLzXMaDoubleCross4",OBJ_LABEL,0,0,0,0,0);
   ObjectSet("TxtHiLLzXMaDoubleCross4",OBJPROP_CORNER,Corner);
   ObjectSet("TxtHiLLzXMaDoubleCross4",OBJPROP_XDISTANCE,XPos );
   ObjectSet("TxtHiLLzXMaDoubleCross4",OBJPROP_YDISTANCE,60+YPos);
   ObjectSetText("TxtHiLLzXMaDoubleCross4","Before Slower MA Price : " + DoubleToStr(slowerMaBefore,Digits) ,14,"Arial",TextColor2);
   
   string currentStatus;
   
   if (isUpTrendCurrent==isUpTrendBefore){
      if (isUpTrendCurrent==true){
         currentStatus="Trend naik";
      } else {
         currentStatus="Trend turun";
      }
   } else {
      CloseThis();
      if (isUpTrendCurrent==true){
         OrderBuy();
         currentStatus="Sebelumnya Trend Turun, tapi kini Trend naik";
      } else {
         OrderSell();
         currentStatus="Sebelumnya Trend Naik, tapi kini Trend turun";
      }
   }
   
   ObjectCreate("TxtHiLLzXMaDoubleCross5",OBJ_LABEL,0,0,0,0,0);
   ObjectSet("TxtHiLLzXMaDoubleCross5",OBJPROP_CORNER,Corner);
   ObjectSet("TxtHiLLzXMaDoubleCross5",OBJPROP_XDISTANCE,XPos );
   ObjectSet("TxtHiLLzXMaDoubleCross5",OBJPROP_YDISTANCE,80+YPos);
   ObjectSetText("TxtHiLLzXMaDoubleCross5",currentStatus ,14,"Arial",TextColor2);
   
   
}


Next module to make transactions buy, sell, and close the transaction:

void OrderBuy() {

double tp;
double sl;

if (TakeProfit==0){
   tp=0;
} else {
   tp=Ask+TakeProfit*Point;
}

if (StopLoss==0){
   sl=0;
} else {
   sl=Ask-StopLoss*Point;
}


int ticket;
for(int i=0;i<3;i++)
{
   string errorMsg;
   ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,Slippage,sl,tp,"HiLLzX Double MA Buy",MagicNumber,0,CLR_NONE);
      
   if(ticket>0){break;}
   RefreshRates();
}

}

void OrderSell() {



double tp;
double sl;

if (TakeProfit==0){
   tp=0;
} else {
   tp=Bid-TakeProfit*Point;
}

if (StopLoss==0){
   sl=0;
} else {
   sl=Bid+StopLoss*Point;
}


int ticket;
for(int i=0;i<3;i++)
{
   string errorMsg;
   ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,Slippage,sl,tp,"HiLLzX Double MA Sell",MagicNumber,0,CLR_NONE);

   if(ticket>0){break;}
   RefreshRates();
}

}

void CloseThis() {
   bool closed = false;
   for (int i = OrdersTotal(); i >=0; i--) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      while(IsTradeContextBusy()) Sleep(100);
      RefreshRates();
      if ( OrderType() == OP_BUY && Symbol() == OrderSymbol() 
      && MagicNumber == OrderMagicNumber() ) {
        closed = OrderClose( OrderTicket(), OrderLots(), Bid, Slippage, White);
      }
      if (OrderType() == OP_SELL && Symbol() == OrderSymbol()
       && MagicNumber == OrderMagicNumber() ) {
        closed = OrderClose( OrderTicket(), OrderLots(), Ask, Slippage, White);
      }
   }
   
return;

}

Finished!

settingma



hasilma

Let us try, do not forget to share and follow our facebook and our youtube channel, happy trading!






- Provided by HiLLzSoft Studio -