11. Testing 1st 2nd Indi - idavidov13/Pine-Script GitHub Wiki

Bulls vs Bears (23/380/80) + Polychromatic Momentum Indicator (close/8/2) 2.5ATR

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ivandavidov13

//@version=5
strategy('1st and 2nd indi with fixed TP/SL', overlay=true, currency=currency.USD, initial_capital=25000, pyramiding=1)

//==============================================================================
//INSERT SECTION
//This section is where users will be required to insert the indicators they
//would like to use for their NNFX Strategy.
//==============================================================================
//INSERT - CONFIRMATION INDICATOR 1
//==============================================================================
// Input settings
len = input.int(title="BvB Period", defval=23, minval=1)
bars_back = input.int(title="Normalized Bars Back", defval=380, minval=1)
tline = input.int(80, title="Line Height")

// Calculation
ma = ta.ema(close, len)
bulls = high - ma
bears = ma - low

// Normalize the values between -100 and 100
min_bulls = ta.lowest(bulls, bars_back)
max_bulls = ta.highest(bulls, bars_back)
norm_bulls = ((bulls - min_bulls) / (max_bulls - min_bulls) - 0.5) * 100

min_bears = ta.lowest(bears, bars_back)
max_bears = ta.highest(bears, bars_back)
norm_bears = ((bears - min_bears) / (max_bears - min_bears) - 0.5) * 100

// Calculate the total and add signals
total = norm_bulls - norm_bears
bullish_o = total > tline
bearish_o = total < -tline


//Insert indicator conditions as Trigger
C1_L_Trigger = ta.crossover(total, 0)
C1_S_Trigger = ta.crossunder(total, 0)
//==============================================================================
//INSERT - CONFIRMATION INDICATOR 2
//==============================================================================
// User inputs
src = input(close, title="Source")
secondlen = input.int(14, title="Length", minval=1)
enable_alert = input.bool(title="Enable Alert?", defval=true)

// Calculation of Polychromatic Momentum Indicator
mom = src - src[secondlen]
pmi = mom - (mom - mom[secondlen]) / secondlen

// Smoothed PMI
smoothing = input.int(2, title="Smoothing", minval=1)
sm_pmi = ta.sma(pmi, smoothing)

// Alert condition
alertLevel = input.float(title="Alert Level", defval=0)
alertMessage = "PMI is crossing the alert level " + str.tostring(alertLevel)
if enable_alert and ta.sma(pmi, 1)[1] < alertLevel and ta.sma(pmi, 1) > alertLevel
    alert(alertMessage)

// Plotting
plot(sm_pmi, title="PMI", color=color.blue, linewidth=2, transp=0)
hline(0, title="Zero Line", color=color.gray, linestyle=hline.style_dotted)
bgcolor(sm_pmi > 0 ? color.new(color.green, 90) : color.new(color.red, 90))


//Insert indicator conditions as filter
C2_L_Filter = sm_pmi > 0
C2_S_Filter = sm_pmi < 0
//==============================================================================
//INSERT - EXIT INDICATOR
//==============================================================================
//Insert indicator variables
//Using indicator 1 as exit indicator - no additional indicators to be inserted 
//in this example. If you wish to use another indicator, insert indicator here.

//Insert indicator conditions as exit trigger - in this example, using C1 as
//exit indicator. If you wish to use another indicator, insert indicator above,
//and provide trigger (either crossover or crossunder) conditions here.
Exit_L = C1_S_Trigger
Exit_S = C1_L_Trigger

//==============================================================================
//COMPLETED SECTION
//This section has been optimised to work with the above indicators the user
//has inserted above. The user does not require to change any code below and
//is completed and optimised for the full NNFX strategy. Users may wish to 
//customise this section of code if they wish to alter the NNFX strategy.
//==============================================================================
//COMPLETE - BACKTEST DATE RANGE
//==============================================================================
//Define variables
Start_D = input.int (   1, "Start Day",   1, 31)
Start_M = input.int (   1, "Start Month", 1, 12)
Start_Y = input.int (2019, "Start Year"        )
End_D   = input.int (   1, "End Day",     1, 31)
End_M   = input.int (   1, "End Month",   1, 12)
End_Y   = input.int (2024, "End Year"          )

