Professional Documents
Culture Documents
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
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])
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)
if (acompra)
strategy.entry("Buy", strategy.long)
if (goShort)
strategy.close("Buy")
if (avenda)
strategy.entry("Short", strategy.short)
if (goLong)
strategy.close("Short")
//strategy.set_range(start_date, end_date)