'How to use most recent pivot high/low in strategy?

I am trying to become more familiar with pinescript and am trying to run a strategy with a risk reward ratio that uses most recent pivot high/low (Longs/Shorts) for the SL. The strategy will run but I am not sure where its getting the last swings from. What do I throw in there to make sure it will only use the last/most recent pivot low/high? Here is what I have:

colorGroupTitle = "Text Color / Label Color"
leftLenH = input.int(title="Pivot High", defval=10, minval=1, inline="Pivot High", group=lengthGroupTitle)
rightLenH = input.int(title="/", defval=10, minval=1, inline="Pivot High", group=lengthGroupTitle)
textColorH = input(title="Pivot High", defval=color.black, inline="Pivot High", group=colorGroupTitle)
labelColorH = input(title="", defval=color.white, inline="Pivot High", group=colorGroupTitle)

leftLenL = input.int(title="Pivot Low", defval=10, minval=1, inline="Pivot Low", group=lengthGroupTitle)
rightLenL = input.int(title="/", defval=10, minval=1, inline="Pivot Low", group=lengthGroupTitle)
textColorL = input(title="Pivot Low", defval=color.black, inline="Pivot Low", group=colorGroupTitle)
labelColorL = input(title="", defval=color.white, inline="Pivot Low", group=colorGroupTitle)

ph = ta.pivothigh(leftLenH, rightLenH)
pl = ta.pivotlow(leftLenL, rightLenL)

drawLabel(_offset, _pivot, _style, _color, _textColor) =>
    if not na(_pivot)
        label.new(bar_index[_offset], _pivot, str.tostring(_pivot, format.mintick), style=_style, color=_color, textcolor=_textColor)

drawLabel(rightLenH, ph, label.style_label_down, labelColorH, textColorH)
drawLabel(rightLenL, pl, label.style_label_up, labelColorL, textColorL)


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



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

profitL = (entry_lvl - pl) * 2 + entry_lvl

profitS = (entry_lvl - ph) * 2 + entry_lvl

plot(entry_lvl,"Entry Level", color.yellow, 2, plot.style_linebr)
plot(profitL, "Long Profit Level", color.green, 2, plot.style_linebr)
plot(profitS, "Short Profit Level", color.red, 2, plot.style_linebr)

if conditions are here
    strategy.entry(id='Long', direction=strategy.long, comment = "L")
strategy.exit("Long", limit=profitL, stop=pl)

Thanks in advance to anyone offering to help out!



Sources

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

Source: Stack Overflow

Solution Source