-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathConnors_ShortTermTrading.cs
More file actions
505 lines (399 loc) · 15.5 KB
/
Copy pathConnors_ShortTermTrading.cs
File metadata and controls
505 lines (399 loc) · 15.5 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//==============================================================================
// Project: TuringTrader, algorithms from books & publications
// Name: Connors_ShortTermTrading
// Description: Strategy, as published in Larry Connors and Cesar Alvarez book
// 'Short Term Trading Strategies That Work'.
// History: 2019iii28, 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.
//==============================================================================
//#define INCLUDE_TRIN_STRATEGY
#region libraries
using System;
using System.Collections.Generic;
using System.Linq;
using TuringTrader.Algorithms.Glue;
using TuringTrader.Indicators;
using TuringTrader.Optimizer;
using TuringTrader.Simulator;
#endregion
namespace TuringTrader.BooksAndPubs
{
#region common algorithm core
public abstract class Connors_ShortTermTrading_Core : AlgorithmPlusGlue
{
#region internal data
protected virtual string MARKET => "SPY";
protected virtual string VOLATILITY => "$VIX";
protected virtual OrderType ORDER_TYPE => OrderType.closeThisBar;
#if INCLUDE_TRIN_STRATEGY
private virtual string TRIN => "#SPXTRIN";
#endif
#endregion
protected abstract int Rules(Instrument market);
#region public override IEnumerable<Bar> Run(DateTime? startTime, DateTime? endTime)
public override IEnumerable<Bar> Run(DateTime? startTime, DateTime? endTime)
{
//========== initialization ==========
StartTime = startTime ?? Globals.START_TIME;
EndTime = endTime ?? Globals.END_TIME;
WarmupStartTime = StartTime - TimeSpan.FromDays(63);
Deposit(Globals.INITIAL_CAPITAL);
CommissionPerShare = Globals.COMMISSION;
var market = AddDataSource(MARKET);
var volatility = AddDataSource(VOLATILITY);
#if INCLUDE_TRIN_STRATEGY
AddDataSource(TRIN);
#endif
//========== simulation loop ==========
foreach (var s in SimTimes)
{
if (!HasInstrument(market) || !HasInstrument(volatility))
continue;
if (!Alloc.Allocation.ContainsKey(market.Instrument))
Alloc.Allocation[market.Instrument] = 0.0;
int buySell = Rules(market.Instrument);
//----- enter positions
if (market.Instrument.Position == 0 && buySell != 0)
{
int numShares = buySell * (int)Math.Floor(NetAssetValue[0] / market.Instrument.Close[0]);
Alloc.Allocation[market.Instrument] += buySell;
market.Instrument.Trade(numShares, OrderType.closeThisBar);
}
//----- exit positions
else if (market.Instrument.Position != 0 && buySell != 0)
{
Alloc.Allocation[market.Instrument] = 0.0;
market.Instrument.Trade(-market.Instrument.Position, ORDER_TYPE);
}
//----- output
if (!IsOptimizing && TradingDays > 0)
{
_plotter.AddNavAndBenchmark(this, market.Instrument);
_plotter.AddStrategyHoldings(this, market.Instrument);
if (Alloc.LastUpdate == SimTime[0])
_plotter.AddTargetAllocationRow(Alloc);
}
var v = 10.0 * NetAssetValue[0] / Globals.INITIAL_CAPITAL;
yield return Bar.NewOHLC(
string.Format("{0}-{1}", this.GetType().Name, market.Instrument.Symbol),
SimTime[0],
v, v, v, v, 0);
}
//========== post processing ==========
if (!IsOptimizing && TradingDays > 0)
{
_plotter.AddTargetAllocation(Alloc);
_plotter.AddOrderLog(this);
_plotter.AddPositionLog(this);
_plotter.AddPnLHoldTime(this);
_plotter.AddMfeMae(this);
_plotter.AddParameters(this);
}
FitnessValue = this.CalcFitness();
}
#endregion
}
#endregion
// Chapter 9: The 2-period RSI - The Trader's Holy Grail of Indicators?
#region The 2-period RSI under 5 on the S&P 500
public class Connors_ShortTermTrading_RsiUnder5 : Connors_ShortTermTrading_Core
{
public override string Name => "Connors' 2-Period RSI Under 5";
[OptimizerParam(0, 20, 1)]
public virtual int ENTRY_MAX_RSI { get; set; } = 5;
protected override int Rules(Instrument market)
{
//----- calculate indicators
var marketSma200 = market.Close.SMA(200);
var marketSma5 = market.Close.SMA(5);
var marketRsi2 = market.Close.RSI(2);
//----- enter positions
if (market.Position == 0)
{
if (market.Close[0] > marketSma200[0]
&& marketRsi2[0] < ENTRY_MAX_RSI)
{
return 1;
}
}
//----- exit positions
else
{
if (market.Close[0] > marketSma5[0])
{
return -1;
}
}
return 0;
}
}
#endregion
#region Cumulative RSIs Strategy
public class Connors_ShortTermTrading_CumulativeRsi : Connors_ShortTermTrading_Core
{
public override string Name => "Connors' Cumulative RSIs";
[OptimizerParam(1, 5, 1)]
public virtual int CUM_RSI_DAYS { get; set; } = 2;
[OptimizerParam(20, 80, 5)]
public virtual int ENTRY_MAX_CUM_RSI { get; set; } = 35;
[OptimizerParam(50, 90, 5)]
public virtual int EXIT_MIN_RSI { get; set; } = 65;
protected override int Rules(Instrument market)
{
//----- calculate indicators
var marketSma200 = market.Close.SMA(200);
var marketRsi2 = market.Close.RSI(2);
var marketCumRsi = marketRsi2.Sum(CUM_RSI_DAYS);
//----- enter positions
if (market.Position == 0)
{
if (market.Close[0] > marketSma200[0]
&& marketCumRsi[0] < ENTRY_MAX_CUM_RSI)
{
return 1;
}
}
//----- exit positions
else
{
if (marketRsi2[0] > EXIT_MIN_RSI)
{
return -1;
}
}
return 0;
}
}
#endregion
#region Chapter 10: Double 7's Strategy
public class Connors_ShortTermTrading_Double7 : Connors_ShortTermTrading_Core
{
public override string Name => "Connors' Double 7's";
[OptimizerParam(5, 10, 1)]
public virtual int DOUBLE_DAYS { get; set; } = 7;
protected override int Rules(Instrument market)
{
var marketSma200 = market.Close.SMA(200);
var marketHi7 = market.TypicalPrice().Highest(DOUBLE_DAYS);
var marketLo7 = market.TypicalPrice().Lowest(DOUBLE_DAYS);
//----- enter positions
if (market.Position == 0)
{
if (market.Close[0] > marketSma200[0]
&& marketLo7[0] < marketLo7[1])
{
return 1;
}
}
//----- exit positions
else
{
if (marketHi7[0] > marketHi7[1])
{
return -1;
}
}
return 0;
}
}
#endregion
// Chapter 12: 5 Strategies to Time the Market
#region 1. VIX Stretches Strategy
public class Connors_ShortTermTrading_VixStretches : Connors_ShortTermTrading_Core
{
public override string Name => "Connors' VIX Stretches";
[OptimizerParam(2, 5, 1)]
public virtual int LE1_MIN_VIX_DAYS { get; set; } = 3;
[OptimizerParam(1, 10, 1)]
public virtual int LE1_MIN_VIX_PCNT { get; set; } = 5;
[OptimizerParam(50, 90, 5)]
public virtual int LX_MIN_MKT_RSI { get; set; } = 65;
protected override int Rules(Instrument market)
{
Instrument volatility = FindInstrument(VOLATILITY);
//----- calculate indicators
var marketSma200 = market.Close.SMA(200);
var marketRsi2 = market.Close.RSI(2);
var volSma10 = volatility.Close.SMA(10);
var volStretch = Enumerable.Range(0, LE1_MIN_VIX_DAYS)
.Aggregate(true, (prev, idx) => prev
&& volatility.Close[idx] > volSma10[idx] * (1.0 + LE1_MIN_VIX_PCNT / 100.0));
//----- enter positions
if (market.Position == 0)
{
if (market.Close[0] > marketSma200[0] && volStretch)
return 1;
}
//----- exit positions
else
{
if (marketRsi2[0] > LX_MIN_MKT_RSI)
return -1;
}
return 0;
}
}
#endregion
#region 2. VIX RSI Strategy
public class Connors_ShortTermTrading_VixRsi : Connors_ShortTermTrading_Core
{
public override string Name => "Connors' VIX RSI";
[OptimizerParam(75, 100, 5)]
public virtual int LE2_MIN_VIX_RSI { get; set; } = 90;
[OptimizerParam(0, 50, 5)]
public virtual int LE2_MAX_MKT_RSI { get; set; } = 30;
[OptimizerParam(50, 90, 5)]
public virtual int LX_MIN_MKT_RSI { get; set; } = 65;
protected override int Rules(Instrument market)
{
Instrument volatility = FindInstrument(VOLATILITY);
//----- calculate indicators
var marketSma200 = market.Close.SMA(200);
var marketRsi2 = market.Close.RSI(2);
var volRsi2 = volatility.Close.RSI(2);
//----- enter positions
if (market.Position == 0)
{
if (market.Close[0] > marketSma200[0]
&& marketRsi2[0] < LE2_MAX_MKT_RSI
&& volRsi2[0] > LE2_MIN_VIX_RSI
&& volatility.Open[0] > volatility.Close[1])
{
return 1;
}
}
//----- exit positions
else
{
if (marketRsi2[0] > LX_MIN_MKT_RSI)
return -1;
}
return 0;
}
}
#endregion
#region 3. The TRIN
#if INCLUDE_TRIN_STRATEGY
public class Connors_ShortTermTrading_Trin : Connors_ShortTermTrading_Core
{
public override string Name => "Connors' TRIN";
[OptimizerParam(45, 75, 5)]
public virtual int LE3_MAX_MKT_RSI { get; set; } = 50;
[OptimizerParam(1, 5, 1)]
public virtual int LE3_MIN_TRIN_UP { get; set; } = 3;
[OptimizerParam(50, 90, 5)]
public virtual int LX_MIN_MKT_RSI { get; set; } = 65;
protected override int Rules()
{
//----- calculate indicators
var marketSma200 = _market.Close.SMA(200);
var marketRsi2 = _market.Close.RSI(2);
var trinDaysUp = Enumerable.Range(0, LE3_MIN_TRIN_UP)
.Aggregate(true, (prev, idx) => prev && _trin.Close[idx] > 1.0);
//----- enter positions
if (_market.Position == 0)
{
if (_market.Close[0] > marketSma200[0]
&& marketRsi2[0] < LE3_MAX_MKT_RSI
&& trinDaysUp)
{
return 1;
}
}
//----- exit positions
else
{
if (marketRsi2[0] > LX_MIN_MKT_RSI)
return -1;
}
return 0;
}
}
#endif
#endregion
#region 4. One More Market Timing Strategy with Cumulative RSIs
public class Connors_ShortTermTrading_MoreCumulativeRsi : Connors_ShortTermTrading_Core
{
public override string Name => "Connors' More Cumulative RSI";
[OptimizerParam(1, 5, 1)]
public virtual int LE4_RSI_CUM_DAYS { get; set; } = 2;
[OptimizerParam(20, 80, 5)]
public virtual int LE4_MAX_CUM_RSI { get; set; } = 45;
[OptimizerParam(50, 90, 5)]
public virtual int LX_MIN_MKT_RSI { get; set; } = 65;
protected override int Rules(Instrument market)
{
//----- calculate indicators
var marketSma200 = market.Close.SMA(200);
var marketRsi3 = market.Close.RSI(3);
var marketCumRsi = marketRsi3.Sum(LE4_RSI_CUM_DAYS);
//----- enter positions
if (market.Position == 0)
{
if (market.Close[0] > marketSma200[0] && marketCumRsi[0] < LE4_MAX_CUM_RSI)
return 1;
}
//----- exit positions
else
{
if (marketRsi3[0] > LX_MIN_MKT_RSI)
return -1;
}
return 0;
}
}
#endregion
#region 5. Trading on the Short Side - The S&P Short Strategy
public class Connors_ShortTermTrading_ShortSide : Connors_ShortTermTrading_Core
{
public override string Name => "Connors' Short";
[OptimizerParam(2, 7, 1)]
public virtual int LE5_MIN_MKT_UP { get; set; } = 4;
protected override int Rules(Instrument market)
{
//----- calculate indicators
var marketSma200 = market.Close.SMA(200);
var marketSma5 = market.Close.SMA(5);
var marketUpDays = Enumerable.Range(0, LE5_MIN_MKT_UP)
.Aggregate(true, (prev, idx) => prev
&& market.Close[idx] > market.Close[idx + 1]);
//----- enter positions
if (market.Position == 0)
{
if (market.Close[0] < marketSma200[0]
&& marketUpDays)
{
return -1;
}
}
//----- exit positions
else
{
if (market.Close[0] < marketSma5[0]
|| market.Close[0] > marketSma200[0]) // this line is not in the book
{
return 1;
}
}
return 0;
}
}
#endregion
}
//==============================================================================
// end of file