Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=4
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © fikira
- // Credits to "PineCoders"; The amazing "Higher Time Frames" are from "PineCoders" (MTF Selection Framework functions)
- study(shorttitle="BB HTF", title="[fikira] Bollinger Bands + Higher Time Frames", overlay=true)
- vTF0 = "None"
- vTF1 = "Discrete Steps (60min, 1D, 3D, 1W, 1M, 12M)"
- vTF2 = "Multiple Of Current TF"
- vTF3 = "Fixed TF"
- Tiny = "Tiny"
- Small = "Small"
- Normal = "Normal"
- Large = "Large"
- LE = input(true, "═════════ Bollinger Bands ══════════")
- // BB
- length = input(20, minval=1)
- src = input(close, "Source")
- basis = sma(src, length)
- mult = input(2.0, "BB Bands StDev", minval=0.001, maxval=50)
- dev = mult * stdev(src, length)
- upper = basis + dev
- lower = basis - dev
- val = input(false, "Labels + Values ?")
- size_ = input(Tiny, " Label Size", options=[Tiny, Small, Normal, Large])
- HTF_1 = input(true, "═════════ HTF Selection 1 ══════════")
- vHtfType = input(vTF2, " Higher Timeframe Selection", options=[vTF0, vTF1, vTF2, vTF3])
- vHtfType2 = input(2., " Multiple of Current TF", minval = 1)
- vHtfType3 = input("240", " Fixed TF", type = input.resolution)
- vHtfRepaints = input(false, "Repainting HTF")
- vHtfSmooth = input(true, "Smooth Higher Timeframe 1")
- smLength = input(2, " Smoothing Length", minval = 1, maxval = 3)
- vOffsetLabels = input(4, " Higher TF 1 Labels Offset")
- var vHtfOn = vHtfType != vTF0
- HTF_2 = input(false, "═════════ HTF Selection 2 ══════════")
- vHtfType_2 = input(vTF2, " Higher Timeframe Selection", options=[vTF0, vTF1, vTF2, vTF3])
- vHtfType2_2 = input(4., " Multiple of Current TF", minval = 1)
- vHtfType3_2 = input("D", " Fixed TF", type = input.resolution)
- vHtfRepaints_2 = input(false, "Repainting HTF")
- vHtfSmooth_2 = input(true, "Smooth Higher Timeframe 2")
- smLength_2 = input(2, " Smoothing Length", minval = 1, maxval = 3)
- vOffsetLabels_2 = input(8, " Higher TF 2 Labels Offset")
- var vHtfOn_2 = vHtfType_2 != vTF0
- HTF_3 = input(false, "═════════ HTF Selection 3 ══════════")
- vHtfType_3 = input(vTF2, " Higher Timeframe Selection", options=[vTF0, vTF1, vTF2, vTF3])
- vHtfType2_3 = input(7., " Multiple of Current TF", minval = 1)
- vHtfType3_3 = input("W", " Fixed TF", type = input.resolution)
- vHtfRepaints_3 = input(false, "Repainting HTF")
- vHtfSmooth_3 = input(true, "Smooth Higher Timeframe 3")
- smLength_3 = input(3, " Smoothing Length", minval = 1, maxval = 3)
- vOffsetLabels_3 = input(12, " Higher TF 3 Labels Offset")
- var vHtfOn_3 = vHtfType_3 != vTF0
- HTF_4 = input(false, "═════════ HTF Selection 4 ══════════")
- vHtfType_4 = input(vTF2, " Higher Timeframe Selection", options=[vTF0, vTF1, vTF2, vTF3])
- vHtfType2_4 = input(10., " Multiple of Current TF", minval = 1)
- vHtfType3_4 = input("M", " Fixed TF", type = input.resolution)
- vHtfRepaints_4 = input(false, "Repainting HTF")
- vHtfSmooth_4 = input(true, "Smooth Higher Timeframe 4")
- smLength_4 = input(3, " Smoothing Length", minval = 1, maxval = 3)
- vOffsetLabels_4 = input(16, " Higher TF 4 Labels Offset")
- var vHtfOn_4 = vHtfType_4 != vTF0
- SIZE = size.tiny
- if size_ == "Tiny"
- SIZE := size.tiny
- else
- if size_ == "Small"
- SIZE := size.small
- else
- if size_ == "Normal"
- SIZE := size.normal
- else
- if size_ == "Large"
- SIZE := size.large
- // —————————— PineCoders MTF Selection Framework functions
- // ————— Converts current "timeframe.multiplier" plus the TF into minutes of type float.
- f_resInMinutes() =>
- _resInMinutes = timeframe.multiplier * (
- timeframe.isseconds ? 1. / 60. :
- timeframe.isminutes ? 1. :
- timeframe.isdaily ? 1440. :
- timeframe.isweekly ? 10080. :
- timeframe.ismonthly ? 43800. : na)
- // ————— Returns resolution of _resolution period in minutes.
- f_tfResInMinutes(_res) =>
- // _res: resolution of any TF (in "timeframe.period" string format).
- security(syminfo.tickerid, _res, f_resInMinutes())
- // ————— Given current resolution, returns next step of HTF.
- f_resNextStep(_res) =>
- // _res: current TF in fractional minutes.
- _res <= 1 ? "60" :
- _res <= 60 ? "1D" :
- _res <= 360 ? "3D" :
- _res <= 1440 ? "1W" :
- _res <= 10080 ? "1M" : "12M"
- // ————— Returns a multiple of current resolution as a string in "timeframe.period" format usable with "security()".
- f_multipleOfRes(_res, _mult) =>
- // _res: current resolution in minutes, in the fractional format supplied by f_resInMinutes() companion function.
- // _mult: Multiple of current TF to be calculated.
- // Convert current float TF in minutes to target string TF in "timeframe.period" format.
- _targetResInMin = _res * max(_mult, 1)
- // Find best string to express the resolution.
- _targetResInMin <= 0.083 ? "5S" :
- _targetResInMin <= 0.251 ? "15S" :
- _targetResInMin <= 0.501 ? "30S" :
- _targetResInMin <= 1440 ? tostring(round(_targetResInMin)) :
- _targetResInMin <= 43800 ? tostring(round(min(_targetResInMin / 1440, 365))) + "D" :
- tostring(round(min(_targetResInMin / 43800, 12))) + "M"
- // ————— Print a label at end of chart.
- f_htfLabel(_txt, _y, _color, _offsetLabels) =>
- _t = int(time + (f_resInMinutes() * _offsetLabels * 60000))
- // Create the label on the dataset's first bar.
- var _lbl = label.new(_t, _y, _txt, xloc.bar_time, yloc.price, #00000000, label.style_none, color.gray, SIZE)
- if barstate.islast
- // Rather than delete and recreate the label on every realtime bar update,
- // simply update the label's information; it's more efficient.
- label.set_xy(_lbl, _t, _y)
- label.set_text(_lbl, _txt)
- label.set_textcolor(_lbl, _color)
- // }
- // ———————————————————— Calcs
- // {
- // ————— HTF calcs
- // Get current resolution in float minutes.
- var vResInMinutes = f_resInMinutes()
- // Get HTF from user-defined mode.
- var vHtf = vHtfType == vTF1 ? f_resNextStep (vResInMinutes) : vHtfType == vTF2 ?
- f_multipleOfRes(vResInMinutes , vHtfType2 ) : vHtfType3
- var vHtf_2 = vHtfType_2 == vTF1 ? f_resNextStep(vResInMinutes) : vHtfType_2 == vTF2 ?
- f_multipleOfRes(vResInMinutes, vHtfType2_2) : vHtfType3_2
- var vHtf_3 = vHtfType_3 == vTF1 ? f_resNextStep(vResInMinutes) : vHtfType_3 == vTF2 ?
- f_multipleOfRes(vResInMinutes, vHtfType2_3) : vHtfType3_3
- var vHtf_4 = vHtfType_4 == vTF1 ? f_resNextStep(vResInMinutes) : vHtfType_4 == vTF2 ?
- f_multipleOfRes(vResInMinutes, vHtfType2_4) : vHtfType3_4
- vHtfSmoothLen = max(2, smLength )
- vHtfSmoothLen_2 = max(2, smLength_2 )
- vHtfSmoothLen_3 = max(2, smLength_3 )
- vHtfSmoothLen_4 = max(2, smLength_4 )
- BBHTF_1 = HTF_1 and not vHtfOn ? basis : vHtfRepaints ? security(syminfo.tickerid, vHtf, basis) :
- security(syminfo.tickerid, vHtf, basis[1], lookahead = barmerge.lookahead_on)
- BBHTF_1 := HTF_1 and vHtfOn and vHtfSmooth ? ema(ema(ema(BBHTF_1, vHtfSmoothLen), vHtfSmoothLen), vHtfSmoothLen) : BBHTF_1
- BBHTF_2 = HTF_2 and not vHtfOn_2 ? basis : vHtfRepaints_2 ? security(syminfo.tickerid, vHtf_2, basis) :
- security(syminfo.tickerid, vHtf_2, basis[1], lookahead = barmerge.lookahead_on)
- BBHTF_2 := HTF_2 and vHtfOn_2 and vHtfSmooth_2 ? ema(ema(ema(BBHTF_2, vHtfSmoothLen_2), vHtfSmoothLen_2), vHtfSmoothLen_2) : BBHTF_2
- BBHTF_3 = HTF_3 and not vHtfOn_3 ? basis : vHtfRepaints_3 ? security(syminfo.tickerid, vHtf_3, basis) :
- security(syminfo.tickerid, vHtf_3, basis[1], lookahead = barmerge.lookahead_on)
- BBHTF_3 := HTF_3 and vHtfOn_3 and vHtfSmooth_3 ? ema(ema(ema(BBHTF_3, vHtfSmoothLen_3), vHtfSmoothLen_3), vHtfSmoothLen_3) : BBHTF_3
- BBHTF_4 = HTF_4 and not vHtfOn_4 ? basis : vHtfRepaints_4 ? security(syminfo.tickerid, vHtf_4, basis) :
- security(syminfo.tickerid, vHtf_4, basis[1], lookahead = barmerge.lookahead_on)
- BBHTF_4 := HTF_4 and vHtfOn_4 and vHtfSmooth_4 ? ema(ema(ema(BBHTF_4, vHtfSmoothLen_4), vHtfSmoothLen_4), vHtfSmoothLen_4) : BBHTF_4
- upperHTF_1 = HTF_1 and not vHtfOn ? upper : vHtfRepaints ? security(syminfo.tickerid, vHtf, upper) :
- security(syminfo.tickerid, vHtf, upper[1], lookahead = barmerge.lookahead_on)
- upperHTF_1 := HTF_1 and vHtfOn and vHtfSmooth ? ema(ema(ema(upperHTF_1, vHtfSmoothLen), vHtfSmoothLen), vHtfSmoothLen) : upperHTF_1
- upperHTF_2 = HTF_2 and not vHtfOn_2 ? upper : vHtfRepaints_2 ? security(syminfo.tickerid, vHtf_2, upper) :
- security(syminfo.tickerid, vHtf_2, upper[1], lookahead = barmerge.lookahead_on)
- upperHTF_2 := HTF_2 and vHtfOn_2 and vHtfSmooth_2 ? ema(ema(ema(upperHTF_2 , vHtfSmoothLen_2), vHtfSmoothLen_2), vHtfSmoothLen_2) : upperHTF_2
- upperHTF_3 = HTF_3 and not vHtfOn_3 ? upper : vHtfRepaints_3 ? security(syminfo.tickerid, vHtf_3, upper) :
- security(syminfo.tickerid, vHtf_3, upper[1], lookahead = barmerge.lookahead_on)
- upperHTF_3 := HTF_3 and vHtfOn_3 and vHtfSmooth_3 ? ema(ema(ema(upperHTF_3, vHtfSmoothLen_3), vHtfSmoothLen_3), vHtfSmoothLen_3) : upperHTF_3
- upperHTF_4 = HTF_4 and not vHtfOn_4 ? upper : vHtfRepaints_4 ? security(syminfo.tickerid, vHtf_4, upper) :
- security(syminfo.tickerid, vHtf_4, upper[1], lookahead = barmerge.lookahead_on)
- upperHTF_4 := HTF_4 and vHtfOn_4 and vHtfSmooth_4 ? ema(ema(ema(upperHTF_4, vHtfSmoothLen_4), vHtfSmoothLen_4), vHtfSmoothLen_4) : upperHTF_4
- lowerHTF_1 = HTF_1 and not vHtfOn ? lower : vHtfRepaints ? security(syminfo.tickerid, vHtf, lower) :
- security(syminfo.tickerid, vHtf, lower[1], lookahead = barmerge.lookahead_on)
- lowerHTF_1 := HTF_1 and vHtfOn and vHtfSmooth ? ema(ema(ema(lowerHTF_1, vHtfSmoothLen), vHtfSmoothLen), vHtfSmoothLen) : lowerHTF_1
- lowerHTF_2 = HTF_2 and not vHtfOn_2 ? lower : vHtfRepaints_2 ? security(syminfo.tickerid, vHtf_2, lower) :
- security(syminfo.tickerid, vHtf_2, lower[1], lookahead = barmerge.lookahead_on)
- lowerHTF_2 := HTF_2 and vHtfOn_2 and vHtfSmooth_2 ? ema(ema(ema(lowerHTF_2 , vHtfSmoothLen_2), vHtfSmoothLen_2), vHtfSmoothLen_2) : lowerHTF_2
- lowerHTF_3 = HTF_3 and not vHtfOn_3 ? lower : vHtfRepaints_3 ? security(syminfo.tickerid, vHtf_3, lower) :
- security(syminfo.tickerid, vHtf_3, lower[1], lookahead = barmerge.lookahead_on)
- lowerHTF_3 := HTF_3 and vHtfOn_3 and vHtfSmooth_3 ? ema(ema(ema(lowerHTF_3, vHtfSmoothLen_3), vHtfSmoothLen_3), vHtfSmoothLen_3) : lowerHTF_3
- lowerHTF_4 = HTF_4 and not vHtfOn_4 ? lower : vHtfRepaints_4 ? security(syminfo.tickerid, vHtf_4, lower) :
- security(syminfo.tickerid, vHtf_4, lower[1], lookahead = barmerge.lookahead_on)
- lowerHTF_4 := HTF_4 and vHtfOn_4 and vHtfSmooth_4 ? ema(ema(ema(lowerHTF_4, vHtfSmoothLen_4), vHtfSmoothLen_4), vHtfSmoothLen_4) : lowerHTF_4
- // }
- // ———————————————————— Plots
- // {
- pbasis = plot(LE ? basis : na, "BB" , color.yellow, linewidth=2)
- pbasis_1 = plot(HTF_1 ? BBHTF_1 : na, "BB HTF 1", color.red , linewidth=2)
- pbasis_2 = plot(HTF_2 ? BBHTF_2 : na, "BB HTF 2", color.lime , linewidth=2)
- pbasis_3 = plot(HTF_3 ? BBHTF_3 : na, "BB HTF 3", color.blue , linewidth=2)
- pbasis_4 = plot(HTF_4 ? BBHTF_4 : na, "BB HTF 4", color.purple, linewidth=2)
- pupper = plot(LE ? upper : na, "Upper" , color.yellow, linewidth=1, transp=50)
- pupper_1 = plot(HTF_1 ? upperHTF_1 : na, "Upper HTF 1", color.red , linewidth=1, transp=50)
- pupper_2 = plot(HTF_2 ? upperHTF_2 : na, "Upper HTF 2", color.lime , linewidth=1, transp=50)
- pupper_3 = plot(HTF_3 ? upperHTF_3 : na, "Upper HTF 3", color.blue , linewidth=1, transp=50)
- pupper_4 = plot(HTF_4 ? upperHTF_4 : na, "Upper HTF 4", color.purple, linewidth=1, transp=50)
- plower = plot(LE ? lower : na, "Lower" , color.yellow, linewidth=1, transp=50)
- plower_1 = plot(HTF_1 ? lowerHTF_1 : na, "Lower HTF 1", color.red , linewidth=1, transp=50)
- plower_2 = plot(HTF_2 ? lowerHTF_2 : na, "Lower HTF 2", color.lime , linewidth=1, transp=50)
- plower_3 = plot(HTF_3 ? lowerHTF_3 : na, "Lower HTF 3", color.blue , linewidth=1, transp=50)
- plower_4 = plot(HTF_4 ? lowerHTF_4 : na, "Lower HTF 4", color.purple, linewidth=1, transp=50)
- fill(pupper , plower , color=color.yellow, transp=97)
- fill(pupper_1, plower_1, color=color.red , transp=97)
- fill(pupper_2, plower_2, color=color.lime , transp=97)
- fill(pupper_3, plower_3, color=color.blue , transp=97)
- fill(pupper_4, plower_4, color=color.purple, transp=97)
- t_BB_1 = valuewhen(basis, basis, 0)
- t_BBhtf_1 = valuewhen(BBHTF_1, BBHTF_1, 0)
- t_BBhtf_2 = valuewhen(BBHTF_2, BBHTF_2, 0)
- t_BBhtf_3 = valuewhen(BBHTF_3, BBHTF_3, 0)
- t_BBhtf_4 = valuewhen(BBHTF_4, BBHTF_4, 0)
- if LE
- f_htfLabel(not val ? "BB " + tostring(length) : val ? "BB " + tostring(length) + "\n" + tostring(t_BB_1) : na,
- basis, color.yellow, 1)
- if HTF_1
- f_htfLabel(
- vHtfType == vTF0 and not val ? "BB " + tostring(length) :
- vHtfType == vTF0 and val ? "BB " + tostring(length) + "\n" + tostring(t_BBhtf_1) :
- vHtfType != vTF0 and not val ? "BB " + tostring(length) + "(TF " + vHtf + ")" :
- vHtfType != vTF0 and val ? "BB " + tostring(length) + "(TF " + vHtf + ")" + "\n" + tostring(t_BBhtf_1) : na,
- vHtfType == vTF0 ? basis : BBHTF_1, color.red, vOffsetLabels * 1)
- if HTF_2
- f_htfLabel(
- vHtfType_2 == vTF0 and not val ? "BB " + tostring(length) :
- vHtfType_2 == vTF0 and val ? "BB " + tostring(length) + "\n" + tostring(t_BBhtf_2) :
- vHtfType_2 != vTF0 and not val ? "BB " + tostring(length) + "(TF " + vHtf_2 + ")" :
- vHtfType_2 != vTF0 and val ? "BB " + tostring(length) + "(TF " + vHtf_2 + ")" + "\n" + tostring(t_BBhtf_2) : na,
- vHtfType_2 == vTF0 ? basis : BBHTF_2, color.lime, vOffsetLabels_2 * 1)
- if HTF_3
- f_htfLabel(
- vHtfType_3 == vTF0 and not val ? "BB " + tostring(length) :
- vHtfType_3 == vTF0 and val ? "BB " + tostring(length) + "\n" + tostring(t_BBhtf_3) :
- vHtfType_3 != vTF0 and not val ? "BB " + tostring(length) + "(TF " + vHtf_3 + ")" :
- vHtfType_3 != vTF0 and val ? "BB " + tostring(length) + "(TF " + vHtf_3 + ")" + "\n" + tostring(t_BBhtf_3) : na,
- vHtfType_3 == vTF0 ? basis : BBHTF_3, color.blue, vOffsetLabels_3 * 1)
- if HTF_4
- f_htfLabel(
- vHtfType_4 == vTF0 and not val ? "BB " + tostring(length) :
- vHtfType_4 == vTF0 and val ? "BB " + tostring(length) + "\n" + tostring(t_BBhtf_4) :
- vHtfType_4 != vTF0 and not val ? "BB " + tostring(length) + "(TF " + vHtf_4 + ")" :
- vHtfType_4 != vTF0 and val ? "BB " + tostring(length) + "(TF " + vHtf_4 + ")" + "\n" + tostring(t_BBhtf_4) : na,
- vHtfType_4 == vTF0 ? basis : BBHTF_4, color.purple, vOffsetLabels_4 * 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement