Unable to set take profit area as static - keeps moving with the price - pinescript v5

Viewed 32

Having a fixed TP issue, the TP price value for the price should stay the same after a position is opened since I assign the TP price to a var float variable, yet it keeps "driving" up/down with the price on each new bar...

// Put a label at candle close, exactly on the chart
candlecloseExactLocationLBL = label.new(bar_index, close, syminfo.ticker)
// Obtain closed candle y-coordinate/ticker price where candle closed
candlecloseLabelExactLocationPriceLabel = label.get_y(id=candlecloseExactLocationLBL)
// Remove the candle close label as it is no longer needed, cleanup
label.delete(candlecloseExactLocationLBL[0])

// Distance between candle close coordinate and ATR at the moment: subtract 1st from second
DistanceBetweenCandleClose_and_ATRLineNow_LONG = candlecloseLabelExactLocationPriceLabel - xATRTrailingStop
DistanceBetweenCandleClose_and_ATRLineNow_SHORT = xATRTrailingStop - candlecloseLabelExactLocationPriceLabel

// TP1 distance is candle close coordinate + ATR distance from candle close at the moment
// Divdie the ATR/signal candle distance by 2
ATR_trend_Long_TP = candlecloseLabelExactLocationPriceLabel + DistanceBetweenCandleClose_and_ATRLineNow_LONG/2
ATR_trend_Long_TP_crossed = ta.crossover(high, ATR_trend_Long_TP)

// Divdie the ATR/signal candle distance by 2
ATR_trend_Short_TP = candlecloseLabelExactLocationPriceLabel - DistanceBetweenCandleClose_and_ATRLineNow_SHORT/2
ATR_trend_Short_TP_crossed = ta.crossunder(low, ATR_trend_Short_TP)

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// At position signal:
// - Calculate LONG/SHORT TP coordinates and assign them to float var variables
// - Initiate a counter (LONG: 1 | SHORT: -1 | noposition: 0
// - plot TP line per TP coordinates float var variable until TP is hit
// - TP is hit: set TP coordinates and counter to 0

// So the positionLongShortNone position counter is either 1 if position is LONG, -1 if position is SHORT and 0 if no position is open
// After TP/SL the counter should be reset to 0
var positionLongShortNone = 0
if ATR_long_signal
    positionLongShortNone := 1
if ATR_short_signal
    positionLongShortNone := -1

var float positionLongTP_priceVAR = 0.0
var float positionShortTP_priceVAR = 0.0

positionLongTP_priceVAR := ATR_trend_Long_TP
positionShortTP_priceVAR := ATR_trend_Short_TP

plot(positionLongShortNone == 1 ? positionLongTP_priceVAR : na, style=plot.style_linebr, color=color.new(color.green, 0), linewidth=1, title='BUY Fixed TP')
plot(positionLongShortNone == -1 ? positionShortTP_priceVAR : na, style=plot.style_linebr, color=color.new(color.green, 0), linewidth=1, title='SELL Fixed TP')
0 Answers
Related