You are on page 1of 4

//@version=5

strategy("HEIKEN PROFIT FAST - BACK TESTE",


shorttitle="HEIKEN PROFIT FAST - BACK TESTE",
overlay = true)

import loxx/loxxexpandedsourcetypes/4

greencolor = #2DD204
redcolor = #D2042D

//factorial calc
fact(int n)=>
float a = 1
for i = 1 to n
a *= i
a

//alpha calc
_alpha(int period, int poles)=>
w = 2.0 * math.pi / period
float b = (1.0 - math.cos(w)) / (math.pow(1.414, 2.0 / poles) - 1.0)
float a = - b + math.sqrt(b * b + 2.0 * b)
a

//n-pole calc
_npolegf(float src, int period, int order)=>
coeffs = matrix.new<float>(order + 1, 3, 0.)
float a = _alpha(period, order)
for r = 0 to order
out = nz(fact(order) / (fact(order - r) * fact(r)), 1)
matrix.set(coeffs, r, 0, out)
matrix.set(coeffs, r, 1, math.pow(a, r))
matrix.set(coeffs, r, 2, math.pow(1.0 - a, r))
float filt = src * matrix.get(coeffs, order, 1)
int sign = 1
for r = 1 to order
filt += sign * matrix.get(coeffs, r, 0) * matrix.get(coeffs, r, 2) *
nz(filt[r])
sign *= -1
filt

//std filter
_filt(float src, int len, float filter)=>
float price = src
float filtdev = filter * ta.stdev(src, len)
price := math.abs(price - price[1]) < filtdev ? price[1] : price
price

smthtype = input.string("Kaufman", "Heiken-Ashi Better Smoothing", options =


["AMA", "T3", "Kaufman"], group= "Source Settings")
srcoption = input.string("Close", "Source", group= "Source Settings",
options =
["Close", "Open", "High", "Low", "Median", "Typical", "Weighted", "Average",
"Average Median Body", "Trend Biased", "Trend Biased (Extreme)",
"HA Close", "HA Open", "HA High", "HA Low", "HA Median", "HA Typical", "HA
Weighted", "HA Average", "HA Average Median Body", "HA Trend Biased", "HA Trend
Biased (Extreme)",
"HAB Close", "HAB Open", "HAB High", "HAB Low", "HAB Median", "HAB Typical",
"HAB Weighted", "HAB Average", "HAB Average Median Body", "HAB Trend Biased", "HAB
Trend Biased (Extreme)"])

period = input.int(25,'Period', group = "Basic Settings")


order = input.int(5,'Order', group = "Basic Settings", minval = 1)

filterop = input.string("Gaussian Filter", "Filter Options", options = ["Price",


"Gaussian Filter", "Both", "None"], group= "Filter Settings")
filter = input.float(1, "Filter Devaitions", minval = 0, group= "Filter Settings")
filterperiod = input.int(10, "Filter Period", minval = 0, group= "Filter Settings")

colorbars = input.bool(true, "Color bars?", group = "UI Options")


showSigs = input.bool(true, "Show signals?", group= "UI Options")

kfl=input.float(0.666, title="* Kaufman's Adaptive MA (KAMA) Only - Fast End",


group = "Moving Average Inputs")
ksl=input.float(0.0645, title="* Kaufman's Adaptive MA (KAMA) Only - Slow End",
group = "Moving Average Inputs")
amafl = input.int(2, title="* Adaptive Moving Average (AMA) Only - Fast", group =
"Moving Average Inputs")
amasl = input.int(30, title="* Adaptive Moving Average (AMA) Only - Slow", group =
"Moving Average Inputs")

haclose = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period,


close)
haopen = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period,
open)
hahigh = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period,
high)
halow = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period,
low)
hamedian = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period,
hl2)
hatypical = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period,
hlc3)
haweighted = request.security(ticker.heikinashi(syminfo.tickerid),
timeframe.period, hlcc4)
haaverage = request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period,
ohlc4)

float src = switch srcoption


