'How to add a stop loss that is NOT updating and get a 1,5 risk reward ratio

i am struggling to find a way to properly add a fixed stop loss and take profit. i'm looking to get the latest pivot high or pivot low value (at the point of entry) as my stop loss. This is the code i'm currently using and it should have everything it needs in order to get the right stop loss.

The only thing i am struggling with is how to get the stop loss value at the point of entry and to keep it that stop loss Also i'm not sure what the best way is to get a 1,5 risk reward ratio as take profit

Would really appreciate it if someone can help me with the final part of my strategy

Pivotprd = input.int(defval=10, title='Pivot Point Period', minval=10, maxval=50)


var float ph = na
var float pl = na
ph := ta.pivothigh(Pivotprd, Pivotprd)
pl := ta.pivotlow(Pivotprd, Pivotprd)

var float last_ph = na
var float last_pl = na

// get latest value of pivotTrendHH
var HHcount = 0
if ph > last_ph
    HHcount := 2
    
if ph < last_ph
    HHcount := 1
// get latest value of pivotTrendLL

var LLcount = 0
if pl < last_pl
    LLcount := 1
    
if pl > last_pl
    LLcount := 2


pivotTrendHH = HHcount == 2 ? "Bullish" : "Bearish"
pivotTrendLL = LLcount == 2 ? "Bullish" : "Bearish"


ph_color = HHcount == 2 ? color.green : color.red
pl_color = LLcount == 2 ? color.green : color.red

if (ph) //If x days ago was a pivot point
    label.new(bar_index-Pivotprd, ph, pivotTrendHH, style=label.style_label_down, size=size.small, yloc=yloc.price, color=ph_color) //NOTE: bar_index-prd, becuse it occured x days ago
    last_ph := ph //Saves price data
if (pl)
    label.new(bar_index-Pivotprd, pl, pivotTrendLL, style=label.style_label_up, size=size.small, yloc=yloc.price, color=pl_color) 
    last_pl := pl //Saves price data


// get value of latest pivot high
var float phVal = na
phVal := ta.valuewhen(ph, high[Pivotprd], 0)
// get value of latest pivot low
var float plVal = na
plVal := ta.valuewhen(pl, low[Pivotprd], 0)

// This is the point where i don't know what to do

var float entryLvl = 0.00
entryLvl := ta.valuewhen(strategy.opentrades == 1, strategy.position_avg_price, 0)

var float longSL = 0.00
var float shortSL = 0.00
longSL := ta.valuewhen(strategy.opentrades == 1, last_pl, 0)
shortSL := ta.valuewhen(strategy.opentrades == 1, last_ph, 0)

plot(longSL,"Stop Loss Level LONG", color.red, 2, plot.style_linebr )
plot(shortSL, "Stop Loss Level SHORT", color.red, 2, plot.style_linebr) 

rrBull = (entryLvl - longSL) * 1.5
rrBear = (shortSL - entryLvl) * 1.5
strategy.exit("Long SL Hit", "Long", stop=longSL)
strategy.exit("Long TP Hit", "Long", profit=rrBull)

strategy.exit("Short SL Hit", "Short", stop=shortSL)
strategy.exit("Short TP Hit", "Short", profit=rrBear)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source