Download MetaTrader 5

Simple Hammer Code. Help Needed !

To add comments, please log in or register
Michael Green
117
Michael Green  

BullishHammer=((Open[i]+Close[i])>(High[i]+Low[i]/3.0));

I am attempting to construct a hammer pattern signal in this case bullish where the body is a third the size of the candle high/low range. Can some one point me to where I am going wrong on this? and also how would I quantify this as the body being in the upper portion of the candle ??

Any help would be appreciated thanks Mickey !!

Bilal Said
363
Bilal Said  
Michael Green:

BullishHammer=((Open[i]+Close[i])>(High[i]+Low[i]/3.0));

I am attempting to construct a hammer pattern signal in this case bullish where the body is a third the size of the candle high/low range. Can some one point me to where I am going wrong on this? and also how would I quantify this as the body being in the upper portion of the candle ??

Any help would be appreciated thanks Mickey !!


 i use this code : try to modify it :


bool CheckMartelo(double PA,double PF,double PMIN,double PMAX)
{



// check IF BULLISH HAMMER

if(PF > PA) 
{
if(((PF - PA)*2) < (PA-PMIN)) // CHECK IF IS HAMMER 
{

      if(((PMAX-PF)) < (PF-PA)) // CHECK SHADOW SIZE
      {
      return true;
      }
}
}

honest_knave
Moderator
2326
honest_knave  
Michael Green:

BullishHammer=((Open[i]+Close[i])>(High[i]+Low[i]/3.0));

I am attempting to construct a hammer pattern signal in this case bullish where the body is a third the size of the candle high/low range. Can some one point me to where I am going wrong on this? and also how would I quantify this as the body being in the upper portion of the candle ??

Any help would be appreciated thanks Mickey !!

bool BullishHammer(int i)
  {
   double body_size = Close[i] - Open[i];
   if(body_size <= 0)                    return(false); // reject (a bearish or doji candle)
   double candle_range = High[i] - Low[i];
   if(body_size < candle_range/3)        return(false); // reject (body is less than 1/3)
   if(Open[i] < Low[i] + candle_range/2) return(false); // reject (body is in lower 1/2)
   return(true);
  }

bool BearishHammer(int i)
  {
   double body_size = Open[i] - Close[i];
   if(body_size <= 0)                    return(false); // reject (a bullish or doji candle)
   double candle_range = High[i] - Low[i];
   if(body_size < candle_range/3)        return(false); // reject (body is less than 1/3)
   if(Open[i] > Low[i] + candle_range/2) return(false); // reject (body is in upper 1/2)
   return(true);
  }
Michael Green
117
Michael Green  
honest_knave:


Body is more than 1/3 of the size of the candle range, body in upper 1/2 of candle:

If you want less than 1/3, be careful about inadvertently comparing a bearish candle in your check.


thanks can understand that however how would i depict the body in the upper portion of the candle using the same code style?

honest_knave
Moderator
2326
honest_knave  
Michael Green:


thanks can understand that however how would i depict the body in the upper portion of the candle using the same code style?


I modified my post above with 2 simple functions.

Simply call them with the index you want to check, and they will return true/false.

Michael Green
117
Michael Green  
honest_knave:


I modified my post above with 2 simple functions.

Simply call them with the index you want to check, and they will return true/false.

 
int counted_bars=IndicatorCounted();
   int limit= Bars-counted_bars;
   for(int i=1;i<limit;i++)
     {
      //---
      double O=(Open[i]);
      double O1=(Open[i+1]);
      double O2=(Open[i+2]);
      double H=(High[i]);
      double H1=(High[i+1]);
      double H2=(High[i+2]);
      double L=(Low[i]);
      double L1=(Low[i+1]);
      double L2=(Low[i+2]);
      double C=(Close[i]);
      double C1=(Close[i+1]);
      double C2=(Close[i+2]);
      //---
      double AvgBody=(Open[i]-Close[i]);
      double AvgBody2=(Open[i+1]-Close[i+1]);
      double AvgBody3=(Open[i+2]-Close[i+2]);
      //---
      double MidPoint=(High[i]+Low[i])/2;
      double MidPoint1=(High[i+1]+Low[i+1])/2;
      double MidPoint2=(High[i+2]+Low[i+2])/2;
      //---
      double AverageBodySize=(Open[i]+Close[i])/2;
      double Body=(Open[i]+Close[i]);
      double HighLow=(High[i]+Low[i]);
      //---
      double body_size=(Close[i] - Open[i]);
      double candle_range=(High[i] - Low[i]);

      double Price=(High[i]+Low[i])/2;
      double PriceBack3=(High[i+2]+Low[i+2])/2;

      //Bullish Patterns
      double BullishEngulfing=((O1>C1) && (C>O) && (C>=O1) && (C1>=O) && ((C-O)>(O1-C1)));
      double BullishHarami=((O1>C1) && (C>O) && (C<=O1) && (C1<=O) && ((C-O)<(O1-C1)));
      //---
      double BullishThreeInsideUp=((Low[i+4]<Low[i+5]) && (Low[i+3]<Low[i+4]) && (High[i+2]<High[i+3]) && (Low[i+2]>Low[i+3]) && (High[i]>High[i+2]));
      double TweezerBottoms=((Open[i+1]==Close[i]) && (Close[i]>Open[i]));
      double MorningStar=((Close[i+2]<Open[i+2]) && (Close[i+1]<Open[i+1] || Close[i+1]>Open[i+1]) && (Close[i+1]<Close[i+2]) && (Open[i+1]<Close[i+2]) && (Close[i]>Open[i]) && (Open[i]>Close[i+1]) && (Open[i]>Open[i+1]));
      double BullishRailwayTrack=((Close[i+1]<Open[i+1]) && (Open[i]>Close[i]) && (Open[i+1]==Open[i]) && (Close[i+1]==Close[i]));
      double PiercingLine=((Close[i+1]<Open[i+1]) && (Close[i]>Open[i]) && (Open[i]<Close[i+1]) && (Close[i]>MidPoint1));
      double BullishHomingPidgeon=((Close[i+1]<Open[i+1]) && (Close[i]<Open[i]) && (High[i]<High[i+1]) && (Low[i]>Low[i+1]) && (Open[i]<Open[i+1]) && (Close[i]>Close[i+1]));
      double BullishHammer=((!body_size <= 0) && (!body_size < candle_range/3) && (!Open[i] < Low[i] + candle_range/2));
      //---
      double BearishThreeSideDown=((High[i+4]>High[i+5]) && (High[i+3]>High[i+4]) && (High[i+2]<High[i+3]) && (Low[i+2]>Low[i+3]) && (Low[i]<Low[i+2]));
      double TweezerTops=((Open[i+1]==Close[i]) && (Close[i]<Open[i]));
      double EveningStar=((Close[i+2]>Open[i+2]) && (Close[i+1]>Open[i+1] || Close[i+1]<Open[i+1]) && (Close[i+1]>Close[i+2]) && (Open[i+1]>Close[i+2]) && (Close[i]<Open[i]) && (Open[i]<Close[i+1]) && (Open[i]<Open[i+1]));
      double BearishRailwayTrack=((Close[i+1]>Open[i+1]) && (Open[i]<Close[i]) && (Open[i+1]==Open[i]) && (Close[i+1]==Close[i]));
      double DarkCloudCover=((Close[i+1]>Open[i+1]) && (Close[i]<Open[i]) && (Open[i]>Close[i+1]) && (Close[i]<MidPoint1));//--Piercing Line Bearish
      double BearishHawk=((Close[i+1]>Open[i+1]) && (Close[i]>Open[i]) && (High[i]>High[i+1]) && (Low[i]<Low[i+1]) && (Open[i]>Open[i+1]) && (Close[i]<Close[i+1]));//---BullishHomingPidgeon

      //---good signal but needs end to repeating
      //double ThreeBlackCrows=((Close[i+2]-Open[i+2]<AvgBody3) && (Close[i+1]-Open[i+1]<AvgBody2) && (Close[i]-Open[i]<AvgBody) && (MidPoint1<MidPoint2) && (MidPoint<MidPoint2));



      //---
      double SmaH4=iMA(NULL,PERIOD_H4,200,0,MODE_SMA,PRICE_MEDIAN,i);
      //---

      //---

      if(show==true)
        {
         Hr4200sma[i]=SmaH4;
        }

      if(BullishHammer)

        {
         UpArrow[i]=Open[i];
        }

      if(BearishHawk)

        {
         DownArrow[i]=Open[i];
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }

Michael Green
117
Michael Green  

double BullishHammer=((!body_size <= 0) && (!body_size < candle_range/3) && (!Open[i] < Low[i] + candle_range/2));

doesn't seem to work ?? I am doing something wrong?

Michael Green
117
Michael Green  
saidmrn:


 i use this code : try to modify it :



can you pass me the double inputs?

(double PA,double PF,double PMIN,double PMAX) thanks 
honest_knave
Moderator
2326
honest_knave  
Michael Green:

double BullishHammer=((!body_size <= 0) && (!body_size < candle_range/3) && (!Open[i] < Low[i] + candle_range/2));

doesn't seem to work ?? I am doing something wrong?


Please try to use SRC button when posting code; it makes it a lot easier to read (I've modified your post above).

Firstly, you are using doubles (numbers) to store something that should really be boolean (true/false), no?

Take a look here for a good guide about doing lookbacks correctly.

The easier approach (in my opinion) is to have distinct function calls for your patterns.

An example using the new format:

#property strict
#property indicator_chart_window
#property indicator_buffers 2

double UpArrow[],
       DownArrow[];

int OnInit()
  {
   SetIndexBuffer(0,UpArrow);
   SetIndexBuffer(1,DownArrow);
   SetIndexStyle(0,DRAW_ARROW,STYLE_SOLID,2,clrLime);
   SetIndexStyle(1,DRAW_ARROW,STYLE_SOLID,2,clrRed);
   SetIndexArrow(0,233);
   SetIndexArrow(1,234);
   return(INIT_SUCCEEDED);
  }

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   for(int i=rates_total-fmax(1,prev_calculated); i>0; i--)
     {
      if(BullishHammer(i)) UpArrow[i]  =Low[i];  else UpArrow[i]  =EMPTY_VALUE;
      if(BearishHammer(i)) DownArrow[i]=High[i]; else DownArrow[i]=EMPTY_VALUE;
     }
   return(rates_total);
  }

bool BullishHammer(int i)
  {
   double body_size = Close[i] - Open[i];
   if(body_size <= 0)                    return(false); // reject (a bearish or doji candle)
   double candle_range = High[i] - Low[i];
   if(body_size < candle_range/3)        return(false); // reject (body is less than 1/3)
   if(Open[i] < Low[i] + candle_range/2) return(false); // reject (body is in lower 1/2)
   return(true);
  }

bool BearishHammer(int i)
  {
   double body_size = Open[i] - Close[i];
   if(body_size <= 0)                    return(false); // reject (a bullish or doji candle)
   double candle_range = High[i] - Low[i];
   if(body_size < candle_range/3)        return(false); // reject (body is less than 1/3)
   if(Open[i] > Low[i] + candle_range/2) return(false); // reject (body is in upper 1/2)
   return(true);
  }

Finally, I'll just point out that you are reinventing the wheel. There are plenty of these pattern-finding indicators already available.

If you're doing it as an exercise to learn, that is great! But I just thought I'd point it out in case you didn't know.

Good luck!

Michael Green
117
Michael Green  
honest_knave:


Please try to use SRC button when posting code; it makes it a lot easier to read (I've modified your post above).

Firstly, you are using doubles (numbers) to store something that should really be boolean (true/false), no?

Take a look here for a good guide about doing lookbacks correctly.

The easier approach (in my opinion) is to have distinct function calls for your patterns.

An example using the new format:

Finally, I'll just point out that you are reinventing the wheel. There are plenty of these pattern-finding indicators already available.

If you're doing it as an exercise to learn, that is great! But I just thought I'd point it out in case you didn't know.

Good luck!


Thanks I am still newish to coding so will try and work in this format from now on :)
To add comments, please log in or register