/
Soto_SectorRotation.cs
241 lines (208 loc) · 10.8 KB
/
Soto_SectorRotation.cs
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
//==============================================================================
// Project: TuringTrader, algorithms from books & publications
// Name: Soto_SectorRotation
// Description: Strategy, as published in Francois Soto's articles on
// Factor-Based (SeekingAlpha):
// https://factorbased.com/seekingalpha/
// https://seekingalpha.com/article/4394646-this-sector-rotation-strategy-made-17-percent-year-since-1991
// https://seekingalpha.com/article/4431639-this-sector-rotation-strategy-made-17-percent-each-year-since-1991-part-2
// https://seekingalpha.com/article/4434713-sector-rotation-strategy-using-the-high-yield-spread
// History: 2021vii21, FUB, created
//------------------------------------------------------------------------------
// Copyright: (c) 2011-2023, 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.
//==============================================================================
#region libraries
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using TuringTrader.Algorithms.Glue;
using TuringTrader.Indicators;
using TuringTrader.Optimizer;
using TuringTrader.Simulator;
#endregion
namespace TuringTrader.BooksAndPubs
{
public class Soto_SectorRotation_HighYieldSpread : AlgorithmPlusGlue
{
public override string Name => "Soto's Sector Rotation (High Yield Spread)";
#region inputs
[OptimizerParam(5, 10, 1)]
public int LONG_TERM_FLT_LEN = 10;
[OptimizerParam(21, 252, 21)]
public int TREND_FLT_LEN = 63;
[OptimizerParam(5, 45, 5)]
public int TREND_FLT_WIDTH = 15;
public virtual Algorithm PF_DECLINE => new Soto_SectorRotation_Decline();
public virtual Algorithm PF_RECOVERY => new Soto_SectorRotation_Recovery();
public virtual Algorithm PF_EARLY => new Soto_SectorRotation_Early();
public virtual Algorithm PF_LATE => new Soto_SectorRotation_Late();
private readonly string BENCHMARK = Indices.SPXTR;
public virtual DateTime START_DATE { get; set; } = DateTime.Parse("01/01/1990", CultureInfo.InvariantCulture);
public virtual DateTime END_DATE { get; set; } = DateTime.Now.Date;
#endregion
#region separate portfolios for the four economic phases
public class Soto_SectorRotation_Decline : LazyPortfolio
{
// Economic output and corporate earnings are negative;
// Outperformers: Staples.
public override string Name => "Soto's Sector Rotation (Decline)";
public override HashSet<Tuple<object, double>> ALLOCATION => new HashSet<Tuple<object, double>>
{
new Tuple<object, double>(Assets.XLP, 0.0), // XLP
};
public override string BENCH => Indices.SPXTR;
}
public class Soto_SectorRotation_Recovery : LazyPortfolio
{
// Economic output and corporate earnings are recovering;
// Outperformers: Materials and discretionary.
public override string Name => "Soto's Sector Rotation (Recovery)";
public override HashSet<Tuple<object, double>> ALLOCATION => new HashSet<Tuple<object, double>>
{
new Tuple<object, double>(Assets.XLB, 0.0), // XLB
new Tuple<object, double>(Assets.XLY, 0.0), // XLY
};
public override string BENCH => Indices.SPXTR;
}
public class Soto_SectorRotation_Early : LazyPortfolio
{
// Economic output and corporate earnings are growing;
// Outperformers: Energy, financials and industrials
public override string Name => "Soto's Sector Rotation (Early)";
public override HashSet<Tuple<object, double>> ALLOCATION => new HashSet<Tuple<object, double>>
{
new Tuple<object, double>(Assets.XLE, 0.0), // XLE
new Tuple<object, double>(Assets.XLF, 0.0), // XLF
new Tuple<object, double>(Assets.XLI, 0.0), // XLI
};
public override string BENCH => Indices.SPXTR;
}
public class Soto_SectorRotation_Late : LazyPortfolio
{
// Economic output and corporate earnings are slowing;
// Outperformers: Info tech and health care.
public override string Name => "Soto's Sector Rotation (Late)";
public override HashSet<Tuple<object, double>> ALLOCATION => new HashSet<Tuple<object, double>>
{
new Tuple<object, double>(Assets.XLK, 0.0), // XLK
new Tuple<object, double>(Assets.XLV, 0.0), // XLV
};
public override string BENCH => Indices.SPXTR;
}
#endregion
#region core logic
public override void Run()
{
//========== initialization ==========
StartTime = Globals.START_TIME;
EndTime = Globals.END_TIME;
Deposit(1e6);
// The high-yield spread is simply the difference between the
// borrowing rate for below-investment-grade corporate bonds
// and a treasury bond measure.
// Soto uses the Barclays Corporate High Yield Index minus
// the 10-Year Treasuries Yield. He also points at a similar
// spread on FRED: https://fred.stlouisfed.org/series/BAMLH0A0HYM2
var treasuryYieldDs = AddDataSource("%10YTCM"); // US 10-Year Treasury Constant Maturity Yield
var corporateYieldDs = AddDataSource("%COBAA"); // Moody's Seasoned Corporate Bonds BAA Yield
var pfDecline = AddDataSource(PF_DECLINE);
var pfRecovery = AddDataSource(PF_RECOVERY);
var pfEarly = AddDataSource(PF_EARLY);
var pfLate = AddDataSource(PF_LATE);
var universe = new List<DataSource> { pfDecline, pfRecovery, pfEarly, pfLate };
var bench = AddDataSource(BENCHMARK);
//========== simulation loop ==========
var yieldSpreadRising = true;
foreach (var simTime in SimTimes)
{
if (!HasInstrument(treasuryYieldDs) || !HasInstrument(corporateYieldDs))
continue;
var yieldSpread = corporateYieldDs.Instrument.Close
.Subtract(treasuryYieldDs.Instrument.Close)
.EMA(3).EMA(3).EMA(3);
var yieldSpreadLongTermAvg = yieldSpread.EMA(LONG_TERM_FLT_LEN * 252);
var yieldSignal = yieldSpread.Subtract(yieldSpreadLongTermAvg);
var yieldSpreadHigh = yieldSignal[0] > 0.0;
// NOTE: In his article, Soto does not disclose how he is determining
// rising and falling trends. This our best guess, which is most likely
// different from the Soto's method.
var yieldSignalMax = yieldSignal.Highest(TREND_FLT_LEN);
var yieldSignalMin = yieldSignal.Lowest(TREND_FLT_LEN);
var yieldSignalR = (yieldSignal[0] - yieldSignalMin[0]) / Math.Max(1e-10, yieldSignalMax[0] - yieldSignalMin[0]);
if (yieldSignalR > 1.0 - TREND_FLT_WIDTH / 100.0) yieldSpreadRising = true;
else if (yieldSignalR < TREND_FLT_WIDTH / 100.0) yieldSpreadRising = false;
if (!HasInstruments(universe))
continue;
if (SimTime[0].Month != NextSimTime.Month)
{
var weights = Instruments
.ToDictionary(i => i, i => 0.0);
if (yieldSpreadHigh)
{
if (yieldSpreadRising) weights[pfDecline.Instrument] = 1.0;
else weights[pfRecovery.Instrument] = 1.0;
}
else
{
if (yieldSpreadRising) weights[pfLate.Instrument] = 1.0;
else weights[pfEarly.Instrument] = 1.0;
}
foreach (var i in Instruments)
{
var shares = (int)Math.Round(weights[i] * NetAssetValue[0] / i.Close[0]);
i.Trade(shares - i.Position);
if (universe.Select(ds => ds.Instrument).Contains(i))
Alloc.Allocation[i] = weights[i];
}
}
// plotter output
if (!IsOptimizing && TradingDays > 0)
{
_plotter.AddNavAndBenchmark(this, FindInstrument(BENCHMARK));
_plotter.AddStrategyHoldings(this, universe.Select(ds => ds.Instrument));
if (Alloc.LastUpdate == SimTime[0])
_plotter.AddTargetAllocationRow(Alloc);
_plotter.SelectChart("Yield Spreads", "Date");
_plotter.SetX(SimTime[0]);
_plotter.Plot("High Yield Spread", yieldSpread[0]);
_plotter.Plot("Long-Term Average", yieldSpreadLongTermAvg[0]);
_plotter.SelectChart("Yield Signal", "Date");
_plotter.SetX(SimTime[0]);
_plotter.Plot("Yield Signal", yieldSignal[0]);
_plotter.Plot("Trend+", yieldSignalMax[0]);
_plotter.Plot("Trend-", yieldSignalMin[0]);
_plotter.Plot("", 0.0);
}
}
//========== post processing ==========
if (!IsOptimizing)
{
_plotter.AddTargetAllocation(Alloc);
_plotter.AddOrderLog(this);
_plotter.AddPositionLog(this);
_plotter.AddPnLHoldTime(this);
_plotter.AddMfeMae(this);
_plotter.AddParameters(this);
}
FitnessValue = this.CalcFitness();
}
#endregion
}
}
//==============================================================================
// end of file