Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // Bayesian BBSMA: https://www.tradingview.com/v/8lXcviYm/
- // nQQE: https://www.tradingview.com/v/CfCuUCeE/
- //@version=4
- study("Bayesian BBSMA + nQQE Oscillator", shorttitle="Bayesian/nQQE osc", precision=4)
- rescale(_src, _oldMin, _oldMax, _newMin, _newMax) =>
- // Rescales series with known min/max.
- // _src : series to rescale.
- // _oldMin, _oldMax: min/max values of series to rescale.
- // _newMin, _newMin: min/max values of rescaled series.
- _newMin + (_newMax - _newMin) * (_src - _oldMin) / max(_oldMax - _oldMin, 10e-10)
- //#################### Bayesian BBSMA
- bbSmaPeriod = input(20, title="BB SMA Period", group="═════ Bayesian BBSMA settings ═════")
- bbStdDevMult = input(2.5, title="BB Standard Deviation", maxval=50.0)
- bbBasis = sma(close, bbSmaPeriod)
- bbStdDev = bbStdDevMult * stdev(close, bbSmaPeriod)
- bbUpper = bbBasis + bbStdDev
- bbLower = bbBasis - bbStdDev
- // AO
- aoFast = input(5, "AO Fast EMA Length")
- aoSlow = input(34, "AO Slow EMA Length")
- ao = sma(hl2, aoFast) - sma(hl2, aoSlow)
- colorAo = change(ao) > 0 ? color.green : color.red
- // AC
- acFast = input(5, "AC Fast SMA Length")
- acSlow = input(34, "AC Slow SMA Length")
- xSMA1_hl2 = sma(hl2, acFast)
- xSMA2_hl2 = sma(hl2, acSlow)
- xSMA1_SMA2 = xSMA1_hl2 - xSMA2_hl2
- xSMA_hl2 = sma(xSMA1_SMA2, acFast)
- ac = xSMA1_SMA2 - xSMA_hl2
- cClr = ac > ac[1] ? color.blue : color.red
- acAo = (ac + ao) / 2
- maAcAoPeriod = input(13, "AC AO MA Period")
- showMaAcAo = input(false, "Show AC AO MA?")
- maAcAo = vwma(acAo, maAcAoPeriod)
- // Combine AC & AO
- acIsBlue = ac > ac[1]
- acIsRed = not (ac > ac[1])
- aoIsGreen = change(ao) > 0
- aoIsRed = not (change(ao) > 0)
- acAoIsBullish = acIsBlue and aoIsGreen
- acAoIsBearish = acIsRed and acIsRed
- acAoColorIndex = acAoIsBullish ? 1 : acAoIsBearish ? -1 : 0
- // Alligator
- smma(src, length) =>
- smma = 0.0
- smma := na(smma[1]) ? sma(src, length) : (smma[1] * (length - 1) + src) / length
- lipsLength = input(title="🐲 Lips Length", defval=5)
- teethLength = input(title="🐲 Teeth Length", defval=8)
- jawLength = input(title="🐲 Jaw Length", defval=13)
- lipsOffset = input(title="🐲 Lips Offset", defval=3)
- teethOffset = input(title="🐲 Teeth Offset", defval=5)
- jawOffset = input(title="🐲 Jaw Offset", defval=8)
- lips = smma(hl2, lipsLength)
- teeth = smma(hl2, teethLength)
- jaw = smma(hl2, jawLength)
- // SMA
- smaPeriod = input(20, title="SMA Period")
- smaValues = sma(close, smaPeriod)
- // Bayesian Theorem Starts
- bayesPeriod = input(20, title="Bayesian Lookback Period")
- // Next candles are breaking Down
- probBbUpperUpSeq = close > bbUpper ? 1 : 0
- probBbUpperUp = sum(probBbUpperUpSeq, bayesPeriod) / bayesPeriod
- probBbUpperDownSeq = close < bbUpper ? 1 : 0
- probBbUpperDown = sum(probBbUpperDownSeq, bayesPeriod) / bayesPeriod
- probUpBbUpper = probBbUpperUp / (probBbUpperUp + probBbUpperDown)
- probBbBasisUpSeq = close > bbBasis ? 1 : 0
- probBbBasisUp = sum(probBbBasisUpSeq, bayesPeriod) / bayesPeriod
- probBbBasisDownSeq = close < bbBasis ? 1 : 0
- probBbBasisDown = sum(probBbBasisDownSeq, bayesPeriod) / bayesPeriod
- probUpBbBasis = probBbBasisUp / (probBbBasisUp + probBbBasisDown)
- probSmaUpSeq = close > smaValues ? 1 : 0
- probSmaUp = sum(probSmaUpSeq, bayesPeriod) / bayesPeriod
- probSmaDownSeq = close < smaValues ? 1 : 0
- probSmaDown = sum(probSmaDownSeq, bayesPeriod) / bayesPeriod
- probUpSma = probSmaUp / (probSmaUp + probSmaDown)
- sigmaProbsDown = nz(probUpBbUpper * probUpBbBasis * probUpSma / probUpBbUpper * probUpBbBasis * probUpSma + ((1 - probUpBbUpper) * (1 - probUpBbBasis) * (1 - probUpSma)))
- // Next candles are breaking Up
- probDownBbUpper = probBbUpperDown / (probBbUpperDown + probBbUpperUp)
- probDownBbBasis = probBbBasisDown / (probBbBasisDown + probBbBasisUp)
- probDownSma = probSmaDown / (probSmaDown + probSmaUp)
- sigmaProbsUp = nz(probDownBbUpper * probDownBbBasis * probDownSma / probDownBbUpper * probDownBbBasis * probDownSma + ( (1 - probDownBbUpper) * (1 - probDownBbBasis) * (1 - probDownSma) ))
- showNextCandleDown = input(true, title="Plot Next Candles Breaking Down?")
- plot(showNextCandleDown ? rescale(sigmaProbsDown * 100,0,100,0,60) : na, title="Next Candle Breaking Down Probs", style=plot.style_area, transp=60, color=color.red, linewidth=2)
- showNextCandleUp = input(true, title="Plot Next Candles Breaking Up?")
- plot(showNextCandleUp ? rescale(sigmaProbsUp * 100,0,100,0,60) : na, title="Next Candle Breaking Up Probs", style=plot.style_area,transp=60, color=color.green, linewidth=2)
- probPrime = nz(sigmaProbsDown * sigmaProbsUp / sigmaProbsDown * sigmaProbsUp + ( (1 - sigmaProbsDown) * (1 - sigmaProbsUp) ))
- showPrime = input(true, title="Plot Prime Probability?")
- plot(showPrime ? rescale(probPrime * 100,0,100,0,60) : na, title="Prime Probability", style=plot.style_area,transp=60, color=color.blue, linewidth=2)
- lowerThreshold = input(15.0, title="Lower Threshold")
- sideways = probPrime < lowerThreshold / 100 and sigmaProbsUp < lowerThreshold / 100 and sigmaProbsDown < lowerThreshold / 100
- longUsingProbPrime = probPrime > lowerThreshold / 100 and probPrime[1] == 0
- longUsingSigmaProbsUp = sigmaProbsUp < 1 and sigmaProbsUp[1] == 1
- shortUsingProbPrime = probPrime == 0 and probPrime[1] > lowerThreshold / 100
- shortUsingSigmaProbsDown = sigmaProbsDown < 1 and sigmaProbsDown[1] == 1
- milanIsRed = acAoColorIndex == -1
- milanIsGreen = acAoColorIndex == 1
- pricesAreMovingAwayUpFromAlligator = close > jaw and open > jaw
- pricesAreMovingAwayDownFromAlligator = close < jaw and open < jaw
- useBWConfirmation = input(false, title="Use Bill Williams indicators for confirmation?")
- bwConfirmationUp = useBWConfirmation ? milanIsGreen and pricesAreMovingAwayUpFromAlligator : true
- bwConfirmationDown = useBWConfirmation ? milanIsRed and pricesAreMovingAwayDownFromAlligator : true
- longSignal = bwConfirmationUp and (longUsingProbPrime or longUsingSigmaProbsUp)
- shortSignal = bwConfirmationDown and (shortUsingProbPrime or shortUsingSigmaProbsDown)
- barcolor(longSignal ? color.lime : na, title="Long Bars")
- barcolor(shortSignal ? color.maroon : na, title="Short Bars")
- //hzl3 = hline(lowerThreshold, color=#333333, linestyle=hline.style_solid)
- //hzl4 = hline(0, color=#333333, linestyle=hline.style_solid)
- //fill(hzl3, hzl4, title="Lower Threshold", color=sideways ? color.gray : color.maroon, transp=70)
- alertcondition(longSignal, title="Long!", message="Bayesian BBSMA - LONG - {{exchange}}:{{ticker}} at {{close}}")
- alertcondition(shortSignal, title="Short!", message="Bayesian BBSMA - SHORT - {{exchange}}:{{ticker}} at {{close}}")
- //#################### nQQE
- src=input(close)
- length = input(14,"RSI Length", minval=1, group="═════ nQQE settings ═════")
- SSF=input(5, "SF RSI SMoothing Factor", minval=1)
- showsignals = input(title="Show Crossing Signals?", type=input.bool, defval=false)
- RSII=ema(rsi(src,length),SSF)
- TR=abs(RSII-RSII[1])
- wwalpha = 1/ length
- WWMA = 0.0
- WWMA := wwalpha*TR + (1-wwalpha)*nz(WWMA[1])
- ATRRSI=0.0
- ATRRSI := wwalpha*WWMA + (1-wwalpha)*nz(ATRRSI[1])
- QQEF=ema(rsi(src,length),SSF)
- QUP=QQEF+ATRRSI*4.236
- QDN=QQEF-ATRRSI*4.236
- QQES=0.0
- QQES:=QUP<nz(QQES[1]) ? QUP : QQEF>nz(QQES[1]) and QQEF[1]<nz(QQES[1]) ? QDN : QDN>nz(QQES[1]) ? QDN : QQEF<nz(QQES[1]) and QQEF[1]>nz(QQES[1]) ? QUP : nz(QQES[1])
- Colorh = QQEF-25>35 ? color.lime : QQEF-25<15 ? color.red : #E8E81A
- QQF=plot(QQEF-25,"FAST",color=color.maroon,linewidth=2)
- plot(QQEF-25,color=Colorh,linewidth=2)
- QQS=plot(QQES-25,"SLOW",color=color.yellow, linewidth=2)
- hline(35,color=color.gray,linestyle=2)
- hline(15,color=color.gray,linestyle=2)
- buySignalr = crossover(QQEF, QQES)
- plotshape(buySignalr and showsignals ? (QQES-25)*0.995 : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.black, textcolor=color.white, transp=50)
- sellSignallr = crossunder(QQEF, QQES)
- plotshape(sellSignallr and showsignals ? (QQES-25)*1.005 : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.black, textcolor=color.white, transp=50)
- alertcondition(cross(QQEF, QQES), title="Cross Alert", message="QQE Crossing Signal!")
- alertcondition(crossover(QQEF, QQES), title="Crossover Alarm", message="QQE BUY SIGNAL!")
- alertcondition(crossunder(QQEF, QQES), title="Crossunder Alarm", message="QQE SELL SIGNAL!")
- alertcondition(crossover(QQEF, 50), title="Cross 0 Up Alert", message="QQE FAST Crossing 0 UP!")
- alertcondition(crossunder(QQEF, 50), title="Cross 0 Down Alert", message="QQE FAST Crossing 0 DOWN!")
- alertcondition(crossover(QQEF, 60), title="Cross 10 Up Alert", message="QQE Above 10 UPTREND SIGNAL!")
- alertcondition(crossunder(QQEF, 40), title="Cross -10 Down Alert", message="QQE Below -10 DOWNTREND SIGNAL!")
- alertcondition(crossunder(QQEF, 60) or crossover(QQEF, 40), title="SIDEWAYS", message="QQE Entering Sideways Market!")
- //end of this part
- //----- User inputs ---------------------
- Lookback1 = input(title="Lookback Left", type=input.integer, defval=56, minval=1)
- Lookback2 = input(title="Lookback Right", type=input.integer, defval=0, minval=0)
- VertLR = input(title="Show Vertical Left/Right", type=input.bool, defval=true)
- VertHL = input(title="Show Vertical Highest/Lowest", type=input.bool, defval=true)
- Count = input(title="Show Counts", type=input.bool, defval=true)
- FullCount = input(title="Count All Bars", type=input.bool, defval=false)
- Fibs = input(title="Show Fib Retrace Lines", type=input.bool, defval=false)
- FibsExtHigh = input(title="Show Fib Ext Highs", type=input.bool, defval=false)
- FibsExtLow = input(title="Show Fib Ext Lows", type=input.bool, defval=false)
- //----- Main ---------------------
- // Get left and right endpoints. Though user input says "left" and "right", they are interchangeable.
- // The larger number is left, smaller number right.
- left = max(Lookback1, Lookback2)
- right = min(Lookback1, Lookback2)
- // Total num bars. Must be > 0 for highest() and lowest() to work. Add 1 to left bar, if needed.
- left := left - right == 0 ? left + 1 : left
- lengthHL = left - right
- // Highest price, looking back between the left/right endpoints
- highBack = highest(high[right], lengthHL)
- // Get # of bars back from present bar (offset).
- // NOTE: highestbars() returns a negative integer. Thus, subtract the right endpoint to get
- // offset from present (right-edge) bar
- highOffset = highestbars(high[right], lengthHL) - right
- // Lowest price, looking back
- lowBack = lowest(low[right], lengthHL)
- lowOffset = lowestbars(low[right], lengthHL) - right
- // Create a series of timestamps per bar. Needed to determine which bars are at the right edge,
- // While "n" starts at zero (left-edge bar) and counts up to the right-edge bar, this timestamp
- // (or tick) series counts the other way: max value at the left-edge, counting down to zero
- // at the right-edge.
- // This tick series is only valid while the indicator loads because "timenow" is the timestamp
- // of loading and "time" is the timestamp of a bar. The difference between the two timestamps
- // shrinks to zero as the righ-edge bar is processed. After loading, "timenow" and "time" are
- // the same value. Thus, "tick" always equals zero as new bars appear. But, for this indicator
- // this is sufficient behavior...
- tick = round((timenow - time) / change(time)) // Each bar is a tick (float) in the timeline
- // Make a boolean mask for bars between left and right endpoints
- mask = FullCount ? true : tick < left and tick >= right ? true : false
- // Determine the value of n at the left endpoint, used for visual counts of bars in mask
- nOffset = float(na)
- nOffset := FullCount ? 0 : tick == left ? bar_index : nz(nOffset[1])
- // Note: It would be nice to count between the highest/lowest bars, instead of left/right bars,
- // but this isn't possible because highOffset and lowOffset are moving around as the
- // indicator loads. Thus, "mask" isn't accurate...
- //---- Plotting ---------------------
- // Horizontal Lines: Highest, Lowest dynamic lines
- // Params trackprice and offset make the horizontal line dynamic, adjusting to new highs/lows
- plot(highBack, title="Highest", color=color.red, linewidth=2, trackprice=true, offset=-9999)
- plot(lowBack, title="Lowest", color=color.green, linewidth=2, trackprice=true, offset=-9999)
- // Veritcal Lines
- // Use barstate to paint/re-paint vertical lines, as new bars come into the data set.
- // islast - True for right most bar in data set. Use this to paint vertical bar, then push it back
- // in time with the offset parameter.
- // isrealtime - False while indicator loads. True for the right most bar, after loading.
- // isconfirmed - False while bar is not closed. True when close price appears.
- // isrealtime and isconfirmed - While indicator loads, ignore isconfirmed. After loading, isconfirmed
- // turns off the vertical line, as the bar closes. The next (new) bar is painted. This keeps
- // the vertical bar moving forward, as new bars appear, erasing the old (previous) vertical bar.
- VertLRShow = VertLR ? barstate.isrealtime and barstate.isconfirmed ? false :
- barstate.islast ? true : false : false
- // Can't use plot() with histogram/column because histbase can't accept "series" values
- bgcolor(VertLRShow ? color.gray : na, title="Vertical Left", transp=60, offset=-left + 1)
- bgcolor(VertLRShow ? color.gray : na, title="Vertical Right", transp=60, offset=-right)
- // Show vertical highest/lowest lines?
- VertHLShow = VertHL ? barstate.isrealtime and barstate.isconfirmed ? false :
- barstate.islast ? true : false : false
- bgcolor(VertHLShow ? color.red : na, title="Vertical Highest", transp=60, offset=highOffset)
- bgcolor(VertHLShow ? color.green : na, title="Vertical Lowest", transp=60, offset=lowOffset)
- // Fib levels extension and retracement
- priceRange = highBack - lowBack
- plot(FibsExtHigh ? lowBack + 3.618 * priceRange : na, title="3.618", color=color.lime, linewidth=2, trackprice=true, offset=-9999)
- plot(FibsExtHigh ? lowBack + 2.618 * priceRange : na, title="2.618", color=color.lime, linewidth=2, trackprice=true, offset=-9999)
- plot(FibsExtHigh ? lowBack + 1.618 * priceRange : na, title="1.618", color=color.lime, linewidth=2, trackprice=true, offset=-9999)
- plot(FibsExtHigh ? lowBack + 1.1 * priceRange : na, color=#88888800, trackprice=true, offset=-9999, editable=false) // invisible line to work around pine plot bug
- plot(Fibs ? lowBack + 1.1 * priceRange : na, color=#88888800, trackprice=true, offset=-9999, editable=false) // invisible line to work around pine plot bug
- plot(Fibs ? lowBack + 0.786 * priceRange : na, title="0.786", color=color.aqua, linewidth=1, trackprice=true, offset=-9999)
- plot(Fibs ? lowBack + 0.618 * priceRange : na, title="0.618", color=color.orange, linewidth=2, trackprice=true, offset=-9999)
- plot(Fibs ? lowBack + 0.50 * priceRange : na, title="0.5", color=color.yellow, linewidth=2, trackprice=true, offset=-9999)
- plot(Fibs ? lowBack + 0.382 * priceRange : na, title="0.382", color=color.orange, linewidth=2, trackprice=true, offset=-9999)
- plot(Fibs ? lowBack + 0.236 * priceRange : na, title="0.236", color=color.aqua, linewidth=1, trackprice=true, offset=-9999)
- plot(Fibs ? highBack - 1.1 * priceRange : na, color=#88888800, trackprice=true, offset=-9999, editable=false) // invisible line to work around pine plot bug
- plot(FibsExtLow ? highBack - 1.1 * priceRange : na, color=#88888800, trackprice=true, offset=-9999, editable=false) // invisible line to work around pine plot bug
- plot(FibsExtLow ? highBack - 1.618 * priceRange : na, title="1.618", color=color.fuchsia, linewidth=2, trackprice=true, offset=-9999)
- plot(FibsExtLow ? highBack - 2.618 * priceRange : na, title="2.618", color=color.fuchsia, linewidth=2, trackprice=true, offset=-9999)
- plot(FibsExtLow ? highBack - 3.618 * priceRange : na, title="3.618", color=color.fuchsia, linewidth=2, trackprice=true, offset=-9999)
- // Lookback bar count
- countMod = mask ? (bar_index - nOffset) % 10 : na
- countInt = mask ? floor((bar_index - nOffset) / 10) % 10 : na
- plotshape(Count and countMod != 0 ? true : na, style=shape.circle, text="-", textcolor=color.gray, color=#88888800, location=location.bottom, size=size.auto, editable=false)
- plotshape(Count and countMod == 0 and countInt == 0 ? true : na, style=shape.circle, text="0", textcolor=color.gray, color=#88888800, location=location.bottom, size=size.auto, editable=false)
- plotshape(Count and countMod == 0 and countInt == 1 ? true : na, style=shape.circle, text="1", textcolor=color.gray, color=#88888800, location=location.bottom, size=size.auto, editable=false)
- plotshape(Count and countMod == 0 and countInt == 2 ? true : na, style=shape.circle, text="2", textcolor=color.gray, color=#88888800, location=location.bottom, size=size.auto, editable=false)
- plotshape(Count and countMod == 0 and countInt == 3 ? true : na, style=shape.circle, text="3", textcolor=color.gray, color=#88888800, location=location.bottom, size=size.auto, editable=false)
- plotshape(Count and countMod == 0 and countInt == 4 ? true : na, style=shape.circle, text="4", textcolor=color.gray, color=#88888800, location=location.bottom, size=size.auto, editable=false)
- plotshape(Count and countMod == 0 and countInt == 5 ? true : na, style=shape.circle, text="5", textcolor=color.gray, color=#88888800, location=location.bottom, size=size.auto, editable=false)
- plotshape(Count and countMod == 0 and countInt == 6 ? true : na, style=shape.circle, text="6", textcolor=color.gray, color=#88888800, location=location.bottom, size=size.auto, editable=false)
- plotshape(Count and countMod == 0 and countInt == 7 ? true : na, style=shape.circle, text="7", textcolor=color.gray, color=#88888800, location=location.bottom, size=size.auto, editable=false)
- plotshape(Count and countMod == 0 and countInt == 8 ? true : na, style=shape.circle, text="8", textcolor=color.gray, color=#88888800, location=location.bottom, size=size.auto, editable=false)
- plotshape(Count and countMod == 0 and countInt == 9 ? true : na, style=shape.circle, text="9", textcolor=color.gray, color=#88888800, location=location.bottom, size=size.auto, editable=false)
- //end of this part
Advertisement
Add Comment
Please, Sign In to add comment