'Can i count bars in pine script and after x bars put a trade not on first bar? PINE SCRIPT

this startegy is based on ssl channel and some other indicators like pivot points and ema. When line above change to green, it place long order and when it turns red it exit but i want to add a filter to this changes bcs sometimes this above line change colors bar after a bar and it turns winning trades into lossing trades becouse of fees. Thanks for any advise and dont judge me for this code, im newbie :P

there is the code:

//@version=5
strategy("channel", overlay=true, pyramiding=0)


//ema
leng = input.int(200, minval=1, title="Length")
src = input(close, title="Source")
offset = input.int(title="Offset", defval=0, minval=-500, maxval=500)
out = ta.ema(src, leng)
plot(out, title="EMA", color=color.blue, offset=offset)

//channel
period=input(title="Period", defval=10)
len=input(title="Period", defval=10)
smaHigh=ta.sma(high, len)
smaLow=ta.sma(low, len)
Hlv = float(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.red)
plot(sslUp, linewidth=2, color=color.lime)

//pivot
gr="LENGTH LEFT / RIGHT"
leftLenH = input.int(title="Pivot High", defval=10, minval=1, inline="Pivot High",group=gr)
rightLenH = input.int(title="/", defval=10, minval=1, inline="Pivot High",group=gr)
colorH = input(title="", defval=#E3EFFD, inline="Pivot High",group=gr)

leftLenL = input.int(title="Pivot Low", defval=10, minval=1, inline="Pivot Low", group=gr)
rightLenL = input.int(title="/", defval=10, minval=1, inline="Pivot Low",group=gr)
colorL = input(title="", defval=#E3EFFD, inline="Pivot Low",group=gr)

ph = ta.pivothigh(leftLenH, rightLenH)
pl = ta.pivotlow(leftLenL, rightLenL)

drawLabel(_offset, _pivot, _style, _color) =>
    if not na(_pivot)
        label.new(bar_index[_offset], _pivot, str.tostring(_pivot, format.mintick), style=_style, color=_color, textcolor=#131722)

drawLabel(rightLenH, ph, label.style_label_down, colorH)
drawLabel(rightLenL, pl, label.style_label_up, colorL)

//long contition
L1 = if (close and open and high and low  > out)
    true
else
    false 
L2 = if (sslDown < sslUp)
    true
else
    false
//L3 = if (sslUp > out)
//    true
//else
//    false
L = if (L2 and L1 == true)
    true
else
    false

if (L == true)
    strategy.entry("el", strategy.long)
plotshape(sslDown < sslUp, location=location.abovebar, style=shape.arrowup,   color=color.green)
exitL = if (sslDown > sslUp)
    true
else
    false
if (exitL == true)
    strategy.close(id="el")
//short contition 
S1 = if (close and open and high and low  < out)
    true
else
    false 
S2 = if (sslDown > sslUp)
    true
else
    false
S = if (S2 and S1 == true)
    true
else
    false
if (S == true)
    strategy.entry("es", strategy.short)
plotshape(sslDown > sslUp, location=location.belowbar, style=shape.arrowup,   color=color.red)
exitS = if (sslDown < sslUp)
    true
else
    false
if (exitS == true)
    strategy.close(id="es")



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source