Download MetaTrader 5

[Coding Advise] - Need help on holding Condition value inside while loop function.

To add comments, please log in or register
Mohamad Zulhairi Baba
6161
Mohamad Zulhairi Baba  

Hi Guys,

My coding below basically to check reliability/validity of Hammer/ShootingStar (period H1) on period M1 chart. - swing up/down on the last 30min before it closed.

My problem is with this line ----> while(TimeCurrent() >= MinSec(1,0) && TimeCurrent() <= MinSec(5,0))

The condition is true, only and only if(HammerCandle && SwingDown==false && TimeCurrent()== MinSec(1,0)) Condition=true;

How to make it true until  TimeCurrent() <= MinSec(5,0)?

 Appreciate your help/suggestion.

void OnTick()
{
   // Trade Logic
   if(Hammer()) ExecuteBuy();
}

bool Hammer()
{
   double LowerShadow, UpperShadow;
   bool  HammerCandle = false;
   bool  ShootingStar = false;
   bool  Condition = false;
  
   double AO = iOpen(NULL,PERIOD_H1,1);
   double AH = iHigh(NULL,PERIOD_H1,1);
   double AL = iLow(NULL,PERIOD_H1,1);
   double AC = iClose(NULL,PERIOD_H1,1);
   double Body = MathAbs(AO-AC);
  
   if(AC<AO) // Bearish
   {
      LowerShadow = AC-AO;
      UpperShadow = AH-AO;
   }
  
   else // Bulish
   {
      LowerShadow = AO-AL;
      UpperShadow = AH-AC;
   }
  
   if(LowerShadow*3 > Body) HammerCandle = true;
  
   // Need help on this line ---------------
   while(TimeCurrent() >= MinSec(1,0) && TimeCurrent() <= MinSec(5,0))
   {  
      if(TimeCurrent()== MinSec(1,0))
      {
         int   shiftLowest = iLowest(NULL, PERIOD_M1, MODE_LOW, 1, 30);
         int   shiftHighest = iHighest(NULL, PERIOD_M1, MODE_HIGH, 1, 30);
         bool  SwingDown = shiftHighest > shiftLowest;
      
         if(HammerCandle && SwingDown==false) Condition=true;
      }
   }
   // Need help on this line ---------------
  
   return(Condition);
}

datetime MinSec(int pMinute = 0, int pSec = 0)
{
   MqlDateTime timeStruct;
  
   TimeToStruct(TimeCurrent(),timeStruct);
   timeStruct.min = pMinute;
   timeStruct.sec = pSec;
  
   datetime useTime = StructToTime(timeStruct);
   return(useTime);
}

void ExecuteBuy()
{
   // Buy Order goes here  
}
whroeder1
15925
whroeder1  
Drop your minsec code and the loop, just test (in OnTick) for a new bar on the H1.
Mohamad Zulhairi Baba
6161
Mohamad Zulhairi Baba  
whroeder1:
Drop your minsec  code and the loop, just test (in OnTick) for a new bar on the H1.

I know it can be done in easier way (in OnTick for new bar on the H1).
the purpose is to validate on M1 candle!

refer pic below consider valid (at least for myself) the 1st pic only. NOT the second pic.


 This type of setup onlyNot this type

whroeder1
15925
whroeder1  
Hairi Baba:I know it can be done in easier way (in OnTick for new bar on the H1). the purpose is to validate on M1 candle!
Then why are you looking at the previous hour candle, instead of the forming hour candle? Drop your minsec  code and the loop, just test (in OnTick) the H1[0].
Mohamad Zulhairi Baba
6161
Mohamad Zulhairi Baba  
whroeder1:
Then why are you looking at the previous hour candle, instead of for forming hour candle? Drop your minsec  code and the loop, just test (in OnTick) the H1[0].

Hi whroeder1, thanks for replying.

