MACD Signal cross-over

Hey babypips members,

I’ve been following these forums for a couple of years now and have learned a lot from the community. I am trying to develop an EA which trades on MACD signal values. It would first identify a trend direction based on a higher timeframe, then cherry pick MACD signal cross-overs to enter in on. I have attached a picture which the overall trend is identified by the blue vertical line, the trend is down. One the M30, the trade window is identified by the red vertical lines. I would like to enter when the value of the signal like for a candle 1 peroid back would be negative and exit either at a give Take profit value or when the MACD signal line of M30 goes positive. Below is text of the first EA i have ever coded by reading MQL4 website and learning what things mean… probably one of the reasons it isn’t working. Could someone please help me with the code so that it functions the way I have explained? Any questions are welcome.

Thanks!

jester2630

//------------------------------------------------------------
//Copyright © 2013, JCWFX, LLC.
//MAGIC EA Number = 201301151 //(yyyy)(mm)(dd)(eanumber)
//Legend
//HTF=Higher Time Frame
//LTF=Lower Time Frame
//TF or tf=time frame

extern double Lots=0.1;

extern int ATRperiod=14; //possibly considered to replace TP and SL values
extern int ATRfactor=1; //factor to apply to ATRvalue
extern int ATRtimeframe=1440; //default value is daily

extern int HTFfast=12; //Higher TF MACD Fast
extern int HTFslow=26; //Higher TF MACD Slow
extern int HTFsignal=9; //Higher TF MACD Signal

extern int LTFfast=12; //Lower TF MACD Fast
extern int LTFslow=26; //Lower TF MACD Slow
extern int LTFsignal=9; //Lower TF MACD Signal

extern int HighTFperiod=1440; //default is daily
extern int LowTFperiod=0; // default is current

//------------------------------------------------------

int start()
{
// Variables

double SignalH1; //macd signal line value (shift 1) of higher timeframe
double SignalH2; //macd signal line value (shift 2) of higher timeframe
double SignalL1; //macd signal line value (shift 1) of lower timeframe
double SignalL2; //macd signal line value (shift 2) of lower timeframe

double ATR; //ATR used to calculate TP and SL
bool buyphase = false; //buyphase set false for default
bool sellphase = false; // sellphase set to false for default

//Signal Generation
SignalH1=iMACD(NULL,HighTFperiod,HTFfast,HTFslow,HTFsignal,PRICE_CLOSE,MODE_SIGNAL,1); //value of macd higher time frame signal line 1 period back
SignalH2=iMACD(NULL,HighTFperiod,HTFfast,HTFslow,HTFsignal,PRICE_CLOSE,MODE_SIGNAL,2); //value of macd higher time frame signal line 2 periods back
SignalL1=iMACD(NULL,LowTFperiod,LTFfast,LTFslow,LTFsignal,PRICE_CLOSE,MODE_SIGNAL,1); //value of macd lower time frame signal line 1 period back
SignalL2=iMACD(NULL,LowTFperiod,LTFfast,LTFslow,LTFsignal,PRICE_CLOSE,MODE_SIGNAL,2); //value of macd lower time frame signal line 2 period back

//Phase Identification
if (SignalH1>0 && SignalH2>0) //Buyphase identified if the last two candles of the higher time frame have positive signal values
{
buyphase=true;
}

  if (SignalH1<0 && SignalH2<0) //Sellphase identified if the last two candles of the higher time frame have negative signal values
     {
     sellphase=true;
     }
    
     return;

//TP and SL levels set via ATR
ATR=iATR(NULL,ATRtimeframe,ATRperiod,1)*ATRfactor;

//Order Triggers
if (buyphase==true && 0>SignalL2 && 0<SignalL1) //buy if buyphase is true and signal live value of 2 perids ago was less than 0 and signal value of 1 peroid ago is greater than 0
{
RefreshRates();

     OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-ATR,Ask+ATR);
     }


  if (sellphase==true && 0&lt;SignalL2 && 0&gt;SignalL1) //sell if sellphase is true and signal live value of 2 perids ago was greater than 0 and signal value of 1 peroid ago is less than 0
     {
     RefreshRates(); 
                           
     OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+ATR,Bid-ATR);
     }         
     
     return;

//Below is possible code to close orders if signal switches on higher tf and trades are still open:

//Close Orders
// if (buyphase==false)
//{
//OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),5,Red);
// }
// if (sellphase==false)
// {
// OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),5,Red);
// }
//return;

return;
}
// End

There are a lot of problems with that code. I will help you with your first problem though - you return to int start - so buyphase will never equal true.

Thank you, dan82au. I guess I need to look into things a little more, even regarding the use of the “return” command. I have removed every return from the code with the exception of the last one and the EA not opens and closes trades appropriately. Thank you for your help!

All the best!
jester2630

all good, I was hoping you could find the problem without me having to elaborate to much :slight_smile:

:slight_smile: :slight_smile: :slight_smile: Finally getting to where I want this code.