Three (3) Moving Average

 
Hello everyone,

I was doing EA which to open trades and close trades when moving averages crosses.

But I get confused how to compare three moving averages in IF statements.

also is there anyway EA can only open buy order when CANDLE start/open below 50SMA and close/ends above 25EMA, and moving averages in sequences.


  int MA_Largest = 100;
  int MA_Large = 50;
  int MA_Smallest = 25;
  
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
double PreviousMAlargest=iMA(NULL,0,MA_Largest_Method,0,MODE_SMA,PRICE_CLOSE,2);
double CurrentMAlargest=iMA(NULL,0,MA_Largest_Method,0,MODE_SMA,PRICE_CLOSE,1);

double PreviousMAlarge=iMA(NULL,0,MA_Large,0,MODE_SMA,PRICE_CLOSE,2);
double CurrentMAlarge=iMA(NULL,0,MA_Large,0,MODE_SMA,PRICE_CLOSE,1);

double PreviousMAsmallest=iMA(NULL,0,MA_Smallest,0,MODE_EMA,PRICE_CLOSE,2);
double CurrentMAsmallest=iMA(NULL,0,MA_Smallest,0,MODE_EMA,PRICE_CLOSE,1)

if((PreviousMAsmallest>PreviousMAlarge) && (PreviousMAlargest>PreviousMAlarge) && (CurrentMAsmallest<CurrentMAlarge) && (CurrentMAlarge <CurrentMAlargest))
      if(OrdersTotal()==0)
         OrderSend(
                  Symbol(),               //Current Symbol
                  OP_BUY,                 // Open Buy
                  Lots,                   // Lotsize user input
                  Ask,                    //Ask price
                  Slippages,              //Slippage userinput
                  Ask-(StopLoss*pips),    // Calculating stop loss in decimal points
                  Ask+(TakeProfit*pips),  //Calculating Take profit in decimal points
                  NULL,                   //No Comment
                  MagicNumber,            //MagicNumber
                  0,                      //No order expiration
                  0                       //No Color
                  );                     
    
if((PreviousMAlargest>PreviousMAlarge) && (PreviousMAlarge > PreviousMAsmallest) && (CurrentMAlargest<CurrentMAlarge) && (CurrentMAlarge <CurrentMAlarge))

      if(OrdersTotal()==0)
         OrderSend(
                  Symbol(),               //Current Symbol
                  OP_SELL,                // Open SELL
                  Lots,                   // Lotsize user input
                  Bid,                    //Bid price
                  Slippages,              //Slippage userinput
                  Bid+(StopLoss*pips),    // Calculating stop loss in decimal points
                  Bid-(TakeProfit*pips),  //Calculating Take profit in decimal points
                  NULL,                   //No Comment
                  MagicNumber,            //MagicNumber
                  0,                      //No order expiration
                  0                       //No Color
                  );  
               
   
  }

If you can help me I will really appreciate you.

Plz I am learning if i make mistake don't surprise me. 


 
In future please post in the correct section
I have moved your topic to the MQL4 and Metatrader 4 section.
 

First, there's an error in your "largest" declaration:

double PreviousMAlargest=iMA(NULL,0,MA_Largest_Method,0,MODE_SMA,PRICE_CLOSE,2);


it should be

double PreviousMAlargest=iMA(NULL,0,MA_Largest,0,MODE_SMA,PRICE_CLOSE,2);

 Also, maybe it would be less confusing if you called your 3 variables something like:

MA_Small

MA_Medium

MA_Large,

but that's just me.

Now, what "cross" are you wanting to take a trade on? When the small crosses the medium and both are above the large for a long?

 
Mohamed Abdulkadir Hersi: I was doing EA which to open trades and close trades when moving averages crosses.

But I get confused how to compare three moving averages in IF statements.

also is there anyway EA can only open buy order when CANDLE start/open below 50SMA and close/ends above 25EMA,

and moving averages in sequences.

If you can help me I will really appreciate you.

  1. Two moving averages cross.
    double aPrev = …, aCurr = …,
           bPrev = …, bCurr = …;
    bool   wasUp = aPrev > bPrev,
            isUp = aCurr > bCurr,
           isCross = isUp != wasUp;
    You test for that at the start of a new candle.

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.

    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 programming forum

  2. Three moving averages do not cross at the same time. You want them to be in the proper order, i.e. long < medium and medium < short. Act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - MQL5 programming forum #1

  3. Of course, there's a way; get the open, close, 25 and 50 of the previous bar and test for your condition.

  4. No idea what "and moving averages in sequences" mean or what it has to do with № 3

  5. Help you with what? You haven't stated a problem, you stated wants. Show us your attempt (using the CODE button) and state the nature of your problem.
              No free help 2017.04.21

 

Thank 

Barraka and William Roeder

I correct the variable errors and little change but it seems still not working. 

 
  int MA_Large = 100;
  int MA_Medium = 50;
  int MA_Small = 25;
  
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
double PreviousMAlarge=iMA(NULL,0,MA_Large,0,MODE_SMA,PRICE_CLOSE,2);
double CurrentMAlarge=iMA(NULL,0,MA_Large,0,MODE_SMA,PRICE_CLOSE,1);

double PreviousMAmedium=iMA(NULL,0,MA_Medium,0,MODE_SMA,PRICE_CLOSE,2);
double CurrentMAmedium=iMA(NULL,0,MA_Medium,0,MODE_SMA,PRICE_CLOSE,1);

