Scalp Tool
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Demech

//@version=5
indicator("Scalp Tool", overlay=true, format=format.price, precision = 2, timeframe = "", timeframe_gaps = false)
import PineCoders/Time/3

// Parameters
length = input(96, title="Length")
slength = input(96, title="slength")
threshold = input.float(1, title="Treshold")
threshold2 = input.float(4, title="Threshold 2")
resistance_length = input(96, title = "Resistance")
support_length = input(96, title = "Support")
resistance_length2 = input(16, title = "Resistance Length BK")
support_length2 = input(16, title = "Support Length BK")
vwmalength = input(200, title = "vwma" )
rsi1_period = input(title='RSI Period 1', defval=14)
rsi1_buy_level = input(title='RSI Buy Level', defval=29)
rsi1_sell_level = input(title='RSI Sell Level', defval=71)
rsi2_period = input(title='RSI Period 2', defval=14)
rsi2_buy_level = input(title='RSI Buy Level 2', defval=38)
rsi2_sell_level = input(title='RSI Sell Level 2', defval=62)
ma1_type = input.string(title='Moving Average Type for RSI 1', defval='NONE', options=['SMA', 'EMA', 'VWMA', 'NONE'])
ma1_period = input(title='Moving Average Period for RSI 1', defval=14)
ma2_type = input.string(title='Moving Average Type for RSI 2', defval='VWMA', options=['SMA', 'EMA', 'VWMA', 'NONE'])
ma2_period = input(title='Moving Average Period for RSI 2', defval=192)

rsi_period = input(14, title = "RSI Periode for Bar Color")
upperBound = input(70, title='Upper Bound')
lowerBound = input(30, title='Lower Bound')
midUpperBound = input(53, title='Mid Upper Bound')
midLowerBound = input(47, title='Mid Lower Bound')
extremeUpperBound = input(80, title='Extreme Upper Bound')
extremeLowerBound = input(20, title='Extreme Lower Bound')

// Parameter Bands
lengthb = input(16, title='Bands')
mult = input.float(1, title = "mult")
mult2 = input.float(2, title = "mult 2")
mult3 = input.float(3, title = "mult 3")
upper_color = input(color.new (color.rgb(1, 250, 10),80),"bull")
lower_color = input(color.new (color.rgb(255, 0, 0),80),"bear")
middle_color = input(color.new(color.gray,100), "neutral")


// Calculat Bands
vwmac = ta.vwma(close, lengthb)
vwmao = ta.vwma(open, lengthb)
vwmah = ta.vwma(high, lengthb)
vwmal = ta.vwma(low, lengthb)

// Calculate the upper and lower bands
upper = vwmah + ((vwmah - vwmac) * mult)
lower = vwmal - ((vwmac - vwmal) * mult)
upper2 = vwmah + ((vwmah - vwmac) * mult2)
lower2 = vwmal - ((vwmac - vwmal) * mult2)
upper3 = vwmah + ((vwmah - vwmac) * mult3)
lower3 = vwmal - ((vwmac - vwmal) * mult3)

// VWMA
vwma = ta.vwma(close, vwmalength)

// Function to calculate the standard deviation
pstdev(Series, Period) =>
    mean = ta.sma(Series, Period)
    summation = 0.0
    for i=0 to Period-1
        sampleMinusMean = nz(Series[i]) - mean
        summation := summation + sampleMinusMean * sampleMinusMean
    math.sqrt(summation / Period)

length := length > bar_index + 1 ? bar_index + 1 : length
slength := slength > bar_index + 1 ? bar_index + 1 : slength

mean    = ta.sma(volume, length)
std     = pstdev(volume, slength)
stdbar  = (volume - mean) / std
dir     = close > open


