Hi,
How can my indicator automatically know about current time frame for the chart (like daily, 4hrs, 1hr, 15min, 5min etc)?
This code snippet can return the current timeframe information as a value in minutes: (not from me, came from a topic on the French forum if I remember correctly)
once NbBar = 1
if BarIndex < NbBar+2 then
MyDay=openday
dayminutes = 1440*(MyDay-MyDay[1])
MyHour=openhour
hourminutes = 60*(MyHour-MyHour[1])
MyMin=openminute
barminutes = MyMin - MyMin[1] + hourminutes + dayminutes
barminutes=abs(barminutes)
Mybarminutes = lowest[NbBar](barminutes)[1]
endif
return Mybarminutes as "Timeframe"
AVTParticipant
Senior
Would anyone be so kind to explain what the code just 2 posts above is doing? I am completely lost on understanding it.
It begins already with the if statement: BarIndex will never/seldom be less than 3 (at least on my computer I see only values around 500 minimum) , so the caculation should never be executed!
I tested all the times I can load with that code, for 4 hours it gives Timeframe=1.500 although the abs(barminutes) is correct 240 and for 7 hours Timeframe=180 with abs(barminutes)=420.
I need to limit calculation in one of my scripts depending on the timeframe of the chart and usually I am of the opinion “one must not invent the wheel for the 101st time”, but I don’t like to just c&p code hoping it will work without understanding it. That’s the code from above:
once NbBar = 1
if BarIndex < NbBar+2 then
MyDay=openday
dayminutes = 1440*(MyDay-MyDay[1])
MyHour=openhour
hourminutes = 60*(MyHour-MyHour[1])
MyMin=openminute
barminutes = MyMin - MyMin[1] + hourminutes + dayminutes
barminutes=abs(barminutes)
Mybarminutes = lowest[NbBar](barminutes)[1]
endif
return Mybarminutes as "Timeframe"
Thanks a lot.
Why
if BarIndex < NbBar+2 then
is not written as
if BarIndex < 3 then
?
Because NbBar NEVER changes!
AVTParticipant
Senior
@robertogozzi
Thanks for your answer. It gave me indirectly the first solution (the if statement says simply: only take the first two bars for caculation) – sometimes I am a blockhead!
You said NbBar never changes – 2 also never changes, so consequently NbBar+2 will ALWAYS be 3 and it is legitimate to write
if BarIndex < 3 then
NbBar is only used once agin in that code, in the line
Mybarminutes = lowest[NbBar](barminutes)[1]
and as it never changes and in the meantime its value has not been changed (by looping or something like that), it is as well legitimate to write
Mybarminutes = lowest[1](barminutes)[1]
and NbBar=1 is not needed any longer. Correct?
Thank you so much.