double PreviousMAsmall=iMA(NULL,0,MA_Small,0,MODE_EMA,PRICE_CLOSE,2);
double CurrentMAsmall=iMA(NULL,0,MA_Small,0,MODE_EMA,PRICE_CLOSE,1)

if((PreviousMAlarge<CurrentMAlarge) && (CurrentMAmedium>PreviousMAmedium) && (CurrentMAsmall>PreviousMAsmall))
      if(OrdersTotal()==0)
         OrderSend(
                  Symbol(),               //Current Symbol
                  OP_BUY,                 // Open Buy
                  Lots,                   // Lotsize user input
                  Ask,                    //Ask price
                  Slippages,              //Slippage userinput
                  Ask-(StopLoss*pips),    // Calculating stop loss in decimal points
                  Ask+(TakeProfit*pips),  //Calculating Take profit in decimal points
                  NULL,                   //No Comment
                  MagicNumber,            //MagicNumber
                  0,                      //No order expiration
                  0                       //No Color
                  );                     
    
if((PreviousMAlarge>CurrentMAlarge) && (CurrentMAmedium<PreviousMAmedium) && (CurrentMAsmall<PreviousMAsmall))

      if(OrdersTotal()==0)
         OrderSend(
                  Symbol(),               //Current Symbol
                  OP_SELL,                // Open SELL
                  Lots,                   // Lotsize user input
                  Bid,                    //Bid price
                  Slippages,              //Slippage userinput
                  Bid+(StopLoss*pips),    // Calculating stop loss in decimal points
                  Bid-(TakeProfit*pips),  //Calculating Take profit in decimal points
                  NULL,                   //No Comment
                  MagicNumber,            //MagicNumber
                  0,                      //No order expiration
                  0                       //No Color
                  );  
               
   
  }

I wan first to check in buy position if Large one(100) cross below Medium (50) one and Medium(50) cross below Small(25) one. then open trade 
and sell Large one(100) cross above Medium (50) one and Medium(50) cross above Small(25) one. then open trade 

close one I need to close when the medium and small one crosses.


https://prnt.sc/s96i6f

 

I checked your picture, and you aren't clear in the way you explain what you want.

You talk about large MA crossing below MA for a long, but what you mean is that the large MA is already below medium. In other words, that medium has crossed above large. And by the looks of your screenshot, that small has crossed above medium.

So you would have smallMA[i] > mediumMA[i] and mediumMA[i]>largeMA[i].

But that's your initial condition, it's not your entry condition, which I think you haven't clearly thought out. If it's really a "cross" that is your trigger, then it doesn't correspond to your picture. And actually, your picture just highlights an ideal point that everyone would like to go long at, but there's no specific condition.

I get your close condition, at least that one is specific, which would simply by smallMA[i]<mediumMA[i]. You don't need to check the previous MA values as you're already in a trade and (I assume) check at the close of each candle if there has been a cross, so this single condition is enough.

 
Barraka:

I checked your picture, and you aren't clear in the way you explain what you want.

You talk about large MA crossing below MA for a long, but what you mean is that the large MA is already below medium. In other words, that medium has crossed above large. And by the looks of your screenshot, that small has crossed above medium.

So you would have smallMA[i] > mediumMA[i] and mediumMA[i]>largeMA[i].

But that's your initial condition, it's not your entry condition, which I think you haven't clearly thought out. If it's really a "cross" that is your trigger, then it doesn't correspond to your picture. And actually, your picture just highlights an ideal point that everyone would like to go long at, but there's no specific condition.

I get your close condition, at least that one is specific, which would simply by smallMA[i]<mediumMA[i]. You don't need to check the previous MA values as you're already in a trade and (I assume) check at the close of each candle if there has been a cross, so this single condition is enough.

so you mean like this 

if (CurrentMAmedium > CurrentMAlarge && PreviousMAmedium < PreviousMAlarge) && (CurrentMAsmall > CurrentMAmedium && PreviousMAsmall < PreviousMAmedium)

what i mean in the picture is like this.


 

No, I mean like the example I gave:

if(smallMA[i] > mediumMA[i] && mediumMA[i]>largeMA[i])

Like I explained, you didn't state what your entry was. I see the picture, but again there's no entry condition. You entry is just a blue box. Can you answer this question: What triggers the entry on your picture?

 
Barraka:

No, I mean like the example I gave:

Like I explained, you didn't state what your entry was. I see the picture, but again there's no entry condition. You entry is just a blue box. Can you answer this question: What triggers the entry on your picture?

I want to enter when all moving averages are aligned and the candle opened below Medium (50SMA) and close above Small (25EMA). 

 

Ah, now we're getting somewhere :D


Well, all moving averages aligned: that's the line of code I wrote above. You just check small above medium, and medium above large.

And for your entry trigger, it would be something like:

if(Open[i+1]<mediumMA[i+1] && Close[i+1] > shortMA[i+1])
        {
        OrderSend(...)
        }
 
Mohamed Abdulkadir Hersi: I want to enter when all moving averages are aligned and the candle opened below Medium (50SMA) and close above Small (25EMA). 

There is no candle in your blue box that with that criteria. The down bar/up bar pair didn't open below the 50 and the right pin bar only closed above the 25.


Reason: