pro_scalper_ai_bullbyte_HmXeAuPG.pine
Copy
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © BullByte
//@version=6
indicator(" Pro Scalper AI [BullByte]", overlay=false, dynamic_requests=true, max_labels_count=500, max_lines_count=500)
//─────────────────────────────────────────────
// 1. HELPER FUNCTION & CONFIRMED DATA
//─────────────────────────────────────────────
// f_nr() forces series to update only on confirmed (closed) bars.
f_nr(src) => barstate.isconfirmed ? src : nz(src[1])
// Freeze price and volume to confirmed values.
cOpen = f_nr(open)
cHigh = f_nr(high)
cLow = f_nr(low)
cClose = f_nr(close)
cVolume = f_nr(volume)
//─────────────────────────────────────────────
// 2. INPUTS
//─────────────────────────────────────────────
useHTF = input.bool(false, "Use Higher Timeframe Trend Filter?", inline="HTF")
htf_tf = input.timeframe("15", "HTF", inline="HTF")
useMACDFilter = input.bool(false, "Use MACD Filter?")
usePinbarFilter = input.bool(false, "Use Pinbar Filter?")
adxLen = input.int(14, "ADX Length")
diLen = input.int(14, "DI Length")
atrLen = input.int(14, "ATR Length")
hmaLen = input.int(21, "HMA Length")
momChoice = input.string("Stochastic RSI", "Momentum Mode", options=["Stochastic RSI","Fisher RSI","Williams %R"])
// Stochastic RSI (Mode 1)
stochRsiLen = input.int(12, "Stoch RSI Length", group="Stoch RSI")
stochKLen = input.int(3, "Stoch RSI %K Smoothing", group="Stoch RSI")
stochDLen = input.int(3, "Stoch RSI %D Smoothing", group="Stoch RSI")
// Fisher RSI (Mode 2)
fisherRsiLen = input.int(14, "Fisher RSI Length", group="Fisher RSI")
// Williams %R (Mode 3)
wprLen = input.int(14, "Williams %R Length", group="Williams %R")
// TDI (optional reference)
tdiRsiLen = input.int(13, "TDI RSI Length")
tdiSignalLen = input.int(7, "TDI Signal Length")
// Weights & Thresholds
wTrend = input.float(0.3, "Trend Weight", step=0.1)
wMomentum = input.float(0.3, "Momentum Weight", step=0.1)
wVolatility = input.float(0.2, "Volatility Weight", step=0.1)
wVolume = input.float(0.2, "Volume Weight", step=0.1)
baseUpper = input.float(25.0, "Base Upper Threshold", step=0.1)
baseLower = input.float(-25.0,"Base Lower Threshold", step=0.1)
dynMultiplier = input.float(0.5, "Dynamic Threshold Multiplier", step=0.1)
oscLookback = input.int(50, "Oscillator Std Dev Lookback")
// (Reserved for further refinement)
useHysteresis = input.bool(true, "Use Adaptive Hysteresis?")
baseHystGap = input.float(1.0, "Base Hysteresis Gap", step=0.1)
hystMultiplier = input.float(1.0, "Hysteresis Volatility Multiplier", step=0.1)
// Dynamic Weighting
useDynamicW = input.bool(true, "Use Dynamic Weighting?")
maxVolFactor = input.float(2.0, "Max Volatility Factor", step=0.1)
maxTrendFactor = input.float(1.5, "Max Trend Factor", step=0.1)
// Volume Lookback
volLookback = input.int(50, "Volume Lookback")
// AI Forecast (optional)
useAI = input.bool(false, "Use AI Forecast?")
ai_length = input.int(20, "AI Length", minval=1)
ai_future = input.int(2, "AI Forecast Bars Ahead", minval=1)
aiAdjustment = input.float(1.5, "AI Threshold Shift", step=0.1)
aiSmoothing = input.bool(true, "Smooth AI Forecast?")
// Latching Mode Toggle:
useLatching = input.bool(true, "Enable Latching?")
// ─────────────────────────────────────────────
// 2A. DASHBOARD SIZE INPUT (New)
// ─────────────────────────────────────────────
// This input allows the user to change the dashboard’s text size.
dashboardSizeOption = input.string(title="Dashboard Text Size", defval="Normal", options=["Tiny", "Small", "Normal", "Large"])
textSizeEnum = dashboardSizeOption == "Tiny" ? size.tiny :
dashboardSizeOption == "Small" ? size.small :
dashboardSizeOption == "Normal" ? size.normal : size.large
//─────────────────────────────────────────────
// 3. INDICATOR CALCULATIONS (Using Confirmed Data)
//─────────────────────────────────────────────
// 3.1 True Range function.
f_tr(_high, _low, _close) =>
math.max(_high - _low, math.abs(_high - nz(_close[1])), math.abs(_low - nz(_close[1])))
// 3.2 ADX & DI Calculation.
dirmov(_len) =>
up = ta.change(cHigh)
down = -ta.change(cLow)
plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
tr = f_tr(cHigh, cLow, cClose)
truerange = ta.rma(tr, _len)
plusVal = nz(100 * ta.rma(plusDM, _len) / truerange)
minusVal = nz(100 * ta.rma(minusDM, _len) / truerange)
[plusVal, minusVal]
[diplus, diminus] = dirmov(diLen)
adxVal = 100 * ta.rma(math.abs(diplus - diminus) / ((diplus + diminus)==0 ? 1 : (diplus + diminus)), adxLen)
trendDir = diplus > diminus ? 1.0 : diminus > diplus ? -1.0 : 0.0
normTrend = trendDir * math.min(math.max((adxVal - 20) / 20, 0.0), 1.0)
confirmedADX = f_nr(adxVal)
confirmedNormTrend = f_nr(normTrend)
// 3.3 Volatility: ATR.
atrVal = ta.rma(f_tr(cHigh, cLow, cClose), atrLen)
normATR = atrVal / cClose
normVolatilityRaw = normATR * 2 - 1
confirmedNormVolatility = f_nr(normVolatilityRaw)
// 3.4 Momentum Calculations.
// Custom tanh for Fisher transform.
f_tanh(_x) =>
e2x = math.exp(2.0 * _x)
(e2x - 1.0) / (e2x + 1.0)
// Mode 1: Stoch RSI.
f_stochRsi(_len, _k, _d) =>
_rsi = ta.rsi(cClose, _len)
lowestVal = ta.lowest(_rsi, _len)
highestVal = ta.highest(_rsi, _len)
stochVal = (_rsi - lowestVal) / math.max((highestVal - lowestVal), 0.0001)
K = ta.sma(stochVal, _k)
(K - 0.5) * 2
// Mode 2: Fisher RSI.
f_fisherRsi(_len) =>
_rsi = ta.rsi(cClose, _len)
normRsi = (_rsi / 100.0) * 2 - 1
clipped = math.max(math.min(normRsi, 0.999), -0.999)
fish = 0.5 * math.log((1 + clipped) / (1 - clipped))
f_tanh(fish)
// Mode 3: Williams %R.
f_wpr(_len) =>
highestHigh = ta.highest(cHigh, _len)
lowestLow = ta.lowest(cLow, _len)
wprVal = (highestHigh - cClose) / math.max(highestHigh - lowestLow, 0.0001) * -100
(wprVal + 50) / 50 - 1
float momentumVal = 0.0
if momChoice == "Mode 1"
momentumVal := f_stochRsi(stochRsiLen, stochKLen, stochDLen)
else if momChoice == "Mode 2"
momentumVal := f_fisherRsi(fisherRsiLen)
else if momChoice == "Mode 3"
momentumVal := f_wpr(wprLen)
confirmedMomentum = f_nr(momentumVal)
// 3.5 TDI (optional reference).
tdiRsi = ta.rsi(cClose, tdiRsiLen)
tdiSignal = ta.ema(tdiRsi, tdiSignalLen)
confirmedTdiRsi = f_nr(tdiRsi)
confirmedTdiSignal = f_nr(tdiSignal)
// 3.6 Volume (OBV Calculation).
f_calcOBV() =>
var float obv = 0.0
obv := nz(obv[1]) + (cClose > cClose[1] ? cVolume : cClose < cClose[1] ? -cVolume : 0)
obv
obvVal = f_calcOBV()
obvMin = ta.lowest(obvVal, volLookback)
obvMax = ta.highest(obvVal, volLookback)
normVolumeRaw = ((obvVal - obvMin) / math.max(obvMax - obvMin, 0.0001)) * 2 - 1
confirmedVolumeNorm = f_nr(normVolumeRaw)
// 3.7 Dynamic Weighting.
float volFactor = 1.0
float trendFactor = 1.0
if useDynamicW
baseVol = 0.005
ratioVol = normATR / baseVol
volFactor := math.min(ratioVol, maxVolFactor)
baseAdx = 25.0
ratioAdx = adxVal / baseAdx
trendFactor := math.min(ratioAdx, maxTrendFactor)
effWTrend = wTrend * trendFactor
effWVolatility = wVolatility * volFactor
effWMomentum = wMomentum
effWVolume = wVolume
totalWeight = effWTrend + effWMomentum + effWVolatility + effWVolume
// 3.8 Composite Oscillator.
oscillatorRaw = (effWTrend * confirmedNormTrend + effWMomentum * confirmedMomentum + effWVolatility * confirmedNormVolatility + effWVolume * confirmedVolumeNorm) / totalWeight
oscillator = oscillatorRaw * 100
confirmedOscillator = f_nr(oscillator)
oscStd = ta.stdev(confirmedOscillator, oscLookback)
upperThreshold = baseUpper + (oscStd * dynMultiplier)
lowerThreshold = baseLower - (oscStd * dynMultiplier)
confirmedUpperThreshold = f_nr(upperThreshold)
confirmedLowerThreshold = f_nr(lowerThreshold)
// 3.9 AI Forecast (optional).
float aiOsc_current = na
float aiOsc = na
float ai_bias = 0.0
if useAI
ai_value = ta.linreg(cClose, ai_length, 0)
ai_slope = ai_value - ta.linreg(cClose, ai_length, 1)
ai_pred = ai_value + ai_slope * ai_future
ai_pred_smooth = aiSmoothing ? ta.sma(ai_pred, 2) : ai_pred
osc_lb = ta.lowest(confirmedOscillator, ai_length)
osc_hb = ta.highest(confirmedOscillator, ai_length)
ai_lb = ta.lowest(ai_pred_smooth, ai_length)
ai_hb = ta.highest(ai_pred_smooth, ai_length)
osc_range = math.max(osc_hb - osc_lb, 0.001)
ai_range = math.max(ai_hb - ai_lb, 0.001)
aiOsc := ((ai_pred_smooth - ai_lb) / ai_range) * osc_range + osc_lb
ai_value_lb = ta.lowest(ai_value, ai_length)
ai_value_hb = ta.highest(ai_value, ai_length)
ai_value_range = math.max(ai_value_hb - ai_value_lb, 0.001)
aiOsc_current := ((ai_value - ai_value_lb) / ai_value_range) * osc_range + osc_lb
osc_mid = (baseUpper + baseLower) / 2
ai_bias := aiOsc - osc_mid
confirmedAiBias = f_nr(ai_bias)
adjustedUpperThreshold = useAI ? confirmedUpperThreshold + (confirmedAiBias > 0 ? aiAdjustment : 0) : confirmedUpperThreshold
adjustedLowerThreshold = useAI ? confirmedLowerThreshold + (confirmedAiBias < 0 ? -aiAdjustment : 0) : confirmedLowerThreshold
confirmedAdjustedUpperThreshold = f_nr(adjustedUpperThreshold)
confirmedAdjustedLowerThreshold = f_nr(adjustedLowerThreshold)
// 3.10 Trend via Local HMA.
hma(src, length) =>
wma1 = ta.wma(src, length)
wma2 = ta.wma(src, math.round(length / 2))
diff = 2 * wma2 - wma1
ta.wma(diff, math.round(math.sqrt(length)))
localHMA = hma(cClose, hmaLen)
trendHMA = cClose > localHMA ? 1.0 : cClose < localHMA ? -1.0 : 0.0
confirmedTrendHMA = f_nr(trendHMA)
// Higher Timeframe trend (if enabled).
htfTrend = 0.0
if useHTF
htfHMA = request.security(syminfo.tickerid, htf_tf, hma(cClose, hmaLen), lookahead=barmerge.lookahead_off)
htfTrend := na(htfHMA) ? 0.0 : cClose > htfHMA ? 1.0 : cClose < htfHMA ? -1.0 : 0.0
confirmedHTFTrend = f_nr(htfTrend)
//─────────────────────────────────────────────
// 4. SIGNAL GENERATION
//─────────────────────────────────────────────
// Define conditions (all using confirmed values):
condStrongBuy = (confirmedOscillator > confirmedAdjustedUpperThreshold) and (confirmedTrendHMA > 0) and (not useHTF or confirmedHTFTrend > 0) and (confirmedADX > 20)
condStrongSell = (confirmedOscillator < confirmedAdjustedLowerThreshold) and (confirmedTrendHMA < 0) and (not useHTF or confirmedHTFTrend < 0) and (confirmedADX > 20)
condEarlyBuy = (confirmedOscillator > baseUpper) and (confirmedOscillator < confirmedAdjustedUpperThreshold)
condEarlySell = (confirmedOscillator < baseLower) and (confirmedOscillator > confirmedAdjustedLowerThreshold)
// 4A. Immediate (Non-Latching) Signal Calculation.
immediateSignal = "No Signal"
if condStrongBuy
immediateSignal := "Strong Buy"
else if condStrongSell
immediateSignal := "Strong Sell"
else if condEarlyBuy
immediateSignal := "Early Buy"
else if condEarlySell
immediateSignal := "Early Sell"
// 4B. Stored (Latched) Signal State Machine.
var string storedState = "Neutral"
if useLatching and barstate.isconfirmed
if storedState == "Neutral"
if condEarlyBuy
storedState := "EarlyBuy"
else if condEarlySell
storedState := "EarlySell"
else if condStrongBuy
storedState := "Long"
else if condStrongSell
storedState := "Short"
else if storedState == "EarlyBuy"
if condStrongBuy
storedState := "Long"
else if not condEarlyBuy
storedState := "Neutral"
else if storedState == "Long"
if condStrongSell
storedState := "Short"
else if condEarlySell
storedState := "EarlySell"
else if storedState == "EarlySell"
if condStrongSell
storedState := "Short"
else if not condEarlySell
storedState := "Neutral"
else if storedState == "Short"
if condStrongBuy
storedState := "Long"
else if condEarlyBuy
storedState := "EarlyBuy"
storedSignal = storedState == "Long" ? "Strong Buy" :
storedState == "Short" ? "Strong Sell" :
storedState == "EarlyBuy" ? "Early Buy" :
storedState == "EarlySell" ? "Early Sell" : "No Signal"
//─────────────────────────────────────────────
// 5. VISUALIZATION
//─────────────────────────────────────────────
// 5A. Plot the composite oscillator and dynamic thresholds.
plotOsc = plot(confirmedOscillator, color=confirmedOscillator >= 0 ? color.green : color.red, linewidth=2, title="Composite Oscillator")
plotZero = plot(0, color=color.gray, title="Zero Line")
plotUpper = plot(confirmedUpperThreshold, color=color.purple, linewidth=1, title="Dynamic Upper Threshold")
plotLower = plot(confirmedLowerThreshold, color=color.purple, linewidth=1, title="Dynamic Lower Threshold")
// Compute fill color based on active signal.
activeSignal = useLatching ? storedSignal : immediateSignal
fillColor = activeSignal == "Strong Buy" ? color.new(color.green, 80) : activeSignal == "Strong Sell" ? color.new(color.red, 80) : activeSignal == "Early Buy" ? color.new(color.lime, 80) : activeSignal == "Early Sell" ? color.new(color.maroon, 80) : color.new(color.blue, 90)
// Fill the area between the dynamic upper and lower thresholds.
fill(plotUpper, plotLower, color=fillColor, title="Band Background")
// 5B. Marker Placement on the Oscillator Pane.
// Immediate (Non-Latching) Signals:
immediateStrongBuy = not useLatching and (immediateSignal == "Strong Buy")
immediateStrongSell = not useLatching and (immediateSignal == "Strong Sell")
immediateEarlyBuy = not useLatching and (immediateSignal == "Early Buy")
immediateEarlySell = not useLatching and (immediateSignal == "Early Sell")
plotshape(immediateStrongBuy, style=shape.circle, color=color.green, title="Immediate Strong Buy", location=location.bottom, size=size.tiny)
plotshape(immediateStrongSell, style=shape.circle, color=color.red, title="Immediate Strong Sell", location=location.top, size=size.tiny)
plotshape(immediateEarlyBuy, style=shape.cross, color=color.lime, title="Immediate Early Buy", location=location.bottom, size=size.tiny)
plotshape(immediateEarlySell, style=shape.cross, color=color.maroon, title="Immediate Early Sell", location=location.top, size=size.tiny)
// Stored (Latched) Signals:
latchedStrongBuy = useLatching and (storedState == "Long")
latchedStrongSell = useLatching and (storedState == "Short")
latchedEarlyBuy = useLatching and (storedState == "EarlyBuy")
latchedEarlySell = useLatching and (storedState == "EarlySell")
plotshape(latchedStrongBuy, style=shape.triangleup, color=color.green, title="Latched Strong Buy", location=location.bottom, size=size.tiny)
plotshape(latchedStrongSell, style=shape.triangledown, color=color.red, title="Latched Strong Sell", location=location.top, size=size.tiny)
plotshape(latchedEarlyBuy, style=shape.diamond, color=color.lime, title="Latched Early Buy", location=location.bottom, size=size.tiny)
plotshape(latchedEarlySell, style=shape.diamond, color=color.maroon, title="Latched Early Sell", location=location.top, size=size.tiny)
// 5C. (Optional) Plot AI Bias if Enabled.
plot(useAI ? confirmedAiBias : na, color=color.blue, title="AI Bias")
// Calculate ADX difference (handle the very first bar)
adxDiff = na(confirmedADX[1]) ? 0 : confirmedADX - confirmedADX[1]
// Choose arrow based on ADX change
adxArrow = adxDiff > 0 ? "⬆️" : adxDiff < 0 ? "⬇️" : ""
// Calculate volume difference (using the normalized OBV volume)
volDiff = na(confirmedVolumeNorm[1]) ? 0 : confirmedVolumeNorm - confirmedVolumeNorm[1]
// Choose arrow based on volume change
volArrow = volDiff > 0 ? "⬆️" : volDiff < 0 ? "⬇️" : ""
//─────────────────────────────────────────────
// 6. DASHBOARD TABLE (Optional)
//─────────────────────────────────────────────
var table dash = table.new(position.top_right, 3, 9, border_width=1)
f_getCellColor(value) =>
value > 0 ? color.new(color.green, 70) : value < 0 ? color.new(color.red, 70) : color.new(color.gray, 80)
if barstate.isconfirmed
table.cell(dash, 0, 0, "ADX", text_color=color.white, bgcolor=color.black, text_size=textSizeEnum)
table.cell(dash, 1, 0, str.tostring(confirmedADX, "#.##") + " " + adxArrow,
text_color=color.white,
bgcolor=(confirmedADX > 25 ? color.new(color.green, 70) : color.new(color.red, 70)),
text_size=textSizeEnum)
table.cell(dash, 0, 1, "Osc", text_color=color.white, bgcolor=color.black, text_size=textSizeEnum)
table.cell(dash, 1, 1, str.tostring(confirmedOscillator, "#.##"), text_color=color.white, bgcolor=f_getCellColor(confirmedOscillator), text_size=textSizeEnum)
table.cell(dash, 0, 2, "Mom Mode", text_color=color.white, bgcolor=color.black, text_size=textSizeEnum)
table.cell(dash, 1, 2, momChoice == "Mode 1" ? "1" : momChoice == "Mode 2" ? "2" : "3", text_color=color.white, bgcolor=color.new(color.gray, 80), text_size=textSizeEnum)
table.cell(dash, 0, 3, "TrendHMA", text_color=color.white, bgcolor=color.black, text_size=textSizeEnum)
table.cell(dash, 1, 3, confirmedTrendHMA > 0 ? "Bullish" : confirmedTrendHMA < 0 ? "Bearish" : "Flat", text_color=color.white, bgcolor=(confirmedTrendHMA>0 ? color.new(color.green, 70) : confirmedTrendHMA<0 ? color.new(color.red, 70) : color.new(color.gray, 80)), text_size=textSizeEnum)
table.cell(dash, 0, 4, "HTF Trend", text_color=color.white, bgcolor=color.black, text_size=textSizeEnum)
htfText = useHTF ? (confirmedHTFTrend>0 ? "Bullish" : confirmedHTFTrend<0 ? "Bearish" : "Flat") : "Off"
table.cell(dash, 1, 4, htfText, text_color=color.white, bgcolor=(confirmedHTFTrend>0 ? color.new(color.green, 70) : confirmedHTFTrend<0 ? color.new(color.red, 70) : color.new(color.gray, 80)), text_size=textSizeEnum)
table.cell(dash, 0, 5, "VolFactor", text_color=color.white, bgcolor=color.black, text_size=textSizeEnum)
table.cell(dash, 1, 5, str.tostring(volFactor, "#.##"), text_color=color.white, bgcolor=(volFactor>1 ? color.new(color.green, 70) : color.new(color.gray, 80)), text_size=textSizeEnum)
table.cell(dash, 0, 6, "Signal", text_color=color.white, bgcolor=color.black, text_size=textSizeEnum)
activeSig = useLatching ? storedSignal : immediateSignal
table.cell(dash, 1, 6, activeSig, text_color=color.white, bgcolor=(activeSig=="Strong Buy" ? color.new(color.green, 70) : activeSig=="Strong Sell" ? color.new(color.red, 70) : color.new(color.gray, 80)), text_size=textSizeEnum)
table.cell(dash, 0, 7, "AI Bias", text_color=color.white, bgcolor=color.black, text_size=textSizeEnum)
aiText = useAI ? str.tostring(confirmedAiBias, "#.##") : "Disabled"
table.cell(dash, 1, 7, aiText, text_color=color.white, bgcolor=f_getCellColor(confirmedAiBias), text_size=textSizeEnum)
table.cell(dash, 0, 8, "Volume", text_color=color.white, bgcolor=color.black, text_size=textSizeEnum)
table.cell(dash, 1, 8, str.tostring(confirmedVolumeNorm, "#.##") + " " + volArrow,
text_color=color.white,
bgcolor=f_getCellColor(confirmedVolumeNorm),
text_size=textSizeEnum)
Show all 409 lines