Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

//@version=4

strategy(title="UT Bot ABC Waddah Attar Trendilo Strategy 9 sierpnia 2023", overlay = true)

// Inputs
a = input(1, title = "Key Vaule. 'This changes the sensitivity'")
c = input(10, title = "ATR Period")
h = input(false, title = "Signals from Heikin Ashi Candles")

xATR = atr(c)
nLoss = a * xATR

src = h ? security(heikinashi(syminfo.tickerid), timeframe.period, close, lookahead = false) : close

xATRTrailingStop = 0.0
xATRTrailingStop := iff(src > nz(xATRTrailingStop[1], 0) and src[1] > nz(xATRTrailingStop[1], 0), max(nz(xATRTrailingStop[1])
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

ema = ema(src,1)
above = crossover(ema, xATRTrailingStop)
below = crossover(xATRTrailingStop, ema)

buy = src > xATRTrailingStop and above


sell = src < xATRTrailingStop and below

barbuy = src > xATRTrailingStop


barsell = src < xATRTrailingStop

plotshape(buy, title = "Buy", text = '', style = shape.labelup, location = location.belowbar, color= color.green, textcolor
plotshape(sell, title = "Sell", text = '', style = shape.labeldown, location = location.abovebar, color= color.red, textcolor

barcolor(barbuy ? color.rgb(255, 255, 255) : na)


barcolor(barsell ? color.red : na)

strategy.entry("long", true, when = buy)


strategy.entry("short", false, when = sell)

//"ABC UT alert", overlay = true, max_bars_back = 500, max_lines_count = 500, max_labels_count = 500)
prd = input(defval = 8, title="ZigZag Period", minval = 2, maxval = 50, group = "Setup")
fiboup = input(defval = 0.618, title = "Fibonacci Max", group = "Setup")
fibodn = input(defval = 0.382, title = "Fibonacci Min", group = "Setup")
errorrate = input(defval = 5.0, title = "Error Rate", minval = 0, maxval = 30, group = "Setup") / 100
showabc = input(defval = true, title = "Show ABC", group = "Extras")
keepabc = input(defval = true, title = "Keep Old ABCs", group = "Extras")
showcloud = input(defval = true, title = "Show Cloud", group = "Extras", inline = "cloud")
c_upcol = input(defval = color.new(#125837, 75), title = "", group = "Extras", inline = "cloud")
c_dncol = input(defval = color.new(#831a1a, 75), title = "", group = "Extras", inline = "cloud")
showzigzag = input(defval = true, title = "Show Zig Zag & Fibo", group = "Extras", inline = "zigzag")
upcol = input(defval = color.rgb(0, 65, 33), title = "", group = "Extras", inline = "zigzag")
dncol = input(defval = color.rgb(102, 0, 0), title = "", group = "Extras", inline = "zigzag")
srcma = input(defval = close, title = "Source for Moving Averages", group = "Trend Cloud")
malen1 = input(defval = 10, title = "SMA 1 Length", minval = 1, group = "Trend Cloud")
malen2 = input(defval = 20, title = "SMA 2 Length", minval = 1, group = "Trend Cloud")
malen3 = input(defval = 55, title = "SMA 3 Length", minval = 1, group = "Trend Cloud")
malen4 = input(defval = 89, title = "SMA 4 Length", minval = 1, group = "Trend Cloud")
malen5 = input(defval = 20, title = "EMA 1 Length", minval = 1, group = "Trend Cloud")
malen6 = input(defval = 40, title = "EMA 2 Length", minval = 1, group = "Trend Cloud")

ma_array = array.new_float(6)
array.set(ma_array, 0, sma(srcma, malen1))
array.set(ma_array, 1, sma(srcma, malen2))
array.set(ma_array, 2, sma(srcma, malen3))
array.set(ma_array, 3, sma(srcma, malen4))
array.set(ma_array, 4, ema(srcma, malen5))
array.set(ma_array, 5, ema(srcma, malen6))

float umax = na
float umin = na
float lmax = na
float lmin = na
int upper = 0
int lower = 0
for x = 1 to 6
ma = array.get(ma_array, x -1)
if ma >= max(open, close)
upper := upper + 1
if na(umax)
umax := ma
umin := ma
else
umax := max(umax, ma)
umin := min(umin, ma)
else if ma <= min(open, close)
lower := lower + 1
if na(lmax)
lmax := ma
lmin := ma
else
lmax := max(lmax, ma)
lmin := min(lmin, ma)

var int trend = 0


trend := lower > 0 and upper == 0 and lower[1] > 0 and upper[1] == 0 ? 1 :
lower == 0 and upper > 0 and lower[1] == 0 and upper[1] > 0 ? -1 :
trend

tucolor = trend == 1 ? c_upcol: na


tdcolor = trend == -1 ? c_dncol : na
fill(plot(umax, color = na), plot(umin, color = na), color = showcloud ? tdcolor : na)
fill(plot(lmax, color = na), plot(lmin, color = na), color = showcloud ? tucolor : na)

//===================================================================

// zigzag part
get_ph_pl_dir(len)=>
float ph = highestbars(high, len) == 0 ? high : na
float pl = lowestbars(low, len) == 0 ? low : na
var dir = 0
dir := iff(ph and na(pl), 1, iff(pl and na(ph), -1, dir))
[ph, pl, dir]

[ph, pl, dir] = get_ph_pl_dir(prd)

var max_array_size = 10
var zigzag = array.new_float(0)

add_to_zigzag(value, bindex)=>
array.unshift(zigzag, bindex)
array.unshift(zigzag, value)
if array.size(zigzag) > max_array_size
array.pop(zigzag)
array.pop(zigzag)

update_zigzag(value, bindex)=>
if array.size(zigzag) == 0
add_to_zigzag(value, bindex)
else
if (dir == 1 and value > array.get(zigzag, 0)) or (dir == -1 and value < array.get(zigzag, 0))
array.set(zigzag, 0, value)
array.set(zigzag, 1, bindex)
0.

dir_changed = change(dir)
if ph or pl
if dir_changed
add_to_zigzag(dir == 1 ? ph : pl, bar_index)
else
update_zigzag(dir == 1 ? ph : pl, bar_index)

if showzigzag and array.size(zigzag) > 5


var line zzline1 = na
var line zzline2 = na
line.delete(zzline1)
line.delete(zzline2)
zzline1 := line.new(x1 = round(array.get(zigzag, 1)) , y1 = array.get(zigzag, 0), x2 = round(array.get(zigzag, 3)), y2 =
zzline2 := line.new(x1 = round(array.get(zigzag, 3)) , y1 = array.get(zigzag, 2), x2 = round(array.get(zigzag, 5)), y2 =
// min/max fibo levels
zzlen = abs(array.get(zigzag, 2) - array.get(zigzag, 4))
fmin = dir == 1 ? array.get(zigzag, 2) + zzlen * (fibodn - errorrate) : array.get(zigzag, 4) + zzlen * ((1 - fibodn) + errorrate
fmax = dir == 1 ? array.get(zigzag, 2) + zzlen * (fiboup + errorrate) : array.get(zigzag, 4) + zzlen * ((1 - fiboup) - errorrate
var line fibo1 = na
var line fibo2 = na
line.delete(fibo1)
line.delete(fibo2)
fibo1 := line.new(x1 = round(array.get(zigzag, 3)), y1 = fmin, x2 = round(array.get(zigzag, 3)) + 1, y2 = fmin, color = color.rgb
fibo2 := line.new(x1 = round(array.get(zigzag, 3)), y1 = fmax, x2 = round(array.get(zigzag, 3)) + 1, y2 = fmax, color = color.rgb
zchange = array.size(zigzag) > 0 ? array.get(zigzag, 0) : 0.0
abc = array.new_float(0)
if change(zchange) and array.size(zigzag) > 5 and ((pl and trend == 1 and dir == -1 and low < array.max(ma_array)) or (ph and
a = array.get(zigzag, 0)
b = array.get(zigzag, 2)
b_loc = array.get(zigzag, 3)
c = array.get(zigzag, 4)
c_loc = array.get(zigzag, 5)
rate = (a - b) / (c - b)
if rate >= (fibodn - fibodn * errorrate) and rate <= (fiboup + fiboup * errorrate)
array.push(abc, b)
array.push(abc, b_loc)
array.push(abc, c)
array.push(abc, c_loc)

draw_line(dir, x1_,y1_, x2_, y2_, x3_, y3_)=>


l1 = line.new(x1 = x1_, y1 = y1_, x2 = x2_, y2 = y2_, color = dir == 1 ? upcol : dncol, width = 2)
l2 = line.new(x1 = x2_, y1 = y2_, x2 = x3_, y2 = y3_, color = dir == 1 ? dncol : upcol, width = 2)
[l1, l2]

draw_label(dir, x1_,y1_, x2_, y2_, x3_, y3_)=>


alabel = label.new( x = x1_,
y = y1_,
text = "C",
style = dir == 1 ? label.style_label_down : label.style_label_up,
color = color.new(color.white, 100),
textcolor = color.rgb(0, 54, 99))
blabel = label.new( x = x2_,
y = y2_,
text = "B",
style = dir == -1 ? label.style_label_down : label.style_label_up,
color = color.new(#751818, 100),
textcolor = color.rgb(0, 67, 122))
clabel = label.new( x = x3_,
y = y3_,
text = "A",
style = dir == 1 ? label.style_label_down : label.style_label_up,
color = color.new(#6d1616, 100),
textcolor = color.rgb(0, 47, 85))
[alabel, blabel, clabel]

var abclines = array.new_line(2)


var abclabels = array.new_label(3)

if showabc and array.size(abc) >= 4


if not keepabc
line.delete(array.pop(abclines))
line.delete(array.pop(abclines))
label.delete(array.pop(abclabels))
label.delete(array.pop(abclabels))
label.delete(array.pop(abclabels))

[l1_, l2_] = draw_line(dir, bar_index, array.get(zigzag, 0), round(array.get(abc, 1)), array.get(abc, 0), round(array.get
array.unshift(abclines, l1_)
array.unshift(abclines, l2_)

[la1_, la2_, la3_] = draw_label(dir, bar_index, array.get(zigzag, 0), round(array.get(abc, 1)), array.get(abc, 0), round
array.unshift(abclabels, la1_)
array.unshift(abclabels, la2_)
array.unshift(abclabels, la3_)

// bounce?
lbounced = false
sbounced = false
for i = 0 to 5
if min(low, low[1]) <= array.get(ma_array, i) and close > array.get(ma_array, i) and close > open
lbounced := true
if max(high, high[1]) >= array.get(ma_array, i) and close < array.get(ma_array, i) and close < open
sbounced := true

// stoch give signal?


sto = sma(stoch(close, high, low, 5), 3)
sto_sig = sma(sto, 3)
lstoch = sto[1] <= sto_sig[1] and sto > sto_sig and sto[1] < 50 //and sto_sig > 20
sstoch = sto[1] >= sto_sig[1] and sto < sto_sig and sto[1] > 50 //and sto_sig < 80

/// check if conditions met


there_is_abc = array.size(abc) != 0
var float last_zz_point = 0.
last_zz_point := array.size(zigzag) > 2 and there_is_abc ? array.get(zigzag, 0) : last_zz_point
var abc_bar_count = 0
abc_bar_count := there_is_abc ? 0 : abc_bar_count + 1
hhh_ = highest(abc_bar_count + 1)
lll_ = lowest(abc_bar_count + 1)

// long condition
long = trend == 1 and abc_bar_count <= 6 and lbounced and lll_ >= last_zz_point
short = trend == -1 and abc_bar_count <= 6 and sbounced and hhh_ <= last_zz_point

plotshape(long, style = shape.triangleup, color = upcol, location = location.belowbar, size = size.small)


plotshape(short, style = shape.triangledown, color = dncol, location = location.abovebar, size = size.small)

alertcondition(long, title = "ABC Long", message = "ABC Long")


alertcondition(short, title = "ABC Short", message = "ABC Short")

////"Combined Indicator Strategy", overlay=true)

// Waddah Attar Explosion V2 [SHK] Indicator Inputs


sensitivity = input(150, title="Sensitivity")
fastLength = input(20, title="FastEMA Length")
slowLength = input(40, title="SlowEMA Length")
channelLength = input(20, title="BB Channel Length")
mult = input(2.0, title="BB Stdev Multiplier")

// Trendilo Indicator Inputs


src1 = input(close, title='Source')
smooth = input(1, title='Smoothing', minval=1)
length = input(50, title='Lookback', minval=1)
offset = input(0.85, title='ALMA Offset', step=0.01)
sigma = input(6, title='ALMA Sigma', minval=0)
bmult = input(1.0, 'Band Multiplier')
cblen = input(false, 'Custom Band Length ? (Else same as Lookback)')
blen = input(20, 'Custom Band Length')

// Waddah Attar Explosion V2 [SHK] Indicator Calculation


calc_macd(source, fastLength, slowLength) =>
fastMA = ema(source, fastLength)
slowMA = ema(source, slowLength)
fastMA - slowMA

calc_BBUpper(source, length, mult) =>


basis = sma(source, length)
dev = mult * stdev(source, length)
basis + dev

calc_BBLower(source, length, mult) =>


basis = sma(source, length)
dev = mult * stdev(source, length)
basis - dev

t1 = (calc_macd(close, fastLength, slowLength) - calc_macd(close[1], fastLength, slowLength)) * sensitivity


e1 = (calc_BBUpper(close, channelLength, mult) - calc_BBLower(close, channelLength, mult))

// Trendilo Indicator Calculation


pch = change(src, smooth) / src * 100
avpch = alma(pch, length, offset, sigma)
blength = cblen ? blen : length
rms = bmult * sqrt(sum(avpch * avpch, blength) / blength)
cdir = avpch > rms ? 1 : avpch < -rms ? -1 : 0

// Strategy Conditions
waddahUpTrend = (t1 >= 0)
trendiloUpTrend = (cdir > 0)

waddahDownTrend = (t1 < 0)


trendiloDownTrend = (cdir < 0)

// Strategy Entry and Exit Conditions


strategy.entry("Buy", strategy.long, when = waddahUpTrend and trendiloUpTrend)
strategy.close("Buy", when = waddahDownTrend or trendiloDownTrend)

strategy.entry("Sell", strategy.short, when = waddahDownTrend and trendiloDownTrend)


strategy.close("Sell", when = waddahUpTrend or trendiloUpTrend)

// Plotting
plotshape(waddahUpTrend, color = color.rgb(76, 175, 79, 43), style = shape.triangleup, title = "Waddah Up Trend")
plotshape(waddahDownTrend, color = color.rgb(255, 82, 82, 54), style = shape.triangledown, title = "Waddah Down Trend")

plotshape(trendiloUpTrend, color = color.rgb(255, 255, 255), style = shape.triangleup, title = "Trendilo Up Trend")
plotshape(trendiloDownTrend, color = color.rgb(255, 251, 0), style = shape.triangledown, title = "Trendilo Down Trend")
//"Najwyższa i Najniższa Cena Sesji", shorttitle="HHLL Session", overlay=true)

var float sessionHigh = na


var float sessionLow = na
var float sessionOpen = na
var line sessionHighLine = na
var line sessionLowLine = na

is_new_session = change(time("D"))

if is_new_session
sessionHigh := high
sessionLow := low
sessionOpen := open
line.delete(sessionHighLine)
line.delete(sessionLowLine)
sessionHighLine := line.new(x1=bar_index[1], y1=sessionHigh, x2=bar_index, y2=sessionHigh, color=color.rgb(253, 253, 253)
sessionLowLine := line.new(x1=bar_index[1], y1=sessionLow, x2=bar_index, y2=sessionLow, color=color.rgb(121, 0, 0), width

if not is_new_session
sessionHigh := max(sessionHigh, high)
sessionLow := min(sessionLow, low)
line.set_xy1(sessionHighLine, x=bar_index[1], y=sessionHigh)
line.set_xy2(sessionHighLine, x=bar_index, y=sessionHigh)
line.set_xy1(sessionLowLine, x=bar_index[1], y=sessionLow)
line.set_xy2(sessionLowLine, x=bar_index, y=sessionLow)

bgcolor(is_new_session ? color.yellow : na, transp=90)

///"4 EMA", shorttitle="4EMA", overlay=true)

length1 = input(55, title="Długość EMA 1")


length2 = input(144, title="Długość EMA 2")
length3 = input(222, title="Długość EMA 3")
length4 = input(333, title="Długość EMA 4")

ema1 = ema(close, length1)


ema2 = ema(close, length2)
ema3 = ema(close, length3)
ema4 = ema(close, length4)

plot(ema1, color=color.rgb(139, 0, 0), title="EMA 1")


plot(ema2, color=color.rgb(73, 73, 73), title="EMA 2")
plot(ema3, color=color.rgb(0, 23, 128), title="EMA 3")
plot(ema4, color=color.rgb(100, 0, 117, 30), title="EMA 4")

//"Najwyższa i Najniższa Cena Dnia + Linie Fibonacciego", overlay = true)

// Wyszukanie najwyższej i najniższej ceny dnia


var float highestHigh = na
var float lowestLow = na

if change(time("D"))
highestHigh := high
lowestLow := low

if not na(highestHigh) and high > highestHigh


highestHigh := high

if not na(lowestLow) and low < lowestLow


lowestLow := low

// Obliczanie poziomów zniesienia Fibonacciego


fibLevels = input(0.618, title = "Poziom Fibonacciego")

fibHigh = highestHigh
fibLow = lowestLow
fibRange = fibHigh - fibLow

fib50 = fibLow + fibRange * 0.5


fib61_8 = fibLow + fibRange * fibLevels
fib38_2 = fibLow + fibRange * (1 - fibLevels)

// Rysowanie linii i etykiet


line.new(x1=bar_index[1], y1=fib50, x2=bar_index, y2=fib50, color=color.rgb(255, 255, 255), width=1)
line.new(x1=bar_index[1], y1=fib61_8, x2=bar_index, y2=fib61_8, color=color.rgb(255, 238, 4, 47), width=1)
line.new(x1=bar_index[1], y1=fib38_2, x2=bar_index, y2=fib38_2, color=color.rgb(176, 39, 39, 31), width=1)

// Rysowanie najwyższej i najniższej ceny dnia


plot(highestHigh, color = color.rgb(254, 255, 254), title = "Najwyższa Cena Dnia")
plot(lowestLow, color = color.rgb(185, 3, 3), title = "Najniższa Cena Dnia")

You might also like