1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
//! [`Expr`]: the serializable strategy AST. A JSON tree (tagged by `"op"`) that the
//! website and the batch runner both emit; the engine's evaluator walks it against a data
//! context to produce a position matrix.
use serde::Deserialize;
fn default_true() -> bool {
true
}
fn default_stoch_d() -> usize {
3
}
fn default_macd_fast() -> usize {
12
}
fn default_macd_slow() -> usize {
26
}
fn default_macd_signal() -> usize {
9
}
fn default_bollinger_k() -> f64 {
2.0
}
fn default_cap_max_weight() -> f64 {
0.3
}
fn default_vol_target() -> f64 {
0.1
}
fn default_vol_target_n() -> usize {
63
}
#[derive(Debug, Deserialize)]
#[serde(tag = "op")]
pub enum Expr {
/// A raw input series by name (e.g. close, pe, revenue_growth).
Data { name: String },
/// A constant scalar value, broadcast across the panel.
Const { value: f64 },
/// Simple moving average of `of` over `n` days.
Average { of: Box<Expr>, n: usize },
/// Exponential moving average of `of` over `n` days.
Ema { of: Box<Expr>, n: usize },
/// Rolling standard deviation of `of` over `n` days.
Std { of: Box<Expr>, n: usize },
/// Relative Strength Index of `of` over `n` days.
Rsi { of: Box<Expr>, n: usize },
/// Percentage change of `of` over `n` days.
PctChange { of: Box<Expr>, n: usize },
/// 1 where `of` rose for `n` consecutive days, else 0.
Rise { of: Box<Expr>, n: usize },
/// `of` lagged forward by `n` days.
Shift { of: Box<Expr>, n: usize },
/// Rolling maximum of `of` over `n` days.
RollingMax { of: Box<Expr>, n: usize },
/// Rolling minimum of `of` over `n` days.
RollingMin { of: Box<Expr>, n: usize },
/// Bollinger mid band: the `n`-day simple moving average of `of`.
BollingerMid { of: Box<Expr>, n: usize },
/// Bollinger upper band: `sma(of, n) + k * std(of, n)` (`k` defaults to 2).
BollingerUpper {
of: Box<Expr>,
n: usize,
#[serde(default = "default_bollinger_k")]
k: f64,
},
/// Bollinger lower band: `sma(of, n) - k * std(of, n)` (`k` defaults to 2).
BollingerLower {
of: Box<Expr>,
n: usize,
#[serde(default = "default_bollinger_k")]
k: f64,
},
/// MACD line: `ema(of, fast) - ema(of, slow)` (defaults 12/26).
Macd {
of: Box<Expr>,
#[serde(default = "default_macd_fast")]
fast: usize,
#[serde(default = "default_macd_slow")]
slow: usize,
},
/// MACD signal line: `signal`-day EMA of the MACD line (defaults 12/26/9).
MacdSignal {
of: Box<Expr>,
#[serde(default = "default_macd_fast")]
fast: usize,
#[serde(default = "default_macd_slow")]
slow: usize,
#[serde(default = "default_macd_signal")]
signal: usize,
},
/// MACD histogram: MACD line minus its signal line (defaults 12/26/9).
MacdHist {
of: Box<Expr>,
#[serde(default = "default_macd_fast")]
fast: usize,
#[serde(default = "default_macd_slow")]
slow: usize,
#[serde(default = "default_macd_signal")]
signal: usize,
},
/// Donchian channel upper band: rolling `n`-day high of `of`.
DonchianHigh { of: Box<Expr>, n: usize },
/// Donchian channel lower band: rolling `n`-day low of `of`.
DonchianLow { of: Box<Expr>, n: usize },
/// Donchian channel mid-line: `(rolling_max + rolling_min) / 2` over `n` days.
DonchianMid { of: Box<Expr>, n: usize },
/// Average True Range over `n` days from high/low/close.
Atr {
high: Box<Expr>,
low: Box<Expr>,
close: Box<Expr>,
n: usize,
},
/// Normalized ATR (percent) over `n` days.
Natr {
high: Box<Expr>,
low: Box<Expr>,
close: Box<Expr>,
n: usize,
},
/// Williams %R over `n` days.
WillR {
high: Box<Expr>,
low: Box<Expr>,
close: Box<Expr>,
n: usize,
},
/// Commodity Channel Index over `n` days.
Cci {
high: Box<Expr>,
low: Box<Expr>,
close: Box<Expr>,
n: usize,
},
/// Stochastic %K over `n` days.
StochK {
high: Box<Expr>,
low: Box<Expr>,
close: Box<Expr>,
n: usize,
},
/// Stochastic %D: `d`-day average of %K over `n` days.
StochD {
high: Box<Expr>,
low: Box<Expr>,
close: Box<Expr>,
n: usize,
#[serde(default = "default_stoch_d")]
d: usize,
},
/// Aroon Up over `n` days from high.
AroonUp { high: Box<Expr>, n: usize },
/// Aroon Down over `n` days from low.
AroonDown { low: Box<Expr>, n: usize },
/// Average Directional Index over `n` days.
Adx {
high: Box<Expr>,
low: Box<Expr>,
close: Box<Expr>,
n: usize,
},
/// Plus Directional Indicator (+DI) over `n` days.
PlusDi {
high: Box<Expr>,
low: Box<Expr>,
close: Box<Expr>,
n: usize,
},
/// Minus Directional Indicator (−DI) over `n` days.
MinusDi {
high: Box<Expr>,
low: Box<Expr>,
close: Box<Expr>,
n: usize,
},
/// On-Balance Volume from close and volume.
Obv { close: Box<Expr>, volume: Box<Expr> },
/// Money Flow Index over `n` days.
Mfi {
high: Box<Expr>,
low: Box<Expr>,
close: Box<Expr>,
volume: Box<Expr>,
n: usize,
},
/// Volume-Weighted Average Price over `n` days from high/low/close/volume.
Vwap {
high: Box<Expr>,
low: Box<Expr>,
close: Box<Expr>,
volume: Box<Expr>,
n: usize,
},
/// 1 where `of` fell for `n` consecutive days, else 0.
Fall { of: Box<Expr>, n: usize },
/// 1 for the `n` highest values per row (cross-section), else 0.
IsLargest { of: Box<Expr>, n: usize },
/// 1 for the `n` lowest values per row (cross-section), else 0.
IsSmallest { of: Box<Expr>, n: usize },
/// 1 where `of` held true at least `nsatisfy` times within the last `nwindow` rows.
Sustain {
of: Box<Expr>,
nwindow: usize,
nsatisfy: Option<usize>,
},
/// 1 on the row where `of` turns false→true (rising edge).
IsEntry { of: Box<Expr> },
/// 1 on the row where `of` turns true→false (falling edge).
IsExit { of: Box<Expr> },
/// Hold true from an entry edge until an exit edge (or the `exit` panel is true).
ExitWhen { entry: Box<Expr>, exit: Box<Expr> },
/// Per-row quantile of `of` across symbols; collapses to one column (`quantile`).
QuantileRow { of: Box<Expr>, c: f64 },
/// Per-row winsorize: clip to empirical quantiles `lower`/`upper` in `[0, 1]`.
Winsorize {
of: Box<Expr>,
lower: f64,
upper: f64,
},
/// Per-row z-score `(x − mean) / std` (population std); constant rows → 0.
Zscore { of: Box<Expr> },
/// Per-row quantile buckets labeled 1..=`n` (NaN preserved).
Bucket { of: Box<Expr>, n: usize },
/// Per-row demean: subtract the cross-sectional mean of non-NaN cells.
Demean { of: Box<Expr> },
/// 1 where `l` is greater than `r`, else 0.
Gt { l: Box<Expr>, r: Box<Expr> },
/// 1 where `l` is less than `r`, else 0.
Lt { l: Box<Expr>, r: Box<Expr> },
/// 1 where `l` is greater than or equal to `r`, else 0.
Ge { l: Box<Expr>, r: Box<Expr> },
/// 1 where `l` is less than or equal to `r`, else 0.
Le { l: Box<Expr>, r: Box<Expr> },
/// Logical AND of two boolean panels.
And { l: Box<Expr>, r: Box<Expr> },
/// Logical OR of two boolean panels.
Or { l: Box<Expr>, r: Box<Expr> },
/// Logical NOT of a boolean panel (NaN is falsy, so `not` of NaN is 1).
Not { of: Box<Expr> },
/// Element-wise `l` + `r`.
Add { l: Box<Expr>, r: Box<Expr> },
/// Element-wise `l` − `r`.
Sub { l: Box<Expr>, r: Box<Expr> },
/// Element-wise `l` × `r`.
Mul { l: Box<Expr>, r: Box<Expr> },
/// Element-wise `l` ÷ `r`.
Div { l: Box<Expr>, r: Box<Expr> },
/// Negation (−`of`).
Neg { of: Box<Expr> },
/// Ceiling of `of`.
Ceil { of: Box<Expr> },
/// Cross-sectional rank of `of` per row; `pct` for 0..1 percentile, `ascending` sets direction.
Rank {
of: Box<Expr>,
#[serde(default = "default_true")]
pct: bool,
#[serde(default = "default_true")]
ascending: bool,
},
/// `of` kept only where `by` is true; elsewhere dropped.
Mask { of: Box<Expr>, by: Box<Expr> },
/// Scale each row so gross weight (Σ|w|) is 1; NaN preserved, zero rows unchanged.
NormalizeRow { of: Box<Expr> },
/// Scale each row of the weight panel `of` toward an annualized portfolio-vol
/// `target` (default 0.1) over a trailing `n`-return window (default 63) of
/// `prices`; deleverage only (scale capped at 1).
VolTarget {
of: Box<Expr>,
prices: Box<Expr>,
#[serde(default = "default_vol_target")]
target: f64,
#[serde(default = "default_vol_target_n")]
n: usize,
},
/// Stateful rotation: enter on `entry`, exit on `exit`, hold up to `nstocks_limit` (prioritised by `rank`). Price stops live in the backtest config, not here.
HoldUntil {
entry: Box<Expr>,
exit: Box<Expr>,
nstocks_limit: Option<usize>,
rank: Option<Box<Expr>>,
},
/// Hold `of`, refreshing on calendar `freq` (W/ME/QE) or on dates where `on` is true.
Rebalance {
of: Box<Expr>,
#[serde(default)]
freq: Option<String>,
#[serde(default)]
on: Option<Box<Expr>>,
},
/// Cross-sectionally regress `of` against the `by` factors, optionally adding a constant.
Neutralize {
of: Box<Expr>,
by: Vec<Expr>,
#[serde(default = "default_true")]
add_const: bool,
},
/// Neutralize `of` within each industry/sector.
NeutralizeIndustry {
of: Box<Expr>,
#[serde(default = "default_true")]
add_const: bool,
},
/// Rank `of` within each industry, optionally limited to `categories`.
IndustryRank {
of: Box<Expr>,
#[serde(default)]
categories: Option<Vec<String>>,
},
/// Cap each industry's gross weight in the weight panel `of` at `max_weight`
/// (scale that industry's names down proportionally; `max_weight` defaults to 0.3).
CapIndustry {
of: Box<Expr>,
#[serde(default = "default_cap_max_weight")]
max_weight: f64,
},
/// Aggregate `of` within each industry using `agg` (e.g. mean).
GroupbyCategory { of: Box<Expr>, agg: String },
/// Boolean mask: `1` where the symbol's industry equals `name` (exact, case-sensitive).
/// Shape follows `of`; symbols missing from the industry map are `0`.
InSector { of: Box<Expr>, name: String },
}