Help - a simple indicator

 

Hi,

I am trying to build a simple indicator that depicts the High and Low within 21 days, and then add some lines in between.

The problem is that in the D1 chart the lines are drawn correctly. When I shift to another timeframe, the line behave erratically, and do not represent the correct values from the D1 chart.


Please help.

Here is the code:


Zeev


========================================================================================================

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

//| comfortZone.mq4 |

//| Copyright Dr. Zeev Ben-Nahum. |

//| http://www.theParisMap.com |

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

#property copyright "Copyright Dr. Zeev Ben-Nahum."

#property link "http://www.theParisMap.com"

#property indicator_chart_window

#property indicator_color1 Blue

#property indicator_color2 Red


//--------- static ----------//

static datetime CurrentBarTime = 0;


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

//| Deleting lines function |

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

void DeleteLines()

{

ObjectDelete("High");

ObjectDelete("Low");

ObjectDelete("Median");

ObjectDelete("midHigh");

ObjectDelete("midLow");

ObjectsRedraw();

}

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

//| Custom indicator initialization function |

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

int init()

{

DeleteLines();

return(0);

}

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

//| Custom indicator deinitialization function |

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

int deinit()

{

DeleteLines();

return(0);

}

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

//| Custom indicator iteration function |

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

//int iHighest( string symbol, int timeframe, int type, int count=WHOLE_ARRAY, int start=0)

//Returns the shift of the maximum value over a specific number of periods depending on type.

//double val;

// calculating the highest value on the 20 consequtive bars in the range

// from the 4th to the 23rd index inclusive on the current chart

//val=High[iHighest(NULL,PERIOD_D1,MODE_HIGH,20,4)]; -> instead of PERIOD_D1 it was 0=current chart timeframe


int start()

{

/// checking if the script should run (one time per bar)

if (iTime(NULL, PERIOD_D1, 0) == CurrentBarTime)

{

return (0);

}

Print ("An hour have passed: " );

CurrentBarTime = iTime(NULL, PERIOD_D1, 0);

//Print("iClose current = ",iClose(NULL,PERIOD_D1,0), ", iClose -1 = ", iClose(NULL,PERIOD_D1,1));

//Print("current close = ", iClose(NULL, PERIOD_D1, 0));

//Print("iTime = ",iTime(NULL,PERIOD_D1,0), ", Time = ", Time[0]-PERIOD_D1*60);

double hiz = NormalizeDouble(High[iHighest(NULL,PERIOD_D1,MODE_HIGH,21,1)],Digits);

double loz = NormalizeDouble(Low[iLowest(NULL,PERIOD_D1,MODE_LOW,21,1)],Digits);

double hiz_loz = hiz - loz;

double median = NormalizeDouble(loz + hiz_loz / 2, Digits);

double midLow = NormalizeDouble(loz + hiz_loz / 4, Digits);

double midHigh = NormalizeDouble(loz + (hiz_loz / 4 * 3), Digits);

ObjectCreate("High", OBJ_HLINE, 0, 0, hiz);

ObjectSet("High", OBJPROP_COLOR, Green);

ObjectSet("High", OBJPROP_STYLE, STYLE_SOLID);

ObjectSet("High", OBJPROP_WIDTH, 2);

ObjectSetText("High","High", 10, "Times New Roman", Green);

ObjectCreate("Low", OBJ_HLINE, 0, 0, loz);

ObjectSet("Low", OBJPROP_COLOR, Red);

ObjectSet("Low", OBJPROP_STYLE, STYLE_SOLID);

ObjectSet("Low", OBJPROP_WIDTH, 2);

ObjectSetText("Low","Low", 10, "Times New Roman", Red);

ObjectCreate("Median", OBJ_HLINE, 0, 0, median);

ObjectSet("Median", OBJPROP_COLOR, Blue);

ObjectSet("Median", OBJPROP_STYLE, STYLE_SOLID);

ObjectSet("Median", OBJPROP_WIDTH, 2);

ObjectSetText("Median","Median", 10, "Times New Roman", Blue);

ObjectCreate("midLow", OBJ_HLINE, 0, 0, midLow);

ObjectSet("midLow", OBJPROP_COLOR, Orange);

ObjectSet("midLow", OBJPROP_STYLE, STYLE_DASH);

ObjectSet("midLow", OBJPROP_WIDTH, 1);

ObjectSetText("midLow","midLow", 10, "Times New Roman", Orange);

ObjectCreate("midHigh", OBJ_HLINE, 0, 0, midHigh);

ObjectSet("midHigh", OBJPROP_COLOR, Aqua);

ObjectSet("midHigh", OBJPROP_STYLE, STYLE_DASH);

ObjectSet("midHigh", OBJPROP_WIDTH, 1);

ObjectSetText("midHigh","midHigh", 50, "Times New Roman", Aqua);

//---- force objects drawing

ObjectsRedraw();

//----

return(0);

}

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

 

Hi Zeev


I think your calculations should use iHigh() instead of High[]. For example,


double hiz = NormalizeDouble( High[ iHighest( NULL, PERIOD_D1, MODE_HIGH, 21, 1 ) ], Digits );


should be


double hiz = NormalizeDouble( iHigh( NULL, PERIOD_D1, iHighest( NULL, PERIOD_D1, MODE_HIGH, 21, 1 ) ), Digits );


High[] use the chart timeframe, whereas iHigh allows you to specify the timeframe in the same way iHighest does.


The same for loz.


Also, I find a few spaces make things easier to read.


Cheers

Jellyean

 
Jellybean:

Hi Zeev


I think your calculations should use iHigh() instead of High[]. For example,


double hiz = NormalizeDouble( High[ iHighest( NULL, PERIOD_D1, MODE_HIGH, 21, 1 ) ], Digits );


should be


double hiz = NormalizeDouble( iHigh( NULL, PERIOD_D1, iHighest( NULL, PERIOD_D1, MODE_HIGH, 21, 1 ) ), Digits );


High[] use the chart timeframe, whereas iHigh allows you to specify the timeframe in the same way iHighest does.


The same for loz.


Also, I find a few spaces make things easier to read.


Cheers

Jellyean

A simple (uneducated) question that was answered in full.

Thanks a million.

Zeev