I've been trying to fix a problem in this PineScript code, but no matter what I try, I can't make it work. It either closes the trade immediately when it enters, or SL and TP don't work at all.
It should use the highest high and lowest low of the last 10 candles when it enters a trade. These values should remain static throughout the trade duration. One code, which worked for me, was updating stop loss every bar, and because of this, it doesn't take profit.
Additionally, the take profit should be 2x the stop loss level. I've tried every combination I could think of, checked every resource online, but I only found 7-8 resources about this specific subject. Most of them are old and don't work. I would be really glad if you could help me.
(https://i.stack.imgur.com/TSl0n.png)
Here's the code
// Define the number of candles for stop loss and take profit calculation
numCandles = 10
var float highestHigh = na
var float lowestLow = na
// Calculate the highest high and lowest low only once when a trade is initiated
if (strategy.position_size != strategy.position_size[1])
highestHigh := ta.highest(high, numCandles)
lowestLow := ta.lowest(low, numCandles)
// Calculate stop loss and take profit levels based on RR parameter
RR = input.int(2, title="Risk-Reward Ratio", minval=1, maxval=10)
longStopLossLevel = lowestLow
longTakeProfitLevel = longStopLossLevel + RR * (longStopLossLevel - strategy.position_avg_price)
shortStopLossLevel = highestHigh
shortTakeProfitLevel = shortStopLossLevel - RR * (strategy.position_avg_price - shortStopLossLevel)
if strategy.equity > strategy.initial_capital * 0.2 and strategy.closedtrades + strategy.opentrades < 1499
strategy.entry('LONG', strategy.long, qty=adjustedLots, when=long_condition, limit=entry[numb])
strategy.entry('SHORT', strategy.short, qty=adjustedLots, when=short_condition, limit=entry[numb])
strategy.exit('L.exit', from_entry='LONG', loss=longStopLossLevel, profit=longTakeProfitLevel)
strategy.exit('S.exit', from_entry='SHORT', loss=shortStopLossLevel, profit=shortTakeProfitLevel)
Tried this too, but same issue persists.
///////StopLOSS/TAKE PROFIT////////
hiLen = input.int(title='Short SL Lookback', defval=20, minval=2,group = "STOPLOSS?TAKE PROFIT")
loLen = input.int(title='Long SL Lookback', defval=20, minval=2)
risk_Reward = input(defval = 1.0,title = "#Risk/Reward#")
//////////ENTRY CONDITION////////
bool openingLongPosition = buy and not (strategy.opentrades.size(strategy.opentrades - 1) > 0)
bool openingShortPosition = sell and not (strategy.opentrades.size(strategy.opentrades - 1) < 0)
bool openingAnyPosition = openingLongPosition or openingShortPosition
float closePriceWhenPositionOpened = ta.valuewhen(openingAnyPosition,close,0)
/////Calculation//////
hiHighs = ta.highest(high, hiLen)
loLows = ta.lowest(low, loLen)
stopl = ta.valuewhen(openingLongPosition,loLows,0)
stops = ta.valuewhen(openingShortPosition,hiHighs,0)
// Define variables to store initial stop-loss and take profit levels
var float initialLongStop = na
var float initialShortStop = na
var float longTakeProfit = na
var float shortTakeProfit = na
//////////ENTRY///////////
if buy
strategy.entry(id='Long', direction=strategy.long)
initialLongStop := stopl
longTakeProfit := closePriceWhenPositionOpened + ((closePriceWhenPositionOpened - initialLongStop) * risk_Reward)
strategy.exit('TP/SL','Long',limit = longTakeProfit,stop = initialLongStop)
if sell
strategy.entry(id='Short', direction=strategy.short)
initialShortStop := stops
shortTakeProfit := closePriceWhenPositionOpened + ((closePriceWhenPositionOpened - initialShortStop) * risk_Reward)
strategy.exit('TP/SL','Short',limit = shortTakeProfit,stop = initialShortStop)