Ruth Buy/Sell Signal for Day Trade and Swing Trade
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Bu kaynak kod Ref Kafe İşletmeciliği Mimarlık ve Yazılım LTD. ŞTİ.'nin mülküdür. izinsiz kopyalamalarda cezai şartlar uygulanacaktır.
// © akselanil

//@version=5
indicator( title = "Ruthito, Free version Ruthito's Buy/Sell Signal for Day Trade and Swing Trade", shorttitle = "Ref_Ruth", overlay = true, format = format.price, precision = 2 )
import TradingView/ta/5

// ---> VARIABLES
var shortPos = 0, var shortPosTP2 = 0, var shortPosTP3 = 0, var shortPosTP4 = 0, var shortPosTP5 = 0
var shortPosStop = false, var shortPosTakeProfit1 = false, var shortPosTakeProfit2 = false, var shortPosTakeProfit3 = false, var shortPosTakeProfit4 = false, var shortPosTakeProfit5 = false
var shortSL = 0.0, var shortEP = 0.0, var shortTP1 = 0.0, var shortTP2 = 0.0, var shortTP3 = 0.0, var shortTP4 = 0.0, var shortTP5 = 0.0

var longPos = 0, var longPosTP2 = 0, var longPosTP3 = 0, var longPosTP4 = 0, var longPosTP5 = 0
var longPosStop = false, var longPosTakeProfit1 = false, var longPosTakeProfit2 = false, var longPosTakeProfit3 = false, var longPosTakeProfit4 = false, var longPosTakeProfit5 = false
var longSL = 0.0, var longEP = 0.0, var longTP1 = 0.0, var longTP2 = 0.0, var longTP3 = 0.0, var longTP4 = 0.0, var longTP5 = 0.0

date = time >= timestamp( syminfo.timezone, 2023, 1, 1, 0, 0 )

var longAlpEP = 0, var longAlpSL = 0, var longAlpTP1 = 0, var longAlpTP2 = 0, var longAlpTP3 = 0, var longAlpTP4 = 0, var longAlpTP5 = 0, var longIndex = 0
var longTP1Ln = 0, var longTP2Ln = 0, var longTP3Ln = 0, var longTP4Ln = 0, var longTP5Ln = 0, var longSLLn = 0
var longBGtp1TR = 100, var longBGtp2TR = 0, var longBGtp3TR = 0, var longBGtp4TR = 0, var longBGtp5TR = 0, var longBGslTR = 0

var shortAlpEP = 0, var shortAlpSL = 0, var shortAlpTP1 = 0, var shortAlpTP2 = 0, var shortAlpTP3 = 0, var shortAlpTP4 = 0, var shortAlpTP5 = 0, var shortIndex = 0
var shortTP1Ln = 0, var shortTP2Ln = 0, var shortTP3Ln = 0, var shortTP4Ln = 0, var shortTP5Ln = 0, var shortSLLn = 0
var shortBGtp1TR = 100, var shortBGtp2TR = 0, var shortBGtp3TR = 0, var shortBGtp4TR = 0, var shortBGtp5TR = 0, var shortBGslTR = 0
// <--- VARIABLES

// ---> TA
[ kc1Median, kc1Upper, kc1Lower ] = ta.kc( series = close, length = 20, mult = 1 )
[ kc2Median, kc2Upper, kc2Lower ] = ta.kc( series = close, length = 20, mult = 2 )
[ kc3Median, kc3Upper, kc3Lower ] = ta.kc( series = close, length = 20, mult = 3 )
[ kc4Median, kc4Upper, kc4Lower ] = ta.kc( series = close, length = 20, mult = 4 )
[ kc5Median, kc5Upper, kc5Lower ] = ta.kc( series = close, length = 20, mult = 5 )

cci = ta.cci( source = close, length = 20 )
roc = ta.roc( source = close, length = 9 )
rsi = ta.rsi( source = close, length = 14 )
sar = ta.sar( start = 0.02, inc = 0.02, max = 0.2 )
bop = ( close - open ) / ( high - low )

