barstate.isrealtime not working in replay mode

212 views Asked by At

barstate.isrealtime not working as I expected.

When it comes to my understanding barstate.isrealtime == false is not working in replay mode. Its working until I click on the replay as it may simulates realtime bars so the statement instantly changes to true.

What is the proper way to use it? or how can I disable barstate data when using replay? or maybe a way to check is replay mode active?

As my script is using request security from different timeframes with different datas of bars and organize it to a table. Its working good until I hop into replay mode and playing the bars back. Its showing inconsistent datas as usual so thats why I want to ignore it and delete the table in replay mode but do not know how. Tried barstate.ishistory also, but same story.

if barstate.isrealtime == false
        table.delete(tabledatas)

Many thanks in advance!


@Mario - here is an example table, I tried what you advised but it always shows the barstate.islast data.

var table tabl1 = table.new('middle' + '_' + 'center', 2, 2, border_color = auto_bg, border_width = 1, frame_color = auto_bg, frame_width = 1)
if barstate.isfirst
    table.cell(tabl1, 0, 0, 'currentvalue' , 0, 0, #ffffff, 'center', text_size=size.huge, bgcolor=color.new(#000000, 0))
    table.cell(tabl1, 1, 0, 'currentvalue' , 0, 0, #ffffff, 'center', text_size=size.huge, bgcolor=color.new(#000000, 0))

else if barstate.islast
    table.cell_set_text(tabl1, 0, 0, 'replayvalue')
    table.cell_set_text(tabl1, 1, 0, 'replayvalue')

replayvalue = can be anything as I will surely put a '-' to it as I do not want to show up any inconsistent value when user in replay mode. So if chart data is real time I want to show 'currentvalue' if replay mode then 'replayvalue'.

if barstate.islast
    var table tabl1 = table.new('middle' + '_' + 'center', 2, 2, border_color = auto_bg, border_width = 1, frame_color = auto_bg, frame_width = 1)
    if barstate.ishistory
        table.cell(tabl1, 0, 0, 'replayvalue' , 0, 0, #ffffff, 'center', text_size=size.huge, bgcolor=color.new(#000000, 0))
        table.cell(tabl1, 1, 0, 'replayvalue' , 0, 0, #ffffff, 'center', text_size=size.huge, bgcolor=color.new(#000000, 0))
    else if barstate.isrealtime
        table.cell(tabl1, 0, 0, 'currentvalue' , 0, 0, #ffffff, 'center', text_size=size.huge, bgcolor=color.new(#000000, 0))
        table.cell(tabl1, 1, 0, 'currentvalue' , 0, 0, #ffffff, 'center', text_size=size.huge, bgcolor=color.new(#000000, 0))

@Mario Here is the updated code based on your reply, but it is doing the same as the first time I did the script. It's okay in real time it shows 'currentvalue', when switching to replay mode it shows 'replayvalue' but when playing the bars in replay mode it shows the 'currentvalue' instead of 'replayvalue' again.

1

There are 1 answers

15
Mario On

barstate.isrealtime is for live data, so it should not work with replay which uses historical data.
But replay changes barstate, why I don't know, in fact no body does, (except the DEVs).

I would use this for strategies when going live, but not for backtesting.

the proper way to initialize tables is...

if barstate.isfirst
    table.cell(...)
else if barstate.islast
    table.cell_set_*()

barstate.isfirst will update the table once only at the beginning of the script,
and barstate.islast will update the table every time the script is at the most right candle/bar.

Here are examples of all barstates from the user manual...

//@version=5
indicator("Bar States", overlay = true, max_labels_count = 500)

stateText() =>
    string txt = ""
    txt += barstate.isfirst     ? "isfirst\n"     : ""
    txt += barstate.islast      ? "islast\n"      : ""
    txt += barstate.ishistory   ? "ishistory\n"   : ""
    txt += barstate.isrealtime  ? "isrealtime\n"  : ""
    txt += barstate.isnew       ? "isnew\n"       : ""
    txt += barstate.isconfirmed ? "isconfirmed\n" : ""
    txt += barstate.islastconfirmedhistory ? "islastconfirmedhistory\n" : ""

labelColor = switch
    barstate.isfirst                => color.fuchsia
    barstate.islastconfirmedhistory => color.gray
    barstate.ishistory              => color.silver
    barstate.isconfirmed            => color.orange
    barstate.isnew                  => color.red
    => color.yellow

label.new(bar_index, na, stateText(), yloc = yloc.abovebar, color = labelColor)

Sorry, as of my knowledge there seems to be no way to check between replay and live chart.
Why would anyone want that to know anyway.
If you have unreliable backtest data, then you should check your code!
Cause it should work all the same, except when using varip.


The closest you will get is using some like this ...

if timenow > time and barstate.isrealtime
    label.new(bar_index, high, "Live")
else
    label.new(bar_index, high, "history")

It uses time to see if its live data or historical data.