RayhanBrito

I need to put an "iff" filter to my bot

BMFBOVESPA:WIN1!   Bovespa Index-Mini Futures
Hi, I'm trying to develop a bot based on some indicators.

So I have this bot based on ATR and SMA. I could figure how to make the bot buy and sell everytime it crosses the ATR.

But what I need to make now is make the bot only buy if the price is above the moving average and cross the ATR or only sell if the price is below the SMA and cross the atr.

the source code is the following



//@version=4

strategy(title="UT Bot", overlay = true)
//CREDITS to HPotter for the orginal code. The guy trying to sell this as his own is a scammer lol.
src = close

// Get user input
res = input(title="EMA Timeframe", type=input.resolution, defval="D")
len = input(title="EMA Length", type=input.integer, defval=50)
col = input(title="Color EMA", type=input.bool, defval=true)
smooth = input(title="Smooth", type=input.bool, defval=false)
// Calculate EMA
ema = ema(close, len)
emaSmooth = security(syminfo.tickerid, res, ema, barmerge.gaps_on, barmerge.lookahead_off)
emaStep = security(syminfo.tickerid, res, ema, barmerge.gaps_off, barmerge.lookahead_off)
// Draw EMA
plot(smooth ? emaSmooth : emaStep, color=col ? close > emaStep ? color.green : color.red : color.black, style=plot.style_line, linewidth=2, title="EMA (HTF)")


keyvalue = input(3, title = "Key Vaule. 'This changes the sensitivity'", step = .5)
atrperiod = input(10, title="ATR Period")
xATR = atr(atrperiod)
nLoss = keyvalue * xATR

xATRTrailingStop = 0.0
xATRTrailingStop := iff(src > nz(xATRTrailingStop, 0) and src > nz(xATRTrailingStop, 0), max(nz(xATRTrailingStop), src - nLoss),
iff(src < nz(xATRTrailingStop, 0) and src < nz(xATRTrailingStop, 0), min(nz(xATRTrailingStop), src + nLoss),
iff(src > nz(xATRTrailingStop, 0), src - nLoss, src + nLoss)))

pos = 0
pos := iff(src < nz(xATRTrailingStop, 0) and src > nz(xATRTrailingStop, 0), 1,
iff(src > nz(xATRTrailingStop, 0) and src < nz(xATRTrailingStop, 0), -1, nz(pos, 0)))

xcolor = pos == -1 ? color.red: pos == 1 ? color.green : color.blue

plot(xATRTrailingStop, color = xcolor, title = "Trailing Stop")
buy = crossover(src,xATRTrailingStop)
sell = crossunder(src,xATRTrailingStop)
barcolor = src > xATRTrailingStop


barcolor(barcolor? color.green:color.red)


strategy.entry("compra", true, when = close > xATRTrailingStop)
strategy.entry("venda", false, when = close < xATRTrailingStop)
Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.