Daily Investments Index Scalp
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © nedled

//@version=5
strategy("Daily Investments Index Scalp", overlay=true,format=format.price,precision = 2, pyramiding = 0, initial_capital = 1000, default_qty_value = 100, default_qty_type = strategy.percent_of_equity)

var entryPrice = 0

StoplossType = input.string(title="Stoploss Type", defval="SWING", options=["ATR", "SWING", "PERCENTAGE"])
takeProfitType = input.string(title="Take Profit Type", defval="ATR", options=["ATR", "SWING", "PERCENTAGE"])

SwingLenght = input.int(title="Swing Stoploss Lookback Candles",defval = 15) 


shortLength =  input.int(title="Short Length",defval = 3) 
mediumLength = input.int(title="Medium Length",  defval=8)
longLength = input.int(title="Long Length",  defval=20)
src = input.source(title="source", defval = close)

medium = ta.sma(src,mediumLength)
short = ta.sma(src,shortLength) / medium
long = ta.sma(src,longLength) / medium

ema = input.int(150,title = "Ema Long Lenght")
emaShort = input.int(35,title = "Ema Short Length")

//multi profit
multiplierTP1 = input.float(1.5,"TP - ATR Multiplier")
multiplierTP2 = input.float(3,"TP - ATR Multiplier")
RRTP1 = input.float(1.2,"TP - TP RR")
RRTP2 = input.float(2,"TP - TP RR")
multiplierSL = input.float(1.5,"SL - ATR Multiplier")

emaValue = ta.ema(src,ema)
emaShortValue = ta.ema(src,emaShort)

plot(emaValue, color= color.red)
plot(emaShortValue, color = color.blue)


