// ============================================================================
// Shiller_EURUSD.c — Zorro / lite-C demonstration script (EUR/USD)
// ============================================================================
// GOAL
// -----
// This script illustrates two classic ideas often discussed in the
// 'variance bounds' / 'dividend–price ratio' (dp) literature:
//
// Part A) An *illustrative* check of "excess volatility":
// compare the variance of price P_t to a discounted-sum proxy P*_t
// constructed from a toy 'dividend' series D_t (here: carry proxy).
//
// Part B) A very simple *return predictability* example using the
// dividend-price ratio dp_t := log(D_t / P_t) to predict future
// K-day returns via a rolling univariate OLS slope.
//
// NOTES (Zorro / lite-C specifics)
// --------------------------------
// • This is a research demo; it trades with a minimal rule only to show how to
// translate a scalar signal into actions. It is *not* a strategy.
// • Positive series index in Zorro points to the PAST. Example: X[1] is
// the previous bar, X[K] is K bars ago; X[0] is the current bar.
// • `vars` are rolling series returned by `series(...)`; `var` is a scalar.
// • `asset("EUR/USD")` selects the EUR/USD symbol defined by your asset list.
// • `Lots` is set to 1 purely for a visible action in Part B.
//
// SAFETY / ROBUSTNESS
// -------------------
// • We clamp denominators with a small epsilon to avoid log(0) or division by 0.
// • Window sizes and horizons are explicitly checked against LookBack and Bar.
//
// VERSION
// -------
// Tested with Zorro 2.x / lite-C syntax.
// ============================================================================
function run()
{
// ------------------------------------------------------------------------
// SESSION / DATA SETTINGS
// ------------------------------------------------------------------------
BarPeriod = 1440; // 1440 minutes = 1 day bars
StartDate = 2010; // start year (use your data span)
LookBack = 600; // bars held in rolling series (must cover max windows)
asset("EUR/USD");
set(PLOTNOW); // auto-plot series as they are produced
// ------------------------------------------------------------------------
// PRICE SERIES
// ------------------------------------------------------------------------
// P_t: close price; R1: 1-bar (1-day) log return (not used later, kept for ref)
vars P = series(priceClose()); // P[0]=today, P[1]=yesterday, ...
vars R1 = series(log(P[0]/P[1])); // ln(P_t / P_{t-1})
// ------------------------------------------------------------------------
// "DIVIDEND" PROXY D_t (here: a constant carry series for demonstration)
// ------------------------------------------------------------------------
// In FX, a carry-like proxy could be the interest rate differential.
// Here we just set a constant daily carry to mimic ~1.5% per annum.
var eps = 1e-12; // small epsilon for safe divisions
var carryDaily = 0.015/252.; // ? 1.5% p.a. / 252 trading days
vars D = series(carryDaily); // D_t aligned with bars
// =========================================================================
// PART A: "Ex post" discounted-sum proxy P*_t for an excess-volatility check
// =========================================================================
// Construct a simple discounted sum of past D_t as a toy P*_t proxy:
// P*_t ? ?_{k=1..Kmax} D_{t-k} / (1+r_d)^k
// This is ONLY an illustration, not a proper present-value model.
int Kmax = 126; // look-back horizon (~6 months)
var r_d = 0.0001; // daily discount ? 0.01% (~2.5% p.a.)
vars Px = series(0); // rolling proxy P*_t
if (Bar > LookBack)
{
// Build discounted sum from *past* values of D (D[1]..D[Kmax])
var sumDisc = 0;
var disc = 1;
int k;
for (k=1; k<=Kmax; k++)
{
disc /= (1 + r_d); // (1+r)^(-k)
var Dp = D[k]; // D_{t-k}
sumDisc += disc * Dp;
}
Px[0] = sumDisc; // write current P*_t proxy
// Compare rolling variances of P and P* over a window W
int W = 500;
if (Bar > LookBack + Kmax + W)
{
// Means
var meanP = 0, meanPx = 0;
int i;
for (i=0; i<W; i++) { meanP += P[i]; meanPx += Px[i]; }
meanP /= (var)W;
meanPx /= (var)W;
// Sample variances
var varP = 0, varPx = 0;
for (i=0; i<W; i++) {
var a = P[i]-meanP;
var b = Px[i]-meanPx;
varP += a*a;
varPx += b*b;
}
varP /= (var)(W-1);
varPx /= (var)(W-1);
// Plots for visual inspection
plot("Var(P)", varP, NEW, 0);
plot("Var(P*)", varPx, 0, 0);
// Console line every ~50 bars
if (Bar%50==0)
printf("\n[EXCESS VOL] W=%d Var(P)=%.6g Var(P*)=%.6g ratio=%.3f",
W, varP, varPx, varP/(varPx+eps));
}
}
// =========================================================================
// PART B: Return predictability via the dividend-price ratio, dp_t
// =========================================================================
// We compute:
// dp_t := log(D_t / P_t)
// and regress *past* K-day realized returns on dp over a rolling window Wreg
// to estimate a slope 'beta'. A positive beta implies higher dp predicts
// higher future returns (in this toy setup).
//
// Then we convert the instantaneous dp z-score into a small long/short
// trade signal, clipped and scaled to the range [-0.5, +0.5] (Lev).
int K = 20; // horizon (~1 month)
vars DP = series(log(max(eps, D[0]) / max(eps, P[0]))); // dp_t series
vars RK = series(log(P[0]/P[K])); // realized K-day return
int Wreg = 500; // regression window
if (Bar > LookBack + K + Wreg)
{
// ----------------------------
// Rolling univariate OLS slope
// ----------------------------
var sumX=0, sumY=0, sumXX=0, sumXY=0;
int i;
for (i=0;i<Wreg;i++){
var x = DP[i]; // predictor
var y = RK[i]; // response (past K-day return)
sumX += x;
sumY += y;
sumXX += x*x;
sumXY += x*y;
}
var meanX = sumX/Wreg;
var meanY = sumY/Wreg;
var denom = sumXX - Wreg*meanX*meanX;
var beta = 0; // OLS slope
if (denom != 0)
beta = (sumXY - Wreg*meanX*meanY)/denom;
plot("beta(dp->Kret)", beta, NEW, 0);
// ----------------------------
// z-score of current dp_t
// ----------------------------
var meanDP=0, varDP=0;
for (i=0;i<Wreg;i++) meanDP += DP[i];
meanDP/=Wreg;
for (i=0;i<Wreg;i++){ var d=DP[i]-meanDP; varDP += d*d; }
varDP /= (Wreg-1);
var sDP = sqrt(max(eps,varDP));
var zDP = (DP[0]-meanDP)/sDP;
// Clip z to avoid huge outliers
var zClip = zDP;
if (zClip > 2) zClip = 2;
if (zClip < -2) zClip = -2;
// Direction follows beta sign
var sig = 0;
if (beta > 0) sig = zClip;
else if (beta < 0) sig = -zClip;
// ----------------------------
// POSITION TRANSLATION
// ----------------------------
// Map raw signal in [-2,+2] to leverage-like knob in [-1,+1],
// then cap at ±0.5 to keep actions small in the demo.
var Target = sig; // raw -2..+2
var MaxLev = 0.5; // clamp bound
var Lev = Target/2.0; // scale to -1..+1 then cap
if (Lev > MaxLev) Lev = MaxLev;
if (Lev < -MaxLev) Lev = -MaxLev;
// Minimal action rule:
// if Lev is meaningfully positive => long; if negative => short; else flat.
// We keep Lots=1 for visibility; no money management here.
if (Lev > 0.05) { exitShort(); enterLong(); Lots = 1; }
else if (Lev < -0.05) { exitLong(); enterShort(); Lots = 1; }
else { exitLong(); exitShort(); }
// Plots for monitoring
plot("z(dp)", zDP, 0, 0);
plot("lev", Lev, 0, 0);
}
// End of run() — Zorro handles bar stepping automatically.
}