// Calculations from second script
rsi = ta.rsi(close, rsi_period)
rsi1 = ta.rsi(close, rsi1_period)
rsi2 = ta.rsi(close, rsi2_period)
ma1_RSI = ma1_type == 'SMA' ? ta.sma(close, ma1_period) : ma1_type == 'EMA' ? ta.ema(close, ma1_period) : ma1_type == 'VWMA' ? ta.vwma(close, ma1_period) : close
ma2_RSI = ma2_type == 'SMA' ? ta.sma(close, ma2_period) : ma2_type == 'EMA' ? ta.ema(close, ma2_period) : ma2_type == 'VWMA' ? ta.vwma(close, ma2_period) : close
barcolor(rsi > extremeUpperBound ? color.new(color.red, 0) : rsi > upperBound and rsi <= extremeUpperBound ? color.new(color.red, 0) : rsi > midUpperBound and rsi <= upperBound ?  color.new(color.rgb(43, 255, 0), 0) : rsi > midLowerBound and rsi <= midUpperBound ? color.new(color.gray, 0) : rsi > lowerBound and rsi <= midLowerBound ? color.new(color.rgb(235, 9, 9), 0) : rsi > extremeLowerBound and rsi <= lowerBound ? color.new(color.green, 0) : color.new(color.green, 0))

// Resistance and Support
resistance = ta.highest(close, resistance_length)
support = ta.lowest(close, support_length)
resistanceh = ta.highest(high, resistance_length)
supportl = ta.lowest(low, support_length)
resistance_bk = ta.highest(open, support_length2)
support_bk = ta.lowest(open, resistance_length2)

// Breakout
bullish_breakout = ta.crossover(close, resistance_bk)
bearish_breakout = ta.crossunder(close, support_bk)

// Bounce logic
bu = low <= low[1] and low < support_bk or (low < support_bk and support)
su = high >= high[1] and high > resistance_bk or (high > resistance_bk and resistance)
bullish_bounce = low < support and low < lower3 and close > lower2 and close > support
bearish_bounce = high > resistance and high > upper3 and close < upper2 and close < resistance

// Buy and Sell Signals based on RSI Crossover with Moving Average filter
rsi1_buy_signal = ta.crossover(rsi1, rsi1_buy_level) and (ma1_type == 'NONE' or close > ma1_RSI)
rsi1_sell_signal = ta.crossunder(rsi1, rsi1_sell_level) and (ma1_type == 'NONE' or close < ma1_RSI)
rsi2_buy_signal = ta.crossover(rsi2, rsi2_buy_level) and (ma2_type == 'NONE' or close > ma2_RSI)
rsi2_sell_signal = ta.crossunder(rsi2, rsi2_sell_level) and (ma2_type == 'NONE' or close < ma2_RSI)

// Buy and Sell Signal
bk_bull_signal = stdbar > threshold2 and dir and bullish_breakout
bk_bear_signal = stdbar > threshold2 and not dir and bearish_breakout

buy_signal = stdbar > threshold  and bullish_bounce
sell_signal = stdbar > threshold and bearish_bounce

buy = bu and support == support_bk and rsi1_buy_signal and buy_signal or (bu and rsi1_buy_signal)
sell = su and resistance == resistance_bk and rsi1_sell_signal and sell_signal or (su and rsi1_sell_signal)

Crossup = ta.crossover(vwmac, vwma)
Crossdown = ta.crossunder(vwmac, vwma)

buyb = open < lower and close > lower or ta.crossover(rsi1, 25)
sellb = open > upper and close < upper or ta.crossunder(rsi1, 75)

big_bull = buy and buyb
big_sell = sell and sellb

// Draw support and resistance lines and signals
r1 = plot(resistance, color=color.new(color.rgb(253, 0, 241), 100), title = "Resistance")
s1 = plot(support, color=color.new(color.rgb(253, 0, 241), 100), title = "Support")
r2 = plot(resistanceh, color=color.new(color.rgb(253, 0, 241), 100), title = "Resistance")
s2 = plot(supportl, color=color.new(color.rgb(253, 0, 241), 100), title = "Support")
fill(r1, r2, color = color.new(color.purple, 80), title = "Resistance")
fill(s1, s2, color = color.new(color.purple, 80), title = "Support")
plot(resistance_bk, color=color.new(color.gray, 100), title = "Resistance BK")
plot(support_bk, color=color.new(color.gray, 100), title = "Support BK")