lengthAtr = input.int(title="ATR Length", defval=14,minval = 1)
smoothingAtr = input.string(title="Smoothing ATR", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
src1 = input(high)
src2 = input(low)

ma_function(source, length) =>
	if smoothingAtr == "RMA"
		ta.rma(source, length)
	else
		if smoothingAtr == "SMA"
			ta.sma(source, length)
		else
			if smoothingAtr == "EMA"
				ta.ema(source, length)
			else
				ta.wma(source, length)


slShort = 0.0
slLong = 0.0
TP_Short_1 = 0.0
TP_Short_2 = 0.0
TP_Long_1 = 0.0
TP_Long_2 = 0.0

if(StoplossType == "ATR")
    slShort := ma_function(ta.tr(true), lengthAtr) * multiplierSL + src1
    slLong := src2 - ma_function(ta.tr(true), lengthAtr) * multiplierSL
else if(StoplossType == "SWING")
    slShort :=   ta.highest(high,SwingLenght)
    slLong  := ta.lowest(low, SwingLenght)


if(takeProfitType == "ATR")
    TP_Short_1 := src2 - ma_function(ta.tr(true), lengthAtr) * multiplierTP1
    TP_Short_2 := src2 - ma_function(ta.tr(true), lengthAtr) * multiplierTP2

    TP_Long_1 := ma_function(ta.tr(true), lengthAtr) * multiplierTP1 + src1
    TP_Long_2 := ma_function(ta.tr(true), lengthAtr) * multiplierTP2 + src1
else if(takeProfitType == "SWING")
    TP_Short_1 := close - ((ta.highest(high,SwingLenght) - close)* RRTP1)
    TP_Short_2 := close - ((ta.highest(high,SwingLenght) - close)* RRTP2)

    TP_Long_1 :=  close + ((close - ta.lowest(low, SwingLenght) ) * RRTP1)
    TP_Long_2 := close + ((close - ta.lowest(low, SwingLenght)  ) * RRTP2)



bool goLong = false
if(ta.crossover(short,long) and close > emaValue and emaShortValue > emaValue and strategy.position_size <= 0)
	goLong := true
	strategy.close_all()
    strategy.entry("Long", strategy.long, stop = slLong, alert_message = "Entered long position")
    strategy.exit("Exit Long TP1", from_entry = "Long", limit = TP_Long_1, qty_percent = 50, stop = slLong)
	strategy.exit("Exit Long TP2", from_entry = "Long", limit = TP_Long_2, stop = slLong)



bool goShort = false
if(ta.crossunder(short,long) and close < emaValue and  emaShortValue < emaValue and strategy.position_size >= 0)
	goShort := true
	strategy.close_all()
    strategy.entry("Short", strategy.short, stop = slShort, alert_message = "Entered short position")
	strategy.exit("Exit Short TP1", from_entry = "Short", limit= TP_Short_1, qty_percent = 50, stop = slShort)
	strategy.exit("Exit Short TP2", from_entry = "Short", limit= TP_Short_2, stop = slShort)



long_stop_level = float(na)
long_stop_level := goLong ? slLong : long_stop_level[1]

long_limit1_level = float(na)
long_limit1_level := goLong ? TP_Long_1 : long_limit1_level[1]
long_limit2_level = float(na)
long_limit2_level := goLong ? TP_Long_2 : long_limit2_level[1]


short_stop_level = float(na)
short_stop_level := goShort ?  slShort : short_stop_level[1]
short_limit1_level = float(na)
short_limit1_level := goShort ? TP_Short_1 : short_limit1_level[1]
short_limit2_level = float(na)
short_limit2_level := goShort ? TP_Short_2 : short_limit2_level[1]

getCurrentStageLong() =>
    var stage = 0
    if strategy.position_size == 0 
        stage := 0
    if stage == 0 and strategy.position_size > 0
        stage := 1
    else if stage == 1 and long_limit1_level[1] < high
		stage := 2

    stage

getCurrentStageShort() =>
    var stage = 0
    if strategy.position_size == 0 
        stage := 0
    if stage == 0 and strategy.position_size < 0
        stage := 1
    else if stage == 1 and short_limit1_level[1] > low
		stage := 2
    

    stage


curLongStage = getCurrentStageLong()

if(curLongStage == 2)
	strategy.exit("Exit Long TP2", from_entry = "Long", limit = long_limit2_level, stop = strategy.opentrades.entry_price(strategy.opentrades - 1))

curShortStage = getCurrentStageShort()

if(curShortStage == 2)
	strategy.exit("Exit Short TP2", from_entry = "Short", limit = short_limit2_level, stop = strategy.opentrades.entry_price(strategy.opentrades - 1))


print(txt) =>
    // Create label on the first bar.
    var lbl = label.new(bar_index, na, txt, xloc.bar_index, yloc.price, color(na), label.style_none, color.gray, size.large, text.align_left)
    // On next bars, update the label's x and y position, and the text it displays.
    label.set_xy(lbl, bar_index, ta.highest(10)[1])
    label.set_text(lbl, txt)


plot(strategy.position_size > 0 ?  long_stop_level : na,title= "SL LONG", color = color.red, style = plot.style_steplinebr)
plot(strategy.position_size > 0 ?  long_limit1_level : na,title= "TP1 LONG", color = color.green, style = plot.style_steplinebr)
plot(strategy.position_size > 0 ?  long_limit2_level : na,title= "TP2 LONG", color = color.green, style = plot.style_steplinebr)

plot(strategy.position_size < 0 ?  short_stop_level : na,title= "SL SHORT", color = color.yellow, style = plot.style_steplinebr)
plot(strategy.position_size < 0 ?  short_limit1_level : na,title= "TP1 SHORT", color = color.green, style = plot.style_steplinebr)
plot(strategy.position_size < 0 ?  short_limit2_level : na,title= "TP2 SHORT", color = color.green, style = plot.style_steplinebr)

// Table Results
//tableBgcolor=#686868
//if barstate.islastconfirmedhistory
  //  var tbl = table.new(position.bottom_right, 3, 3, bgcolor = tableBgcolor, frame_width = 2, frame_color = color.black, border_width=2,border_color=color.black)
    //table.cell(tbl, 0, 1, "Total Trades: " + str.tostring(strategy.closedtrades),           bgcolor=color.gray,text_size=size.small,text_color=color.white,text_valign=text.align_center)
    //table.cell(tbl, 0, 2, "Win Rate: " +  str.tostring(math.round(strategy.wintrades / strategy.closedtrades * 100,2)) + "%", bgcolor=color.gray,text_size=size.small,text_color=color.white,text_valign=text.align_center)
    //table.cell(tbl, 0, 0, "Initial Capital: " + str.tostring(strategy.initial_capital),     bgcolor=color.gray,text_size=size.small,text_color=color.white,text_valign=text.align_center)
    //table.cell(tbl, 1, 0, "Net Profit: "  + str.tostring(math.round(strategy.netprofit,2)) + " ("+  str.tostring(math.round(strategy.netprofit / strategy.initial_capital * 100,2)) + "%"+ ")", bgcolor=strategy.netprofit>0?color.teal:color.maroon,text_size=size.small,text_color=color.white,text_valign=text.align_center)
    //table.cell(tbl, 1, 1, "Current equity: "+ str.tostring(math.round(strategy.equity,2))+ "",    bgcolor=strategy.netprofit>0?color.teal:color.red,text_size=size.small,text_color=color.white,text_valign=text.align_center)
    //table.cell(tbl, 1, 2, "Max DrawDown: "+ str.tostring(math.round(strategy.max_drawdown))+ " ("+ str.tostring(math.round(strategy.max_drawdown/ strategy.initial_capital * 100,2))+ "%"+ ")",     bgcolor=color.maroon,text_size=size.small,text_color=color.white,text_valign=text.align_center)