//Define start and end time stamps
Start_Date = timestamp(Start_Y, Start_M, Start_D, 00, 00, 00)
End_Date   = timestamp(End_Y,     End_M,   End_D, 00, 00, 00)

//Date Range Condition
Date_Range() =>
    if time >= Start_Date and time <= End_Date
        true
    else
        false

//==============================================================================
//COMPLETE - ATR MONEY MANAGEMENT
//==============================================================================
//Declair money management variables
Risk_Percent     = input.float(title='Percent Risk Per Trade', defval=0.02, minval=0.001, maxval=1)
Atr_Multi_Profit = input.float(title='Atr Profit Multiple',    defval=2.5,  minval=0.1, step=0.5)
Atr_Multi_Loss   = input.float(title='Atr Loss Multiple',      defval=1.5,  minval=0.1, step=0.1)

//ATR indicator
Atr_length = input.int(title="ATR Length", defval=14, minval=1)
Atr_smoothing = input.string(title="ATR Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
ma_function(source, length) =>
	switch Atr_smoothing
		"RMA" => ta.rma(source, length)
		"SMA" => ta.sma(source, length)
		"EMA" => ta.ema(source, length)
		=> ta.wma(source, length)

Atr = ma_function(ta.tr(true), Atr_length)

//Calculate position size based off initial account capital, risk per trade and atr distance
Position_Size = math.round(strategy.initial_capital * Risk_Percent / (Atr_Multi_Loss * Atr))

//==============================================================================
//COMPLETE - ENTRY CONDITIONS
//==============================================================================
//Insert Long and Short conditions for C1 Trigger, all other indicators are filters
Condition_L_1 = strategy.position_size <= 0 and Date_Range() and C1_L_Trigger
Condition_S_1 = strategy.position_size >= 0 and Date_Range() and C1_S_Trigger

//Plot long and short conditions
plotshape(Condition_L_1, color=color.rgb(255, 0, 255), style=shape.labelup,   location=location.belowbar)
plotshape(Condition_S_1, color=color.rgb(255, 0, 255), style=shape.labeldown, location=location.abovebar)

//==============================================================================
//COMPLETE - ENTRY ORDERS
//==============================================================================
//Compile all long and short entry conditions
Entry_Long  = Condition_L_1 and C2_L_Filter
Entry_Short = Condition_S_1 and C2_S_Filter

//Submit long and short orders based on entry conditions
if Entry_Long
    strategy.entry(id='Entry_L_1', comment = "Long Entry",  direction=strategy.long,  qty=Position_Size)
    
if Entry_Short
    strategy.entry(id='Entry_S_1', comment = "Short Entry", direction=strategy.short, qty=Position_Size)

//==============================================================================
//COMPLETE - TAKE PROFIT AND STOP LOSS CONDITIONS
//==============================================================================
//Store Price on new entry signal
Entry_Price = strategy.opentrades.entry_price(strategy.opentrades - 1)

//Store ATR value on new entry signal
Entry_Atr = float(0.0)
if strategy.position_size == 0 or Entry_Long or Entry_Short
    Entry_Atr := Atr
else
    Entry_Atr := Entry_Atr[1]

//Calculate stop loss and take profit distance (in price)
Risk_Profit = Entry_Atr * Atr_Multi_Profit
Risk_Loss   = Entry_Atr * Atr_Multi_Loss 

// Take profit level calculation
Profit_L = float(na)
Profit_S = float(na)

// Stop loss level calculation
Stop_L = float(na)
Stop_S = float(na)

if strategy.position_size > 0  // For Long Trades
    Profit_L := Entry_Price + Risk_Profit
    Stop_L := Entry_Price - Risk_Loss
else
    Profit_L := na
    Stop_L := na

if strategy.position_size < 0  // For Short Trades
    Profit_S := Entry_Price - Risk_Profit
    Stop_S := Entry_Price + Risk_Loss
else
    Profit_S := na
    Stop_S := na

// Plot profit and stop loss levels for long and short trades
plot(Profit_L, color=color.lime, style=plot.style_linebr, linewidth=2)
plot(Profit_S, color=color.lime, style=plot.style_linebr, linewidth=2)
plot(Stop_L, color=color.red, style=plot.style_linebr, linewidth=2)
plot(Stop_S, color=color.red, style=plot.style_linebr, linewidth=2)

//==============================================================================
//COMPLETE - EXIT ORDERS
//==============================================================================
// Exit long trades
strategy.exit(id = 'Exit_L_1', from_entry ='Entry_L_1', comment='Long Exit 1',  stop = Stop_L, limit = Profit_L)

// Exit short trades
strategy.exit(id = 'Exit_S_1', from_entry ='Entry_S_1', comment='Short Exit 1', stop = Stop_S, limit = Profit_S)

//==============================================================================
//COMPLETE - CLOSE ORDERS
//==============================================================================
// Close long trades
if Exit_L
    strategy.close(id='Entry_L_1', comment='EXIT INDICATOR')

// Close short trades
if Exit_S
    strategy.close(id='Entry_S_1', comment='EXIT INDICATOR')

//==============================================================================

image

SSL (9/9) + Polychromatic Momentum Indicator (close/7/2) 2.5ATR

image

SSL (9/9) + Polychromatic Momentum Indicator (ohlc4/7/2) 2.5ATR

image

SSL (9/9) + Polychromatic Momentum Indicator (hlcc4/7/2) 2.5ATR

image

Vortex (10) + Detrended Price Oscillator (13)

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ivandavidov13

//@version=5
strategy('1st and 2nd indi with fixed TP/SL', overlay=true, currency=currency.USD, initial_capital=25000, pyramiding=1)

//==============================================================================
//INSERT SECTION
//This section is where users will be required to insert the indicators they
//would like to use for their NNFX Strategy.
//==============================================================================
//INSERT - CONFIRMATION INDICATOR 1
//==============================================================================
//Insert indicator variables
first_period_ = input.int(14, title="Length", minval=2)
VMP = math.sum( math.abs( high - low[1]), first_period_ )
VMM = math.sum( math.abs( low - high[1]), first_period_ )
STR = math.sum( ta.atr(1), first_period_ )
VIP = VMP / STR
VIM = VMM / STR
plot(VIP, title="VI +", color=#2962FF)
plot(VIM, title="VI -", color=#E91E63)

//Insert indicator conditions as Trigger
C1_L_Trigger = ta.crossover(VIP, VIM)
C1_S_Trigger = ta.crossover(VIM, VIP)
//==============================================================================
//INSERT - CONFIRMATION INDICATOR 2
//==============================================================================
// User inputs
period_ = input.int(21, title="Length", minval=1)
isCentered = input(false, title="Centered")
barsback = period_/2 + 1
ma = ta.sma(close, period_)
dpo = isCentered ? close[barsback] - ma : close - ma[barsback]
plot(dpo, offset = isCentered ? -barsback : 0, title="Detrended Price Oscillator", color=#43A047)
hline(0, title="Zero Line", color = #787B86)

//Insert indicator conditions as filter
C2_L_Filter = dpo > 0
C2_S_Filter = dpo < 0
//==============================================================================
//INSERT - EXIT INDICATOR
//==============================================================================
//Insert indicator variables
//Using indicator 1 as exit indicator - no additional indicators to be inserted 
//in this example. If you wish to use another indicator, insert indicator here.

//Insert indicator conditions as exit trigger - in this example, using C1 as
//exit indicator. If you wish to use another indicator, insert indicator above,
//and provide trigger (either crossover or crossunder) conditions here.
Exit_L = C1_S_Trigger
Exit_S = C1_L_Trigger

//==============================================================================
//COMPLETED SECTION
//This section has been optimised to work with the above indicators the user
//has inserted above. The user does not require to change any code below and
//is completed and optimised for the full NNFX strategy. Users may wish to 
//customise this section of code if they wish to alter the NNFX strategy.
//==============================================================================
//COMPLETE - BACKTEST DATE RANGE
//==============================================================================
//Define variables
Start_D = input.int (   1, "Start Day",   1, 31)
Start_M = input.int (   1, "Start Month", 1, 12)
Start_Y = input.int (2019, "Start Year"        )
End_D   = input.int (   1, "End Day",     1, 31)
End_M   = input.int (   1, "End Month",   1, 12)
End_Y   = input.int (2024, "End Year"          )

//Define start and end time stamps
Start_Date = timestamp(Start_Y, Start_M, Start_D, 00, 00, 00)
End_Date   = timestamp(End_Y,     End_M,   End_D, 00, 00, 00)

//Date Range Condition
Date_Range() =>
    if time >= Start_Date and time <= End_Date
        true
    else
        false

//==============================================================================
//COMPLETE - ATR MONEY MANAGEMENT
//==============================================================================
//Declair money management variables
Risk_Percent     = input.float(title='Percent Risk Per Trade', defval=0.02, minval=0.001, maxval=1)
Atr_Multi_Profit = input.float(title='Atr Profit Multiple',    defval=2.5,  minval=0.1, step=0.5)
Atr_Multi_Loss   = input.float(title='Atr Loss Multiple',      defval=1.5,  minval=0.1, step=0.1)

//ATR indicator
Atr_length = input.int(title="ATR Length", defval=14, minval=1)
Atr_smoothing = input.string(title="ATR Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
ma_function(source, length) =>
	switch Atr_smoothing
		"RMA" => ta.rma(source, length)
		"SMA" => ta.sma(source, length)
		"EMA" => ta.ema(source, length)
		=> ta.wma(source, length)

Atr = ma_function(ta.tr(true), Atr_length)

//Calculate position size based off initial account capital, risk per trade and atr distance
Position_Size = math.round(strategy.initial_capital * Risk_Percent / (Atr_Multi_Loss * Atr))

//==============================================================================
//COMPLETE - ENTRY CONDITIONS
//==============================================================================
//Insert Long and Short conditions for C1 Trigger, all other indicators are filters
Condition_L_1 = strategy.position_size <= 0 and Date_Range() and C1_L_Trigger
Condition_S_1 = strategy.position_size >= 0 and Date_Range() and C1_S_Trigger

//Plot long and short conditions
plotshape(Condition_L_1, color=color.rgb(255, 0, 255), style=shape.labelup,   location=location.belowbar)
plotshape(Condition_S_1, color=color.rgb(255, 0, 255), style=shape.labeldown, location=location.abovebar)

//==============================================================================
//COMPLETE - ENTRY ORDERS
//==============================================================================
//Compile all long and short entry conditions
Entry_Long  = Condition_L_1 and C2_L_Filter
Entry_Short = Condition_S_1 and C2_S_Filter

//Submit long and short orders based on entry conditions
if Entry_Long
    strategy.entry(id='Entry_L_1', comment = "Long Entry",  direction=strategy.long,  qty=Position_Size)
    
if Entry_Short
    strategy.entry(id='Entry_S_1', comment = "Short Entry", direction=strategy.short, qty=Position_Size)

//==============================================================================
//COMPLETE - TAKE PROFIT AND STOP LOSS CONDITIONS
//==============================================================================
//Store Price on new entry signal
Entry_Price = strategy.opentrades.entry_price(strategy.opentrades - 1)

//Store ATR value on new entry signal
Entry_Atr = float(0.0)
if strategy.position_size == 0 or Entry_Long or Entry_Short
    Entry_Atr := Atr
else
    Entry_Atr := Entry_Atr[1]

//Calculate stop loss and take profit distance (in price)
Risk_Profit = Entry_Atr * Atr_Multi_Profit
Risk_Loss   = Entry_Atr * Atr_Multi_Loss 

// Take profit level calculation
Profit_L = float(na)
Profit_S = float(na)

// Stop loss level calculation
Stop_L = float(na)
Stop_S = float(na)

if strategy.position_size > 0  // For Long Trades
    Profit_L := Entry_Price + Risk_Profit
    Stop_L := Entry_Price - Risk_Loss
else
    Profit_L := na
    Stop_L := na

if strategy.position_size < 0  // For Short Trades
    Profit_S := Entry_Price - Risk_Profit
    Stop_S := Entry_Price + Risk_Loss
else
    Profit_S := na
    Stop_S := na

// Plot profit and stop loss levels for long and short trades
plot(Profit_L, color=color.lime, style=plot.style_linebr, linewidth=2)
plot(Profit_S, color=color.lime, style=plot.style_linebr, linewidth=2)
plot(Stop_L, color=color.red, style=plot.style_linebr, linewidth=2)
plot(Stop_S, color=color.red, style=plot.style_linebr, linewidth=2)

//==============================================================================
//COMPLETE - EXIT ORDERS
//==============================================================================
// Exit long trades
strategy.exit(id = 'Exit_L_1', from_entry ='Entry_L_1', comment='Long Exit 1',  stop = Stop_L, limit = Profit_L)

// Exit short trades
strategy.exit(id = 'Exit_S_1', from_entry ='Entry_S_1', comment='Short Exit 1', stop = Stop_S, limit = Profit_S)

//==============================================================================
//COMPLETE - CLOSE ORDERS
//==============================================================================
// Close long trades
if Exit_L
    strategy.close(id='Entry_L_1', comment='EXIT INDICATOR')

// Close short trades
if Exit_S
    strategy.close(id='Entry_S_1', comment='EXIT INDICATOR')

//==============================================================================

image

Twiggs Money Flow (12) + Recursive Median Filter (7/9/close)

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © ivandavidov13

//@version=5
strategy('1st and 2nd indi with fixed TP/SL', overlay=true, currency=currency.USD, initial_capital=25000, pyramiding=1)

//==============================================================================
//INSERT SECTION
//This section is where users will be required to insert the indicators they
//would like to use for their NNFX Strategy.
//==============================================================================
//INSERT - CONFIRMATION INDICATOR 1
//==============================================================================
// Function for calculating the WiMA (Weighted Moving Average)
WiMA(src, length) =>
    var MA_s = 0.0
    MA_s := (src + nz(MA_s[1]) * (length - 1)) / length
    MA_s

// Inputs
length = input.int(12, title="Period")

// Calculating values for Twiggs Money Flow
tr_h = math.max(close[1], high)
tr_l = math.min(close[1], low)
tr_c = tr_h - tr_l
adv = volume * ((close - tr_l) - (tr_h - close)) / (tr_c == 0 ? 9999999 : tr_c)
wv = volume + volume[1]
wmV = WiMA(wv, length)
wmA = WiMA(adv, length)
tmf = wmV == 0 ? 0 : wmA / wmV
tmff = tmf > 0.2499 ? tmf : 0
tmfm = tmf < -0.2499 ? tmf : 0

// Plotting
plot(tmf, color=color.aqua, style=plot.style_area)
plot(tmff, color=color.green, style=plot.style_area)
plot(tmfm, color=color.red, style=plot.style_area)

// Horizontal line at zero
hline(0, "Zero Line", color=color.gray)

//Insert indicator conditions as Trigger
C1_L_Trigger = ta.crossover(tmf, 0)
C1_S_Trigger = ta.crossunder(tmf, 0)
//==============================================================================
//INSERT - CONFIRMATION INDICATOR 2
//==============================================================================
//Insert indicator variables
medianLength = input.int(title="Median Length", defval=5, minval=1)
lowpassLength = input.int(title="Lowpass Length", defval=12, minval=1)
highlightMovements = input.bool(title="Highlight Movements ?", defval=true)
src = input.source(title="Source", defval=close)

median(src, length) =>
    ta.percentile_nearest_rank(src, length, 50)

PI = 2 * math.asin(1)

alphaArg = 2 * PI / lowpassLength

alpha = 0.0
alpha := math.cos(alphaArg) != 0
     ? (math.cos(alphaArg) + math.sin(alphaArg) - 1) / math.cos(alphaArg)
     : nz(alpha[1])

rmf = 0.0
rmf := alpha * median(src, medianLength) + (1 - alpha) * nz(rmf[1])

rmfColor = highlightMovements ? (rmf > rmf[1] ? color.green : color.red) : color.new(#6d1e7f, 0)
plot(rmf, title="RMF", linewidth=2, color=rmfColor)

//Insert indicator conditions as filter
C2_L_Filter = rmf > rmf[1]
C2_S_Filter = rmf < rmf[1]
//==============================================================================
//INSERT - EXIT INDICATOR
//==============================================================================
//Insert indicator variables
//Using indicator 1 as exit indicator - no additional indicators to be inserted 
//in this example. If you wish to use another indicator, insert indicator here.

//Insert indicator conditions as exit trigger - in this example, using C1 as
//exit indicator. If you wish to use another indicator, insert indicator above,
//and provide trigger (either crossover or crossunder) conditions here.
Exit_L = C1_S_Trigger
Exit_S = C1_L_Trigger

//==============================================================================
//COMPLETED SECTION
//This section has been optimised to work with the above indicators the user
//has inserted above. The user does not require to change any code below and
//is completed and optimised for the full NNFX strategy. Users may wish to 
//customise this section of code if they wish to alter the NNFX strategy.
//==============================================================================
//COMPLETE - BACKTEST DATE RANGE
//==============================================================================
//Define variables
Start_D = input.int (   1, "Start Day",   1, 31)
Start_M = input.int (   1, "Start Month", 1, 12)
Start_Y = input.int (2019, "Start Year"        )
End_D   = input.int (   1, "End Day",     1, 31)
End_M   = input.int (   1, "End Month",   1, 12)
End_Y   = input.int (2024, "End Year"          )

//Define start and end time stamps
Start_Date = timestamp(Start_Y, Start_M, Start_D, 00, 00, 00)
End_Date   = timestamp(End_Y,     End_M,   End_D, 00, 00, 00)

//Date Range Condition
Date_Range() =>
    if time >= Start_Date and time <= End_Date
        true
    else
        false

//==============================================================================
//COMPLETE - ATR MONEY MANAGEMENT
//==============================================================================
//Declair money management variables
Risk_Percent     = input.float(title='Percent Risk Per Trade', defval=0.02, minval=0.001, maxval=1)
Atr_Multi_Profit = input.float(title='Atr Profit Multiple',    defval=2.5,  minval=0.1, step=0.5)
Atr_Multi_Loss   = input.float(title='Atr Loss Multiple',      defval=1.5,  minval=0.1, step=0.1)

//ATR indicator
Atr_length = input.int(title="ATR Length", defval=14, minval=1)
Atr_smoothing = input.string(title="ATR Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
ma_function(source, length) =>
	switch Atr_smoothing
		"RMA" => ta.rma(source, length)
		"SMA" => ta.sma(source, length)
		"EMA" => ta.ema(source, length)
		=> ta.wma(source, length)

Atr = ma_function(ta.tr(true), Atr_length)

//Calculate position size based off initial account capital, risk per trade and atr distance
Position_Size = math.round(strategy.initial_capital * Risk_Percent / (Atr_Multi_Loss * Atr))

//==============================================================================
//COMPLETE - ENTRY CONDITIONS
//==============================================================================
//Insert Long and Short conditions for C1 Trigger, all other indicators are filters
Condition_L_1 = strategy.position_size <= 0 and Date_Range() and C1_L_Trigger
Condition_S_1 = strategy.position_size >= 0 and Date_Range() and C1_S_Trigger

//Plot long and short conditions
plotshape(Condition_L_1, color=color.rgb(255, 0, 255), style=shape.labelup,   location=location.belowbar)
plotshape(Condition_S_1, color=color.rgb(255, 0, 255), style=shape.labeldown, location=location.abovebar)

//==============================================================================
//COMPLETE - ENTRY ORDERS
//==============================================================================
//Compile all long and short entry conditions
Entry_Long  = Condition_L_1 and C2_L_Filter
Entry_Short = Condition_S_1 and C2_S_Filter

//Submit long and short orders based on entry conditions
if Entry_Long
    strategy.entry(id='Entry_L_1', comment = "Long Entry",  direction=strategy.long,  qty=Position_Size)
    
if Entry_Short
    strategy.entry(id='Entry_S_1', comment = "Short Entry", direction=strategy.short, qty=Position_Size)

//==============================================================================
//COMPLETE - TAKE PROFIT AND STOP LOSS CONDITIONS
//==============================================================================
//Store Price on new entry signal
Entry_Price = strategy.opentrades.entry_price(strategy.opentrades - 1)

//Store ATR value on new entry signal
Entry_Atr = float(0.0)
if strategy.position_size == 0 or Entry_Long or Entry_Short
    Entry_Atr := Atr
else
    Entry_Atr := Entry_Atr[1]

//Calculate stop loss and take profit distance (in price)
Risk_Profit = Entry_Atr * Atr_Multi_Profit
Risk_Loss   = Entry_Atr * Atr_Multi_Loss 

// Take profit level calculation
Profit_L = float(na)
Profit_S = float(na)

// Stop loss level calculation
Stop_L = float(na)
Stop_S = float(na)

if strategy.position_size > 0  // For Long Trades
    Profit_L := Entry_Price + Risk_Profit
    Stop_L := Entry_Price - Risk_Loss
else
    Profit_L := na
    Stop_L := na

if strategy.position_size < 0  // For Short Trades
    Profit_S := Entry_Price - Risk_Profit
    Stop_S := Entry_Price + Risk_Loss
else
    Profit_S := na
    Stop_S := na

// Plot profit and stop loss levels for long and short trades
plot(Profit_L, color=color.lime, style=plot.style_linebr, linewidth=2)
plot(Profit_S, color=color.lime, style=plot.style_linebr, linewidth=2)
plot(Stop_L, color=color.red, style=plot.style_linebr, linewidth=2)
plot(Stop_S, color=color.red, style=plot.style_linebr, linewidth=2)

//==============================================================================
//COMPLETE - EXIT ORDERS
//==============================================================================
// Exit long trades
strategy.exit(id = 'Exit_L_1', from_entry ='Entry_L_1', comment='Long Exit 1',  stop = Stop_L, limit = Profit_L)

// Exit short trades
strategy.exit(id = 'Exit_S_1', from_entry ='Entry_S_1', comment='Short Exit 1', stop = Stop_S, limit = Profit_S)

//==============================================================================
//COMPLETE - CLOSE ORDERS
//==============================================================================
// Close long trades
if Exit_L
    strategy.close(id='Entry_L_1', comment='EXIT INDICATOR')

// Close short trades
if Exit_S
    strategy.close(id='Entry_S_1', comment='EXIT INDICATOR')

//==============================================================================

image