Download MetaTrader 5

Review my code?

To add comments, please log in or register
Robert
11
Robert  

Hey y'all,

 I recently started working with MQL4 and I need some help with my code.  Although it runs and I can execute my EA, it doesn't work like it's supposed to.  Any help or tips wold be greatly appreciated.  The code is attached below:

 

EDIT:

I've had some comments about what I mean by how it doesn't work.  It runs and I can attach it to charts but it doesn't send out alerts.  Basically, I want this to run at the end of every session, and analyze the last 3 candlesticks for the Japanese Candlestick patterns below for the D1 and the H4 charts.   

 

 

//+------------------------------------------------------------------+

//| candlesticks.mq4 |

//| Copyright © 2014, MetaQuotes Software Corp. |

//| MetaTrader 4, MetaTrader 5, TeamWox / MetaQuotes Software Corp. |

//+------------------------------------------------------------------+

#property copyright "Copyright © 2014, MetaQuotes Software Corp."

#property link "http://www.metaquotes.net"





string symbol = "EURUSD";



double this_high = iHigh(symbol,PERIOD_H4,1);

double this_low = iLow(symbol,PERIOD_H4,1);

double this_open = iOpen(symbol,PERIOD_H4,1);

double this_close = iClose(symbol,PERIOD_H4,1);



double last_high = iHigh(symbol,PERIOD_H4,2);

double last_low = iLow(symbol,PERIOD_H4,2);

double last_open = iOpen(symbol,PERIOD_H4,2);

double last_close = iClose(symbol,PERIOD_H4,2);


double this_body = MathAbs(this_close - this_open);

double last_body = MathAbs(last_close - last_open);

double this_candle = this_high - this_low;

double last_candle = last_high - last_low;



double this_body = MathAbs(this_close - this_open);

double last_body = MathAbs(last_close - last_open);

double three_body = MathAbs(three_close - three_open);

double this_candle = this_high - this_low;

double last_candle = last_high - last_low;

double three_candle = three_high - three_low;



//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init()

