1UNIVERSE = the same seven equity indices
3function positions(bar):
4 # no signal, no timing: always fully invested
5 for symbol in UNIVERSE:
6 w[symbol] = 1 / vol_36m(symbol, bar.prev) # equal risk
8 return w
10# ── execution ────────────────────────────────────────────────────────
12COST_PER_TRADE = 15 bps of the notional that changes hands
14function on_bar(bar, book):
15 if not bar.is_month_end:
16 return # decisions are made monthly only
18 target = positions(bar) # the rule above
19 target = scale_to_gross(target, 1.0)
21 rebalance(book, target, bar)
23function scale_to_gross(w, limit):
24 gross = sum(abs(w)) # total capital at work
26 if gross == 0:
27 return w # flat is a valid target
29 return w * limit / gross # leverage fixed, never implicit
31function rebalance(book, target, bar):
32 for symbol in union(book.symbols, target.symbols):
33 delta = target[symbol] - book.weight(symbol)
35 if delta > 0:
36 buy(symbol, delta, fill = next_bar.close)
37 else if delta < 0:
38 sell(symbol, -delta, fill = next_bar.close)
40 book.charge(COST_PER_TRADE * abs(delta))
42# Orders are filled at the next month's close, never the one the decision
43# was made on. There is no stop_loss(), take_profit(), limit order or
44# position cap: a holding changes only when positions() returns a
45# different target at the next month end.