I believe you did misunderstood the purpose of my code (or I didn't understand your comment?)
The code is to verify that previous H1 candle is hammer && Last 30 minutes (in the Previous H1 Candle) is swing up  && The checking is within 5minutes on opening current H1 candle

Anyway, I did update (simplified) the code as below. 

 

 

bool Hammer()
{
   double LowerShadow, UpperShadow;
   bool  Hammer = false;
   bool  ShootingStar = false;
   bool  Condition = false;
   bool  M30_SwingUp = false;
  
   // Previous H1 Candle
   double AO = iOpen(NULL,PERIOD_H1,1);
   double AH = iHigh(NULL,PERIOD_H1,1);
   double AL = iLow(NULL,PERIOD_H1,1);
   double AC = iClose(NULL,PERIOD_H1,1);
   double H1_Body = MathAbs(AO-AC);
  
   // Previous M30 Candle
   double MO = iOpen(NULL,PERIOD_M30,1);
   double MC = iClose(NULL,PERIOD_M30,1);
   double M30_Body = MathAbs(MO-MC);
  
   if(AC<AO) // Bearish
   {
      LowerShadow = AC-AO;
      UpperShadow = AH-AO;
   }
  
   else // Bulish
   {
      LowerShadow = AO-AL;
      UpperShadow = AH-AC;
   }
  
   if(LowerShadow*3 > H1_Body) Hammer = true; // To check Previous H1 Candle is Hammer
   if(MC>MO) M30_SwingUp = true; // To check Previous M30 is swing up
  
   if(Hammer && M30_SwingUp) Condition=true; // Valid Condition for Hammer

   return(Condition);
}
nicholishen
911
nicholishen  
Hairi Baba:

Hi whroeder1, thanks for replying.

I believe you did misunderstood the purpose of my code (or I didn't understand your comment?)
The code is to verify that previous H1 candle is hammer && Last 30 minutes (in the Previous H1 Candle) is swing up  && The checking is within 5minutes on opening current H1 candle

Anyway, I did update (simplified) the code as below. 

 

 

bool Hammer()
{
   double LowerShadow, UpperShadow;
   bool  Hammer = false;
   bool  ShootingStar = false;
   bool  Condition = false;
   bool  M30_SwingUp = false;
  
   // Previous H1 Candle
   double AO = iOpen(NULL,PERIOD_H1,1);
   double AH = iHigh(NULL,PERIOD_H1,1);
   double AL = iLow(NULL,PERIOD_H1,1);
   double AC = iClose(NULL,PERIOD_H1,1);
   double H1_Body = MathAbs(AO-AC);
  
   // Previous M30 Candle
   double MO = iOpen(NULL,PERIOD_M30,1);
   double MC = iClose(NULL,PERIOD_M30,1);
   double M30_Body = MathAbs(MO-MC);
  
   if(AC<AO) // Bearish
   {
      LowerShadow = AC-AO;
      UpperShadow = AH-AO;
   }
  
   else // Bulish
   {
      LowerShadow = AO-AL;
      UpperShadow = AH-AC;
   }
  
   if(LowerShadow*3 > H1_Body) Hammer = true; // To check Previous H1 Candle is Hammer
   if(MC>MO) M30_SwingUp = true; // To check Previous M30 is swing up
  
   if(Hammer && M30_SwingUp) Condition=true; // Valid Condition for Hammer

   return(Condition);
}

You might want to checkout the candlestick pattern library for mql5. https://www.mql5.com/en/code/291

CiMA              m_MA;

double            MA(int ind)                const { return(m_MA.Main(ind));             }
double            CloseAvg(int ind)          const { return(MA(ind));                    }
double            MidPoint(int ind)          const { return(0.5*(High(ind)+Low(ind)));   }

bool CCandlePattern::CheckPatternHammer()
  {
//--- Hammer
   if((MidPoint(1)<CloseAvg(2))                                  && // down trend
      (MathMin(Open(1),Close(1))>(High(1)-(High(1)-Low(1))/3.0)) && // body in upper 1/3
      (Close(1)<Close(2)) && (Open(1)<Open(2)))                     // body gap
      return(true);
//---
   return(false);
  }
MQL5 Wizard - Candlestick Patterns Class
MQL5 Wizard - Candlestick Patterns Class
  • votes: 41
  • 2011.02.16
  • MetaQuotes Software Corp.
  • www.mql5.com
The class can be used for creation of trade signal classes with reversal candlestick patterns.
To add comments, please log in or register