I am trying to enter a stop loss for a Pine Script V5 strategy. The stop loss is -0.10% of the red line of the indicator. To find this level I have created a (white) line. I must then necessarily close the trade at the exact price of the white line. At the moment I have tried thousands of solutions all of which close only after the breakout candle (this is great for repainting but not sufficient for the solution I am looking for now). Can anyone tell me how to get a precise exit on the white line ?
`//@version=5
strategy("", overlay = true, currency = currency.EUR,
initial_capital = 1000, commission_type = strategy.commission.percent, commission_value = 0.03,
default_qty_value = 100, default_qty_type = strategy.percent_of_equity)
len = input(title = 'PERIOD', defval = 10, group = "SETTING INDICATOR")
smaHigh = ta.sma(high, len)
smaLow = ta.sma(low, len)
Hlv = int(na)
Hlv := close > smaHigh ? 1 : close < smaLow ? -1 : Hlv[1]
sslDown = Hlv < 0 ? smaHigh : smaLow
sslUp = Hlv < 0 ? smaLow : smaHigh
plot(sslDown, linewidth = 2, color = color.new(color.red, 0))
plot(sslUp, linewidth = 2, color = color.new(color.lime, 0))
LongEntry = ta.crossover (sslUp, sslDown) and barstate.isconfirmed
LongExit = ta.crossunder(sslUp, sslDown) and barstate.isconfirmed
isLong = LongEntry //and inDateRange
if (isLong and barstate.isconfirmed)
strategy.entry(id = "BUY", direction = strategy.long, qty = 100)
var int bar = 0
var int count = 0
if LongEntry
bar := bar_index
bar
count := bar_index - bar + 1
isExit = if LongExit
close
StopLoss = input.float(defval = 0.10, title = "STOP LOSS VALUE", minval = 0.00)
distance = (sslDown * StopLoss) / 100
Value = (sslDown - distance)
SL = ta.valuewhen(LongEntry, Value, 0)
plot(SL, color = color.white)
Close = if ta.crossunder(close, SL)
close
strategy.exit("CLOSE LONG", from_entry = "BUY", stop = Close)
//strategy.close_all(when = SL)
//strategy.close_all(when = isExitStopLoss, comment = "CLOSE TRADE STOP LOSS")`
Use the SL variable as a stop=
argument instead of the Close, which returns na
when there is no ta.crosunder()
condition met:
//@version=5
strategy("", overlay = true, currency = currency.EUR,
initial_capital = 1000, commission_type = strategy.commission.percent, commission_value = 0.03,
default_qty_value = 100, default_qty_type = strategy.percent_of_equity)
len = input(title = 'PERIOD', defval = 10, group = "SETTING INDICATOR")
smaHigh = ta.sma(high, len)
smaLow = ta.sma(low, len)
Hlv = int(na)
Hlv := close > smaHigh ? 1 : close < smaLow ? -1 : Hlv[1]
sslDown = Hlv < 0 ? smaHigh : smaLow
sslUp = Hlv < 0 ? smaLow : smaHigh
plot(sslDown, linewidth = 2, color = color.new(color.red, 0))
plot(sslUp, linewidth = 2, color = color.new(color.lime, 0))
LongEntry = ta.crossover (sslUp, sslDown) and barstate.isconfirmed
LongExit = ta.crossunder(sslUp, sslDown) and barstate.isconfirmed
isLong = LongEntry //and inDateRange
if (isLong and barstate.isconfirmed)
strategy.entry(id = "BUY", direction = strategy.long, qty = 100)
var int bar = 0
var int count = 0
if LongEntry
bar := bar_index
bar
count := bar_index - bar + 1
isExit = if LongExit
close
StopLoss = input.float(defval = 0.10, title = "STOP LOSS VALUE", minval = 0.00)
distance = (sslDown * StopLoss) / 100
Value = (sslDown - distance)
SL = ta.valuewhen(LongEntry, Value, 0)
plot(SL, color = color.white)
// Close = if ta.crossunder(close, SL)
// close
//plot(Close) // <- returns 'na' if there is no crossunder
strategy.exit("CLOSE LONG", from_entry = "BUY", stop = SL)
//strategy.close_all(when = SL)
//strategy.close_all(when = isExitStopLoss, comment = "CLOSE TRADE STOP LOSS")