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[1], 0) and src[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), src - nLoss),
iff(src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), src + nLoss),
iff(src > nz(xATRTrailingStop[1], 0), src - nLoss, src + nLoss)))
pos = 0
pos := iff(src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0), 1,
iff(src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 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)
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[1], 0) and src[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1]), src - nLoss),
iff(src < nz(xATRTrailingStop[1], 0) and src[1] < nz(xATRTrailingStop[1], 0), min(nz(xATRTrailingStop[1]), src + nLoss),
iff(src > nz(xATRTrailingStop[1], 0), src - nLoss, src + nLoss)))
pos = 0
pos := iff(src[1] < nz(xATRTrailingStop[1], 0) and src > nz(xATRTrailingStop[1], 0), 1,
iff(src[1] > nz(xATRTrailingStop[1], 0) and src < nz(xATRTrailingStop[1], 0), -1, nz(pos[1], 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)
Penafian
Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.
Penafian
Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.