// Drawing the bands and the moving average
plot(vwmac, color=color.new(color.aqua, 0), title = "vwmac")
plot(vwmao, color=color.new(color.yellow, 0), title = "vwmao")
plot(vwma, color=color.new(color.black, 0), title = "vwma" )
b1 = plot(upper, color=color.new(color.green, 100), title = "Upper Band")
b2 = plot(lower, color=color.new(color.red, 100), title = "Lower Band")
b3 = plot(upper2, color=color.new(color.aqua, 100), title = "Upper Band 2")
b4 = plot(lower2, color=color.new(color.maroon, 100), title = "Lower Band 2")
b5 = plot(upper3, color=color.new(color.yellow, 100), title = "Upper Band 3")
b6 = plot(lower3, color=color.new(color.yellow, 100), title = "Lower Band 3")
fillcolor = vwmac > vwma and vwmac > vwmao ? upper_color : vwmac < vwma and vwmac < vwmao ? lower_color : middle_color
fill(b1, b2, color=fillcolor, title = "Fill Upper Lower Band")
fill(b1, b3, color=color.new(color.gray, 50), title = "Fill Upper to Upper 2 Band")
fill(b2, b4, color=color.new(color.gray,50), title = "Fill Lower to Lower 2 Band")
fill(b3, b5, color=color.new(color.yellow,100), title = "Fill Lower to Lower 3 Band")
fill(b4, b6, color=color.new(color.yellow,100), title = "Fill Lower to Lower 2 Band")

// Create priority levels
priority_5 = rsi1_buy_signal or rsi1_sell_signal or rsi2_buy_signal or rsi2_sell_signal
priority_4 = bk_bull_signal or bk_bear_signal
priority_3 = buy_signal or sell_signal
priority_2 = buy or sell or buyb or sellb
priority_1 = big_bull or big_sell

// Enable oder disable Signals
p5_enabled = input(true, title="enable RSI Signals")
p4_enabled = input(true, title="enable Breakout Signals")
p3_enabled = input(true, title="enable Long or Short Signals")
p2_enabled = input(true, title="enable buy, sell , buy Band and sell Band")
p1_enabled = input(true, title="enable Big Bull or Big Sell")

// If a priority is not enabled, its signals become part of the next lower priority level
if not p5_enabled
    priority_4 := priority_4 or priority_5
    priority_5 := na
if not p4_enabled
    priority_3 := priority_3 or priority_4
    priority_4 := na
if not p3_enabled
    priority_2 := priority_2 or priority_3
    priority_3 := na
if not p2_enabled
    priority_1 := priority_1 or priority_2
    priority_2 := na
if not p1_enabled
    priority_1 := na


// Check if a higher priority signal is present
p5 = priority_5 and not (priority_4 or priority_3 or priority_2 or priority_1) and p5_enabled
p4 = priority_4 and not (priority_3 or priority_2 or priority_1) and p4_enabled
p3 = priority_3 and not (priority_2 or priority_1) and p3_enabled
p2 = priority_2 and not priority_1 and p2_enabled
p1 = priority_1 and p1_enabled

// Plot for Prioriti 5
plotshape(series=p5 and rsi1_buy_signal, style=shape.triangleup, location=location.belowbar, color=color.new(color.green, 0), size=size.small, title='RSI 1 Buy Signal')
plotshape(series=p5 and rsi1_sell_signal, style=shape.triangledown, location=location.abovebar, color=color.new(color.red, 0), size=size.small, title='RSI 1 Sell Signal')
plotshape(series=p5 and rsi2_buy_signal, style=shape.triangleup, location=location.belowbar, color=color.new(color.blue, 0), size=size.small, title='RSI 2 Buy Signal')
plotshape(series=p5 and rsi2_sell_signal, style=shape.triangledown, location=location.abovebar, color=color.new(color.orange, 0), size=size.small, title='RSI 2 Sell Signal')

