Draw object on predefined timeframe (chart)

 

Hello folks.

I'm making calculations for the box size according to H1. So if my chart is set on H1 while the box is drawn I'm fine. If I'm on another timeframe, I'm messed up, because the box is drawn according to the candles and not time.

How can I tell in the code to draw the box on H1, no matter what time I currently have?
In case this is not working: How can I approach what I need?


void Draw_Box()
   {
   int HighestIndex = iHighest(NULL,PERIOD_H1,MODE_HIGH,LengthOfBox,1);
   HighestHighInBox = High[HighestIndex];
   int LowestIndex = iLowest(NULL,PERIOD_H1,MODE_LOW,LengthOfBox,1);
   LowestLowInBox = Low[LowestIndex];

   if (ObjectFind("Box") == -1)
      {
      ObjectCreate("Box", OBJ_RECTANGLE, 0, Time[LengthOfBox], HighestHighInBox, Time[1], LowestLowInBox);
      }
   ObjectSet("Box", OBJPROP_TIME1, Time[LengthOfBox]);
   ObjectSet("Box", OBJPROP_PRICE1, HighestHighInBox);
   ObjectSet("Box", OBJPROP_TIME2, Time[1]);
   ObjectSet("Box", OBJPROP_PRICE2, LowestLowInBox);
   ObjectSet("Box", OBJPROP_COLOR, BoxColor);
   }

Just to clarify: If I calculate a box for the last 4 hours, it will be including 4 H1 candles. If I have my chart in H4 when the box is drawn, the box will include 4 H4 candles, while it should only be one in my case.

Anybody some input?


Thanks.
WorstCases

 

I think your problem is your LengthOfBox variable, is it specified in H1 candles ?

That is OK, but if it is the case you can't use these to work out the time for the ends of the box . . .

   ObjectSet("Box", OBJPROP_TIME1, Time[LengthOfBox]);   // <--- only works on H1

   ObjectSet("Box", OBJPROP_PRICE1, HighestHighInBox);

   ObjectSet("Box", OBJPROP_TIME2, Time[1]);             // <--- only works on H1

. . . instead try . . .

   ObjectSet("Box", OBJPROP_TIME1, iTime(NULL, PERIOD_H1, LengthOfBox) );   // <--- works on any TF

   ObjectSet("Box", OBJPROP_PRICE1, HighestHighInBox);

   ObjectSet("Box", OBJPROP_TIME2, iTime(NULL, PERIOD_H1, 1) );             // <--- works on any TF

You will also have a similar issue for the top and bottom of the box but I'll leave that to you to address.

 
Will get right on it. Thank you!
 
int HighestIndex = iHighest(NULL,PERIOD_H1,MODE_HIGH,LengthOfBox,1);
   HighestHighInBox = iHigh[NULL,PERIOD_H1,HighestIndex];   //<-------
   int LowestIndex = iLowest(NULL,PERIOD_H1,MODE_LOW,LengthOfBox,1);
   LowestLowInBox = iLow[NULL,PERIOD_H1,LowestIndex];       //<-------


Got it! Thank you for the task.

Always better to teach somebody fishing than just giving him a fish.