dema = ta.dema( src = close, length = 200 )
// <--- TA

// ---> LONG STRATEGY
upCondtion1 = ta.crossunder( low, kc3Lower )
upCondtion2 = ta.crossunder( cci, -95.0 )
upCondtion3 = -0.1 < roc and roc > -0.2
upCondtion4 = ta.crossunder( bop, -0.9 )
upCondtion5 = ta.crossunder( 18, rsi )
upCondtion6 = kc1Median < sar
upCondtion7 = ohlc4 < kc1Median

longSignal = upCondtion1 and upCondtion2 and upCondtion3 and upCondtion4 and upCondtion6 and upCondtion7

if longSignal
    longLbl = label.new( x = bar_index, y = kc5Lower, text = "Long Signal\n\nTP1: " + str.tostring( kc1Lower, "#.##" ) + "\nTP2: " + str.tostring( kc1Median, "#.##" ) + "\nTP3: " + str.tostring( kc1Upper, "#.##" ) + "\nTP4: " + str.tostring( kc2Upper, "#.##" ) + "\nTP5: " + str.tostring( kc3Upper, "#.##" ) + "\n\nSL: " + str.tostring( kc5Lower, "#.##" ), style = label.style_label_up, color = color.rgb( 221, 240, 58 ), textcolor = color.black, textalign = text.align_center, text_font_family = font.family_monospace, size = size.normal )

    longEP := kc3Lower
    longSL := kc5Lower
    longTP1 := kc1Lower
    longTP2 := kc1Median
    longTP3 := kc1Upper
    longTP4 := kc2Upper
    longTP5 := kc3Upper
    longPos := 1
    longPosTP2 := 1
    longPosTP3 := 1
    longPosTP4 := 1
    longPosTP5 := 1
    longAlpEP := 0
    longAlpSL := 0
    longAlpTP1 := 0
    longAlpTP2 := 0
    longAlpTP3 := 0
    longAlpTP4 := 0
    longAlpTP5 := 0
    longTP1Ln := 1
    longTP2Ln := 1
    longTP3Ln := 1
    longTP4Ln := 1
    longTP5Ln := 1
    longSLLn := 1
    longBGtp1TR := 100
    longBGtp2TR := 100
    longBGtp3TR := 100
    longBGtp4TR := 100
    longBGtp5TR := 100
    longBGslTR := 100

if longPos == 1 and low < longSL
    longPosStop := true
    longPos := 0
    longPosTP2 := 0
    longPosTP3 := 0
    longPosTP4 := 0
    longPosTP5 := 0
else
    longPosStop := false

if longPos == 1 and high > longTP1
    longPosTakeProfit1 := true
    longPos := 0
else
    longPosTakeProfit1 := false

if longPosTP2 == 1 and high > longTP2
    longPosTakeProfit2 := true
    longPosTP2 := 0
else
    longPosTakeProfit2 := false

if longPosTP3 == 1 and high > longTP3
    longPosTakeProfit3 := true
    longPosTP3 := 0
else
    longPosTakeProfit3 := false

if longPosTP4 == 1 and high > longTP4
    longPosTakeProfit4 := true
    longPosTP4 := 0
else
    longPosTakeProfit4 := false

if longPosTP5 == 1 and high > longTP5
    longPosTakeProfit5 := true
    longPosTP5 := 0
else
    longPosTakeProfit5 := false

if high > longTP1 == true and longTP1Ln == 1
    longAlpTP1 := na, longBGtp1TR := 0
if high > longTP2 == true and longTP2Ln == 1
    longTP2Ln := 0, longAlpTP2 := na, longBGtp2TR := 0
if high > longTP3 == true and longTP3Ln == 1
    longTP3Ln := 0, longAlpTP3 := na, longBGtp3TR := 0
if high > longTP4 == true and longTP4Ln == 1
    longTP4Ln := 0, longAlpTP4 := na, longBGtp4TR := 0
if high > longTP5 == true and longTP5Ln == 1
    longTP5Ln := 0, longAlpEP := na, longAlpSL := na, longAlpTP1 := na, longAlpTP2 := na, longAlpTP3 := na, longAlpTP4 := na, longAlpTP5 := na, longBGtp5TR := 0
if low < longSL == true and longSLLn == 1
    longSLLn := 0, longAlpEP := na, longAlpSL := na, longAlpTP1 := na, longAlpTP2 := na, longAlpTP3 := na, longAlpTP4 := na, longAlpTP5 := na, longBGslTR := 0
// <--- LONG STRATEGY

// ---> SHORT STRATEGY
downCondtion1 = ta.crossover( kc3Upper, high )
downCondtion2 = ta.crossover( 165.0, cci )
downCondtion3 = 0.7 > roc and roc < 0.9
downCondtion4 = ta.crossunder( bop, -0.4 )
downCondtion5 = ta.crossover( rsi, 76 )
downCondtion6 = kc1Median > sar
downCondtion7 = ohlc4 > kc1Median

shortSignal = downCondtion1 and downCondtion2 and downCondtion3 and downCondtion4 and downCondtion6 and downCondtion7

if shortSignal
    shortLbl = label.new( x = bar_index, y = kc5Upper, text = "Short Signal\n\nTP1: " + str.tostring( kc1Upper, "#.##" ) + "\nTP2: " + str.tostring( kc1Median, "#.##" ) + "\nTP3: " + str.tostring( kc1Lower, "#.##" ) + "\nTP4: " + str.tostring( kc2Lower, "#.##" ) + "\nTP5: " + str.tostring( kc3Lower, "#.##" ) + "\n\nSL: " + str.tostring( kc5Upper, "#.##" ), style = label.style_label_down, color = color.rgb( 221, 240, 58 ), textcolor = color.black, textalign = text.align_center, text_font_family = font.family_monospace, size = size.normal )

    shortEP := kc3Upper
    shortSL := kc5Upper
    shortTP1 := kc1Upper
    shortTP2 := kc1Median
    shortTP3 := kc1Lower
    shortTP4 := kc2Lower
    shortTP5 := kc3Lower
    shortPos := 1
    shortPosTP2 := 1
    shortPosTP3 := 1
    shortPosTP4 := 1
    shortPosTP5 := 1
    shortAlpEP := 0
    shortAlpSL := 0
    shortAlpTP1 := 0
    shortAlpTP2 := 0
    shortAlpTP3 := 0
    shortAlpTP4 := 0
    shortAlpTP5 := 0
    shortTP1Ln := 1
    shortTP2Ln := 1
    shortTP3Ln := 1
    shortTP4Ln := 1
    shortTP5Ln := 1
    shortSLLn := 1
    shortBGtp1TR := 100
    shortBGtp2TR := 100
    shortBGtp3TR := 100
    shortBGtp4TR := 100
    shortBGtp5TR := 100
    shortBGslTR := 100

if shortPos == 1 and high > shortSL
    shortPosStop := true
    shortPos := 0
    shortPosTP2 := 0
    shortPosTP3 := 0
    shortPosTP4 := 0
    shortPosTP5 := 0
else
    shortPosStop := false

if shortPos == 1 and low < shortTP1
    shortPosTakeProfit1 := true
    shortPos := 0
else
    shortPosTakeProfit1 := false

if shortPosTP2 == 1 and low < shortTP2
    shortPosTakeProfit2 := true
    shortPosTP2 := 0
else
    shortPosTakeProfit2 := false

if shortPosTP3 == 1 and low < shortTP3
    shortPosTakeProfit3 := true
    shortPosTP3 := 0
else
    shortPosTakeProfit3 := false

if shortPosTP4 == 1 and low < shortTP4
    shortPosTakeProfit4 := true
    shortPosTP4 := 0
else
    shortPosTakeProfit4 := false

if shortPosTP5 == 1 and low < shortTP5
    shortPosTakeProfit5 := true
    shortPosTP5 := 0
else
    shortPosTakeProfit5 := false

if low < shortTP1 == true and shortTP1Ln == 1
    shortAlpTP1 := na, shortBGtp1TR := 0
if low < shortTP2 == true and shortTP2Ln == 1
    shortTP2Ln := 0, shortAlpTP2 := na, shortBGtp2TR := 0
if low < shortTP3 == true and shortTP3Ln == 1
    shortTP3Ln := 0, shortAlpTP3 := na, shortBGtp3TR := 0
if low < shortTP4 == true and shortTP4Ln == 1
    shortTP4Ln := 0, shortAlpTP4 := na, shortBGtp4TR := 0
if low < shortTP5 == true and shortTP5Ln == 1
    shortTP5Ln := 0, shortAlpEP := na, shortAlpSL := na, shortAlpTP1 := na, shortAlpTP2 := na, shortAlpTP3 := na, shortAlpTP4 := na, shortAlpTP5 := na, shortBGtp5TR := 0
if high > shortSL == true and shortSLLn == 1
    shortSLLn := 0, shortAlpEP := na, shortAlpSL := na, shortAlpTP1 := na, shortAlpTP2 := na, shortAlpTP3 := na, shortAlpTP4 := na, shortAlpTP5 := na, shortBGslTR := 0
// <--- SHORT STRATEGY

// ---> PLOT
demaPlot = plot( series = dema, color = color.rgb( red = 202, green = 204, blue = 202 ), linewidth = 1, display = display.all, title = "DEMA", editable = false )
// <--- PLOT

// ---> STRATEGY PLOT
bgcolor( longSignal ? color.new( color = color.lime, transp = 60 ) : na, title = "Background color for Long Signals", editable = false )
bgcolor( shortSignal ? color.rgb( red = 255, green = 0, blue = 0 ) : na, title = "Background color for Short Signals", editable = false )

plotshape( longSignal, title = "Long Signal", style = shape.triangleup, color = color.green, location = location.belowbar, text = "Long Signal", textcolor = color.white, size = size.normal )
plotshape( shortSignal, title = "Short Signal", style = shape.triangledown, color = color.red, location = location.abovebar, text = "Short Signal", textcolor = color.white, size = size.normal )

bgcolor( shortPosTakeProfit1 ? color.rgb( red = 116, green = 173,blue = 237 ) : na, title = "Background color for Short Signal TP", editable = false )
plotshape( shortPosTakeProfit1, title = "Short Signal TP1", style = shape.triangleup, color = color.rgb( red = 116, green = 173, blue = 237 ), location = location.belowbar, text = "Short Signal\nTP1", textcolor = color.rgb( red = 255, green = 255, blue = 255 ), size = size.normal )

bgcolor( shortPosTakeProfit2 ? color.rgb( red = 132, green = 113, blue = 209 ) : na, title = "Background color for Short Signal TP", editable = false )
plotshape( shortPosTakeProfit2, title = "Short Signal TP2", style = shape.triangleup, color = color.rgb( red = 116, green = 221, blue = 237 ), location = location.belowbar, text = "Short Signal\nTP2", textcolor = color.rgb( red = 255, green = 255, blue = 255 ), size = size.normal )

bgcolor( shortPosTakeProfit3 ? color.rgb( red = 75, green = 0, blue = 130 ) : na, title = "Background color for Short Signal TP", editable = false )
plotshape( shortPosTakeProfit3, title = "Short Signal TP3", style = shape.triangleup, color = color.rgb( red = 116, green = 221, blue = 237 ), location = location.belowbar, text = "Short Signal\nTP3", textcolor = color.rgb( red = 255, green = 255, blue = 255 ), size = size.normal )

bgcolor( shortPosTakeProfit4 ? color.rgb( red = 177, green = 113, blue = 209 ) : na, title = "Background color for Short Signal TP", editable = false )
plotshape( shortPosTakeProfit4, title = "Short Signal TP4", style = shape.triangleup, color = color.rgb( red = 116, green = 221, blue = 237 ), location = location.belowbar, text = "Short Signal\nTP4", textcolor = color.rgb( red = 255, green = 255, blue = 255 ), size = size.normal )

