-
Notifications
You must be signed in to change notification settings - Fork 6
/
InstantaneousTrendAlgorithm.cs
520 lines (457 loc) · 20.2 KB
/
InstantaneousTrendAlgorithm.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
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using QuantConnect.Algorithm.CSharp.Common;
using QuantConnect.Data.Market;
using QuantConnect.Indicators;
using QuantConnect.Logging;
using QuantConnect.Orders;
using QuantConnect.Securities;
using QuantConnect.Util;
namespace QuantConnect.Algorithm.CSharp
{
class InstantaneousTrendAlgorithm : QCAlgorithm
{
#region "Variables"
private DateTime _startDate = new DateTime(2015, 9, 17);
private DateTime _endDate = new DateTime(2015, 9, 17);
private decimal _portfolioAmount = 10000;
private decimal _transactionSize = 15000;
private string symbol = "AAPL";
private int barcount = 0;
private RollingWindow<IndicatorDataPoint> Price;
private InstantaneousTrend trend;
private RollingWindow<IndicatorDataPoint> trendHistory;
#region lists
#endregion
#region "logging P&L"
// P & L
private int sharesOwned = 0;
decimal tradeprofit = 0m;
decimal tradefees = 0m;
decimal tradenet = 0m;
private decimal lasttradefees = 0;
decimal profit = 0m;
decimal fees = 0m;
private decimal netprofit = 0;
private decimal dayprofit = 0;
private decimal dayfees = 0;
private decimal daynet = 0;
private decimal lastprofit = 0;
private decimal lastfees = 0;
private decimal totalProfit = 0;
private int lasttradecount;
private DateTime tradingDate;
private decimal nExitPrice = 0;
private OrderStatus tradeResult;
#endregion
#region "Custom Logging"
private ILogHandler mylog = Composer.Instance.GetExportedValueByTypeName<ILogHandler>("CustomFileLogHandler");
private ILogHandler dailylog = Composer.Instance.GetExportedValueByTypeName<ILogHandler>("DailyFileLogHandler");
//private ILogHandler transactionlog = Composer.Instance.GetExportedValueByTypeName<ILogHandler>("TransactionFileLogHandler");
private readonly OrderTransactionFactory _orderTransactionFactory;
private string ondataheader = @"Time,BarCount,trade size,Volume,Open,High,Low,Close,EndTime,Period,DataType,IsFillForward,Time,Symbol,Value,Price,,Time,Price,Trend,comment,signal, Entry Price, Exit Price,Trade Result,orderId, unrealized, shares owned,trade profit, trade fees, trade net,last trade fees, profit, fees, net, day profit, day fees, day net, Portfolio Value";
private string dailyheader = @"Trading Date,Daily Profit, Daily Fees, Daily Net, Cum profit, Cum Fees, Cum Net, Trades/day, Portfolio Value, Shares Owned";
private string transactionheader = @"Symbol,Quantity,Price,Direction,Order Date,Settlement Date, Amount,Commission,Net,Nothing,Description,Action Id,Order Id,RecordType,TaxLotNumber";
private List<OrderTransaction> _transactions;
private List<OrderEvent> _orderEvents = new List<OrderEvent>();
private int _tradecount = 0;
#endregion
// Strategy
private InstantTrendStrategy iTrendStrategy;
private bool shouldSellOutAtEod = true;
private int orderId = 0;
private int tradesize;
private OrderSignal signal;
private decimal nEntryPrice = 0;
private string comment;
private OrderTransactionProcessor _orderTransactionProcessor = new OrderTransactionProcessor();
#endregion
/// <summary>
/// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
/// </summary>
/// <seealso cref="QCAlgorithm.SetStartDate(System.DateTime)"/>
/// <seealso cref="QCAlgorithm.SetEndDate(System.DateTime)"/>
/// <seealso cref="QCAlgorithm.SetCash(decimal)"/>
public override void Initialize()
{
#region logging
var algoname = this.GetType().Name;
mylog.Debug(algoname);
mylog.Debug(ondataheader);
dailylog.Debug(algoname);
dailylog.Debug(dailyheader);
_transactions = new List<OrderTransaction>();
string filepath = AssemblyLocator.ExecutingDirectory() + "transactions.csv";
if (File.Exists(filepath)) File.Delete(filepath);
#endregion
//Initialize dates
SetStartDate(_startDate);
SetEndDate(_endDate);
SetCash(_portfolioAmount);
//Add as many securities as you like. All the data will be passed into the event handler:
AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
// Indicators
Price = new RollingWindow<IndicatorDataPoint>(14); // The price history
// ITrend
trend = new InstantaneousTrend(7);
trendHistory = new RollingWindow<IndicatorDataPoint>(14);
// The ITrendStrategy
iTrendStrategy = new InstantTrendStrategy(symbol, 14, this);
iTrendStrategy.ShouldSellOutAtEod = shouldSellOutAtEod;
#region lists
#endregion
var security = Securities[symbol];
security.TransactionModel = new ConstantFeeTransactionModel(1.0m);
}
/// <summary>
/// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
/// </summary>
/// <param name="data">TradeBars IDictionary object with your stock data</param>
public void OnData(TradeBars data)
{
#region logging
comment = string.Empty;
tradingDate = this.Time;
#endregion
barcount++;
// Add the history for the bar
var time = this.Time;
Price.Add(idp(time, (data[symbol].Close + data[symbol].Open) / 2));
// Update the indicators
trend.Update(idp(time, Price[0].Value));
trendHistory.Add(CalculateNewTrendHistoryValue(barcount, time, Price, trend));
#region lists
#endregion
if (Portfolio[symbol].Invested)
{
tradesize = Math.Abs(Portfolio[symbol].Quantity);
}
else
{
tradesize = (int)(_transactionSize / Convert.ToInt32(Price[0].Value + 1));
}
if (barcount > 100)
comment = "";
CanceledUnfilledLimitOrder();
Strategy(data);
#region logging
sharesOwned = Portfolio[symbol].Quantity;
string logmsg =
string.Format(
"{0},{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}",
time,
barcount,
tradesize,
data[symbol].Volume,
data[symbol].Open,
data[symbol].High,
data[symbol].Low,
data[symbol].Close,
data[symbol].EndTime,
data[symbol].Period,
data[symbol].DataType,
data[symbol].IsFillForward,
data[symbol].Time,
data[symbol].Symbol,
data[symbol].Value,
data[symbol].Price,
"",
time.ToShortTimeString(),
Price[0].Value,
trend.Current.Value,
comment,
signal,
nEntryPrice,
nExitPrice,
tradeResult,
orderId,
Portfolio.TotalUnrealisedProfit,
sharesOwned,
tradeprofit,
tradefees,
tradenet,
Portfolio.TotalPortfolioValue,
"",
"",
""
);
mylog.Debug(logmsg);
// reset the trade profit
tradeprofit = 0;
tradefees = 0;
tradenet = 0;
#endregion
if (time.Hour == 16)
{
trend.Reset();
trendHistory.Reset();
barcount = 0;
Plot("Strategy Equity", "Portfolio", Portfolio.TotalPortfolioValue);
}
}
private IndicatorDataPoint CalculateNewTrendHistoryValue(int barcount, DateTime time, RollingWindow<IndicatorDataPoint> price, InstantaneousTrend tr)
{
if (barcount < 7 && barcount > 2)
{
return (idp(time, (price[0].Value + 2 * price[1].Value + price[2].Value) / 4));
}
else
{
return (idp(time, tr.Current.Value)); //add last iteration value for the cycle
}
}
/// <summary>
/// Run the strategy associated with this algorithm
/// </summary>
/// <param name="data">TradeBars - the data received by the OnData event</param>
private string Strategy(TradeBars data)
{
#region "Strategy Execution"
string ret = "";
if (SellOutEndOfDay(data))
{
// if there were limit order tickets to cancel, wait a bar to execute the strategy
iTrendStrategy.Barcount = barcount; // for debugging
iTrendStrategy.nEntryPrice = nEntryPrice;
signal = iTrendStrategy.ExecuteStrategy(data, tradesize, trend.Current, out comment);
#region lists
#endregion
}
#endregion
return ret;
}
/// <summary>
/// If the order did not fill within one bar, cancel it and assume the market moved away from the limit order
/// </summary>
private bool CanceledUnfilledLimitOrder()
{
#region "Unfilled Limit Orders"
bool retval = false;
//var tickets = Transactions.GetOrderTickets(p => p.Time > Transactions.UtcTime.AddMinutes(-2));
foreach (OrderTicket orderTicket in Transactions.GetOrderTickets().Where(orderTicket => orderTicket.Status == OrderStatus.Submitted || orderTicket.Status == OrderStatus.Invalid))
{
orderTicket.Cancel();
retval = true;
}
#endregion
return retval;
}
public bool SellOutEndOfDay(TradeBars data)
{
if (shouldSellOutAtEod)
{
if (this.Time.Hour == 15 && this.Time.Minute > 49 || this.Time.Hour == 16)
{
if (Portfolio[symbol].IsLong)
{
Sell(symbol, Portfolio[symbol].AbsoluteQuantity);
}
if (Portfolio[symbol].IsShort)
{
Buy(symbol, Portfolio[symbol].AbsoluteQuantity);
}
// Daily Profit
#region logging
if (this.Time.Hour == 16)
{
CalculateDailyProfits();
sharesOwned = Portfolio[symbol].Quantity;
var _transactionsAsCsv = CsvSerializer.Serialize<OrderTransaction>(",", _transactions, true);
StringBuilder sb = new StringBuilder();
foreach (string s in _transactionsAsCsv)
sb.AppendLine(s);
string attachment = sb.ToString();
Notify.Email("nicholasstein@cox.net",
"Todays Trades " + this.Time.ToLongDateString(),
"Number of Trades: " + _tradecount,
attachment);
SendTransactionsToFile();
_transactions = new List<OrderTransaction>();
}
#endregion
return false;
}
}
return true;
}
/// <summary>
/// Handle order events
/// </summary>
/// <param name="orderEvent">the order event</param>
public override void OnOrderEvent(OrderEvent orderEvent)
{
base.OnOrderEvent(orderEvent);
ProcessOrderEvent(orderEvent);
}
/// <summary>
/// Local processing of the order event
/// </summary>
/// <param name="orderEvent">OrderEvent - the order event</param>
private void ProcessOrderEvent(OrderEvent orderEvent)
{
var security = Securities[orderEvent.Symbol];
IEnumerable<OrderTicket> tickets;
var tm = this.BrokerageModel.GetTransactionModel(security);
if (orderEvent.Status == OrderStatus.Filled)
_orderEvents.Add(orderEvent);
orderId = orderEvent.OrderId;
tradeResult = orderEvent.Status;
switch (orderEvent.Status)
{
case OrderStatus.New:
case OrderStatus.None:
case OrderStatus.Submitted:
tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
break;
case OrderStatus.Canceled:
tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
iTrendStrategy.orderFilled = false;
break;
case OrderStatus.Filled:
case OrderStatus.PartiallyFilled:
tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
if (tickets != null)
{
foreach (OrderTicket ticket in tickets)
{
iTrendStrategy.orderFilled = true;
if (Portfolio[orderEvent.Symbol].Invested)
{
nEntryPrice = Portfolio[symbol].IsLong ? orderEvent.FillPrice : orderEvent.FillPrice * -1;
nExitPrice = 0;
}
else
{
nExitPrice = nEntryPrice < 0 ? orderEvent.FillPrice : orderEvent.FillPrice * -1;
nEntryPrice = 0;
}
#region "log the ticket as a OrderTransacton"
var transactionFactory = new OrderTransactionFactory((QCAlgorithm)this);
var t = transactionFactory.Create(orderEvent, ticket, false);
_transactions.Add(t);
_orderTransactionProcessor.ProcessTransaction(t);
_tradecount++;
if (_orderTransactionProcessor.TotalProfit != totalProfit)
{
CalculateTradeProfit();
}
totalProfit = _orderTransactionProcessor.TotalProfit;
#endregion
}
}
break;
}
}
#region "Profit Calculations for logging"
private void CalculateTradeProfit()
{
var lasttrade = _orderTransactionProcessor.Trades.LastOrDefault();
tradefees = _orderTransactionProcessor.LastTradeCommission;
if (lasttrade != null) tradeprofit = lasttrade.GainOrLoss;
}
private void CalculateDailyProfits()
{
foreach (SecurityHolding holding in Portfolio.Values)
{
#region logging
dayprofit = holding.Profit - lastprofit;
dayfees = holding.TotalFees - lastfees;
daynet = dayprofit - dayfees;
lastprofit = holding.Profit;
lastfees = holding.TotalFees;
string msg = String.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10}",
tradingDate.ToShortDateString(),
dayprofit,
dayfees,
daynet,
holding.Profit,
holding.TotalFees,
holding.Profit - holding.TotalFees,
_tradecount - lasttradecount,
Portfolio.TotalPortfolioValue,
sharesOwned,
""
);
dailylog.Debug(msg);
lasttradecount = _tradecount;
dayprofit = 0;
dayfees = 0;
daynet = 0;
#endregion
}
}
#endregion
public override void OnEndOfAlgorithm()
{
Debug(string.Format("\nAlgorithm Name: {0}\n Ending Portfolio Value: {1} ", this.GetType().Name, Portfolio.TotalPortfolioValue));
//foreach (string symbol in Symbols)
//{
// string filename = string.Format("ITrendDebug_{0}.csv", symbol);
// string filePath = @"C:\Users\JJ\Desktop\MA y señales\ITrend Debug\" + filename;
// // JJ do not delete this line it locates my engine\bin\debug folder
// // I just uncomment it when I run on my local machine
// filePath = AssemblyLocator.ExecutingDirectory() + filename;
// if (File.Exists(filePath)) File.Delete(filePath);
// File.AppendAllText(filePath, stockLogging[i].ToString());
// Debug(string.Format("\nSymbol Name: {0}, Ending Portfolio Value: {1} ", symbol, Portfolio[symbol].Profit));
//}
//SendOrderEventsToFile();
SendTradesToFile();
}
private void SendTradesToFile()
{
string filepath = AssemblyLocator.ExecutingDirectory() + "trades.csv";
if (File.Exists(filepath)) File.Delete(filepath);
var liststring = CsvSerializer.Serialize<MatchedTrade>(",", _orderTransactionProcessor.Trades, true);
using (StreamWriter fs = new StreamWriter(filepath, true))
{
foreach (var s in liststring)
fs.WriteLine(s);
fs.Flush();
fs.Close();
}
}
private void SendTransactionsToFile()
{
string filepath = AssemblyLocator.ExecutingDirectory() + "transactions.csv";
//if (File.Exists(filepath)) File.Delete(filepath);
var liststring = CsvSerializer.Serialize<OrderTransaction>(",", _transactions, true);
using (StreamWriter fs = new StreamWriter(filepath, true))
{
foreach (var s in liststring)
fs.WriteLine(s);
fs.Flush();
fs.Close();
}
}
private void SendOrderEventsToFile()
{
string filepath = AssemblyLocator.ExecutingDirectory() + "orderEvents.csv";
if (File.Exists(filepath)) File.Delete(filepath);
var liststring = CsvSerializer.Serialize<OrderEvent>(",", _orderEvents, true);
using (StreamWriter fs = new StreamWriter(filepath, true))
{
foreach (var s in liststring)
fs.WriteLine(s);
fs.Flush();
fs.Close();
}
}
/// <summary>
/// Convenience function which creates an IndicatorDataPoint
/// </summary>
/// <param name="time">DateTime - the bar time for the IndicatorDataPoint</param>
/// <param name="value">decimal - the value for the IndicatorDataPoint</param>
/// <returns>a new IndicatorDataPoint</returns>
/// <remarks>I use this function to shorten the a Add call from
/// new IndicatorDataPoint(this.Time, value)
/// Less typing.</remarks>
private IndicatorDataPoint idp(DateTime time, decimal value)
{
return new IndicatorDataPoint(time, value);
}
}
}