So i made code that enters short under certain conditions, placing order that will enter position with SL and TP at the same time. I need to check if order remains inactive for 14 bars, then it needs to be canceled. I tried using variables, but cant understand when they "exist" and not So here what i already have:
if condition1 and close and and high < ema and high < sma and ema < sma
strategy.entry("Short" + str.tostring(bar_index), strategy.short, stop=low)
strategy.exit("Exit short", str.tostring(bar_index), stop = high, limit = high - 3 * (high - low))
Here is my full code if needed:
//@version=5
strategy("ara", overlay=true, margin_long=100, margin_short=100)
inventoryretracementpercentage = input.int(45, title='Inventory Retracement Percentage %', maxval=100)
sma = request.security(syminfo.tickerid, "60", ta.ema(close, 20))
ema = ta.ema(close, 40)
candlerange = math.abs(high - low)
candlebody = math.abs(close - open)
percenttodecimal = inventoryretracementpercentage / 100
rangeverification = candlebody < percenttodecimal * candlerange
pricelevelforretracementx = low + percenttodecimal * candlerange
pricelevelforretracementy = high - percenttodecimal * candlerange
sl = rangeverification == 1 and high > pricelevelforretracementy and close < pricelevelforretracementy and open < pricelevelforretracementy
ss = rangeverification == 1 and low < pricelevelforretracementx and close > pricelevelforretracementx and open > pricelevelforretracementx
li = sl ? pricelevelforretracementy : ss ? pricelevelforretracementx : (pricelevelforretracementx + pricelevelforretracementy) / 2
plotshape(sl, style=shape.arrowdown, location=location.abovebar, color=color.new(color.red, 0), title='Long Bar')
plotshape(ss, style=shape.arrowup, location=location.belowbar, color=color.new(color.green, 0), title='Short Bar')
plot(sma, title='Slow Speed Line', linewidth=2, color=color.new(#ffffff, 0))
plot(ema, title='Fast Primary Trend Line', linewidth=3, color=color.new(#cfe600, 0))
alertcondition(sl, title='BUY', message='BUY')
alertcondition(ss, title='SELL', message='SELL')
if ss and close and low > ema and low > sma and ema > sma
strategy.entry("Short" + str.tostring(bar_index), strategy.short, stop=low)
strategy.exit("Exit short", str.tostring(bar_index), stop = high, limit = high - 3 * (high - low))
if ss and close and high < ema and high < sma and ema < sma
strategy.entry("Short" + str.tostring(bar_index), strategy.short, stop=low)
strategy.exit("Exit short", "Short" + str.tostring(bar_index), stop = high, limit = high - 3 * (high - low))
If you need to cancel the trade entry if your limit entry was not satisfied within some number of bars, you can use ta.barssince()
and strategy.cancel_all()
Code example based on the first example:
entry_condition = condition1 and close and and high < ema and high < sma and ema < sma
bars_since_entry = ta.barssince(entry_condition)
if entry_condition
strategy.entry("Short" + str.tostring(bar_index), strategy.short, stop=low)
strategy.exit("Exit short", str.tostring(bar_index), stop = high, limit = high - 3 * (high - low))
if bars_since_entry >= 14
strategy.cancel_all()