Advertisement
PineCoders

BB HTF2

Feb 19th, 2020
1,278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.11 KB | None | 0 0
  1. //@version=4
  2. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  3. // © fikira
  4. // Credits to "PineCoders"; The amazing "Higher Time Frames" are from "PineCoders" (MTF Selection Framework functions)
  5. study(shorttitle="BB HTF", title="[fikira] Bollinger Bands + Higher Time Frames", overlay=true)
  6.  
  7. vTF0 = "None"
  8. vTF1 = "Discrete Steps (60min, 1D, 3D, 1W, 1M, 12M)"
  9. vTF2 = "Multiple Of Current TF"
  10. vTF3 = "Fixed TF"
  11.  
  12. Tiny = "Tiny"
  13. Small = "Small"
  14. Normal = "Normal"
  15. Large = "Large"
  16.  
  17. LE = input(true, "═════════ Bollinger Bands ══════════")
  18.  
  19. // BB
  20.  
  21. length = input(20, minval=1)
  22. src = input(close, "Source")
  23. basis = sma(src, length)
  24.  
  25. mult = input(2.0, "BB Bands StDev", minval=0.001, maxval=50)
  26. dev = mult * stdev(src, length)
  27. upper = basis + dev
  28. lower = basis - dev
  29.  
  30. val = input(false, "Labels + Values ?")
  31. size_ = input(Tiny, "  Label Size", options=[Tiny, Small, Normal, Large])
  32.  
  33. HTF_1 = input(true, "═════════ HTF Selection 1 ══════════")
  34. vHtfType = input(vTF2, "  Higher Timeframe Selection", options=[vTF0, vTF1, vTF2, vTF3])
  35. vHtfType2 = input(2., "   Multiple of Current TF", minval = 1)
  36. vHtfType3 = input("240", "   Fixed TF", type = input.resolution)
  37. vHtfRepaints = input(false, "Repainting HTF")
  38. vHtfSmooth = input(true, "Smooth Higher Timeframe 1")
  39. smLength = input(2, "  Smoothing Length", minval = 1, maxval = 3)
  40. vOffsetLabels = input(4, "  Higher TF 1 Labels Offset")
  41.  
  42. var vHtfOn = vHtfType != vTF0
  43.  
  44. HTF_2 = input(false, "═════════ HTF Selection 2 ══════════")
  45. vHtfType_2 = input(vTF2, "  Higher Timeframe Selection", options=[vTF0, vTF1, vTF2, vTF3])
  46. vHtfType2_2 = input(4., "   Multiple of Current TF", minval = 1)
  47. vHtfType3_2 = input("D", "   Fixed TF", type = input.resolution)
  48. vHtfRepaints_2 = input(false, "Repainting HTF")
  49. vHtfSmooth_2 = input(true, "Smooth Higher Timeframe 2")
  50. smLength_2 = input(2, "  Smoothing Length", minval = 1, maxval = 3)
  51. vOffsetLabels_2 = input(8, "  Higher TF 2 Labels Offset")
  52.  
  53. var vHtfOn_2 = vHtfType_2 != vTF0
  54.  
  55. HTF_3 = input(false, "═════════ HTF Selection 3 ══════════")
  56. vHtfType_3 = input(vTF2, "  Higher Timeframe Selection", options=[vTF0, vTF1, vTF2, vTF3])
  57. vHtfType2_3 = input(7., "   Multiple of Current TF", minval = 1)
  58. vHtfType3_3 = input("W", "   Fixed TF", type = input.resolution)
  59. vHtfRepaints_3 = input(false, "Repainting HTF")
  60. vHtfSmooth_3 = input(true, "Smooth Higher Timeframe 3")
  61. smLength_3 = input(3, "  Smoothing Length", minval = 1, maxval = 3)
  62. vOffsetLabels_3 = input(12, "  Higher TF 3 Labels Offset")
  63.  
  64. var vHtfOn_3 = vHtfType_3 != vTF0
  65.  
  66. HTF_4 = input(false, "═════════ HTF Selection 4 ══════════")
  67. vHtfType_4 = input(vTF2, "  Higher Timeframe Selection", options=[vTF0, vTF1, vTF2, vTF3])
  68. vHtfType2_4 = input(10., "   Multiple of Current TF", minval = 1)
  69. vHtfType3_4 = input("M", "   Fixed TF", type = input.resolution)
  70. vHtfRepaints_4 = input(false, "Repainting HTF")
  71. vHtfSmooth_4 = input(true, "Smooth Higher Timeframe 4")
  72. smLength_4 = input(3, "  Smoothing Length", minval = 1, maxval = 3)
  73. vOffsetLabels_4 = input(16, "  Higher TF 4 Labels Offset")
  74.  
  75. var vHtfOn_4 = vHtfType_4 != vTF0
  76.  
  77.  
  78. SIZE = size.tiny
  79. if size_ == "Tiny"
  80. SIZE := size.tiny
  81. else
  82. if size_ == "Small"
  83. SIZE := size.small
  84. else
  85. if size_ == "Normal"
  86. SIZE := size.normal
  87. else
  88. if size_ == "Large"
  89. SIZE := size.large
  90.  
  91. // —————————— PineCoders MTF Selection Framework functions
  92. // ————— Converts current "timeframe.multiplier" plus the TF into minutes of type float.
  93. f_resInMinutes() =>
  94. _resInMinutes = timeframe.multiplier * (
  95. timeframe.isseconds ? 1. / 60. :
  96. timeframe.isminutes ? 1. :
  97. timeframe.isdaily ? 1440. :
  98. timeframe.isweekly ? 10080. :
  99. timeframe.ismonthly ? 43800. : na)
  100.  
  101. // ————— Returns resolution of _resolution period in minutes.
  102. f_tfResInMinutes(_res) =>
  103. // _res: resolution of any TF (in "timeframe.period" string format).
  104. security(syminfo.tickerid, _res, f_resInMinutes())
  105.  
  106. // ————— Given current resolution, returns next step of HTF.
  107. f_resNextStep(_res) =>
  108. // _res: current TF in fractional minutes.
  109. _res <= 1 ? "60" :
  110. _res <= 60 ? "1D" :
  111. _res <= 360 ? "3D" :
  112. _res <= 1440 ? "1W" :
  113. _res <= 10080 ? "1M" : "12M"
  114.  
  115. // ————— Returns a multiple of current resolution as a string in "timeframe.period" format usable with "security()".
  116. f_multipleOfRes(_res, _mult) =>
  117. // _res: current resolution in minutes, in the fractional format supplied by f_resInMinutes() companion function.
  118. // _mult: Multiple of current TF to be calculated.
  119. // Convert current float TF in minutes to target string TF in "timeframe.period" format.
  120. _targetResInMin = _res * max(_mult, 1)
  121. // Find best string to express the resolution.
  122. _targetResInMin <= 0.083 ? "5S" :
  123. _targetResInMin <= 0.251 ? "15S" :
  124. _targetResInMin <= 0.501 ? "30S" :
  125. _targetResInMin <= 1440 ? tostring(round(_targetResInMin)) :
  126. _targetResInMin <= 43800 ? tostring(round(min(_targetResInMin / 1440, 365))) + "D" :
  127. tostring(round(min(_targetResInMin / 43800, 12))) + "M"
  128.  
  129. // ————— Print a label at end of chart.
  130. f_htfLabel(_txt, _y, _color, _offsetLabels) =>
  131. _t = int(time + (f_resInMinutes() * _offsetLabels * 60000))
  132. // Create the label on the dataset's first bar.
  133. var _lbl = label.new(_t, _y, _txt, xloc.bar_time, yloc.price, #00000000, label.style_none, color.gray, SIZE)
  134. if barstate.islast
  135. // Rather than delete and recreate the label on every realtime bar update,
  136. // simply update the label's information; it's more efficient.
  137. label.set_xy(_lbl, _t, _y)
  138. label.set_text(_lbl, _txt)
  139. label.set_textcolor(_lbl, _color)
  140.  
  141. // }
  142.  
  143. // ———————————————————— Calcs
  144. // {
  145. // ————— HTF calcs
  146. // Get current resolution in float minutes.
  147. var vResInMinutes = f_resInMinutes()
  148.  
  149. // Get HTF from user-defined mode.
  150. var vHtf = vHtfType == vTF1 ? f_resNextStep (vResInMinutes) : vHtfType == vTF2 ?
  151. f_multipleOfRes(vResInMinutes , vHtfType2 ) : vHtfType3
  152.  
  153. var vHtf_2 = vHtfType_2 == vTF1 ? f_resNextStep(vResInMinutes) : vHtfType_2 == vTF2 ?
  154. f_multipleOfRes(vResInMinutes, vHtfType2_2) : vHtfType3_2
  155.  
  156. var vHtf_3 = vHtfType_3 == vTF1 ? f_resNextStep(vResInMinutes) : vHtfType_3 == vTF2 ?
  157. f_multipleOfRes(vResInMinutes, vHtfType2_3) : vHtfType3_3
  158.  
  159. var vHtf_4 = vHtfType_4 == vTF1 ? f_resNextStep(vResInMinutes) : vHtfType_4 == vTF2 ?
  160. f_multipleOfRes(vResInMinutes, vHtfType2_4) : vHtfType3_4
  161.  
  162. vHtfSmoothLen = max(2, smLength )
  163. vHtfSmoothLen_2 = max(2, smLength_2 )
  164. vHtfSmoothLen_3 = max(2, smLength_3 )
  165. vHtfSmoothLen_4 = max(2, smLength_4 )
  166.  
  167.  
  168. BBHTF_1 = HTF_1 and not vHtfOn ? basis : vHtfRepaints ? security(syminfo.tickerid, vHtf, basis) :
  169. security(syminfo.tickerid, vHtf, basis[1], lookahead = barmerge.lookahead_on)
  170. BBHTF_1 := HTF_1 and vHtfOn and vHtfSmooth ? ema(ema(ema(BBHTF_1, vHtfSmoothLen), vHtfSmoothLen), vHtfSmoothLen) : BBHTF_1
  171.  
  172. BBHTF_2 = HTF_2 and not vHtfOn_2 ? basis : vHtfRepaints_2 ? security(syminfo.tickerid, vHtf_2, basis) :
  173. security(syminfo.tickerid, vHtf_2, basis[1], lookahead = barmerge.lookahead_on)
  174. BBHTF_2 := HTF_2 and vHtfOn_2 and vHtfSmooth_2 ? ema(ema(ema(BBHTF_2, vHtfSmoothLen_2), vHtfSmoothLen_2), vHtfSmoothLen_2) : BBHTF_2
  175.  
  176. BBHTF_3 = HTF_3 and not vHtfOn_3 ? basis : vHtfRepaints_3 ? security(syminfo.tickerid, vHtf_3, basis) :
  177. security(syminfo.tickerid, vHtf_3, basis[1], lookahead = barmerge.lookahead_on)
  178. BBHTF_3 := HTF_3 and vHtfOn_3 and vHtfSmooth_3 ? ema(ema(ema(BBHTF_3, vHtfSmoothLen_3), vHtfSmoothLen_3), vHtfSmoothLen_3) : BBHTF_3
  179.  
  180. BBHTF_4 = HTF_4 and not vHtfOn_4 ? basis : vHtfRepaints_4 ? security(syminfo.tickerid, vHtf_4, basis) :
  181. security(syminfo.tickerid, vHtf_4, basis[1], lookahead = barmerge.lookahead_on)
  182. BBHTF_4 := HTF_4 and vHtfOn_4 and vHtfSmooth_4 ? ema(ema(ema(BBHTF_4, vHtfSmoothLen_4), vHtfSmoothLen_4), vHtfSmoothLen_4) : BBHTF_4
  183.  
  184.  
  185. upperHTF_1 = HTF_1 and not vHtfOn ? upper : vHtfRepaints ? security(syminfo.tickerid, vHtf, upper) :
  186. security(syminfo.tickerid, vHtf, upper[1], lookahead = barmerge.lookahead_on)
  187. upperHTF_1 := HTF_1 and vHtfOn and vHtfSmooth ? ema(ema(ema(upperHTF_1, vHtfSmoothLen), vHtfSmoothLen), vHtfSmoothLen) : upperHTF_1
  188.  
  189. upperHTF_2 = HTF_2 and not vHtfOn_2 ? upper : vHtfRepaints_2 ? security(syminfo.tickerid, vHtf_2, upper) :
  190. security(syminfo.tickerid, vHtf_2, upper[1], lookahead = barmerge.lookahead_on)
  191. upperHTF_2 := HTF_2 and vHtfOn_2 and vHtfSmooth_2 ? ema(ema(ema(upperHTF_2 , vHtfSmoothLen_2), vHtfSmoothLen_2), vHtfSmoothLen_2) : upperHTF_2
  192.  
  193. upperHTF_3 = HTF_3 and not vHtfOn_3 ? upper : vHtfRepaints_3 ? security(syminfo.tickerid, vHtf_3, upper) :
  194. security(syminfo.tickerid, vHtf_3, upper[1], lookahead = barmerge.lookahead_on)
  195. upperHTF_3 := HTF_3 and vHtfOn_3 and vHtfSmooth_3 ? ema(ema(ema(upperHTF_3, vHtfSmoothLen_3), vHtfSmoothLen_3), vHtfSmoothLen_3) : upperHTF_3
  196.  
  197. upperHTF_4 = HTF_4 and not vHtfOn_4 ? upper : vHtfRepaints_4 ? security(syminfo.tickerid, vHtf_4, upper) :
  198. security(syminfo.tickerid, vHtf_4, upper[1], lookahead = barmerge.lookahead_on)
  199. upperHTF_4 := HTF_4 and vHtfOn_4 and vHtfSmooth_4 ? ema(ema(ema(upperHTF_4, vHtfSmoothLen_4), vHtfSmoothLen_4), vHtfSmoothLen_4) : upperHTF_4
  200.  
  201.  
  202. lowerHTF_1 = HTF_1 and not vHtfOn ? lower : vHtfRepaints ? security(syminfo.tickerid, vHtf, lower) :
  203. security(syminfo.tickerid, vHtf, lower[1], lookahead = barmerge.lookahead_on)
  204. lowerHTF_1 := HTF_1 and vHtfOn and vHtfSmooth ? ema(ema(ema(lowerHTF_1, vHtfSmoothLen), vHtfSmoothLen), vHtfSmoothLen) : lowerHTF_1
  205.  
  206. lowerHTF_2 = HTF_2 and not vHtfOn_2 ? lower : vHtfRepaints_2 ? security(syminfo.tickerid, vHtf_2, lower) :
  207. security(syminfo.tickerid, vHtf_2, lower[1], lookahead = barmerge.lookahead_on)
  208. lowerHTF_2 := HTF_2 and vHtfOn_2 and vHtfSmooth_2 ? ema(ema(ema(lowerHTF_2 , vHtfSmoothLen_2), vHtfSmoothLen_2), vHtfSmoothLen_2) : lowerHTF_2
  209.  
  210. lowerHTF_3 = HTF_3 and not vHtfOn_3 ? lower : vHtfRepaints_3 ? security(syminfo.tickerid, vHtf_3, lower) :
  211. security(syminfo.tickerid, vHtf_3, lower[1], lookahead = barmerge.lookahead_on)
  212. lowerHTF_3 := HTF_3 and vHtfOn_3 and vHtfSmooth_3 ? ema(ema(ema(lowerHTF_3, vHtfSmoothLen_3), vHtfSmoothLen_3), vHtfSmoothLen_3) : lowerHTF_3
  213.  
  214. lowerHTF_4 = HTF_4 and not vHtfOn_4 ? lower : vHtfRepaints_4 ? security(syminfo.tickerid, vHtf_4, lower) :
  215. security(syminfo.tickerid, vHtf_4, lower[1], lookahead = barmerge.lookahead_on)
  216. lowerHTF_4 := HTF_4 and vHtfOn_4 and vHtfSmooth_4 ? ema(ema(ema(lowerHTF_4, vHtfSmoothLen_4), vHtfSmoothLen_4), vHtfSmoothLen_4) : lowerHTF_4
  217.  
  218. // }
  219.  
  220. // ———————————————————— Plots
  221. // {
  222. pbasis = plot(LE ? basis : na, "BB" , color.yellow, linewidth=2)
  223. pbasis_1 = plot(HTF_1 ? BBHTF_1 : na, "BB HTF 1", color.red , linewidth=2)
  224. pbasis_2 = plot(HTF_2 ? BBHTF_2 : na, "BB HTF 2", color.lime , linewidth=2)
  225. pbasis_3 = plot(HTF_3 ? BBHTF_3 : na, "BB HTF 3", color.blue , linewidth=2)
  226. pbasis_4 = plot(HTF_4 ? BBHTF_4 : na, "BB HTF 4", color.purple, linewidth=2)
  227.  
  228. pupper = plot(LE ? upper : na, "Upper" , color.yellow, linewidth=1, transp=50)
  229. pupper_1 = plot(HTF_1 ? upperHTF_1 : na, "Upper HTF 1", color.red , linewidth=1, transp=50)
  230. pupper_2 = plot(HTF_2 ? upperHTF_2 : na, "Upper HTF 2", color.lime , linewidth=1, transp=50)
  231. pupper_3 = plot(HTF_3 ? upperHTF_3 : na, "Upper HTF 3", color.blue , linewidth=1, transp=50)
  232. pupper_4 = plot(HTF_4 ? upperHTF_4 : na, "Upper HTF 4", color.purple, linewidth=1, transp=50)
  233.  
  234. plower = plot(LE ? lower : na, "Lower" , color.yellow, linewidth=1, transp=50)
  235. plower_1 = plot(HTF_1 ? lowerHTF_1 : na, "Lower HTF 1", color.red , linewidth=1, transp=50)
  236. plower_2 = plot(HTF_2 ? lowerHTF_2 : na, "Lower HTF 2", color.lime , linewidth=1, transp=50)
  237. plower_3 = plot(HTF_3 ? lowerHTF_3 : na, "Lower HTF 3", color.blue , linewidth=1, transp=50)
  238. plower_4 = plot(HTF_4 ? lowerHTF_4 : na, "Lower HTF 4", color.purple, linewidth=1, transp=50)
  239.  
  240. fill(pupper , plower , color=color.yellow, transp=97)
  241. fill(pupper_1, plower_1, color=color.red , transp=97)
  242. fill(pupper_2, plower_2, color=color.lime , transp=97)
  243. fill(pupper_3, plower_3, color=color.blue , transp=97)
  244. fill(pupper_4, plower_4, color=color.purple, transp=97)
  245.  
  246. t_BB_1 = valuewhen(basis, basis, 0)
  247. t_BBhtf_1 = valuewhen(BBHTF_1, BBHTF_1, 0)
  248. t_BBhtf_2 = valuewhen(BBHTF_2, BBHTF_2, 0)
  249. t_BBhtf_3 = valuewhen(BBHTF_3, BBHTF_3, 0)
  250. t_BBhtf_4 = valuewhen(BBHTF_4, BBHTF_4, 0)
  251.  
  252. if LE
  253. f_htfLabel(not val ? "BB " + tostring(length) : val ? "BB " + tostring(length) + "\n" + tostring(t_BB_1) : na,
  254. basis, color.yellow, 1)
  255.  
  256. if HTF_1
  257. f_htfLabel(
  258. vHtfType == vTF0 and not val ? "BB " + tostring(length) :
  259. vHtfType == vTF0 and val ? "BB " + tostring(length) + "\n" + tostring(t_BBhtf_1) :
  260. vHtfType != vTF0 and not val ? "BB " + tostring(length) + "(TF " + vHtf + ")" :
  261. vHtfType != vTF0 and val ? "BB " + tostring(length) + "(TF " + vHtf + ")" + "\n" + tostring(t_BBhtf_1) : na,
  262. vHtfType == vTF0 ? basis : BBHTF_1, color.red, vOffsetLabels * 1)
  263.  
  264. if HTF_2
  265. f_htfLabel(
  266. vHtfType_2 == vTF0 and not val ? "BB " + tostring(length) :
  267. vHtfType_2 == vTF0 and val ? "BB " + tostring(length) + "\n" + tostring(t_BBhtf_2) :
  268. vHtfType_2 != vTF0 and not val ? "BB " + tostring(length) + "(TF " + vHtf_2 + ")" :
  269. vHtfType_2 != vTF0 and val ? "BB " + tostring(length) + "(TF " + vHtf_2 + ")" + "\n" + tostring(t_BBhtf_2) : na,
  270. vHtfType_2 == vTF0 ? basis : BBHTF_2, color.lime, vOffsetLabels_2 * 1)
  271.  
  272. if HTF_3
  273. f_htfLabel(
  274. vHtfType_3 == vTF0 and not val ? "BB " + tostring(length) :
  275. vHtfType_3 == vTF0 and val ? "BB " + tostring(length) + "\n" + tostring(t_BBhtf_3) :
  276. vHtfType_3 != vTF0 and not val ? "BB " + tostring(length) + "(TF " + vHtf_3 + ")" :
  277. vHtfType_3 != vTF0 and val ? "BB " + tostring(length) + "(TF " + vHtf_3 + ")" + "\n" + tostring(t_BBhtf_3) : na,
  278. vHtfType_3 == vTF0 ? basis : BBHTF_3, color.blue, vOffsetLabels_3 * 1)
  279.  
  280. if HTF_4
  281. f_htfLabel(
  282. vHtfType_4 == vTF0 and not val ? "BB " + tostring(length) :
  283. vHtfType_4 == vTF0 and val ? "BB " + tostring(length) + "\n" + tostring(t_BBhtf_4) :
  284. vHtfType_4 != vTF0 and not val ? "BB " + tostring(length) + "(TF " + vHtf_4 + ")" :
  285. vHtfType_4 != vTF0 and val ? "BB " + tostring(length) + "(TF " + vHtf_4 + ")" + "\n" + tostring(t_BBhtf_4) : na,
  286. vHtfType_4 == vTF0 ? basis : BBHTF_4, color.purple, vOffsetLabels_4 * 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement