i have a pine script which calculates trailing stop price and the problem is when the entry is made the trail stop shifts downward if the price reverses immediately after the entry is made and resulting is loses to be huge for intraday move and risk remains open naked. below is the code and what the best can be done so that i can exit with a fixed stop loss if price reverses on the first candle. below is the code i am using.
//////////////////////////////////// TSL_STOP_BUY = input.int(title = "TSL_STOP_BUY", defval = 150, minval = 1, maxval = 10000, step = 1)
TSL_STOP_SELL = input.int(title = "TSL_STOP_SELL", defval = 150, minval = 1, maxval = 10000, step = 1)
longstopprice = 0.0
shortstopprice = 0.0
longstopprice := if (strategy.position_size > 0)
stopvalue = close - TSL_STOP_BUY
math.max(stopvalue,longstopprice[1])
else 0
shortstopprice := if (strategy.position_size < 0)
stopvalue = close + TSL_STOP_SELL
math.min(stopvalue,shortstopprice[1])
else 9999999
if indicatoroptionF
strategy.entry("long", strategy.long, when = buywI, comment = "B")
if tralingstoploss
strategy.exit("long", from_entry = "long", stop = longstopprice, comment = "TSL EXIT BUY" )
if indicatoroptionG
strategy.entry("short", strategy.short, when = shortwI, comment = "S")
if tralingstoploss
strategy.exit("short", from_entry = "short", stop = shortstopprice, comment = "TSL EXIT SHORT"
////////////////////////////////////////////////////