// Plot for Prioriti 4
plotshape(series=p4 and bk_bull_signal, title="Buy Signal Breakout", location=location.belowbar, color=color.rgb(3, 139, 250), style=shape.labelup, text="Bull", textcolor = color.white)
plotshape(series=p4 and bk_bear_signal, title="Sell Signal Breakout", location=location.abovebar, color=color.rgb(252, 2, 2), style=shape.labeldown, text="Bear", textcolor = color.white)

// Plot for Prioriti 3
plotshape(series=p3 and buy_signal, title="Long", location=location.belowbar, color=color.green, style=shape.labelup, text="Long", textcolor = color.white)
plotshape(series=p3 and sell_signal, title="Short", location=location.abovebar, color=color.red, style=shape.labeldown, text="Short", textcolor = color.white)

// Plot for Prioriti 2
plotshape(series=p2 and buy, title="buy", location=location.belowbar, color=color.green, style=shape.labelup, text="buy", textcolor = color.white)
plotshape(series=p2 and sell, title="sell", location=location.abovebar, color=color.red, style=shape.labeldown, text="sell", textcolor = color.white)
plotshape(series=p2 and buyb, title="buy Band", location=location.belowbar, color=color.green, style=shape.labelup, text="b", textcolor = color.white)
plotshape(series=p2 and sellb, title="sell Band", location=location.abovebar, color=color.red, style=shape.labeldown, text="s", textcolor = color.white)

// Plot for Prioriti 1
plotshape(series=p1 and big_bull, title="BIG BUY", location=location.belowbar, color=color.green, style=shape.labelup, text="Big", textcolor = color.white)
plotshape(series=p1 and big_sell, title="BIG SELL", location=location.abovebar, color=color.red, style=shape.labeldown, text="Big", textcolor = color.white)

// Alerts for Buy and Sell Signals
alertcondition(condition=bk_bull_signal, title='Bullish Breakout', message='Bull Breakout')
alertcondition(condition=bk_bear_signal, title='Bearish Breakout', message='Bear Breakout')
alertcondition(condition=buy, title='Buy', message='Buy')
alertcondition(condition=sell, title='Sell', message='Sell')
alertcondition(condition=buyb, title='Band Buy', message='Buy Band')
alertcondition(condition=sellb, title='Band Sell', message='Sell Band')
alertcondition(condition=buy_signal, title='Volume and Bounce Buy', message='Volume and Bounce Buy')
alertcondition(condition=sell_signal, title='Volume and Bounce Sell', message='Volume and Bounce Sell')
alertcondition(condition=big_bull, title='Big Bull', message='Big Bull')
alertcondition(condition=big_sell, title='Big Sell', message='Big Sell')

// Alerts RSI
alertcondition(condition=rsi1_buy_signal, title='RSI 1 Buy Signal', message='RSI 1 Buy Signal')
alertcondition(condition=rsi1_sell_signal, title='RSI 1 Sell Signal', message='RSI 1 Sell Signal')
alertcondition(condition=rsi2_buy_signal, title='RSI 2 Buy Signal', message='RSI 2 Buy Signal')
alertcondition(condition=rsi2_sell_signal, title='RSI 2 Sell Signal', message='RSI 2 Sell Signal')

combined_alert = bk_bull_signal or bk_bear_signal or buy or sell or buy_signal or sell_signal or rsi1_buy_signal or rsi1_sell_signal or rsi2_buy_signal or rsi2_sell_signal or big_bull or big_sell or buyb or sellb
alertcondition(combined_alert, title='Combined Alert', message='A signal has triggered!')
