Advertisement
xmd79

"Pullback BB-OB-MTF-BETA

Jan 7th, 2023
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.77 KB | None | 0 0
  1. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  2. // © LordKaan
  3.  
  4. //@version=5
  5.  
  6. indicator("Pullback BB-OB-MTF-BETA", overlay=true,max_bars_back=100)
  7.  
  8.  
  9. vTF0 = 'None'
  10. vTF1 = 'Discrete Steps (60min, 1D, 3D, 1W, 1M, 12M)'
  11. vTF2 = 'Multiple Of Current TF'
  12. vTF3 = 'Fixed TF'
  13.  
  14. Tiny = 'Tiny'
  15. Small = 'Small'
  16. Normal = 'Normal'
  17. Large = 'Large'
  18.  
  19. LE = true
  20. //Inputs
  21. length = 20
  22. src = close
  23. mult = 2
  24.  
  25. //OB In
  26. lookback = 100
  27.  
  28.  
  29. plotBands = false
  30. plotOb = true
  31. plotCon = true
  32.  
  33. //Calc Bands
  34. basis = ta.sma(src, length)
  35. dev = mult * ta.stdev(src, length)
  36. upper = basis + dev
  37. lower = basis - dev
  38. offset = 0
  39.  
  40. reachDev = (mult + 0.1) * ta.stdev(src, length)
  41. upper2 = basis + reachDev
  42. lower2 = basis - reachDev
  43.  
  44. //Order Blocks
  45. obHigh = ta.highest(upper, lookback)
  46. obLow = ta.lowest(lower, lookback)
  47.  
  48. obHigh2 = ta.highest(upper2, lookback)
  49. obLow2 = ta.lowest(lower2, lookback)
  50.  
  51. // size_ = input.string(Tiny, '  Label Size', options=[Tiny, Small, Normal, Large])
  52.  
  53.  
  54.  
  55. HTF_1 = true
  56. vHtfType = input.string(vTF3, '  Higher Timeframe Selection', options=[vTF0, vTF3])
  57. vHtfType2 = 2
  58. vHtfType3 = input.timeframe('60', '   Fixed TF')
  59. vHtfRepaints = true
  60. vHtfSmooth = true
  61. smLength = 2
  62. vOffsetLabels = 4
  63.  
  64. var vHtfOn = vHtfType != vTF0
  65.  
  66. // SIZE = size.tiny
  67. // if size_ == 'Tiny'
  68. // SIZE := size.tiny
  69. // SIZE
  70. // else
  71. // if size_ == 'Small'
  72. // SIZE := size.small
  73. // SIZE
  74. // else
  75. // if size_ == 'Normal'
  76. // SIZE := size.normal
  77. // SIZE
  78. // else
  79. // if size_ == 'Large'
  80. // SIZE := size.large
  81. // SIZE
  82.  
  83. // —————————— PineCoders MTF Selection Framework functions
  84. // ————— Converts current "timeframe.multiplier" plus the TF into minutes of type float.
  85. f_resInMinutes() =>
  86. _resInMinutes = timeframe.multiplier * (timeframe.isseconds ? 1. / 60. : timeframe.isminutes ? 1. : timeframe.isdaily ? 1440. : timeframe.isweekly ? 10080. : timeframe.ismonthly ? 43800. : na)
  87. _resInMinutes
  88.  
  89. // ————— Returns resolution of _resolution period in minutes.
  90. f_tfResInMinutes(_res) =>
  91. // _res: resolution of any TF (in "timeframe.period" string format).
  92. request.security(syminfo.tickerid, _res, f_resInMinutes())
  93.  
  94. // ————— Given current resolution, returns next step of HTF.
  95. f_resNextStep(_res) =>
  96. // _res: current TF in fractional minutes.
  97. _res <= 1 ? '60' : _res <= 60 ? '1D' : _res <= 360 ? '3D' : _res <= 1440 ? '1W' : _res <= 10080 ? '1M' : '12M'
  98.  
  99. // ————— Returns a multiple of current resolution as a string in "timeframe.period" format usable with "security()".
  100. f_multipleOfRes(_res, _mult) =>
  101. // _res: current resolution in minutes, in the fractional format supplied by f_resInMinutes() companion function.
  102. // _mult: Multiple of current TF to be calculated.
  103. // Convert current float TF in minutes to target string TF in "timeframe.period" format.
  104. _targetResInMin = _res * math.max(_mult, 1)
  105. // Find best string to express the resolution.
  106. _targetResInMin <= 0.083 ? '5S' : _targetResInMin <= 0.251 ? '15S' : _targetResInMin <= 0.501 ? '30S' : _targetResInMin <= 1440 ? str.tostring(math.round(_targetResInMin)) : _targetResInMin <= 43800 ? str.tostring(math.round(math.min(_targetResInMin / 1440, 365))) + 'D' : str.tostring(math.round(math.min(_targetResInMin / 43800, 12))) + 'M'
  107.  
  108. // // ————— Print a label at end of chart.
  109. // f_htfLabel(_txt, _y, _color, _offsetLabels) =>
  110. // _t = int(time + f_resInMinutes() * _offsetLabels * 60000)
  111. // // Create the label on the dataset's first bar.
  112. // var _lbl = label.new(_t, _y, _txt, xloc.bar_time, yloc.price, #00000000, label.style_none, color.gray, SIZE)
  113. // if barstate.islast
  114. // // Rather than delete and recreate the label on every realtime bar update,
  115. // // simply update the label's information; it's more efficient.
  116. // label.set_xy(_lbl, _t, _y)
  117. // label.set_text(_lbl, _txt)
  118. // label.set_textcolor(_lbl, _color)
  119.  
  120. // }
  121.  
  122. // ————— HTF calcs
  123. // Get current resolution in float minutes.
  124. var vResInMinutes = f_resInMinutes()
  125.  
  126. // Get HTF from user-defined mode.
  127. var vHtf = vHtfType == vTF1 ? f_resNextStep(vResInMinutes) : vHtfType == vTF2 ? f_multipleOfRes(vResInMinutes, vHtfType2) : vHtfType3
  128.  
  129. vHtfSmoothLen = math.max(2, smLength)
  130. BBHTF_1 = HTF_1 and not vHtfOn ? basis : vHtfRepaints ? request.security(syminfo.tickerid, vHtf, basis) : request.security(syminfo.tickerid, vHtf, basis[1], lookahead=barmerge.lookahead_on)
  131. BBHTF_1 := HTF_1 and vHtfOn and vHtfSmooth ? ta.ema(ta.ema(ta.ema(BBHTF_1, vHtfSmoothLen), vHtfSmoothLen), vHtfSmoothLen) : BBHTF_1
  132.  
  133. upperHTF_1 = HTF_1 and not vHtfOn ? upper : vHtfRepaints ? request.security(syminfo.tickerid, vHtf, upper) : request.security(syminfo.tickerid, vHtf, upper[1], lookahead=barmerge.lookahead_on)
  134. upperHTF_1 := HTF_1 and vHtfOn and vHtfSmooth ? ta.ema(ta.ema(ta.ema(upperHTF_1, vHtfSmoothLen), vHtfSmoothLen), vHtfSmoothLen) : upperHTF_1
  135.  
  136. lowerHTF_1 = HTF_1 and not vHtfOn ? lower : vHtfRepaints ? request.security(syminfo.tickerid, vHtf, lower) : request.security(syminfo.tickerid, vHtf, lower[1], lookahead=barmerge.lookahead_on)
  137. lowerHTF_1 := HTF_1 and vHtfOn and vHtfSmooth ? ta.ema(ta.ema(ta.ema(lowerHTF_1, vHtfSmoothLen), vHtfSmoothLen), vHtfSmoothLen) : lowerHTF_1
  138.  
  139. obHighHTF_1 = HTF_1 and not vHtfOn ? obHigh : vHtfRepaints ? request.security(syminfo.tickerid, vHtf, obHigh ) : request.security(syminfo.tickerid, vHtf, obHigh [1], lookahead=barmerge.lookahead_on)
  140. obHighHTF_1 := HTF_1 and vHtfOn and vHtfSmooth ? ta.ema(ta.ema(ta.ema(obHighHTF_1, vHtfSmoothLen), vHtfSmoothLen), vHtfSmoothLen) : obHighHTF_1
  141.  
  142. obLowHTF_1 = HTF_1 and not vHtfOn ? obLow : vHtfRepaints ? request.security(syminfo.tickerid, vHtf, obLow ) : request.security(syminfo.tickerid, vHtf, obLow [1], lookahead=barmerge.lookahead_on)
  143. obLowHTF_1 := HTF_1 and vHtfOn and vHtfSmooth ? ta.ema(ta.ema(ta.ema(obLowHTF_1, vHtfSmoothLen), vHtfSmoothLen), vHtfSmoothLen) : obLowHTF_1
  144.  
  145. obHigh2HTF_1 = HTF_1 and not vHtfOn ? obHigh : vHtfRepaints ? request.security(syminfo.tickerid, vHtf, obHigh2 ) : request.security(syminfo.tickerid, vHtf, obHigh2 [1], lookahead=barmerge.lookahead_on)
  146. obHigh2HTF_1 := HTF_1 and vHtfOn and vHtfSmooth ? ta.ema(ta.ema(ta.ema(obHigh2HTF_1, vHtfSmoothLen), vHtfSmoothLen), vHtfSmoothLen) : obHigh2HTF_1
  147.  
  148. obLow2HTF_1 = HTF_1 and not vHtfOn ? obLow2 : vHtfRepaints ? request.security(syminfo.tickerid, vHtf, obLow2 ) : request.security(syminfo.tickerid, vHtf, obLow2 [1], lookahead=barmerge.lookahead_on)
  149. obLow2HTF_1 := HTF_1 and vHtfOn and vHtfSmooth ? ta.ema(ta.ema(ta.ema(obLow2HTF_1, vHtfSmoothLen), vHtfSmoothLen), vHtfSmoothLen) : obLow2HTF_1
  150.  
  151.  
  152.  
  153. // }
  154.  
  155.  
  156. // ———————————————————— Plots
  157. // {
  158.  
  159.  
  160.  
  161. //Plots
  162. //Upper Order Block
  163. o1 = plot(plotOb ? plotCon ? obHighHTF_1==obHighHTF_1[1] ? obHighHTF_1 : na : obHighHTF_1 :na, title="Order Block High", color=#FF0000, style=plot.style_linebr)
  164. o2 = plot(plotOb ? plotCon ? obHighHTF_1==obHighHTF_1[1] ? obHigh2HTF_1 : na : obHigh2HTF_1 :na, title="Order Block High 2", color=#FF0000, style=plot.style_linebr)
  165.  
  166. fill(o1,o2, color=color.new(#FF0000,80), title="Sell Fill")
  167.  
  168. //Lower Order Block
  169. o3 = plot(plotOb ? plotCon ? obLowHTF_1==obLowHTF_1[1] ? obLowHTF_1 : na : obLowHTF_1 :na, title="Order Block Low", color=#00E600, style=plot.style_linebr)
  170. o4 = plot(plotOb ? plotCon ? obLowHTF_1==obLowHTF_1[1] ? obLow2HTF_1 : na : obLow2HTF_1 :na, title="Order Block Low 2", color=#00E600, style=plot.style_linebr)
  171.  
  172. fill(o3,o4, color=color.new(#00E600,80), title="Buy Fill")
  173.  
  174.  
  175. //Plot bands
  176. //plot(plotBands ? basis : na, "Basis", color=#FF6D00, offset = offset)
  177. //p1 = plot(plotBands ? upper : na, "Upper", color=#2962FF, offset = offset)
  178. //p2 = plot(plotBands ? lower : na, "Lower", color=#2962FF, offset = offset)
  179.  
  180.  
  181.  
  182. //Conditions
  183. upperob = plotOb ? plotCon ? obHighHTF_1==obHighHTF_1[1] ? obHighHTF_1 : na : obHighHTF_1 :na
  184. lowerob = plotOb ? plotCon ? obLowHTF_1==obLowHTF_1[1] ? obLowHTF_1 : na : obLowHTF_1 :na
  185. upperob2 = plotOb ? plotCon ? obHighHTF_1==obHighHTF_1[1] ? obHigh2HTF_1 : na : obHigh2HTF_1 :na
  186. lowerob2 = plotOb ? plotCon ? obLowHTF_1==obLowHTF_1[1] ? obLow2HTF_1 : na : obLow2HTF_1 :na
  187.  
  188.  
  189.  
  190. gobear = high > upperob and close < upperob
  191. gobull = low < lowerob and close > lowerob
  192.  
  193.  
  194. plotshape(gobear, text='Be', style=shape.labeldown, color=color.new(color.white, 100), textcolor=color.new(color.red, 0), location=location.abovebar)
  195. plotshape(gobull, text='Bu', style=shape.labelup, color=color.new(color.white, 100), textcolor=color.new(color.lime, 0), location=location.belowbar)
  196. alertcondition(gobear,title="Potential Bearish", message="Potential bearish ")
  197. alertcondition(gobull,title="Potential Bullish", message="Potential bullish ")
  198.  
  199. // // check any condition you needed to fire
  200. // checkForAlert()=>
  201. // alertMsg = ""
  202.  
  203. // // check for potential rejection from bullish OB
  204. // if gobull
  205. // alertMsg += str.format("Potential Bullish rejection for {0}!\n", syminfo.tickerid,alert.freq_once_per_bar_close)
  206.  
  207. // // ccheck for potential rejection from bearish OB
  208. // if gobear
  209. // alertMsg += str.format("Potential Bearish rejection for {0}!\n", syminfo.tickerid,alert.freq_once_per_bar_close)
  210.  
  211. // // check for odd
  212. // //if bar_index % 2 == 0
  213. // // alertMsg += str.format("bar index is odd for {0}!\n", syminfo.ticker)
  214.  
  215. // // any other checks needed
  216.  
  217. // alertMsg
  218.  
  219. // fireAlert(ticker, freq = alert.freq_once_per_bar_close)=>
  220. // msg = request.security(ticker, timeframe.period, checkForAlert())
  221. // if str.length(msg) > 0
  222. // alert(msg, freq)
  223.  
  224. // Symbol1 = input.symbol("", title="Alert Symbols", group = "Select Symbols you want an alert from (Once Per Bar ONLY) -- Only need to create 1 'Any alert () function call'")
  225. // Symbol2 = input.symbol("", title="Alert Symbols", group = "Select Symbols you want an alert from (Once Per Bar ONLY) -- Only need to create 1 'Any alert () function call'")
  226. // Symbol3 = input.symbol("", title="Alert Symbols", group = "Select Symbols you want an alert from (Once Per Bar ONLY) -- Only need to create 1 'Any alert () function call'")
  227. // Symbol4 = input.symbol("", title="Alert Symbols", group = "Select Symbols you want an alert from (Once Per Bar ONLY) -- Only need to create 1 'Any alert () function call'")
  228. // Symbol5 = input.symbol("", title="Alert Symbols", group = "Select Symbols you want an alert from (Once Per Bar ONLY) -- Only need to create 1 'Any alert () function call'")
  229. // Symbol6 = input.symbol("", title="Alert Symbols", group = "Select Symbols you want an alert from (Once Per Bar ONLY) -- Only need to create 1 'Any alert () function call'")
  230. // Symbol7 = input.symbol("", title="Alert Symbols", group = "Select Symbols you want an alert from (Once Per Bar ONLY) -- Only need to create 1 'Any alert () function call'")
  231. // Symbol8 = input.symbol("", title="Alert Symbols", group = "Select Symbols you want an alert from (Once Per Bar ONLY) -- Only need to create 1 'Any alert () function call'")
  232. // Symbol9 = input.symbol("", title="Alert Symbols", group = "Select Symbols you want an alert from (Once Per Bar ONLY) -- Only need to create 1 'Any alert () function call'")
  233. // Symbol10 = input.symbol("", title="Alert Symbols", group = "Select Symbols you want an alert from (Once Per Bar ONLY) -- Only need to create 1 'Any alert () function call'")
  234. // Symbol11 = input.symbol("", title="Alert Symbols", group = "Select Symbols you want an alert from (Once Per Bar ONLY) -- Only need to create 1 'Any alert () function call'")
  235. // Symbol12 = input.symbol("", title="Alert Symbols", group = "Select Symbols you want an alert from (Once Per Bar ONLY) -- Only need to create 1 'Any alert () function call'")
  236. // Symbol13 = input.symbol("", title="Alert Symbols", group = "Select Symbols you want an alert from (Once Per Bar ONLY) -- Only need to create 1 'Any alert () function call'")
  237. // Symbol14 = input.symbol("", title="Alert Symbols", group = "Select Symbols you want an alert from (Once Per Bar ONLY) -- Only need to create 1 'Any alert () function call'")
  238. // Symbol15 = input.symbol("", title="Alert Symbols", group = "Select Symbols you want an alert from (Once Per Bar ONLY) -- Only need to create 1 'Any alert () function call'")
  239.  
  240. // fireAlert(Symbol1)
  241. // fireAlert(Symbol2)
  242. // fireAlert(Symbol3)
  243. // fireAlert(Symbol4)
  244. // fireAlert(Symbol5)
  245. // fireAlert(Symbol6)
  246. // fireAlert(Symbol7)
  247. // fireAlert(Symbol8)
  248. // fireAlert(Symbol9)
  249. // fireAlert(Symbol10)
  250. // fireAlert(Symbol11)
  251. // fireAlert(Symbol12)
  252. // fireAlert(Symbol13)
  253. // fireAlert(Symbol14)
  254. // fireAlert(Symbol15)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement