[APD] Sharegenius Swing Trading Strategy
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © AkhileshPDalvi

//@version=5
indicator(title='[APD] Sharegenius Swing Trading Method', shorttitle='[APD] SST', overlay=true)

// <<< User Input >>> {
lines = input.bool(defval=true, inline='1', title='Indicator | Show/Hide | Dashboard')
Dash = input.bool(defval=true, inline='1', title='')

tb1 = input.bool(group='Target 🎯', defval=true, title='', inline='1')
targetper1 = input.int(group='Target 🎯', defval=20, title='1st Target in %', minval=1, inline='1', tooltip='Based on trend change the Target')
tb2 = input.bool(group='Target 🎯', defval=true, title='', inline='2')
targetper2 = input.int(group='Target 🎯', defval=15, title='2nd Target in %', minval=1, inline='2', tooltip='Based on trend change the Target')
tb3 = input.bool(group='Target 🎯', defval=true, title='', inline='3')
targetper3 = input.int(group='Target 🎯', defval=10, title='3rd Target in %', minval=1, inline='3', tooltip='Based on trend change the Target')

histData = input.int(group='Dashboard Settings 🔨', defval=0, title='Historical Dashboard Readings', minval=0, tooltip='Enter number of bars back\nFor Example to check previous days Readings enter 1\n\nNote = Use Measure Tool to check bar no of particular date from current date.')

upperlength = input.int(group='SST Settings 🔨', defval=20, title='Upper Period', minval=1, tooltip='20 is recommended')
lowerlength = input.int(group='SST Settings 🔨', defval=20, title='Lower Period', minval=1, tooltip='20 is recommended')
capital = input.int(group='SST Settings 🔨', defval=10000, minval=1, title='Capital Per Trade', tooltip='For averaging keep Capital Per Trade same')

showRs = input.bool(group='RS Settings 🔨', defval=false ,title="Show RS", tooltip='Don\'t plot RS unless needed. It will affect the chart because of RS values.')
benchmark = input.symbol(group='RS Settings 🔨', defval="NSE:NIFTY",title="Benchmark")
scr = input.source(group='RS Settings 🔨', defval=close ,title="Source")
len = input.int(group='RS Settings 🔨', defval=55 ,title="RS Length", minval=1, step=1)
// ------------------------------------------------------------ }

// <<< SST Calculation >>> {
lower = ta.lowest(lowerlength)
upper = ta.highest(upperlength)
gtt = upper - upper * 0.05
target1 = upper + upper * (targetper1 / 100)
target2 = upper + upper * (targetper2 / 100)
target3 = upper + upper * (targetper3 / 100)
diff = (upper / close - 1) * 100
qty = upper <= capital ? math.ceil(capital / upper) : 0
// ------------------------------------------------------------ }

// <<< RS Calculation >>> {
baseSymbol = request.security(syminfo.tickerid, timeframe.period, scr)
comparativeSymbol = request.security(benchmark, timeframe.period, scr)
rs = (baseSymbol / baseSymbol[len] / (comparativeSymbol / comparativeSymbol[len]) - 1)
// ------------------------------------------------------------ }

// <<< Plotting SST >>> {
P1 = plot(lines ? lower : na, title='Lower level', color=color.new(color.red, 0), linewidth=1)
P2 = plot(lines ? gtt : na, title='Start GTT', color=color.new(color.blue, 0), linewidth=1)
P3 = plot(lines ? upper : na, title='GTT Price', color=color.new(color.blue, 0), linewidth=2)
T1 = plot(lines and tb1 ? target1 : na, title='1st Target', color=color.new(color.lime,0), linewidth=2)
T2 = plot(lines and tb2 ? target2 : na, title='2nd Target', color=color.new(color.lime,50), linewidth=2)
T3 = plot(lines and tb3 ? target3 : na, title='3rd Target', color=color.new(color.lime,70), linewidth=2)
fill(P2, P3, color.new(color.blue, 90), title='Buy zone')
fill(T1, T3, color.new(color.lime, 95), title='Target zone')  
// ------------------------------------------------------------ }

// <<< Plotting RS >>> {
plot(showRs ? rs : na, title='RS', color=color.red, linewidth=2)
// ------------------------------------------------------------ }

// <<< Plotting Dashboard >>> {
Dash_tooltip = 'Note =' + '\n 1] This Script is for educational purposes only. Nothing in this content should be interpreted as financial advice or a recommendation to buy or sell any sort of security or investment.' + '\n 2] Trade in fundamentally good stocks & strong stocks those are outperforming index because we do not take stoploss in this strategy.' + '\n\n How to use this indicator = ' + '\n 1] Track the 20-Day Low of instrument.' + '\n 2] Create a GTT order which is 5% above 20 DL.' + '\n 3] If the share makes a new 20 DL before getting purchased, then update your GTT order to be 5% above new 20 DL.' + '\n 4] Next GTT will be started when average price falls by 10%. (GTT will be created as 5% above last 20 DL or Buy on 10%, 20%, 30% fall in average price.' + '\n 5] Sell target is 5% of average price. Sell all units or Set sell target on buy price.' + '\n 6] No stop loss needed as we buy when the stock falls.'

if Dash == true
    var table gttTable = table.new(position.top_right, 8, 6, bgcolor=color.white, frame_color=color.white, frame_width=2, border_width=2)

    if barstate.islast
        table.cell(table_id = gttTable, column = 0, row = 0, text = "Sharegenius Swing Trading Dashboard", text_halign=text.align_center, tooltip=Dash_tooltip)
        table.cell(table_id = gttTable, column = 0, row = 1, text = histData > 0 ? str.tostring(histData) + ' Bars ago' : 'Latest Value', text_halign=text.align_center, bgcolor=color.new(color.blue,10), text_color=color.white)
        table.cell(table_id = gttTable, column = 0, row = 4, text = 'With Love ❤️ by Akhilesh P Dalvi', text_halign=text.align_center, tooltip='Special Thanks 🙏 To :-\n1] Mahesh Kaushik For Strategy \n2] F.I.R.E. in India For Enhanced SST.')        
        table.merge_cells(gttTable, 0, 0, 7, 0)
        table.merge_cells(gttTable, 0, 1, 7, 1)
        table.merge_cells(gttTable, 0, 4, 7, 4)
        table.cell(table_id = gttTable, column = 0, row = 2, text = str.upper(syminfo.type) + " NAME", bgcolor=color.new(color.black,10), text_color=color.white)
        table.cell(table_id = gttTable, column = 0, row = 3, text = syminfo.ticker, bgcolor=color.new(color.gray,0), text_color=color.white)
    
        table.cell(table_id = gttTable, column = 1, row = 2, text = "CMP", bgcolor=color.new(color.black,10), text_color=color.white)
        table.cell(table_id = gttTable, column = 1, row = 3, text = str.tostring(math.round(close[histData],2)), bgcolor=color.gray, text_color=color.white)
    
        table.cell(table_id = gttTable, column = 2, row = 2, text = "ACTION", bgcolor=color.new(color.black,10), text_color=color.white)
        table.cell(table_id = gttTable, column = 2, row = 3, text =  (close[histData] > gtt ? 'Create GTT Order' : 'Wait & Watch'), bgcolor=(close[histData] > gtt ? color.green : color.blue), text_color=color.white)
    
        table.cell(table_id = gttTable, column = 3, row = 2, text = "GTT", bgcolor=color.new(color.black,10), text_color=color.white)
        table.cell(table_id = gttTable, column = 3, row = 3, text = str.tostring(math.round(upper[histData],2)), bgcolor=color.blue, text_color=color.white)
        
        table.cell(table_id = gttTable, column = 4, row = 2, text = "UPDATE GTT", bgcolor=color.new(color.black,10), text_color=color.white)
        table.cell(table_id = gttTable, column = 4, row = 3, text = gtt[histData] < gtt[histData + 1] or gtt[histData] > gtt[histData + 1] ? 'YES' : 'NO', bgcolor= gtt[histData] < gtt[histData + 1] or gtt[histData] > gtt[histData + 1] ? color.green : color.gray, text_color=color.white)
        
        table.cell(table_id = gttTable, column = 5, row = 2, text = "DIFF", bgcolor=color.new(color.black,10), text_color=color.white, tooltip= 'Diff in ' + str.tostring(upperlength) + ' DH & CMP')
        table.cell(table_id = gttTable, column = 5, row = 3, text = str.tostring(math.round(diff[histData], 2)) + " %", bgcolor= math.round(diff[histData], 2) < 5 ? color.green : color.gray, text_color=color.white, tooltip= 'Diff in ' + str.tostring(upperlength) + ' DH & CMP')
    
        table.cell(table_id = gttTable, column = 6, row = 2, text = "QTY", bgcolor=color.new(color.black,10), text_color=color.white, tooltip= 'Capital Per Trade is ' + str.tostring(capital))
        table.cell(table_id = gttTable, column = 6, row = 3, text = str.tostring(qty[histData]), bgcolor= color.gray, text_color=color.white, tooltip= 'Capital Per Trade is ' + str.tostring(capital))
    
        table.cell(table_id = gttTable, column = 7, row = 2, text = "RS " + str.tostring(len), bgcolor=color.new(color.black,10), text_color=color.white)
        table.cell(table_id = gttTable, column = 7, row = 3, text = str.tostring(math.round(rs[histData],2)), bgcolor= color.gray, text_color=color.white)
// ------------------------------------------------------------ }