Advertisement
Guest User

BB ATR SR (Fred)

a guest
Dec 5th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. //@version=3
  2. study("BB ATR SR", shorttitle="BB ATR SR", overlay=true)
  3.  
  4. custom_timeframe = input(title="Use another Timeframe?", type=bool, defval=false)
  5. highTimeFrame = input(title="Select The Timeframe", type=resolution, defval="60")
  6. res = custom_timeframe ? highTimeFrame : period
  7.  
  8. //
  9. // ATR RS/RL
  10. //
  11.  
  12. atr_src = close
  13.  
  14. atr_length = input(14, minval=1)
  15. atr_mult = input(2.0, minval=0.001, maxval=50)
  16. atr_maLen =input(7,title="maLength")
  17.  
  18. atr_highest = highest(3)
  19. atr_highest_src = security(tickerid, res, high, false)
  20. atr_lowest = highest(3)
  21. atr_lowest_src = security(tickerid, res, low, false)
  22.  
  23. atr_sma = sma(atr_src, atr_length)
  24. atr_basis = security(tickerid, res, atr_sma, false)
  25.  
  26. atr_atr = atr(atr_length)
  27. atr_dev = atr_mult * security(tickerid, res, atr_atr, false)
  28.  
  29. atr_upper = atr_basis + atr_dev
  30. atr_lower = atr_basis - atr_dev
  31. atr_bbr = (atr_src - atr_lower) / (atr_upper - atr_lower)
  32.  
  33. atr_bbe = ema(atr_bbr, atr_maLen)
  34. atr_up = atr_bbe[1]>atr_bbe and atr_bbe[2]<atr_bbe[1]?atr_bbe:na
  35. atr_bt = atr_bbe[1]<atr_bbe and atr_bbe[2]>atr_bbe[1]?atr_bbe:na
  36.  
  37. atr_topH = na(atr_up)==0 ? atr_highest_src : na
  38. atr_bottomL = na(atr_bt)==0 ? atr_lowest_src : na
  39.  
  40. atr_tf = fixnan(atr_topH)
  41. atr_bf = fixnan(atr_bottomL)
  42.  
  43. plot(atr_tf,color=red,style=circles,linewidth=2,offset=-1)
  44. plot(atr_bf,color=green,style=circles,linewidth=2,offset=-1)
  45.  
  46. //
  47. // ATR BB
  48. //
  49.  
  50. OverBought = input(70, minval=0)
  51. OverSold = input(20, maxval=100)
  52.  
  53. src = close
  54. Length = input(50)
  55.  
  56. normalize(series) =>
  57. h = highest(series, Length)
  58. l = lowest(series, Length)
  59. res = (series - l) / (h - l)
  60.  
  61. len = input(5, minval=1)
  62. ma2 = wma(src*volume, len) / wma(volume, len) // vwma
  63. // ma2 = wma(src, len) // no volumes
  64.  
  65. result1 = normalize(ma2)
  66.  
  67. z = 100 * result1 // *100 is it because of rsi ?
  68.  
  69. // https://www.tradingview.com/wiki/Context_Switching,_The_‘security’_Function
  70. // use this to avoid repaints
  71. resCustom = input("120", title="Time frame", options=["03", "15", "30", "45", "60","120","180","240","360","720","1440"]) // custom tfs
  72. value = security(tickerid, resCustom, z[1], barmerge.gaps_off, barmerge.lookahead_on)
  73.  
  74. length = input(title="Bollinger Length", type=integer, defval=20, minval=1)
  75. multiplier = input(title="Bollinger Deviation", type=float, defval=2, minval=1)
  76. overbought = input(title="Overbought", type=integer, defval=1, minval=1)
  77. oversold = input(title="Oversold", type=integer, defval=0, minval=1)
  78.  
  79. // inputs
  80. per = input(24, type=integer, minval=15, maxval=60)
  81.  
  82. //
  83. // donchian channel + fibo
  84. //
  85.  
  86. hb = highest(high, per) // High Border
  87. lb = lowest(low, per) // Low Border
  88. dist = hb - lb // range of the channel
  89. med = (hb + lb) / 2 // median of the channel
  90.  
  91. hf = hb - dist*0.236 // Highest Fib
  92. chf = hb - dist*0.382 // Center High Fib
  93. clf = hb - dist*0.618 // Center Low Fib
  94. lf = hb - dist*0.764 // Lowest Fib
  95.  
  96. //
  97. // pullback levels
  98. //
  99.  
  100. // entry markers
  101. tol = atr(per) * 0.2 // tolerance for placing triangles and prediction candles at borders
  102.  
  103. //initialise mutatables
  104. hftrue = true
  105. lftrue = true
  106. cftrue = true
  107.  
  108. evupin = crossover(close, hf) // market enters up trend
  109. evupout = crossunder(close, hf) // market leaves up trend
  110. hftrue := evupin ? true : evupout ? false : hftrue[1] // mutatable true if in up trend
  111. evdownin = crossunder(close, lf) // market enters down trend
  112. evdownout = crossover(close, lf) // market leaves down trend
  113. lftrue := evdownin ? true : evdownout ? false : lftrue[1] // mutatable true if in down trend
  114. cftrue := not hftrue and not lftrue ? true : false // mutatable true if instrument is ranging
  115.  
  116. // identify last bar
  117. last = barstate.islast
  118. last := barstate.isrealtime and isintraday ? not barstate.isconfirmed :barstate.islast
  119. // barstate.islast works also when all bars are confirmed and are history. However in real time and intradaycharts bars never lose this state.
  120. // In that situation 'not barstate.isconfirmed' (like not ishistory) is the better alternative. if you use intraday charts after closing time of
  121. // the markets "barstate.isrealtime and isintraday" will be false and the script will use barstate.islast.
  122.  
  123. // situation (plotting only)
  124. hbdtrue = last and hftrue // up trend
  125. lbdtrue = last and lftrue // down trend
  126.  
  127. //
  128. // Plottings
  129. //
  130.  
  131. // plot channel and fibzones
  132. phb = plot(hbdtrue ? na : hb, title="high border", color=yellow)
  133. plb = plot(lbdtrue ? na : lb, title="low border", color=yellow)
  134.  
  135. // plot entry markers
  136. plotshape(evupout ? hb + tol : na, style=shape.cross, location=location.absolute, color=red, size=size.tiny)
  137. plotshape(evdownout ? lb - tol : na, style=shape.cross, location=location.absolute, color=aqua, size=size.tiny)
  138.  
  139. //
  140. // slow and fast RSI
  141. //
  142.  
  143. RSIlength = input(14, title="RSI Period Length")
  144. RSIvaluehigh = input(55, title="RSI High")
  145. RSIvaluelow = input(40, title="RSI Low")
  146.  
  147. price = close
  148. vrsi = rsi(price, RSIlength)
  149. buy = vrsi < RSIvaluelow
  150. sell = vrsi > RSIvaluehigh
  151.  
  152. RSIlength1 = input(5, title="RSI Period Length1")
  153. RSIvaluehigh1 = input(55, title="RSI High")
  154. RSIvaluelow1 = input(40, title="RSI Low")
  155.  
  156. price1 = close
  157. vrsi1 = rsi(price1, RSIlength1)
  158. buy1 = vrsi1 < RSIvaluelow1
  159. sell1 = vrsi > RSIvaluehigh1 // not used
  160.  
  161. //
  162. // Bollinger Bands
  163. //
  164.  
  165. BBlength = input(20, minval=1,title="Bollinger Bands SMA Period Length")
  166. BBmult = input(2.0, minval=0.001, maxval=50,title="Bollinger Bands Standard Deviation")
  167. BBbasis = sma(price, BBlength)
  168. BBdev = BBmult * stdev(price, BBlength)
  169. BBupper = BBbasis + BBdev
  170. BBlower = BBbasis - BBdev
  171.  
  172. source = close
  173. buyEntry = crossover(source, BBlower)
  174. sellEntry = crossunder(source, BBupper)
  175.  
  176. up1 = value < OverSold and crossover(close, atr_bf) and evdownout ? lb-tol : na
  177. up = value < OverSold and buy and vrsi < vrsi1 and evdownout ? lb-tol : na
  178.  
  179. down2 = value > OverBought and crossunder(close, atr_tf) and evupout ? hb+tol : na
  180. down = value > OverBought and sell and vrsi > vrsi1 and evupout ? hb+tol : na
  181. down1 = value > OverBought and vrsi > vrsi1 and evupout ? hb+tol : na
  182.  
  183. // plotting bb
  184. plot(BBbasis, color=aqua,title="Bollinger Bands SMA Basis Line")
  185. p1 = plot(BBupper, color=silver,title="Bollinger Bands Upper Line")
  186. p2 = plot(BBlower, color=silver,title="Bollinger Bands Lower Line")
  187. fill(p1, p2) // channel
  188.  
  189. //
  190. // plottings signals and alerts
  191. //
  192.  
  193. plotshape(up, title="buy", style=shape.triangleup,location=location.belowbar, color=green, transp=0, size=size.small)
  194. plotshape(down, title="sell", style=shape.triangledown,location=location.abovebar, color=red, transp=0, size=size.small)
  195. plotshape(up1, title="buy1", style=shape.triangleup,location=location.belowbar, color=green, transp=0, size=size.tiny)
  196. plotshape(down1, title="down", style=shape.triangledown,location=location.abovebar, color=red, transp=0, size=size.tiny)
  197. plotshape(down2, title="down1", style=shape.triangledown,location=location.abovebar, color=orange, transp=0, size=size.tiny)
  198.  
  199. //
  200. // signals alerts
  201. //
  202.  
  203. alertcondition(up, title='buy', message='go long')
  204. alertcondition(up1, title='buy1', message='go long1')
  205. alertcondition(down, title='sell', message='go short')
  206. alertcondition(down1, title='down', message='go short1')
  207. alertcondition(down2, title='down1', message='go short2')
  208.  
  209. alertcondition(up or up1 or down or down1 or down2, title='signal', message='go')
  210. alertcondition(up or up1, title='longs', message='go longs')
  211. alertcondition(down or down1 or down2, title='shorts', message='go shorts')
  212.  
  213. alertcondition(up or up1 or down or down1 or down2 or evupout or evdownout, title='all', message='gogo')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement