Indicator for bar duration

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #42489 quote
    Alberto Menichetti
    Participant
    Junior

    Hi all,

    I’m using tick bars and I would need an indicator that plots how long did every bar last (in seconds); is that possible? If yes, could you please give me an hint?

    Thanks,

    Alberto

    #42503 quote
    Wing
    Participant
    Veteran

    Does something like this work?

    LastBarTime=Time
    
    BarDuration=Time-LastBarTime[1]
    #42519 quote
    Alberto Menichetti
    Participant
    Junior

    Time seems to return the bar open time in format hhmmss; in any other programming language I would use the bitwise operator & to calculate the seconds elapsed; something like that
    <pre class=”lang:probuilder decode:true “>bt = Time-Time[1]

    BarDuration = (bt & 0xFF0000)*3600 + (bt & 0x00FF00)*60 + (bt & 0x0000FF)
    How could I do that in prorealtime?

    Thanks,

    Alberto

    #42527 quote
    JC_Bywan
    Moderator
    Master

    Hi,

    just a quick moderation message: please update your country flag by selecting a location in your profile settings. Thank you 🙂

    Please see pdf manuals from help menu of the platform, “time” is the closing time of a bar, and “opentime” is the opening time of a bar.

    For a cfd x ticks chart, you would know when the current x ticks bar starts, but unlike for classical “pure time” timeframes (1mn, 1h, 1day etc…) with an x ticks bar you wouldn’t know when it ends as long as it’s not complete yet… so using “time” keyword in a code for x ticks charts is not going to work because code is read on current bar and end of current x ticks bar is not yet known. I hope older forum members will forgive me for being a parrot on this subject: you would be better off working with “opentime” keyword when it comes to x ticks charts.

    So only when an x ticks bar is complete can you know how long it lasted, by making the difference between current new bar’s opentime, and the freshly complete previous bar’s opentime.

    #42531 quote
    robertogozzi
    Moderator
    Master

    Use the above code from Wing in a 1-sec TF and you will get what you want, because in the “hhmmss” the “ss” part has meaningful data.

    Instead, if you use the above code in 1-min TF you will see (with GRAPH) that “LastBarTime” will have at some point, say, 221500 (hhmmss), because the “ss” part is meaningless. In that case you simply have to  divide “BarDuration” by 100, then multiply it by 60 to get the correct number of seconds, but it will always be 60 or a multiple of it, since “time” will always tell you the time of the next bar open, which, unless used in TFs expressed in seconds, will always be in minutes!

    If you use the same code in a 1-hour TF you will have to divide it by 10000 (because also the “mm” part is meaningless) then multiply it by 3600.

    So, to summerize, only a 1-sec TF will tell you exactly the numer of seconds elapsed, but in that case you don’t even need a special piece of code, you just have to count bars as they elapse.

    As for ticks… I don’t know.

    #42532 quote
    Alberto Menichetti
    Participant
    Junior

    Hi Noobywan,

    flag updated 😉

    About the main request, as you can imagine I’m quite new to prorealtime; I read the docs but it’s still unclear to me how things work. I understood your point, so every indicator applied on a price different from open is one bar lagged?

    Let me explain better with an example: using 1hour timeframe I won’t see the moving average applied on close price related to bar at 10.00 until the bar at 11.00 opens, isn’t it? In the platforms I used until now the indicator applied to the current bar is updated during the formation of that bar.

    #42534 quote
    JC_Bywan
    Moderator
    Master

    Code is applied to current bar, and for classic timeframes (seconds or minutes or hours etc…) you’ll see your indicator updated live during each move of currentbar, there’s no lag, like other platforms you used until now… What I described was only for x ticks charts when you can’t know in advance when a bar ends until it’s ended, so only for those would you have info one bar later, this is by definition of construction of x ticks bars independant of time, not by platform type…

    #44794 quote
    Alberto Menichetti
    Participant
    Junior

    If someone needs it….

    // utilizzato nelle barre a tick, o comunque non a tempo, per valutare la durata della barra in secondi
    // ritorna il numero di secondi trascorsi per il completamento di ogni singola barra
    
    ptime = Time[1]
    ctime = Time
    
    // estraggo la differenza in ore
    if ctime>=10000 then
    hend = (ctime - (ctime mod 10000))/10000
    ctime = (ctime mod 10000)
    else
    hend = 0
    endif
    if ptime>=10000 then
    hstart = (ptime - (ptime mod 10000))/10000
    ptime = (ptime mod 10000)
    else
    hstart = 0
    endif
    
    // estraggo la differenza in minuti
    if ctime>=100 then
    mend = (ctime - (ctime mod 100))/100
    ctime = (ctime mod 100)
    else
    mend = 0
    endif
    if ptime>=100 then
    mstart = (ptime - (ptime mod 100))/100
    ptime = (ptime mod 100)
    else
    mstart = 0
    endif
    
    secstarttime = hstart*3600+mstart*60+ptime
    secendtime = hend*3600+mend*60+ctime
    
    barsec = secendtime-secstarttime
    // correggo l'eventuale cambio di giorno
    if barsec<0 then
    barsec = 3600*24-secstarttime+secendtime
    endif
    
    return barsec as "seconds/bar"
    Wing and nnrichard thanked this post
    #44829 quote
    Nicolas
    Keymaster
    Master

    @Alberto

    Thanks for your indicator, I will certainly add it to the Library if you don’t mind. Very useful!

    #44841 quote
    Alberto Menichetti
    Participant
    Junior

    @Nicolas

    you can add it wherever you think is best suited for the forum users

    #219449 quote
    YvesRobert
    Participant
    New

    Hello, I would like to use this indicator seconds/bar inside my code but how to extract this data ?

    I display this indicator below my graphic and then, how to read the seconds in my code using TF 5 minutes ?

    Thank you and have a good day.

    #219486 quote
    Nicolas
    Keymaster
    Master

    To get a bar duration in seconds, you can make a subtract between the current timestamp and the timestamp of the Open:

    duration = timestamp-opentimestamp
    
    return duration as "bar duration in seconds"

    However, a 5-minutes bar will also last 300 seconds 😉

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.

Indicator for bar duration


ProBuilder: Indicators & Custom Tools

New Reply
Summary

This topic contains 11 replies,
has 2 voices, and was last updated by Nicolas
2 years, 5 months ago.

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