"Close" => loxxexpandedsourcetypes.rclose()
"Open" => loxxexpandedsourcetypes.ropen()
"High" => loxxexpandedsourcetypes.rhigh()
"Low" => loxxexpandedsourcetypes.rlow()
"Median" => loxxexpandedsourcetypes.rmedian()
"Typical" => loxxexpandedsourcetypes.rtypical()
"Weighted" => loxxexpandedsourcetypes.rweighted()
"Average" => loxxexpandedsourcetypes.raverage()
"Average Median Body" => loxxexpandedsourcetypes.ravemedbody()
"Trend Biased" => loxxexpandedsourcetypes.rtrendb()
"Trend Biased (Extreme)" => loxxexpandedsourcetypes.rtrendbext()
"HA Close" => loxxexpandedsourcetypes.haclose(haclose)
"HA Open" => loxxexpandedsourcetypes.haopen(haopen)
"HA High" => loxxexpandedsourcetypes.hahigh(hahigh)
"HA Low" => loxxexpandedsourcetypes.halow(halow)
"HA Median" => loxxexpandedsourcetypes.hamedian(hamedian)
"HA Typical" => loxxexpandedsourcetypes.hatypical(hatypical)
"HA Weighted" => loxxexpandedsourcetypes.haweighted(haweighted)
"HA Average" => loxxexpandedsourcetypes.haaverage(haaverage)
"HA Average Median Body" => loxxexpandedsourcetypes.haavemedbody(haclose,
haopen)
"HA Trend Biased" => loxxexpandedsourcetypes.hatrendb(haclose, haopen,
hahigh, halow)
"HA Trend Biased (Extreme)" => loxxexpandedsourcetypes.hatrendbext(haclose,
haopen, hahigh, halow)
"HAB Close" => loxxexpandedsourcetypes.habclose(smthtype, amafl, amasl, kfl,
ksl)
"HAB Open" => loxxexpandedsourcetypes.habopen(smthtype, amafl, amasl, kfl,
ksl)
"HAB High" => loxxexpandedsourcetypes.habhigh(smthtype, amafl, amasl, kfl,
ksl)
"HAB Low" => loxxexpandedsourcetypes.hablow(smthtype, amafl, amasl, kfl, ksl)
"HAB Median" => loxxexpandedsourcetypes.habmedian(smthtype, amafl, amasl,
kfl, ksl)
"HAB Typical" => loxxexpandedsourcetypes.habtypical(smthtype, amafl, amasl,
kfl, ksl)
"HAB Weighted" => loxxexpandedsourcetypes.habweighted(smthtype, amafl, amasl,
kfl, ksl)
"HAB Average" => loxxexpandedsourcetypes.habaverage(smthtype, amafl, amasl,
kfl, ksl)
"HAB Average Median Body" => loxxexpandedsourcetypes.habavemedbody(smthtype,
amafl, amasl, kfl, ksl)
"HAB Trend Biased" => loxxexpandedsourcetypes.habtrendb(smthtype, amafl,
amasl, kfl, ksl)
"HAB Trend Biased (Extreme)" =>
loxxexpandedsourcetypes.habtrendbext(smthtype, amafl, amasl, kfl, ksl)
=> haclose

src := filterop == "Both" or filterop == "Price" and filter > 0 ? _filt(src,


filterperiod, filter) : src

out = _npolegf(src, period, order)

out := filterop == "Both" or filterop == "Gaussian Filter" and filter > 0 ?


_filt(out, filterperiod, filter) : out

sig = nz(out[1])

state = 0
if (out > sig)
state := 1
if (out < sig)
state := -1

pregoLong = out > sig and (nz(out[1]) < nz(sig[1]) or nz(out[1]) == nz(sig[1]))
pregoShort = out < sig and (nz(out[1]) > nz(sig[1]) or nz(out[1]) == nz(sig[1]))

contsw = 0
contsw := nz(contsw[1])
contsw := pregoLong ? 1 : pregoShort ? -1 : nz(contsw[1])

goLong = pregoLong and nz(contsw[1]) == -1


goShort = pregoShort and nz(contsw[1]) == 1

var color colorout = na


colorout := state == -1 ? redcolor : state == 1 ? greencolor : nz(colorout[1])
//plot(out, "N-Pole GF", color = colorout, linewidth = 3)
barcolor(colorbars ? colorout : na)
//loxxexpandedsourcetypes.habclose()
habcloser = loxxexpandedsourcetypes.habclose("AMA", 6, 20, 5, 25)
//habcloser = ta.ema(close,200)
plot(habcloser, color = color.yellow, linewidth = 2)
//ema12 = ta.ema(close,12)
//ema26 = ta.ema(close,26)
//plot(ema12, color = color.yellow, linewidth = 2)
//plot(ema26, color = color.rgb(59, 193, 255), linewidth = 2)

compra = showSigs and goLong// and ema12 > ema26


venda = showSigs and goShort// and ema12 < ema26

acompra = compra and close > habcloser// and close > close[1]// and close >
ta.ema(close,200)
avenda = venda and close < habcloser// and close < close[1]//nd close <
ta.ema(close,200)

plotshape(acompra, title = "Long", color = color.yellow, textcolor = color.yellow,


text = "COMPRA", style = shape.triangleup, location = location.belowbar, size =
size.tiny)
plotshape(avenda, title = "Short", color = color.fuchsia, textcolor =
color.fuchsia, text = "VENDA", style = shape.triangledown, location =
location.abovebar, size = size.tiny)

// Aplicação das regras de entrada e saída

if (acompra)
strategy.entry("Buy", strategy.long)
if (goShort)
strategy.close("Buy")

if (avenda)
strategy.entry("Short", strategy.short)
if (goLong)
strategy.close("Short")

start_date = timestamp(year=2008, month=1, day=1)


end_date = timestamp(year=2023, month=1, day=1)

//plot(sma, title="SMA", color=red, linewidth=2, trackprice=true,


style=plot.style_line)

//strategy.set_range(start_date, end_date)

You might also like