Self-Learning Cyber Cycles

# ─────────────────────────────────────────────
# │ MarketFragments.com | DNA & Market │
# │ info@marketfragments.com │
# │ www.marketfragments.com │
# ─────────────────────────────────────────────
# Time →
# │
# █ █ █│ █
# █ █ █ │ █ █
# █ █ █ │ █ █ █ ╭─╮
# █ █ █ │ █ █ █ █ ╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ ╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ █ █ ╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ █ █ █ █╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ █ █ █ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ █ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ ╰─────╯
# ──────┴──────────────────────────────────────────────────────────────
# T1 T2 T3 T4 T5 T6
#
declare lower;
# Name: Cyber Cycles Self Learning Alpha for Correlation to Price
# --- Input Parameters ---
input price = close;
input pLength = 5; # Period length for the P-value calculation
input flength =100;
# Define three starting points for alpha values
def minAlpha1 = 0.0001;
def minAlpha2 = 0.001;
def minAlpha3 = 0.01;
def step = 0.001;
# Precompute the smoothing outside the fold
def smooth = ExpAverage(price, 4);
# --- Cycle, Max Correlation, and Best Alpha for minAlpha1 ---
def cycle1 = fold i1 = 0 to 50 with alphaResult = Double.NaN do (
compoundValue(6,
Sqr(1 - 0.5 * (minAlpha1 + i1 * step)) * (smooth - 2 * smooth[1] + smooth[2]) +
2 * (1 - (minAlpha1 + i1 * step)) * cycle1[1] -
Sqr(1 - (minAlpha1 + i1 * step)) * cycle1[2],
(price - 2 * price[1] + price[2]) / 4
)
);
def maxCorrelation1 = fold j1 = 0 to flength with highestCorrelation = -1.0 do (
if (if isnan(correlation(
compoundValue(6,
Sqr(1 - 0.5 * (minAlpha1 + j1 * step)) * (smooth - 2 * smooth[1] + smooth[2]) +
2 * (1 - (minAlpha1 + j1 * step)) * cycle1[1] -
Sqr(1 - (minAlpha1 + j1 * step)) * cycle1[2],
(price - 2 * price[1] + price[2]) / 4), price, pLength))
then 0 else
correlation(compoundValue(6,
Sqr(1 - 0.5 * (minAlpha1 + j1 * step)) * (smooth - 2 * smooth[1] + smooth[2]) +
2 * (1 - (minAlpha1 + j1 * step)) * cycle1[1] -
Sqr(1 - (minAlpha1 + j1 * step)) * cycle1[2],
(price - 2 * price[1] + price[2]) / 4), price, pLength))
> highestCorrelation
then correlation(compoundValue(6,
Sqr(1 - 0.5 * (minAlpha1 + j1 * step)) * (smooth - 2 * smooth[1] + smooth[2]) +
2 * (1 - (minAlpha1 + j1 * step)) * cycle1[1] -
Sqr(1 - (minAlpha1 + j1 * step)) * cycle1[2],
(price - 2 * price[1] + price[2]) / 4), price, pLength)
else highestCorrelation
);
def bestAlpha1 = fold k1 = 0 to flength with selectedAlpha = Double.NaN do (
if (if isnan(correlation(
compoundValue(6,
Sqr(1 - 0.5 * (minAlpha1 + k1 * step)) * (smooth - 2 * smooth[1] + smooth[2]) +
2 * (1 - (minAlpha1 + k1 * step)) * cycle1[1] -
Sqr(1 - (minAlpha1 + k1 * step)) * cycle1[2],
(price - 2 * price[1] + price[2]) / 4), price, pLength))
then 0 else
correlation(compoundValue(6,
Sqr(1 - 0.5 * (minAlpha1 + k1 * step)) * (smooth - 2 * smooth[1] + smooth[2]) +
2 * (1 - (minAlpha1 + k1 * step)) * cycle1[1] -
Sqr(1 - (minAlpha1 + k1 * step)) * cycle1[2],
(price - 2 * price[1] + price[2]) / 4), price, pLength))
== maxCorrelation1
then (minAlpha1 + k1 * step)
else selectedAlpha
);
# --- Repeat for minAlpha2 ---
def cycle2 = fold i2 = 0 to flength with alphaResult1 = Double.NaN do (
compoundValue(6,
Sqr(1 - 0.5 * (minAlpha2 + i2 * step)) * (smooth - 2 * smooth[1] + smooth[2]) +
2 * (1 - (minAlpha2 + i2 * step)) * cycle2[1] -
Sqr(1 - (minAlpha2 + i2 * step)) * cycle2[2],
(price - 2 * price[1] + price[2]) / 4
)
);
def maxCorrelation2 = fold j2 = 0 to 50 with highestCorrelation1 = -1.0 do (
if (if isnan(correlation(
compoundValue(6,
Sqr(1 - 0.5 * (minAlpha2 + j2 * step)) * (smooth - 2 * smooth[1] + smooth[2]) +
2 * (1 - (minAlpha2 + j2 * step)) * cycle2[1] -
Sqr(1 - (minAlpha2 + j2 * step)) * cycle2[2],
(price - 2 * price[1] + price[2]) / 4), price, pLength))
then 0 else
correlation(compoundValue(6,
Sqr(1 - 0.5 * (minAlpha2 + j2 * step)) * (smooth - 2 * smooth[1] + smooth[2]) +
2 * (1 - (minAlpha2 + j2 * step)) * cycle2[1] -
Sqr(1 - (minAlpha2 + j2 * step)) * cycle2[2],
(price - 2 * price[1] + price[2]) / 4), price, pLength))
> highestCorrelation1
then correlation(compoundValue(6,
Sqr(1 - 0.5 * (minAlpha2 + j2 * step)) * (smooth - 2 * smooth[1] + smooth[2]) +
2 * (1 - (minAlpha2 + j2 * step)) * cycle2[1] -
Sqr(1 - (minAlpha2 + j2 * step)) * cycle2[2],
(price - 2 * price[1] + price[2]) / 4), price, pLength)
else highestCorrelation1
);
def bestAlpha2 = fold k2 = 0 to flength with selectedAlpha2 = Double.NaN do (
if (if isnan(correlation(
compoundValue(6,
Sqr(1 - 0.5 * (minAlpha2 + k2 * step)) * (smooth - 2 * smooth[1] + smooth[2]) +
2 * (1 - (minAlpha2 + k2 * step)) * cycle2[1] -
Sqr(1 - (minAlpha2 + k2 * step)) * cycle2[2],
(price - 2 * price[1] + price[2]) / 4), price, pLength))
then 0 else
correlation(compoundValue(6,
Sqr(1 - 0.5 * (minAlpha2 + k2 * step)) * (smooth - 2 * smooth[1] + smooth[2]) +
2 * (1 - (minAlpha2 + k2 * step)) * cycle2[1] -
Sqr(1 - (minAlpha2 + k2 * step)) * cycle2[2],
(price - 2 * price[1] + price[2]) / 4), price, pLength))
== maxCorrelation2
then (minAlpha2 + k2 * step)
else selectedAlpha2
);
# --- Repeat for minAlpha3 ---
def cycle3 = fold i3 = 0 to flength with alphaResult3 = Double.NaN do (
compoundValue(6,
Sqr(1 - 0.5 * (minAlpha3 + i3 * step)) * (smooth - 2 * smooth[1] + smooth[2]) +
2 * (1 - (minAlpha3 + i3 * step)) * cycle3[1] -
Sqr(1 - (minAlpha3 + i3 * step)) * cycle3[2],
(price - 2 * price[1] + price[2]) / 4
)
);
def maxCorrelation3 = fold j3 = 0 to flength with highestCorrelation3 = -1.0 do (
if (if isnan(correlation(
compoundValue(6,
Sqr(1 - 0.5 * (minAlpha3 + j3 * step)) * (smooth - 2 * smooth[1] + smooth[2]) +
2 * (1 - (minAlpha3 + j3 * step)) * cycle3[1] -
Sqr(1 - (minAlpha3 + j3 * step)) * cycle3[2],
(price - 2 * price[1] + price[2]) / 4), price, pLength))
then 0 else
correlation(compoundValue(6,
Sqr(1 - 0.5 * (minAlpha3 + j3 * step)) * (smooth - 2 * smooth[1] + smooth[2]) +
2 * (1 - (minAlpha3 + j3 * step)) * cycle3[1] -
Sqr(1 - (minAlpha3 + j3 * step)) * cycle3[2],
(price - 2 * price[1] + price[2]) / 4), price, pLength))
> highestCorrelation3
then correlation(compoundValue(6,
Sqr(1 - 0.5 * (minAlpha3 + j3 * step)) * (smooth - 2 * smooth[1] + smooth[2]) +
2 * (1 - (minAlpha3 + j3 * step)) * cycle3[1] -
Sqr(1 - (minAlpha3 + j3 * step)) * cycle3[2],
(price - 2 * price[1] + price[2]) / 4), price, pLength)
else highestCorrelation3
);
def bestAlpha3 = fold k3 = 0 to flength with selectedAlpha3 = Double.NaN do (
if (if isnan(correlation(
compoundValue(6,
Sqr(1 - 0.5 * (minAlpha3 + k3 * step)) * (smooth - 2 * smooth[1] + smooth[2]) +
2 * (1 - (minAlpha3 + k3 * step)) * cycle3[1] -
Sqr(1 - (minAlpha3 + k3 * step)) * cycle3[2],
(price - 2 * price[1] + price[2]) / 4), price, pLength))
then 0 else
correlation(compoundValue(6,
Sqr(1 - 0.5 * (minAlpha3 + k3 * step)) * (smooth - 2 * smooth[1] + smooth[2]) +
2 * (1 - (minAlpha3 + k3 * step)) * cycle3[1] -
Sqr(1 - (minAlpha3 + k3 * step)) * cycle3[2],
(price - 2 * price[1] + price[2]) / 4), price, pLength))
== maxCorrelation3
then (minAlpha3 + k3 * step)
else selectedAlpha3
);
def cyclefinal1 = compoundValue(6, Sqr(1 - 0.5 *Round(bestAlpha1, 2)) * (smooth - 2 * smooth[1] + smooth[2]) + 2 * (1 -Round(bestAlpha1, 2)) * cyclefinal1[1] - Sqr(1 -Round(bestAlpha1, 2)) * cyclefinal1[2], (price - 2 * price[1] + price[2]) / 4);
def cyclefinal2 = compoundValue(6, Sqr(1 - 0.5 *Round(bestAlpha2, 2)) * (smooth - 2 * smooth[1] + smooth[2]) + 2 * (1 -Round(bestAlpha2, 2)) * cyclefinal2[1] - Sqr(1 -Round(bestAlpha2, 2)) * cyclefinal2[2], (price - 2 * price[1] + price[2]) / 4);
def cyclefinal3 = compoundValue(6, Sqr(1 - 0.5 *Round(bestAlpha3, 2) ) * (smooth - 2 * smooth[1] + smooth[2]) + 2 * (1 -Round(bestAlpha3, 2) ) * cyclefinal3[1] - Sqr(1 -Round(bestAlpha3, 2) ) * cyclefinal3[2], (price - 2 * price[1] + price[2]) / 4);
# Plots
plot ZeroLine = 0;
plot cycleTest1 =cyclefinal1 ;
plot Signal1 = cycleTest1[1];
plot cycleTest2 = cyclefinal2;
plot Signal2 = cycleTest2[1];
plot cycleTest3 =cyclefinal3;
plot Signal3 = cycleTest3[1];
cycleTest1.assignValueColor(if cyclefinal1 >cyclefinal1[1] then color.green else color.red);
cycleTest2.assignValueColor(if cyclefinal2>cyclefinal2[1] then color.green else color.red);
cycleTest3.assignValueColor(if cyclefinal3>cyclefinal3[1] then color.green else color.red);
ZeroLine.SetDefaultColor(color.white);
addCloud( cycleTest1,Signal1,color.green,color.red);
addCloud( cycleTest2,Signal2,color.green,color.red);
addCloud( cycleTest3,Signal3,color.green,color.red);
# Display Labels for Verification
AddLabel(yes, "Cycle 1 - Best Alpha: " + Round(bestAlpha1, 2) + " | Max Correlation: " + maxCorrelation1, Color.WHITE);
AddLabel(yes, "Cycle 2 - Best Alpha: " + Round(bestAlpha2, 2) + " | Max Correlation: " + maxCorrelation2, Color.WHITE);
AddLabel(yes, "Cycle 3 - Best Alpha: " + Round(bestAlpha3, 2) + " | Max Correlation: " + maxCorrelation3, Color.WHITE);

