Regression Spread

Regression Spread is a strategy that trades AUDUSD and NZDUSD on 1-hour bars. It holds at most 2 open trades.

several marketsstatistics and matrices

regression-spread.abx
1strategy "Regression Spread"
2markets: AUDUSD, NZDUSD
3bars: 1h
4max_open: 2
5
6input lookback = 250
7input entry_z = 2.2
8
9aud = bars(AUDUSD)
10nzd = bars(NZDUSD)
11model = ols(aud.close, [nzd.close], lookback)
12z = zscore(model.residual, lookback)
13fit_ok = model.r_squared > 0.6
14flat = trades.open(tag: "spread").len == 0
15
16when fit_ok and z > entry_z and flat:
17sell market: AUDUSD, risk: 0.5%, stop: atr(14, on: aud) * 3, tag: "spread"
18buy market: NZDUSD, risk: 0.5%, stop: atr(14, on: nzd) * 3, tag: "spread"
19
20when fit_ok and z < -entry_z and flat:
21buy market: AUDUSD, risk: 0.5%, stop: atr(14, on: aud) * 3, tag: "spread"
22sell market: NZDUSD, risk: 0.5%, stop: atr(14, on: nzd) * 3, tag: "spread"
23
24when abs(z) < 0.25 or not fit_ok:
25close_all tag: "spread"
What this script says

Regression Spread is a strategy that trades AUDUSD and NZDUSD on 1-hour bars. It holds at most 2 open trades.

You can change 2 inputs: lookback (default 250) and entry_z (default 2.2).

It calculates aud as AUDUSD bars, nzd as NZDUSD bars, model as the ols (target aud.close, factors nzd.close, length lookback), z as the z-score of model.residual over lookback bars, fit_ok as whether model.r_squared is above 0.6 and flat as whether the number of open trades tagged "spread" is 0.

When fit_ok and z is above entry_z and flat, it sells AUDUSD at market, risking 0.5% of the balance, with a stop 3 × the 14-bar ATR of aud from the entry and tagged "spread"; it also buys NZDUSD at market, risking 0.5% of the balance, with a stop 3 × the 14-bar ATR of nzd from the entry and tagged "spread".

When fit_ok and z is below -entry_z and flat, it buys AUDUSD at market, risking 0.5% of the balance, with a stop 3 × the 14-bar ATR of aud from the entry and tagged "spread"; it also sells NZDUSD at market, risking 0.5% of the balance, with a stop 3 × the 14-bar ATR of nzd from the entry and tagged "spread".

When the absolute value of z is below 0.25 or not fit_ok, it closes all trades tagged "spread".

Try this in the Terminal. AlgoBars is free: $0 a month, no card needed.

Create my free account