How to get current TimeFrame inside indicator code?

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #31811 quote
    karunswaro
    Participant
    Junior

    Hi,

    How can my indicator automatically know about current time frame for the chart (like daily, 4hrs, 1hr, 15min, 5min etc)?

    #31871 quote
    Nicolas
    Keymaster
    Legend

    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"
    #31882 quote
    karunswaro
    Participant
    Junior

    Perfect thanks!!!

    #41604 quote
    AVT
    Participant
    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.

    #41606 quote
    robertogozzi
    Moderator
    Legend

    Why

    if BarIndex < NbBar+2 then

    is not written as

    if BarIndex < 3 then

    ?

    Because NbBar NEVER changes!

    AVT thanked this post
    #41658 quote
    AVT
    Participant
    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.

    #41678 quote
    robertogozzi
    Moderator
    Legend

    Correct!

    Roberto

    AVT thanked this post
Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.
ProRealAI ProRealAI New

Stuck on this ProBuilder code?

Describe what this topic is trying to build, in plain English, and ProRealAI writes the ProRealTime™ indicator, screener or system for you.

Available in 7 languages
Try ProRealAI

How to get current TimeFrame inside indicator code?


ProBuilder: Indicators & Custom Tools

New Reply
Author
author-avatar
karunswaro @karunswaro Participant
Summary

This topic contains 6 replies,
has 4 voices, and was last updated by robertogozzi
9 years, 1 month ago.

Topic Details
Forum: ProBuilder: Indicators & Custom Tools
Language: English
Started: 04/12/2017
Status: Active
Attachments: No files
Logo Logo
Loading...