PHP Code:
//@version=5
indicator("**", overlay = true,max_boxes_count=500,max_lines_count=500)
color greencolor = #2DD204
color redcolor = #D2042D
int Maxncomp = 5
int MaxLag = 20
int MaxArrayLength = 100
// Calculation of the function Sn, needed to calculate the eigenvalues
// Negative determinants are counted there
gaussSn(matrix<float> A, float l, int n)=>
array<float> w = array.new<float>(n, 0)
matrix<float> B = matrix.copy(A)
int count = 0
int cp = 0
float c = 0.
float s1 = 0.
float s2 = 0.
for i = 0 to n - 1
matrix.set(B, i, i, matrix.get(B, i, i) - l)
for k = 0 to n - 2
for i = k + 1 to n - 1
if matrix.get(B, k, k) == 0
for i1 = 0 to n - 1
array.set(w, i1, matrix.get(B, i1, k))
matrix.set(B, i1, k, matrix.get(B, i1, k + 1))
matrix.set(B, i1, k + 1, array.get(w, i1))
cp := cp + 1
c := matrix.get(B, i, k) / matrix.get(B, k, k)
for j = 0 to n - 1
matrix.set(B, i, j, matrix.get(B, i, j) - matrix.get(B, k, j) * c)
count := 0
s1 := 1
for i = 0 to n - 1
s2 := matrix.get(B, i, i)
if s2 < 0
count := count + 1
count
// Calculation of eigenvalues by the bisection method}
// The good thing is that as many eigenvalues are needed, so many will count,
// saves a lot of resources
gaussbisectionl(matrix<float> A, int k, int n)=>
float e1 = 0.
float maxnorm = 0.
float cn = 0.
float a1 = 0.
float b1 = 0.
float c = 0.
for i = 0 to n - 1
cn := 0
for j = 0 to n - 1
cn := cn + matrix.get(A, i, i)
if maxnorm < cn
maxnorm := cn
a1 := 0
b1 := 10 * maxnorm
e1 := 1.0 * maxnorm / 10000000
while math.abs(b1 - a1) > e1
c := 1.0 * (a1 + b1) / 2
if gaussSn(A, c, n) < k
a1 := c
else
b1 := c
float out = (a1 + b1) / 2.0
out
// Calculates eigenvectors for already computed eigenvalues
svector(matrix<float> A, float l, int n, array<float> V)=>
int cp = 0
matrix<float> B = matrix.copy(A)
float c = 0
array<float> w = array.new<float>(n, 0)
for i = 0 to n - 1
matrix.set(B, i, i, matrix.get(B, i, i) - l)
for k = 0 to n - 2
for i = k + 1 to n - 1
if matrix.get(B, k, k) == 0
for i1 = 0 to n - 1
array.set(w, i1, matrix.get(B, i1, k))
matrix.set(B, i1, k, matrix.get(B, i1, k + 1))
matrix.set(B, i1, k + 1, array.get(w, i1))
cp += 1
c := 1.0 * matrix.get(B, i, k) / matrix.get(B, k, k)
for j = 0 to n - 1
matrix.set(B, i, j, matrix.get(B, i, j) - matrix.get(B, k, j) * c)
array.set(V, n - 1, 1)
c := 1
for i = n - 2 to 0
array.set(V, i, 0)
for j = i to n - 1
array.set(V, i, array.get(V, i) - matrix.get(B, i, j) * array.get(V, j))
array.set(V, i, array.get(V, i) / matrix.get(B, i, i))
c += math.pow(array.get(V, i), 2)
for i = 0 to n - 1
array.set(V, i, array.get(V, i) / math.sqrt(c))
// Fast Singular SSA - "Caterpillar" method
// X-vector of the original series
// n-length
// l-lag length
// s-number of eigencomponents
// (there the original series is divided into components, and then restored, here you set how many components you need)
// Y - the restored row (smoothed by the caterpillar)
fastsingular(array<float> X, int n1, int l1, int s1)=>
int n = math.min(MaxArrayLength, n1)
int l = math.min(MaxLag, l1)
int s = math.min(Maxncomp, s1)
matrix<float> A = matrix.new<float>(l, l, 0.)
matrix<float> B = matrix.new<float>(n, l, 0.)
matrix<float> Bn = matrix.new<float>(l, n, 0.)
matrix<float> V = matrix.new<float>(l, n, 0.)
matrix<float> Yn = matrix.new<float>(l, n, 0.)
var array<float> vtarr = array.new<float>(l, 0.)
array<float> ls = array.new<float>(MaxLag, 0)
array<float> Vtemp = array.new<float>(MaxLag, 0)
array<float> Y = array.new<float>(n, 0)
int k = n - l + 1
// We form matrix A in the method that I downloaded from the site of the creators of this matrix S
for i = 0 to l - 1
for j = 0 to l - 1
matrix.set(A, i, j, 0)
for m = 0 to k - 1
matrix.set(A, i, j, matrix.get(A, i, j) + array.get(X, i + m) * array.get(X, m + j))
matrix.set(B, m, j, array.get(X, m + j))
//Find the eigenvalues and vectors of the matrix A
for i = 0 to s - 1
array.set(ls, i, gaussbisectionl(A, l - i, l))
svector(A, array.get(ls, i), l, Vtemp)
for j = 0 to l - 1
matrix.set(V, i, j, array.get(Vtemp, j))
// The restored matrix is formed
for i1 = 0 to s - 1
for i = 0 to k - 1
matrix.set(Yn, i1, i, 0)
for j = 0 to l - 1
matrix.set(Yn, i1, i, matrix.get(Yn, i1, i) + matrix.get(B, i, j) * matrix.get(V, i1, j))
for i = 0 to l - 1
for j = 0 to k - 1
matrix.set(Bn, i, j, matrix.get(V, i1, i) * matrix.get(Yn, i1, j))
//Diagonal averaging (series recovery)
int kb = k
int lb = l
for i = 0 to n - 1
matrix.set(Yn, i1, i, 0)
if i < lb - 1
for j = 0 to i
if l <= k
matrix.set(Yn, i1, i, matrix.get(Yn, i1, i) + matrix.get(Bn, j, i - j))
if l > k
matrix.set(Yn, i1, i, matrix.get(Yn, i1, i) + matrix.get(Bn, i - j, j))
matrix.set(Yn, i1, i, matrix.get(Yn, i1, i) / (1.0 * (i + 1)))
if (lb - 1 <= i) and (i < kb - 1)
for j = 0 to lb - 1
if l <= k
matrix.set(Yn, i1, i, matrix.get(Yn, i1, i) + matrix.get(Bn, j, i - j))
if l > k
matrix.set(Yn, i1, i, matrix.get(Yn, i1, i) + matrix.get(Bn, i - j, j))
matrix.set(Yn, i1, i, matrix.get(Yn, i1, i) / (1.0 * lb))
if kb - 1 <= i
for j = i - kb + 1 to n - kb
if l <= k
matrix.set(Yn, i1, i, matrix.get(Yn, i1, i) + matrix.get(Bn, j, i - j))
if l > k
matrix.set(Yn, i1, i, matrix.get(Yn, i1, i) + matrix.get(Bn, i - j, j))
matrix.set(Yn, i1, i, matrix.get(Yn, i1, i) / (1.0 * (n - i)))
// Here, if not summarized, then there will be separate decomposition components
// process by own functions
for i = 0 to n - 1
array.set(Y, i, 0)
for i1 = 0 to s - 1
array.set(Y, i, array.get(Y, i) + matrix.get(Yn, i1, i))
Y
float src = input.source(close, "Source", group = "Basic Settings")
int lag = input.int(10, "Lag", group = "Basic Settings")
int ncomp = input.int(2, "Number of Computations", group = "Basic Settings")
int ssapernorm = input.int(20, "SSA Period Normalization", group = "Basic Settings")
int numbars = input.int(33, "Number of Bars", group = "Basic Settings")
int backbars = input.int(40, "Number of Bars to Render", group = "Basic Settings", tooltip ="How many bars to plot.The higher the number the slower the computation.")
bool colorbars = input.bool(true, "Color bars?", group = "UI Options")
bool showSigs = input.bool(false, "Show signals?", group = "UI Options")
float out = 0.
float sig = 0.
float[] srcVal = array.new_float(numbars + 1, 0)
if last_bar_index - bar_index < backbars
for i = 0 to numbars - 1
array.set(srcVal, i, nz(src[i]))
float[] pv = fastsingular(srcVal, numbars, lag, ncomp)
out := array.get(pv, 0)
sig := out[1]
color colorout = out > sig ? greencolor : redcolor
bool goLong = ta.crossover(out, sig)
bool goShort = ta.crossunder(out, sig)
plot(last_bar_index - bar_index < backbars ? out : na, "TR", color = colorout, linewidth = 1)
barcolor(last_bar_index - bar_index < backbars and colorbars ? colorout : na)
//@version=5
// Custom cosh function
cosh(float x) =>
(math.exp(x) + math.exp(-x)) / 2
// Custom acosh function
acosh(float x) =>
x < 1 ? na : math.log(x + math.sqrt(x * x - 1))
// Custom sinh function
sinh(float x) =>
(math.exp(x) - math.exp(-x)) / 2
// Custom asinh function
asinh(float x) =>
math.log(x + math.sqrt(x * x + 1))
// Custom inverse tangent function
atan(float x) =>
math.pi / 2 - math.atan(1 / x)
// Chebyshev Type I Moving Average
chebyshevI(float src, int len, float ripple) =>
a = 0.
b = 0.
g = 0.
chebyshev = 0.
a := cosh(1 / len * acosh(1 / (1 - ripple)))
b := sinh(1 / len * asinh(1 / ripple))
g := (a - b) / (a + b)
chebyshev := (1 - g) * src + g * nz(chebyshev[1])
chebyshev
bool_to_float(bool source) =>
source ? 1 : 0
ema(source)=>
var float ema = 0.0
var int count = 0
count := nz(count[1]) + 1
ema := (1.0 - 2.0 / (count + 1.0)) * nz(ema[1]) + 2.0 / (count + 1.0) * source
ema
atan2(y, x) =>
var float angle = 0.0
if x > 0
angle := math.atan(y / x)
else
if x < 0 and y >= 0
angle := math.atan(y / x) + math.pi
else
if x < 0 and y < 0
angle := math.atan(y / x) - math.pi
else
if x == 0 and y > 0
angle := math.pi / 2
else
if x == 0 and y < 0
angle := -math.pi / 2
angle
degrees(float source) =>
source * 180 / math.pi
tra()=>
atr = ema(ta.tr)
slope = (close - close[10]) / (atr * 10)
angle_rad = atan2(slope, 1)
degrees = degrees(angle_rad)
source = ta.sma((degrees > 0 ? high : low), 2)
mats(source, length) =>
smooth = 0.
higher_high = math.max(math.sign(ta.change(ta.highest(length))), 0)
lower_low = math.max(math.sign(ta.change(ta.lowest(length)) * -1), 0)
time_constant = math.pow(ta.sma(bool_to_float(higher_high or lower_low), length), 2)
smooth := nz(smooth[1] + time_constant * (source - smooth[1]), source)
wilders_period = length * 4 - 1
atr = math.abs(nz(smooth[1]) - smooth)
ma_atr = ta.ema(atr, wilders_period)
delta_fast_atr = ta.ema(ma_atr, wilders_period) * length * 0.4
result = 0.0
if smooth > nz(result[1])
if smooth - delta_fast_atr < result[1]
result := result[1]
else
result := smooth - delta_fast_atr
else
if smooth + delta_fast_atr > result[1]
result := result[1]
else
result := smooth + delta_fast_atr
// Return
result
length = input.int(2, "Length", 2)
up_color = input.color(color.lime, "", inline = "color")
down_color = input.color(color.red, "", inline = "color")
enable_glow = input.bool(true, "Enable Glow", inline = "color")
mats = mats(tra(), length)
atr = ta.atr(length)
colour = ta.sma(close, 2) > mats ? up_color : down_color
atr_10 = ema(ta.tr) / 2
alpha = color.new(color.black, 100)
max = mats + atr_10
min = mats - atr_10
center = plot(mats, "TS", colour, editable = true)
Yer İmleri