{

//----



//----

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----



//----

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start()

{

patterns();

patterns_2();



return(0);

}

//+------------------------------------------------------------------+



//custom function 

int patterns()

{



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

//white and black marubozus

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

if(((this_high > 0.95*this_close) && (this_high < 1.05*this_close)) && ((this_low < 0.95*this_open) && (this_low > 1.05*this_open)))

{

Alert("This is a white marubozu - bullish!");


}


if(((this_high > 0.95*this_open) && (this_high < 1.05*this_open)) && ((this_low < 0.95*this_close) && (this_low > 1.05*this_close)))

{

Alert("This is a black marubozu - bearish!");


}





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

//hammer

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

if((this_open < this_close) && ((this_high > 0.9*this_open) && (this_high < 1.1*this_open)) && this_candle >= 2*this_body)

{

Alert("This is a hammer - bullish!");


}


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

//hanging man

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

if((this_open > this_close) && ((this_high > 0.9*this_open) && (this_high < 1.1*this_open)) && this_candle >= 2*this_body)

{

Alert("This is a hanging man - bearish!");


             

}


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

//inverted hammer

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

if((this_open < this_close) && ((this_low > 0.9*this_open) && (this_low < 1.1*this_open)) && this_candle >= 2*this_body)

{

Alert("This is an inverted hammer - bullish!");

            

}


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

//shooting star

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

if((this_open > this_close) && ((this_low > 0.9*this_close) && (this_low < 1.1*this_close)) && this_candle >= 2*this_body)

{

Alert("This is a shooting star - bearish!");

            

}






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

//bullish engulfing

//bearish engulfing

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

if((this_close > this_open) && (last_close < last_open)  && (this_open < last_close) && (this_close > last_open))

{

Alert("This is bullish engulfing!");

            

}



if((this_close < this_open) && (last_close > last_open)  && (this_open > last_close) && (this_close < last_open))

{

Alert("This is bearish engulfing!");

           

}





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

//tweezer tops

//tweezer bottoms

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

if((last_close > last_open) && (this_close < this_open) && ((last_open < last_close) && ((last_low > 0.9*last_open) && (last_low < 1.1*last_open)) && last_candle >= 1.75*last_body) && ((this_open > this_close) && ((this_low > 0.9*this_close) && (this_low < 1.1*this_close)) && this_candle >= 1.75*this_body))

{

Alert("This is tweezer tops - bearish!");

           

}


if((last_close < last_open) && (this_close < this_open) && ((last_open > last_close) && ((last_high > 0.9*last_open) && (last_high < 1.1*last_open)) && last_candle >= 1.75*last_body) && ((this_open < this_close) && ((this_high > 0.9*this_open) && (this_high < 1.1*this_open)) && this_candle >= 1.75*this_body))

{

Alert("This is tweezer bottoms - bullish!");

            

}






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

//morning star

//evening star

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

if((three_close < three_open) && (last_open < three_close) && (last_close < three_close) && (this_close > this_open) && (this_close > (three_close + .5*three_body)))

{

Alert("This is a morning star - bullish!");

           

}


if((three_close > three_open) && (last_open > three_close) && (last_close > three_close) && (this_close < this_open) && (this_close < (three_close + .5*three_body)))

{

Alert("This is an evening star - bearish!");

            

}





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

//three black crows

//three white soldiers

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

if((three_close < three_open) && (last_close < last_open) && (this_close < this_open) && (last_body > three_body) && (this_body > last_body) && (last_close < 1.1*last_low || last_close > 0.9*last_low) && (this_close < 1.1*this_low && this_close > 0.9*this_low))

{

Alert("This is three black crows - bearish!");

            

}


if((three_close > three_open) && (last_close > last_open) && (this_close > this_open) && (last_body > three_body) && (this_body > last_body) && (last_close < 1.1*last_high || last_close > 0.9*last_high) && (this_close < 1.1*this_high && this_close > 0.9*this_high))

{

Alert("This is three white soldiers - bullish!");



}





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

//three inside up

//three inside down

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

if((three_close < three_open) && (three_body > .75*three_candle) && (last_close > three_close + 0.5*three_body) && (this_close > this_open) && (this_close > three_high))

{

Alert("This is three inside up - bullish!");

            

}


if((three_close > three_open) && (three_body > .75*three_candle) && (last_close < three_close - 0.5*three_body) && (this_close < this_open) && (this_close < three_low))

{

Alert("This is three inside down - bearish!");

            

}



return(0);

}







//custom function number 2

int patterns_2()

{


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

//piercing line

//dark cloud cover

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

if((last_close < last_open) && (last_body >= 0.65*last_candle) && (this_open < 0.95*last_close) && (this_close > this_open) && (this_close > last_close + 0.5*last_body))

{

Alert("This is a piercing line - bullish!");

            

}


if((last_close > last_open) && (last_body >= 0.65*last_candle) && (this_open > 1.05*last_close) && (this_close < this_open) && (this_close < last_close - 0.5*last_body))

{

Alert("This is dark cloud cover - bearish!");

           

}



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

//bullish harami

//bearish harami

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

if((this_close > this_open) && (last_close < last_open)  && (this_open > last_close) && (this_close < last_open))

{

Alert("This is bullish harami!");

           

}



if((this_close < this_open) && (last_close > last_open)  && (this_open < last_close) && (this_close > last_open))

{

Alert("This is bearish harami!");

       

}


return(0);

} 
Keith Watford
Moderator
9641
Keith Watford  
rha1:

 Although it runs and I can execute my EA, it doesn't work like it's supposed to.

Keith Watford
Moderator
9641
Keith Watford  

How are we to know what the EA is supposed to do? You don't tell us!

 

Please use the SRC  button to post code

whroeder1
14784
whroeder1  
rha1: doesn't work like it's supposed to.
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it.

  2. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.
  3. string symbol = "EURUSD";
    Don't you want it to work on any chart? Why are you hard coding the pair?
  4. double this_high = iHigh(symbol,PERIOD_H4,1);
    Don't you want those variables to update. Currently constant as of when you add the EA.
To add comments, please log in or register