Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- //@author=LucF for PineCoders
- // This script shows different uses of the security() function using current and previous ([1]) series value, and all permutations of "gaps=" and "lookahead=" parameters.
- // - It shows a warning if chart is not at 1min interval.
- // - It also shows bar state labels if Input setting allows for them.
- // Script only shows repainting plots on a 1 min chart.
- // Code adapted by LucF from original by pinetech at https://www.tradingview.com/script/DF3ivfux-Repainting-Yes-No/ and in v4 documentation.
- // For a discussion of the security() function and its parameters, see:
- // https://www.tradingview.com/pine-script-docs/en/v4/essential/Context_switching_the_security_function.html
- // For a discussion of bar states, see:
- // https://www.tradingview.com/pine-script-docs/en/v4/essential/Bar_states_Built-in_variables_barstate.html
- study("How to avoid repainting & Bar State Labels", overlay=true)
- showBarStates = input(true, "Show bar state labels")
- // Value we will be fetching from higher TF.
- data = close
- // Upper TF we will be fetching data from.
- tf = "5"
- // Determine if we are on a 1min chart.
- int1Min = timeframe.period=="1"
- // Function used to call security() without repeating syntax every time.
- Security(_idx, _gaps, _look) =>
- // Notice use of "_" prefix for function arguments and all variables local to function block, so there is no risk of confusion with global scope vars.
- // Also notice this function has 2 external dependencies: tf and data, which must be previously defined in global scope for function to work.
- // The function has access global scope vars in read-only mode.
- _gaps and _look? security(syminfo.tickerid, tf, data[_idx], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on) :
- _gaps and not _look? security(syminfo.tickerid, tf, data[_idx], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_off) :
- not _gaps and _look? security(syminfo.tickerid, tf, data[_idx], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on) :
- not _gaps and not _look? security(syminfo.tickerid, tf, data[_idx], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off) :na
- // ————— Show all combos of security() calls if we are on 1min TF.
- // idx gaps lookahead
- plot( int1Min? Security( 0, false, false) :na, "0, false, false", color=color.purple, style=plot.style_linebr)
- plot( int1Min? Security( 0, false, true) :na, "0, false, true", color=color.blue, style=plot.style_linebr)
- plot( int1Min? Security( 0, true, false) :na, "0, true, false", color=color.purple, style=plot.style_linebr, linewidth=8)
- plot( int1Min? Security( 0, true, true) :na, "0, true, true", color=color.blue, style=plot.style_linebr, linewidth=8)
- plot( int1Min? Security( 1, false, false) :na, "1, false, false", color=color.red, style=plot.style_linebr)
- // —————————— Best choice: no repaint and no gaps.
- plot( int1Min? Security( 1, false, true) :na, "1, false, true (BEST)", color=color.orange, style=plot.style_linebr, linewidth=16)
- plot( int1Min? Security( 1, true, false) :na, "1, true, false", color=color.red, style=plot.style_linebr, linewidth=8)
- // No repaint but has gaps (na values).
- plot( int1Min? Security( 1, true, true) :na, "1, true, true (OK but gaps)", color=color.fuchsia, style=plot.style_linebr, linewidth=24)
- // Review of best choice syntax:
- // [No need to specify gaps off as it's the default.]
- // v4: security(syminfo.tickerid, "5", close[1], lookahead=barmerge.lookahead_on)
- // v3: security(tickerid, "5", close[1], lookahead=barmerge.lookahead_on)
- // Example: https://www.tradingview.com/pine-script-docs/en/v4/essential/Drawings.html#pivot-points-standard
- // ————— Alternate but more complex way of avoiding repainting using barmerge.lookahead_off.
- // Note that it is also possible to avoid repainting using lookahead off,
- // but we then need to include logic to change indexing for the real-time bar, so it's more complicated.
- indexHighTF = barstate.isrealtime ? 1 : 0
- indexCurrTF = barstate.isrealtime ? 0 : 1
- a0 = security(syminfo.tickerid, tf, data[indexHighTF], lookahead=barmerge.lookahead_off)
- a = a0[indexCurrTF]
- plot( int1Min? a:na, "01, false, false (Good)", color=color.white, style=plot.style_linebr, linewidth=4)
- // —————————— Bar states
- // See: https://www.tradingview.com/pine-script-docs/en/v4/essential/Bar_states_Built-in_variables_barstate.html
- // Shows difference between historical and real-time bar, and when a real-time bar becomes confirmed.
- // To see confirmed bars be re-interpreted as historical bars, recompile with default value of ShowBarStates set to true,
- // let real-time bars become confirmed and then refresh browser.
- first = barstate.isfirst
- last = barstate.islast
- hist = barstate.ishistory
- rt = barstate.isrealtime
- new = barstate.isnew
- conf = barstate.isconfirmed
- // —————— Only show bar state labels if required.
- if showBarStates
- // Buld text label.
- t = new ? "new" : conf ? "conf" : "intra-bar"
- t := t + (hist ? "\nhist" : rt ? "\nrt" : "")
- t := t + (first ? "\nfirst" : last ? "\nlast" : "")
- // Show label above bar.
- label.new(bar_index, na, yloc=yloc.abovebar, text=t, color=hist ? color.green : color.red, size=size.auto)
- // —————— Show instructions to use 1min chart if TF != 1min.
- // Stable ypos for label.
- labelYPos = sma(high,50)
- // Number of bars in the future we position the label.
- barInFuture = 2
- // Need to initialize label id var because it will be used in 2 different local blocks and because we use na value to only create it once.
- // Because we initialize variable to na value, we need to explicitly declare its type, otherwise the compiler will not be able to infer variable type automatically and will throw error.
- // Also we need this variable's value to persist throughout bars so we use v4's "var" keyword to initialize it only once so its value is propagated through successive bars.
- var warningLabel = label(na)
- if last and warningLabel==na and not int1Min
- // Only create label once, when on last bar, its id still contains its na initialazation value, and we are not on a 1min chart.
- warningLabel := label.new(time+barInFuture*(time-time[1]), labelYPos, xloc=xloc.bar_time, yloc=yloc.price, text="Use 1min\nChart", color=color.silver, textcolor=color.new(color.white,0), size=size.large)
- // Rather than creating a new label at each new real-time bar and ending up with a trail of useless labels or having to delete them, we only change the xpos of the existing label.
- if last and not int1Min
- label.set_x(warningLabel, time+barInFuture*(time-time[1]))
- // Make background red if TF != 1min. (thx @midtwnsk8trguy)
- bgcolor(not int1Min? color.new(color.red,85):na)
- // isOneMinuteChart() => timeframe.period=="1" // 1D Chart
- // isThreeMinuteChart() => timeframe.period=="3" // 3 minute chart
- // isFiveMinuteChart() => timeframe.period=="5" // 5D Chart
- // isMonthlyChart() => timeframe.period=="M" // 1M/Monthly Chart
- // isQuarterlyChart() => timeframe.period=="3M" // 3M/QUarterly Chart
- // isYearlyChart() => timeframe.period=="D" // 1Y or 1 day chart
- // isFiveYearChart() => timeframe.period=="W" // 5Y or 1 week chart
- // isAllChart() => timeframe.period=="M" // All chart
- // isSunday() => dayofweek==dayofweek.sunday ? true : false
- // isMonday() => dayofweek==dayofweek.monday ? true : false
- // isTuesday() => dayofweek==dayofweek.tuesday ? true : false
- // isWednesday() => dayofweek==dayofweek.wednesday ? true : false
- // isThursday() => dayofweek==dayofweek.thursday ? true : false
- // isFriday() => dayofweek==dayofweek.friday ? true : false
- // isSaturday() => dayofweek==dayofweek.saturday ? true : false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement