Repository navigation
Expand file tree
/
Copy pathKeller_BAA_v2.cs
More file actions
325 lines (284 loc) · 12.1 KB
/
Copy pathKeller_BAA_v2.cs
File metadata and controls
325 lines (284 loc) · 12.1 KB
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
//==============================================================================
// Project: TuringTrader, algorithms from books & publications
// Name: Keller_BAA_v2
// Description: Bold Asset Allocation (BAA) strategy, as published in
// Wouter J. Keller's paper
// 'Relative and Absolute Momentum in Times of Rising/Low Yields:
// Bold Asset Allocation (BAA)'
// https://papers.ssrn.com/sol3/papers.cfm?abstract_id=4166845
// History: 2022xi21, EFB, created
//------------------------------------------------------------------------------
// Copyright: (c) 2011-2025, Bertram Enterprises LLC dba TuringTrader.
// https://www.turingtrader.org
// License: This file is part of TuringTrader, an open-source backtesting
// engine/ trading simulator.
// TuringTrader is free software: you can redistribute it and/or
// modify it under the terms of the GNU Affero General Public
// License as published by the Free Software Foundation, either
// version 3 of the License, or (at your option) any later version.
// TuringTrader is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public
// License along with TuringTrader. If not, see
// https://www.gnu.org/licenses/agpl-3.0.
//==============================================================================
// OPTIONAL_CHARTS
// if defined, render optional charts showing momentum
// in the offensive, defensive, and canary universes.
//#define OPTIONAL_CHARTS
#region libraries
using System;
using System.Collections.Generic;
using System.Linq;
using TuringTrader.GlueV2;
using TuringTrader.SimulatorV2;
using TuringTrader.SimulatorV2.Assets;
using TuringTrader.SimulatorV2.Indicators;
#endregion
namespace TuringTrader.BooksAndPubsV2
{
#region BAA Core
public abstract class Keller_BAA_Core : Algorithm
{
public override string Name => "Keller's Bold Asset Allocation (BAA)";
#region configuration
//--- offensive configuration
/// <summary>
/// Offensive universe.
/// </summary>
public abstract List<string> SEL_O { get; set; }
/// <summary>
/// Number of offensive assets to hold.
/// </summary>
public virtual int TO { get; set; }
//--- defensive configuration
/// <summary>
/// Defensive universe.
/// </summary>
public virtual List<string> SEL_D { get; set; } = new List<string>
{
ETF.TIP,
ETF.DBC,
ETF.BIL,
ETF.IEF,
ETF.TLT,
ETF.LQD,
ETF.BND,
};
/// <summary>
/// Cash asset (defaults to BIL).
/// </summary>
public virtual string CASH { get; set; } = ETF.BIL;
/// <summary>
/// Number of defensive assets to hold.
/// </summary>
public virtual int TD { get; set; } = 3;
//--- protective configuration
/// <summary>
/// Protective (canary) universe.
/// </summary>
public virtual List<string> SEL_P { get; set; } = new List<string>
{
ETF.SPY,
ETF.VWO,
ETF.VEA,
ETF.BNDX,
};
/// <summary>
/// Breadth parameter for protective universe.
/// </summary>
public virtual int B { get; set; } = 1;
//--- misc configuration
/// <summary>
/// Transaction cost (default = 0.1%).
/// </summary>
//public virtual double TC { get; set; } = 0.1;
public virtual OrderType ORDER_TYPE { get; set; } = OrderType.closeThisBar;
public virtual string BENCH { get; set; } = Benchmark.PORTFOLIO_60_40;
public virtual double RANK_MOM(string asset) =>
Asset(asset).Close[0] / Asset(asset).Monthly().Close.SMA(13)[0] - 1.0;
public virtual double PROT_MOM(string asset) =>
new List<int> { 1, 3, 6, 12 }.Sum(m =>
(12.0 / m) * (Asset(asset).Close[0] / Asset(asset).Monthly().Close[m] - 1.0));
#endregion
#region strategy logic
public override void Run()
{
//========== initialization ==========
StartDate = StartDate ?? AlgorithmConstants.START_DATE;
EndDate = EndDate ?? AlgorithmConstants.END_DATE;
WarmupPeriod = TimeSpan.FromDays(365);
// Keller assumes 0.1% transaction cost
//((Account_Default)Account).Friction = TC / 100.0;
((Account_Default)Account).Friction = AlgorithmConstants.FRICTION;
//========== simulation loop ==========
SimLoop(() =>
{
// Keller's strategy only trades once per month
if (SimDate.Month != NextSimDate.Month || IsFirstBar)
{
//----- qualify and rank assets
// rank offensive universe based on SMA(12) = SMA13
// NOTE: see paper, page 3, footnote 5:
// Keller's SMA(12) is the average of the last 13 prices
var offensiveMom = SEL_O
.ToDictionary(
a => a,
a => RANK_MOM(a));
var offensiveAssets = offensiveMom
.OrderByDescending(kv => kv.Value)
.Select(kv => kv.Key)
.Take(TO)
.ToList();
// rank defensive assets based on SMA(12). In addition,
// disqualify any assets with a return lower than BIL.
// Note that we only use this absolute momentum filter
// for the defensive (but not the offensive) universe.
var defensiveMom = SEL_D
.ToDictionary(
a => a,
a => RANK_MOM(a));
var defensiveAsssets = defensiveMom
.OrderByDescending(kv => kv.Value)
.Select(kv => kv.Value >= defensiveMom[CASH] ? kv.Key : CASH)
.Take(TD)
.ToList();
//----- money management
// evaluate canary universe based on 13612W momentum
// see paper, page 4, footnote 7 for calculation
var canaryMom = SEL_P
.ToDictionary(
a => a,
a => PROT_MOM(a));
var numBadCanaryAssets = canaryMom
.Where(kv => kv.Value < 0.0)
.Count();
// calculate offensive and defensive holdings based
// on the breadth of the canary universe
var pcntDefensive = Math.Min(1.0, (double)numBadCanaryAssets / B);
var pcntOffensive = 1.0 - pcntDefensive;
// initially, assume to close all open positions
var weights = Positions
.ToDictionary(
kv => kv.Key,
kv => 0.0);
// assign weights for offensive and defensive assets.
// note how we add to previously existing allocations, as
// the offensive and defensive assets may overlap.
foreach (var a in offensiveAssets)
weights[a] = (weights.ContainsKey(a) ? weights[a] : 0.0) + pcntOffensive / TO;
foreach (var a in defensiveAsssets)
weights[a] = (weights.ContainsKey(a) ? weights[a] : 0.0) + pcntDefensive / TD;
//----- order management
// we assume that Keller's simulation trades on the close of the month,
// but the order type can be configured to trade on the first open of the month
foreach (var kv in weights)
Asset(kv.Key).Allocate(kv.Value, ORDER_TYPE);
#if OPTIONAL_CHARTS
//----- optional charts (for debugging and analysis)
if (!IsOptimizing && Plotter.AllData.Count > 0)
{
Plotter.SelectChart("Offensive Universe", "Date");
Plotter.SetX(SimDate);
foreach (var kv in offensiveMom)
Plotter.Plot(Asset(kv.Key).Description, kv.Value);
Plotter.SelectChart("Defensive Universe", "Date");
Plotter.SetX(SimDate);
foreach (var kv in defensiveMom)
Plotter.Plot(Asset(kv.Key).Description, kv.Value);
Plotter.SelectChart("Canary Universe", "Date");
Plotter.SetX(SimDate);
foreach (var kv in canaryMom)
Plotter.Plot(Asset(kv.Key).Description, kv.Value);
Plotter.Plot("Offensive/ Defensive", numBadCanaryAssets == 0 ? SEL_P.Count : -SEL_P.Count);
}
#endif
}
//----- main chart
if (!IsOptimizing)
{
Plotter.SelectChart(Name, "Date");
Plotter.SetX(SimDate);
Plotter.Plot(Name, NetAssetValue);
Plotter.Plot(Asset(BENCH).Description, Asset(BENCH).Close[0]);
}
});
//========== post processing ==========
if (!IsOptimizing)
{
Plotter.AddTargetAllocation();
Plotter.AddHistoricalAllocations();
Plotter.AddTradeLog();
}
}
#endregion
}
#endregion
//--- main strategies
#region BAA-G12 (balanced)
public class Keller_BAA_G12 : Keller_BAA_Core
{
public override string Name => "Keller's Bold Asset Allocation (BAA-G12)";
public override List<string> SEL_O { get; set; } = new List<string>
{
ETF.SPY,
ETF.QQQ,
ETF.IWM,
ETF.VGK,
ETF.EWJ,
ETF.VWO,
ETF.VNQ,
ETF.DBC,
ETF.GLD,
ETF.TLT,
ETF.HYG,
ETF.LQD,
};
public override int TO => 6;
}
#endregion
#region BAA-G4 (aggressive)
public class Keller_BAA_G4 : Keller_BAA_Core
{
public override string Name => "Keller's Bold Asset Allocation (BAA-G4)";
public override List<string> SEL_O { get; set; } = new List<string>
{
ETF.QQQ,
ETF.VWO,
ETF.VEA,
ETF.BND,
};
public override int TO => 1;
}
#endregion
//--- strategy variants
#region BAA-G12/T3 (less diversified balanced)
public class Keller_BAA_G12T3 : Keller_BAA_G12
{
public override string Name => "Keller's Bold Asset Allocation (BAA-G12/T3)";
public override int TO => 3;
}
#endregion
#region BAA-G4/T2 (less concentrated aggressive)
public class Keller_BAA_G4T2 : Keller_BAA_G4
{
public override string Name => "Keller's Bold Asset Allocation (BAA-G4/T2)";
public override int TO => 2;
}
#endregion
#region BAA-SPY (max simplicity)
public class Keller_BAA_SPY : Keller_BAA_Core
{
public override string Name => "Keller's Bold Asset Allocation (BAA-SPY)";
public override List<string> SEL_O { get; set; } = new List<string>
{
ETF.SPY,
};
public override int TO => 1;
}
#endregion
}
//==============================================================================
// end of file