bgcolor( shortPosTakeProfit5 ? color.rgb( red = 238, green = 130, blue = 238 ) : na, title = "Background color for Short Signal TP", editable = false )
plotshape( shortPosTakeProfit5, title = "Short Signal TP5", style = shape.triangleup, color = color.rgb( red = 116, green = 221, blue = 237 ), location = location.belowbar, text = "Short Signal\nTP5", textcolor = color.rgb( red = 255, green = 255, blue = 255 ), size = size.normal )

bgcolor( shortPosStop ? color.new( color = #d976ec, transp = 60 ) : na, title = "Background color for Short Signal SL", editable = false )
plotshape( shortPosStop, title = "Short Signal Stop-Loss", style = shape.triangleup, color = color.rgb( red = 116, green = 221, blue = 237 ), location = location.belowbar, text = "Short Signal Stopped", textcolor = color.rgb( red = 255, green = 255, blue = 255 ), size = size.normal )

bgcolor( longPosTakeProfit1 ? color.rgb( red = 221, green = 240, blue = 58, transp = longBGtp1TR ) : na, title = "Background color for Long Signal TP", editable = false )
plotshape( longPosTakeProfit1, title = "Long Signal TP1", style = shape.triangledown, color = color.rgb( red = 116, green = 221, blue = 237, transp = longBGtp1TR ), location = location.abovebar, text = "Long Signal\nTP1", textcolor = color.rgb( red = 255, green = 255, blue = 255 ), size = size.normal )

bgcolor( longPosTakeProfit2 ? color.rgb( red = 255, green = 255, blue = 0, transp = longBGtp2TR ) : na, title = "Background color for Long Signal TP", editable = false )
plotshape( longPosTakeProfit2, title = "Long Signal TP2", style = shape.triangledown, color = color.rgb( red = 116, green = 221, blue = 237, transp = longBGtp2TR ), location = location.abovebar, text = "Long Signal\nTP2", textcolor = color.rgb( red = 255, green = 255, blue = 255 ), size = size.normal )

bgcolor( longPosTakeProfit3 ? color.rgb( red = 240, green = 218, blue = 58, transp = longBGtp3TR ) : na, title = "Background color for Long Signal TP", editable = false )
plotshape( longPosTakeProfit3, title = "Long Signal TP3", style = shape.triangledown, color = color.rgb( red = 116, green = 221, blue = 237, transp = longBGtp3TR ), location = location.abovebar, text = "Long Signal\nTP3", textcolor = color.rgb( red = 255, green = 255, blue = 255 ), size = size.normal )

bgcolor( longPosTakeProfit4 ? color.rgb( red = 255, green = 165, blue = 0, transp = longBGtp4TR ) : na, title = "Background color for Long Signal TP", editable = false )
plotshape( longPosTakeProfit4, title = "Long Signal TP4", style = shape.triangledown, color = color.rgb( red = 116, green = 221, blue = 237, transp = longBGtp4TR ), location = location.abovebar, text = "Long Signal\nTP4", textcolor = color.rgb( red = 255, green = 255, blue = 255 ), size = size.normal )

bgcolor( longPosTakeProfit5 ? color.rgb( red = 240, green = 133, blue = 8, transp = longBGtp5TR ) : na, title = "Background color for Long Signal TP", editable = false )
plotshape( longPosTakeProfit5, title = "Long Signal TP5", style = shape.triangledown, color = color.rgb( red = 116, green = 221, blue = 237, transp = longBGtp5TR ), location = location.abovebar, text = "Long Signal\nTP5", textcolor = color.rgb( red = 255, green = 255, blue = 255 ), size = size.normal )

bgcolor( longPosStop ? color.new( color = #d976ec, transp = longBGslTR ) : na, title = "Background color for Long Signal SL", editable = false )
plotshape( longPosStop, title = "Long Signal Stop-Loss", style = shape.triangledown, color = color.rgb( red = 116, green = 221, blue = 237, transp = longBGslTR ), location = location.abovebar, text = "Long Signal Stopped", textcolor = color.rgb( red = 255, green = 255, blue = 255 ), size = size.normal )

plot( shortEP, color = color.rgb( red = 116, green = 221, blue = 237, transp = shortAlpEP ), style = plot.style_steplinebr, linewidth = 3, offset = 0, title = "Short Signal Stop Loss level", editable = false, display = display.all )
plot( shortSL, color = color.rgb( red = 255, green = 0, blue = 0, transp = shortAlpSL ), style = plot.style_steplinebr, linewidth = 3, offset = 0, title = "Short Signal Stop Loss level", editable = false, display = display.all )
plot( shortTP1, color = color.rgb( red = 116, green = 173, blue = 237, transp = shortAlpTP1 ), style = plot.style_steplinebr, linewidth = 3, offset = 0, title = "Short Signal Stop Loss level", editable = false, display = display.all )
plot( shortTP2, color = color.rgb( red = 132, green = 113, blue = 209, transp = shortAlpTP2 ) , style = plot.style_steplinebr, linewidth = 3, offset = 0, title = "Short Signal Stop Loss level", editable = false, display = display.all )
plot( shortTP3, color = color.rgb( red = 75, green = 0, blue = 130, transp = shortAlpTP3 ), style = plot.style_steplinebr, linewidth = 3, offset = 0, title = "Short Signal Stop Loss level", editable = false, display = display.all )
plot( shortTP4, color = color.rgb( red = 177, green = 113, blue = 209, transp = shortAlpTP4 ), style = plot.style_steplinebr, linewidth = 3, offset = 0, title = "Short Signal Stop Loss level", editable = false, display = display.all )
plot( shortTP5, color = color.rgb( red = 238, green = 130, blue = 238, transp = shortAlpTP5 ), style = plot.style_steplinebr, linewidth = 3, offset = 0, title = "Short Signal Stop Loss level", editable = false, display = display.all )

plot( longEP, color = color.rgb( red = 116, green = 221, blue = 237, transp = longAlpEP ) , style = plot.style_steplinebr, linewidth = 3, offset = 0, title = "Long Signal Entry Level", editable = false, display = display.all )
plot( longSL, color = color.rgb( red = 255, green = 0, blue = 0, transp = longAlpSL ), style = plot.style_steplinebr, linewidth = 3, offset = 0, title = "Long Signal Stop Loss level", editable = false, display = display.all )
plot( longTP1, color = color.rgb( red = 221, green = 240, blue = 58, transp = longAlpTP1 ), style = plot.style_steplinebr, linewidth = 3, offset = 0, title = "Long Signal Take Profit 1 Level", editable = false, display = display.all )
plot( longTP2, color = color.rgb( red = 255, green = 255, blue = 0, transp = longAlpTP2 ), style = plot.style_steplinebr, linewidth = 3, offset = 0, title = "Long Signal Take Profit 2 Level", editable = false, display = display.all )
plot( longTP3, color = color.rgb( red = 240, green = 218, blue = 58, transp = longAlpTP3 ), style = plot.style_steplinebr, linewidth = 3, offset = 0, title = "Long Signal Take Profit 3 Level", editable = false, display = display.all )
plot( longTP4, color = color.rgb( red = 255, green = 165, blue = 0, transp = longAlpTP4 ), style = plot.style_steplinebr, linewidth = 3, offset = 0, title = "Long Signal Take Profit 4 Level", editable = false, display = display.all )
plot( longTP5, color = color.rgb( red = 240,green = 133, blue = 8, transp = longAlpTP5 ), style = plot.style_steplinebr, linewidth = 3, offset = 0, title = "Long Signal Take Profit 5 Level", editable = false, display = display.all )
// <--- STRATEGY PLOT