MACD line crosses the Signal line

 

Hiya,

i'm new to this, so please forgive me if its obvious :)


If i write the MACD code,  double MACD = iMACD (_Symbol, _Period, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 0);

How can i get a MACD line and the Signal line alert at cross over?
What i would like to make is,

When the price is Above the 200EMA and the MACD line crosses the Signal line to make a buy comment,
When the price is Below the 200EMA and the MACD line crosses the Signal line to make a sell comment.

So it would only Buy when above 200ema and only sell when below..

I've gotten this far; 


void OnTick()

  {

   // create a string variable for the signal

   string signal ="";  

  

   // Calculate EMA and MACD

   double MyMovingAverage = iMA(_Symbol, _Period, 200, 0, MODE_EMA, PRICE_CLOSE, 0);  

   double MACD = iMACD (_Symbol, _Period, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 0);

   

   // if the EMA is below the Bid price

   if (MyMovingAverage<Bid)

   {

      // set the signal to buy

      signal="buy";

   }

   

   // if the EMA is above the Ask price   

   if (MyMovingAverage>Ask)

   {

      // set the signal to sell

      signal="sell";

   }

   

   // Output the signal on the chart

   Comment ("The current signal is: ",signal);

  }


But now i'm stuck haha

 

<Deleted>

 
<Deleted>
 
xanderh:
This is making so much more sense. thanks.
You're welcome.
 

This is what i have brother, seems to be working quite well


if((iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0)<iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0))||(iMACD(NULL,0,13,26,9,PRICE_CLOSE,MODE_MAIN,0)<iMACD(NULL,0,13,26,9,PRICE_CLOSE,MODE_SIGNAL,0))

^That's the buy


if((iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0)>iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0))||(iMACD(NULL,0,13,26,9,PRICE_CLOSE,MODE_MAIN,0)>iMACD(NULL,0,13,26,9,PRICE_CLOSE,MODE_SIGNAL,0))

^ and that's the sell

Mind you there is more to it in order for it to work 100% with the use of other indicators

 

Moving average is OK but it isn't enough for 100% confirmation.

You really should add Stochastic in conjunction with it, i use a few other indicators but no offense i really don't want to give away all my secrets because i plan on selling my bot once I work out the few small bugs it is currently plagued with

 
  1. WindmillMQL: You are using iMA and IMACD incorrectly.  you have to create a handle …
       double MyMovingAverage = iMA(_Symbol, _Period, 200, 0, MODE_EMA, PRICE_CLOSE, 0);  
       double MACD = iMACD (_Symbol, _Period, 12, 26, 9, PRICE_CLOSE, MODE_MAIN, 0);
    No he's not; he just posted MT4 code in the wrong place. (The zero is the shift.)

  2. xanderh: How can i get a MACD line and the Signal line alert at cross over?

    Why did you post your MT4 question in the MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  3. You've got the current values. Get the previous ones and check for a cross.

    double aPrev = …, aCurr = …,
           bPrev = …, bCurr = …;
    bool   wasUp = aPrev > bPrev,
            isUp = aCurr > bCurr,
           isCross = isUp != wasUp;
  4. Colin Lundrigan: This is what i have brother, seems to be working quite well
    OP asked for a cross, your code just returns a trend.
 
In future please post in the correct section
I will move this topic to the MQL4 and Metatrader 4 section.
 
Colin Lundrigan:


Please edit your post and use the code button(Alt+S) to paste code.