the strategy encodes the Campbell–Shiller insight that high dividend/“carry” yields relative to price predict higher future returns, wraps that valuation in a z-scored ????????????, and lets a linear neural unit blend it with momentum/volatility states to produce directional edge; trades are taken when the learned edge clears a small threshold.
Code
// ============================================================================
// Shiller NN EUR/USD — ML trading demo (Zorro / lite-C)
//
// • Learner: PERCEPTRON (+BALANCED) on a compact 8-feature vector.
// • Trigger: edge = predL - predS with a simple RSI + momentum fallback.
// • Plots: (name, value, color, style); panels anchored correctly.
// ============================================================================
#include <default.c>
// ===== Configuration =====
#define LOOKBACK 200 // Bars required before running the model/logic
#define BarMins 1440 // Daily bars
#define LotsFixed 1 // Position size per entry
#define EdgeMin 0.05 // ML edge threshold (predL - predS) to trigger a trade
// ===== Safe helpers =====
var safe_div(var a, var b)
{
if (invalid(a) || invalid(b) || b == 0) return 0;
return a/b;
}
var safe_logratio(var a, var b)
{
if (invalid(a) || invalid(b) || b == 0) return 0;
var r = a/b;
if (invalid(r) || r <= 0) return 0;
return log(r);
}
// NOTE: plot(name, value, color, style)
// We'll pass color first, then style flags (e.g., LINE, NEW, MAIN, etc.)
void plot_safe(string name, var v, int color, int style)
{
if (!invalid(v) && v > -1e-12 && v < 1e12)
plot(name, v, color, style);
}
// ===== Main strategy =====
function run()
{
// Session (file-free; no RULES ? no rule files created/loaded)
StartDate = 2010;
EndDate = 2025;
BarPeriod = BarMins;
LookBack = LOOKBACK;
Capital = 10000;
set(PARAMETERS|PLOTNOW);
Hedge = 0;
asset("EUR/USD");
// Base series
vars PC = series(priceClose()); // Close series for returns/momentum
vars PX = series(price()); // Price series for RSI
// Derived series (allocated once, updated every bar)
static vars R1; if (!R1) R1 = series(0); // 1-bar log return
static vars MOM5; if (!MOM5) MOM5 = series(0); // 5-bar momentum
static vars MOM20; if (!MOM20) MOM20 = series(0); // 20-bar momentum
static vars DP; if (!DP) DP = series(0); // dp proxy = log(D/P)
if (Bar > 0) R1[0] = safe_logratio(PC[0], PC[1]); else R1[0] = 0;
if (Bar > 5) MOM5[0] = safe_logratio(PC[0], PC[5]); else MOM5[0] = 0;
if (Bar > 20) MOM20[0] = safe_logratio(PC[0], PC[20]); else MOM20[0] = 0;
// Constant carry vs price ? dp_t = log(D_t / P_t) (simple demonstration proxy)
var carryDaily = 0.015/252.; // ~1.5% p.a.
if (Bar > 0) DP[0] = safe_logratio(carryDaily, PC[0]); else DP[0] = 0;
// Indicators with warmup guards
var vola = 0; if (Bar >= 21) vola = StdDev(R1, 20);
var atr20 = 0; if (Bar >= 21) atr20 = ATR(20);
var rsi14 = 50; if (Bar >= 15) rsi14 = RSI(PX, 14);
// dp z-score (window Wz)
var zDP = 0; int Wz = 500;
if (Bar >= Wz) {
int i; var mean = 0, s2 = 0;
for (i = 0; i < Wz; i++) mean += DP[i];
mean /= (var)Wz;
for (i = 0; i < Wz; i++) { var d = DP[i] - mean; s2 += d*d; }
var sd = sqrt(max(1e-12, s2/(Wz - 1)));
if (sd > 0) {
zDP = (DP[0] - mean) / sd;
if (zDP > 10) zDP = 10;
if (zDP < -10) zDP = -10;
}
}
if (Bar < LOOKBACK) {
// Anchor price panel early so the chart opens immediately
plot_safe("Close", priceClose(), 0, MAIN|LINE);
return;
}
// Feature vector (?20)
var F[8];
F[0] = zDP;
F[1] = MOM5[0];
F[2] = MOM20[0];
F[3] = vola;
F[4] = rsi14/100.;
F[5] = safe_div(atr20, PC[0]);
F[6] = R1[0];
F[7] = safe_logratio(PC[0], PC[10]);
// Built-in ML (file-free: no RULES)
var predL = adviseLong (PERCEPTRON + BALANCED, 0, F, 8);
var predS = adviseShort(PERCEPTRON + BALANCED, 0, F, 8);
// Trading logic: edge trigger with simple fallback
var edge = predL - predS;
Lots = LotsFixed;
int didTrade = 0;
if (!invalid(edge) && edge > EdgeMin) {
exitShort(); enterLong(); didTrade = 1;
} else if (!invalid(edge) && edge < -EdgeMin) {
exitLong(); enterShort(); didTrade = 1;
}
if (!didTrade) {
if (rsi14 > 55 && MOM5[0] > 0) { exitShort(); enterLong(); didTrade = 1; }
else if (rsi14 < 45 && MOM5[0] < 0) { exitLong(); enterShort(); didTrade = 1; }
else { exitLong(); exitShort(); }
}
// ===== Plots (color, style) =====
// Price on the main chart
plot_safe("Close", priceClose(), 0, MAIN|LINE);
// Open a NEW indicator panel with 'edge', then add other lines to the same panel
plot_safe("edge", edge, 0, NEW|LINE); // NEW opens the panel
plot_safe("predL", predL, 0, LINE);
plot_safe("predS", predS, 0, LINE);
plot_safe("RSI", rsi14, 0, LINE);
plot_safe("z(dp)", zDP, 0, LINE);
}
A walkford version :
Code
// ============================================================================
// BLA03x10_WFO_train_test.c
// EUR/USD — Single-script ML trading with explicit Train/Test separation.
// Zorro / lite-C
//
// • Train run (click “Train”): writes per-cycle RULE files (no TESTNOW).
// • Test/Trade run (click “Test” or “Trade”): loads the RULE files created in Train.
// • Learner: PERCEPTRON + BALANCED
// • WFO supported via NumWFOCycles + DataHorizon (same in Train & Test).
// • Consistent asset() + algo() + advise* signature in both phases.
// • No ternary operator; guarded indicators; compact 8-feature vector.
// ============================================================================
#include <default.c>
// ===== Switches / Knobs =====
#define USE_WFO 1 // 0 = single IS block; 1 = Walk-Forward (IS/OOS cycles)
#define WFO_CYCLES 6 // number of WFO cycles (Train & Test must match)
#define WFO_OOS_BARS 252 // OOS length per cycle in bars (daily ? 1y)
#define LOOKBACK 200 // warmup bars before any model/logic
#define BarMins 1440 // 1D bars
#define LotsFixed 1 // fixed position size
#define EdgeMin 0.05 // edge = predL - predS threshold
// ===== Safe helpers =====
var safe_div(var a,var b){ if(invalid(a)||invalid(b)||b==0) return 0; return a/b; }
var safe_logratio(var a,var b){
if(invalid(a)||invalid(b)||b==0) return 0;
var r=a/b; if(invalid(r)||r<=0) return 0; return log(r);
}
// plot(name, value, color, style)
void plot_safe(string n, var v, int c, int s){ if(!invalid(v)&&v>-1e12&&v<1e12) plot(n,v,c,s); }
// ===== Main =====
function run()
{
// -------- Session (identical in Train and Test except RULES/Hedge flags) --------
StartDate = 2010;
EndDate = 2025;
BarPeriod = BarMins;
LookBack = LOOKBACK;
Capital = 10000;
if(USE_WFO){
NumWFOCycles = WFO_CYCLES;
DataHorizon = WFO_OOS_BARS; // OOS size per cycle
} else {
NumWFOCycles = 1;
DataHorizon = 0;
}
set(PARAMETERS|PLOTNOW); // common flags
// Explicit Train/Test separation (no TESTNOW auto phase switching)
if(Train){
set(RULES); // write RULE files during Train only
Hedge = 2; // generate both L/S samples while training
} else {
Hedge = 0; // Test/Trade loads RULE files, no writing
}
asset("EUR/USD");
algo("dp"); // part of RULE key: <Asset>:<Algo>:<L|S>
// -------- Base series --------
vars PC = series(priceClose()); // close for returns/momentum
vars PX = series(price()); // price for RSI
// -------- Derived series (allocated once, updated every bar) --------
static vars R1; if(!R1) R1 = series(0); // 1-bar log return
static vars MOM5; if(!MOM5) MOM5 = series(0); // 5-bar log momentum
static vars MOM20; if(!MOM20) MOM20 = series(0); // 20-bar log momentum
static vars DP; if(!DP) DP = series(0); // dp proxy = log(D/P)
if(Bar > 0) R1[0] = safe_logratio(PC[0],PC[1]); else R1[0] = 0;
if(Bar > 5) MOM5[0] = safe_logratio(PC[0],PC[5]); else MOM5[0] = 0;
if(Bar > 20) MOM20[0] = safe_logratio(PC[0],PC[20]); else MOM20[0] = 0;
var carryDaily = 0.015/252.;
if(Bar > 0) DP[0] = safe_logratio(carryDaily, PC[0]); else DP[0] = 0;
// -------- Indicators with guards --------
var vola = 0; if(Bar >= 21) vola = StdDev(R1,20);
var atr20 = 0; if(Bar >= 21) atr20 = ATR(20);
var rsi14 = 50; if(Bar >= 15) rsi14 = RSI(PX,14);
// -------- dp z-score (window Wz) --------
var zDP = 0; int Wz = 500;
if(Bar >= Wz){
int i; var mean=0, s2=0;
for(i=0;i<Wz;i++) mean += DP[i];
mean /= (var)Wz;
for(i=0;i<Wz;i++){ var d = DP[i]-mean; s2 += d*d; }
var sd = sqrt(max(1e-12, s2/(Wz-1)));
if(sd > 0){
zDP = (DP[0]-mean)/sd;
if(zDP > 10) zDP = 10;
if(zDP < -10) zDP = -10;
}
}
// Anchor main panel early
if(Bar < LOOKBACK){
plot_safe("Close", priceClose(), BLACK, MAIN|LINE);
return;
}
// -------- Feature vector (? 20) --------
var F[8];
F[0] = zDP;
F[1] = MOM5[0];
F[2] = MOM20[0];
F[3] = vola;
F[4] = rsi14/100.;
F[5] = safe_div(atr20, PC[0]);
F[6] = R1[0];
F[7] = safe_logratio(PC[0], PC[10]);
// -------- Built-in ML calls (IDENTICAL in Train & Test) --------
// Important: same method flags, same slot number, same feature length & order,
// same asset() and algo() ? ensures matching rule keys and file names.
var predL = adviseLong (PERCEPTRON + BALANCED, 0, F, 8);
var predS = adviseShort(PERCEPTRON + BALANCED, 0, F, 8);
// -------- Execution logic (common) --------
var edge = predL - predS;
Lots = LotsFixed;
int didTrade = 0;
if(!invalid(edge) && edge > EdgeMin){ exitShort(); enterLong(); didTrade = 1; }
else if(!invalid(edge) && edge < -EdgeMin){ exitLong(); enterShort(); didTrade = 1; }
if(!didTrade){
if(rsi14 > 55 && MOM5[0] > 0) { exitShort(); enterLong(); didTrade = 1; }
else if(rsi14 < 45 && MOM5[0] < 0) { exitLong(); enterShort(); didTrade = 1; }
else { exitLong(); exitShort(); }
}
// -------- Plots --------
plot_safe("Close", priceClose(), BLACK, MAIN|LINE);
plot_safe("edge", edge, GREEN, NEW|LINE);
plot_safe("predL", predL, BLUE, LINE);
plot_safe("predS", predS, RED, LINE);
plot_safe("RSI", rsi14, 0x404040, LINE);
plot_safe("z(dp)", zDP, 0x800080, LINE);
}