Help with HAMMER code please

 
Hello,

I am starting newly in mql4. Trying to pick black and white hammers in candlesticks with the following indicator. However, in the chart sometimes candlesticks get wrong identification text (white hammer candle is identified as black hammer candle and vice versa). Please help me with this situation, thanks in advance.

#property indicator_chart_window

int limit;
//---- buffers
string hammer[200000];
//+------------------------------------------------------------------+
//| CuStom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{

return(0);
}
//+------------------------------------------------------------------+
//| CuStor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
ObjectsDeleteAll(hammer,OBJ_TEXT);
//----
return(0);
}
//+------------------------------------------------------------------+
//| CuStom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{

int N;
int N1;
int N2;
string text;



int counted_bars=IndicatorCounted();
limit=Bars-counted_bars;


for(N = 1; N < limit; N++) {
hammer[N]= CharToStr(N);

N1 = N + 1;
N2 = N + 2;


//----
//---- check for possible errors
if(counted_bars<0) {
Alert("NO Bars..");
return(-1);
}

// Check for Hammer White
if (((Close[N1]>Open[N1]) && ((Open[N1]-Low[N1])>=2*(Close[N1]-Open[N1])) && ((High[N1]-Close[N1])<=(Open[N1]-Low[N1])*0.10))) {

ObjectCreate(hammer[N], OBJ_TEXT, 0, Time[N1], Low[N1] - Point);
ObjectSetText(hammer[N], "WHmr", 9, "Times New Roman", LawnGreen);

}

// Check for Hammer Black
if (((Close[N1]<Open[N1]) && ((Close[N1]-Low[N1])>=2*(Open[N1]-Close[N1])) && ((High[N1]-Open[N1])<=(Close[N1]-Low[N1])*0.10))) {

ObjectCreate(hammer[N], OBJ_TEXT, 0, Time[N1], Low[N1] - Point);
ObjectSetText(hammer[N], "BHmr", 9, "Times New Roman", LawnGreen);

}



//----
} // End of for loop
return(0);
}
 
svezir:
Hello,

I am starting newly in mql4. Trying to pick black and white hammers in candlesticks with the following indicator. However, in the chart sometimes candlesticks get wrong identification text (white hammer candle is identified as black hammer candle and vice versa). Please help me with this situation, thanks in advance.

Can you show a screen/chart grab showing an example of what yo mean by some candlesticks getting wrongly identified . . . . I know what you mean but I don't see how it can be from your code so an example might help.
 

Thank you for your interest in this matter, I have attached two pictures of the wrong labeling.


 
I think your problem is your Object names . . . your most recent Object has a name of "2" when a few more bars pass and you get a new hammer what will it's object name be ?
 
RaptorUK:
I think your problem is your Object names . . . your most recent Object has a name of "2" when a few more bars pass and you get a new hammer what will it's object name be ?

Exactly.

  1. for(N = 1; N < limit; N++) {
    hammer[N]= CharToStr(N);
    
    N1 = N + 1;
    N2 = N + 2;
    CharToStr takes a character, 0-127, your passing invalid values.
  2. You don't need to save the names
  3. You could just use
    string name = "hmnr" + TimeToStr(TimeCurrent(), TIME_MINUTES);
    ObjectCreate(name..);
  4. Your creating an indicator, you shouldn't be creating objects at all, Look at the fractal indicator.
Reason: