Advertisement
PineCoders

How to avoid repainting & Bar State Labels

Jul 27th, 2019
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.90 KB | None | 0 0
  1. //@version=4
  2. //@author=LucF for PineCoders
  3.  
  4. // This script shows different uses of the security() function using current and previous ([1]) series value, and all permutations of "gaps=" and "lookahead=" parameters.
  5. // - It shows a warning if chart is not at 1min interval.
  6. // - It also shows bar state labels if Input setting allows for them.
  7. // Script only shows repainting plots on a 1 min chart.
  8. // Code adapted by LucF from original by pinetech at https://www.tradingview.com/script/DF3ivfux-Repainting-Yes-No/ and in v4 documentation.
  9.  
  10. // For a discussion of the security() function and its parameters, see:
  11. // https://www.tradingview.com/pine-script-docs/en/v4/essential/Context_switching_the_security_function.html
  12. // For a discussion of bar states, see:
  13. // https://www.tradingview.com/pine-script-docs/en/v4/essential/Bar_states_Built-in_variables_barstate.html
  14.  
  15. study("How to avoid repainting & Bar State Labels", overlay=true)
  16.  
  17. showBarStates = input(true, "Show bar state labels")
  18.  
  19. // Value we will be fetching from higher TF.
  20. data = close
  21. // Upper TF we will be fetching data from.
  22. tf = "5"
  23. // Determine if we are on a 1min chart.
  24. int1Min = timeframe.period=="1"
  25.  
  26. // Function used to call security() without repeating syntax every time.
  27. Security(_idx, _gaps, _look) =>
  28. // 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.
  29. // Also notice this function has 2 external dependencies: tf and data, which must be previously defined in global scope for function to work.
  30. // The function has access global scope vars in read-only mode.
  31. _gaps and _look? security(syminfo.tickerid, tf, data[_idx], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_on) :
  32. _gaps and not _look? security(syminfo.tickerid, tf, data[_idx], gaps=barmerge.gaps_on, lookahead=barmerge.lookahead_off) :
  33. not _gaps and _look? security(syminfo.tickerid, tf, data[_idx], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_on) :
  34. not _gaps and not _look? security(syminfo.tickerid, tf, data[_idx], gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off) :na
  35.  
  36. // ————— Show all combos of security() calls if we are on 1min TF.
  37. // idx gaps lookahead
  38. plot( int1Min? Security( 0, false, false) :na, "0, false, false", color=color.purple, style=plot.style_linebr)
  39. plot( int1Min? Security( 0, false, true) :na, "0, false, true", color=color.blue, style=plot.style_linebr)
  40. plot( int1Min? Security( 0, true, false) :na, "0, true, false", color=color.purple, style=plot.style_linebr, linewidth=8)
  41. plot( int1Min? Security( 0, true, true) :na, "0, true, true", color=color.blue, style=plot.style_linebr, linewidth=8)
  42.  
  43. plot( int1Min? Security( 1, false, false) :na, "1, false, false", color=color.red, style=plot.style_linebr)
  44. // —————————— Best choice: no repaint and no gaps.
  45. plot( int1Min? Security( 1, false, true) :na, "1, false, true (BEST)", color=color.orange, style=plot.style_linebr, linewidth=16)
  46. plot( int1Min? Security( 1, true, false) :na, "1, true, false", color=color.red, style=plot.style_linebr, linewidth=8)
  47. // No repaint but has gaps (na values).
  48. plot( int1Min? Security( 1, true, true) :na, "1, true, true (OK but gaps)", color=color.fuchsia, style=plot.style_linebr, linewidth=24)
  49.  
  50. // Review of best choice syntax:
  51. // [No need to specify gaps off as it's the default.]
  52. // v4: security(syminfo.tickerid, "5", close[1], lookahead=barmerge.lookahead_on)
  53. // v3: security(tickerid, "5", close[1], lookahead=barmerge.lookahead_on)
  54. // Example: https://www.tradingview.com/pine-script-docs/en/v4/essential/Drawings.html#pivot-points-standard
  55.  
  56. // ————— Alternate but more complex way of avoiding repainting using barmerge.lookahead_off.
  57. // Note that it is also possible to avoid repainting using lookahead off,
  58. // but we then need to include logic to change indexing for the real-time bar, so it's more complicated.
  59. indexHighTF = barstate.isrealtime ? 1 : 0
  60. indexCurrTF = barstate.isrealtime ? 0 : 1
  61. a0 = security(syminfo.tickerid, tf, data[indexHighTF], lookahead=barmerge.lookahead_off)
  62. a = a0[indexCurrTF]
  63. plot( int1Min? a:na, "01, false, false (Good)", color=color.white, style=plot.style_linebr, linewidth=4)
  64.  
  65.  
  66. // —————————— Bar states
  67. // See: https://www.tradingview.com/pine-script-docs/en/v4/essential/Bar_states_Built-in_variables_barstate.html
  68. // Shows difference between historical and real-time bar, and when a real-time bar becomes confirmed.
  69. // To see confirmed bars be re-interpreted as historical bars, recompile with default value of ShowBarStates set to true,
  70. // let real-time bars become confirmed and then refresh browser.
  71.  
  72. first = barstate.isfirst
  73. last = barstate.islast
  74. hist = barstate.ishistory
  75. rt = barstate.isrealtime
  76. new = barstate.isnew
  77. conf = barstate.isconfirmed
  78.  
  79. // —————— Only show bar state labels if required.
  80. if showBarStates
  81. // Buld text label.
  82. t = new ? "new" : conf ? "conf" : "intra-bar"
  83. t := t + (hist ? "\nhist" : rt ? "\nrt" : "")
  84. t := t + (first ? "\nfirst" : last ? "\nlast" : "")
  85. // Show label above bar.
  86. label.new(bar_index, na, yloc=yloc.abovebar, text=t, color=hist ? color.green : color.red, size=size.auto)
  87.  
  88. // —————— Show instructions to use 1min chart if TF != 1min.
  89. // Stable ypos for label.
  90. labelYPos = sma(high,50)
  91. // Number of bars in the future we position the label.
  92. barInFuture = 2
  93. // 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.
  94. // 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.
  95. // 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.
  96. var warningLabel = label(na)
  97. if last and warningLabel==na and not int1Min
  98. // Only create label once, when on last bar, its id still contains its na initialazation value, and we are not on a 1min chart.
  99. 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)
  100. // 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.
  101. if last and not int1Min
  102. label.set_x(warningLabel, time+barInFuture*(time-time[1]))
  103.  
  104. // Make background red if TF != 1min. (thx @midtwnsk8trguy)
  105. bgcolor(not int1Min? color.new(color.red,85):na)
  106.  
  107. // isOneMinuteChart() => timeframe.period=="1" // 1D Chart
  108. // isThreeMinuteChart() => timeframe.period=="3" // 3 minute chart
  109. // isFiveMinuteChart() => timeframe.period=="5" // 5D Chart
  110. // isMonthlyChart() => timeframe.period=="M" // 1M/Monthly Chart
  111. // isQuarterlyChart() => timeframe.period=="3M" // 3M/QUarterly Chart
  112. // isYearlyChart() => timeframe.period=="D" // 1Y or 1 day chart
  113. // isFiveYearChart() => timeframe.period=="W" // 5Y or 1 week chart
  114. // isAllChart() => timeframe.period=="M" // All chart
  115.  
  116. // isSunday() => dayofweek==dayofweek.sunday ? true : false
  117. // isMonday() => dayofweek==dayofweek.monday ? true : false
  118. // isTuesday() => dayofweek==dayofweek.tuesday ? true : false
  119. // isWednesday() => dayofweek==dayofweek.wednesday ? true : false
  120. // isThursday() => dayofweek==dayofweek.thursday ? true : false
  121. // isFriday() => dayofweek==dayofweek.friday ? true : false
  122. // isSaturday() => dayofweek==dayofweek.saturday ? true : false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement