Advertisement
Guest User

Super Bollinger Bands with Cancellation

a guest
Jun 5th, 2025
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.12 KB | Money | 0 0
  1. //@version=5
  2. indicator(shorttitle='Heiken Ashi SBB with cancellation', title='SBB_cancel', overlay=true, timeframe='')
  3.  
  4. //SETTINGS
  5. src = input(close, title='Source')
  6. length = input.int(34, minval=1, title='Bollinger Bands Length')
  7. len = input.int(9, minval=1, title='Trend Line Period')
  8. Periods = input(title='ATR Period', defval=1)
  9. Multiplier = input.float(title='ATR Multiplier', step=0.1, defval=0.9)
  10. mult = input.float(1.750, minval=0.001, maxval=50, title='Dev')
  11. offset = input.int(0, 'Offset', minval=-500, maxval=500)
  12. changeATR = input(title='Change ATR Calculation Method ?', defval=true)
  13. showsignals = input(title='Show Buy/Sell Signals ?', defval=true)
  14. highlighting = input(title='Highlighter On/Off ?', defval=true)
  15.  
  16. //BOLLINGER BANDS
  17. basis = ta.sma(src, length)
  18. dev = mult * ta.stdev(src, length)
  19. upper = basis + dev
  20. lower = basis - dev
  21. p1 = plot(upper, 'bb_upper', color=color.new(#f70606, 0), offset=offset)
  22. p2 = plot(lower, 'bb_lower', color=color.new(color.lime, 0), offset=offset)
  23. fill(p1, p2, title='RangeColor', color=color.new(color.purple, 90))
  24.  
  25. // Plot the basis line too for completeness
  26. plot(basis, 'bb_basis', color=color.new(color.gray, 50), offset=offset, display=display.none)
  27.  
  28. ////SMA
  29. smma = 0.0
  30. smma := na(smma[1]) ? ta.ema(src, len) : (smma[1] * (len - 1) + src) / len
  31. plot(smma, 'trend_line_smma', color=color.new(#ff9800, 0))
  32.  
  33. //Average True Range
  34. atr2 = ta.sma(ta.tr, Periods)
  35. atr = changeATR ? ta.atr(Periods) : atr2
  36. up = src - Multiplier * atr
  37. up1 = nz(up[1], up)
  38. up := close[1] > up1 ? math.max(up, up1) : up
  39. dn = src + Multiplier * atr
  40. dn1 = nz(dn[1], dn)
  41. dn := close[1] < dn1 ? math.min(dn, dn1) : dn
  42. trend = 1
  43. trend := nz(trend[1], trend)
  44. trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
  45.  
  46. //Signal Generator & Trend HighLighter
  47. buySignal = trend == 1 and trend[1] == -1
  48. sellSignal = trend == -1 and trend[1] == 1
  49.  
  50. // Original signals (keep them)
  51. plotshape(buySignal and showsignals ? up : na, title='Buy', text='Buy', location=location.belowbar, style=shape.triangleup, size=size.tiny, color=color.new(color.green, 20), textcolor=color.new(#0a5500, 20))
  52. plotshape(sellSignal and showsignals ? dn : na, title='Sell', text='Sell', location=location.abovebar, style=shape.triangledown, size=size.tiny, color=color.new(color.red, 20), textcolor=color.new(#530000, 0))
  53.  
  54. // Fix the unnamed plots and add proper export-friendly names
  55. mPlot = plot(ohlc4, title='price_ohlc4', style=plot.style_linebr, linewidth=0, display=display.none)
  56. upPlot = plot(trend == 1 ? up : na, title='atr_support_line', style=plot.style_linebr, linewidth=0, color=color.new(color.green, 100))
  57. dnPlot = plot(trend == 1 ? na : dn, title='atr_resistance_line', style=plot.style_linebr, linewidth=0, color=color.new(color.red, 100))
  58.  
  59. // Add additional export data with scripting-friendly names
  60. plot(trend, 'trend_direction', display=display.data_window)
  61. plot(buySignal ? 1 : 0, 'buy_signal', display=display.data_window)
  62. plot(sellSignal ? 1 : 0, 'sell_signal', display=display.data_window)
  63. plot(atr, 'atr_value', display=display.data_window)
  64. plot(up, 'atr_support_value', display=display.data_window)
  65. plot(dn, 'atr_resistance_value', display=display.data_window)
  66.  
  67. // Add change condition for trend reversals
  68. changeCond = trend != trend[1]
  69. plot(changeCond ? 1 : 0, 'trend_change', display=display.data_window)
  70.  
  71. // Add volatility and momentum indicators for better analysis
  72. plot(ta.stdev(src, length), 'bollinger_stdev', display=display.data_window)
  73. plot(volume, 'volume', display=display.data_window)
  74.  
  75. longFillColor = highlighting ? trend == 1 ? color.green : color.green : color.green
  76. shortFillColor = highlighting ? trend == -1 ? color.red : color.red : color.red
  77. fill(mPlot, upPlot, title='Buying Trend Highligter', color=longFillColor, transp=70)
  78. fill(mPlot, dnPlot, title='Selling Trend Highligter', color=shortFillColor, transp=70)
  79.  
  80. // FIXED GEMINI CODE - barstate.time should be just time
  81. varip bool live_buy_established_this_bar = false
  82. varip float live_buy_price_at_establishment = na
  83. varip bool live_sell_established_this_bar = false
  84. varip float live_sell_price_at_establishment = na
  85. varip int last_live_bar_time_processed = na
  86.  
  87. if (barstate.isrealtime)
  88. // Force reset if this is a genuinely new real-time bar
  89. if (time != last_live_bar_time_processed)
  90. live_buy_established_this_bar := false
  91. live_sell_established_this_bar := false
  92. live_buy_price_at_establishment := na
  93. live_sell_price_at_establishment := na
  94. // Update the tracker to this new bar's time
  95. last_live_bar_time_processed := time
  96.  
  97. if (buySignal)
  98. if (not live_buy_established_this_bar)
  99. live_buy_price_at_establishment := up
  100. live_buy_established_this_bar := true
  101. live_sell_established_this_bar := false
  102. live_sell_price_at_establishment := na
  103. // Ensure this bar's time is marked as processed for this tick
  104. if (barstate.isrealtime)
  105. last_live_bar_time_processed := time
  106.  
  107. if (sellSignal)
  108. if (not live_sell_established_this_bar)
  109. live_sell_price_at_establishment := dn
  110. live_sell_established_this_bar := true
  111. live_buy_established_this_bar := false
  112. live_buy_price_at_establishment := na
  113. // Ensure this bar's time is marked as processed for this tick
  114. if (barstate.isrealtime)
  115. last_live_bar_time_processed := time
  116.  
  117. bool show_live_buy_X = barstate.isrealtime and live_buy_established_this_bar and not buySignal
  118. bool show_live_sell_X = barstate.isrealtime and live_sell_established_this_bar and not sellSignal
  119.  
  120. // --- Plotting for Live Bar Indelible Signals + X ---
  121. plotshape(live_buy_established_this_bar and showsignals and barstate.isrealtime ? live_buy_price_at_establishment : na,
  122. title='Live Indelible Buy', text='Buy', style=shape.triangleup,
  123. location=location.absolute, color=color.new(color.green, 20), textcolor=color.new(#0a5500, 20), size=size.tiny)
  124.  
  125. plotchar(show_live_buy_X and showsignals ? live_buy_price_at_establishment : na,
  126. title='Live Buy Cancelled (X)', char='×',
  127. location=location.absolute, color=color.new(color.red, 0), size=size.small)
  128.  
  129. plotshape(live_sell_established_this_bar and showsignals and barstate.isrealtime ? live_sell_price_at_establishment : na,
  130. title='Live Indelible Sell', text='Sell', style=shape.triangledown,
  131. location=location.absolute, color=color.new(color.red, 20), textcolor=color.new(#530000, 0), size=size.tiny)
  132.  
  133. plotchar(show_live_sell_X and showsignals ? live_sell_price_at_establishment : na,
  134. title='Live Sell Cancelled (X)', char='×',
  135. location=location.absolute, color=color.new(color.green, 0), size=size.small)
  136.  
  137. //Buy & Sell Alert Settings
  138. alertcondition(buySignal, title='Buy', message='BUY - {{ticker}}, {{interval}}, Entry={{open}}, SL={{low}}, TP={{high}}, Secure=20Pips, Make Profit=50Pips, Trade Goal=100Pips')
  139. alertcondition(sellSignal, title='Sell', message='SELL - {{ticker}}, {{interval}}, Entry={{open}}, SL={{high}}, TP={{low}}, Secure=20Pips, Make Profit=50Pips, Trade Goal=100Pips')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement