18 using System.Collections.Concurrent;
19 using System.Collections.Generic;
20 using System.Diagnostics;
23 using Newtonsoft.Json;
43 private DateTime _testStartTime;
44 private DateTime _lastRuntimeStatisticsDate;
45 private DateTime _lastAlphaRuntimeStatisticsDate;
47 private TextWriter _writer;
48 private readonly
object _sync =
new object();
49 private readonly ConcurrentQueue<string> _preInitializeLines;
50 private readonly Dictionary<string, string> _currentRuntimeStatistics;
51 private readonly Dictionary<string, string> _currentAlphaRuntimeStatistics;
55 private static readonly
bool HighFidelityLogging =
Config.
GetBool(
"regression-high-fidelity-logging",
false);
57 private static readonly
bool IsTest = !Process.GetCurrentProcess().ProcessName.Contains(
"Lean.Launcher");
63 ? $
"./regression/{AlgorithmId}.{Language.ToLower()}.details.log"
64 : $
"./{AlgorithmId}/{DateTime.Now:yyyy-MM-dd-hh-mm-ss}.{Language.ToLower()}.details.log";
76 _testStartTime = DateTime.UtcNow;
77 _preInitializeLines =
new ConcurrentQueue<string>();
78 _currentRuntimeStatistics =
new Dictionary<string, string>();
79 _currentAlphaRuntimeStatistics =
new Dictionary<string, string>();
87 base.SetAlgorithm(algorithm, startingPortfolioValue);
90 Directory.CreateDirectory(fileInfo.DirectoryName);
99 WriteLine($
"{_testStartTime}: Starting regression test");
102 while (_preInitializeLines.TryDequeue(out line))
116 WriteLine($
"{Algorithm.UtcTime}: Total Portfolio Value: {Algorithm.Portfolio.TotalPortfolioValue}");
119 WriteLine($
"{Environment.NewLine}{Algorithm.Portfolio.CashBook}");
123 var symbol = kvp.Key;
124 var security = kvp.Value;
125 if (!security.HoldStock)
132 $
"{Algorithm.UtcTime}: " +
133 $
"Holdings: {symbol.Value} ({symbol.ID}): " +
134 $
"Price: {security.Price} " +
135 $
"Quantity: {security.Holdings.Quantity} " +
136 $
"Value: {security.Holdings.HoldingsValue} " +
137 $
"LastData: {security.GetLastData()}"
142 base.SamplePerformance(time, value);
157 WriteLine(
"==============================================================");
158 WriteLine($
" Symbol: {order.Symbol}");
159 WriteLine($
" Order: {order}");
160 WriteLine($
" Event: {newEvent}");
161 WriteLine($
" Position: {Algorithm.Portfolio[newEvent.Symbol].Quantity}");
165 WriteLine($
"Underlying: {underlyingHolding.Quantity}");
167 WriteLine($
" Cash: {Algorithm.Portfolio.Cash:0.00}");
168 WriteLine($
" Portfolio: {Algorithm.Portfolio.TotalPortfolioValue:0.00}");
169 WriteLine(
"==============================================================");
172 base.OrderEvent(newEvent);
180 base.SecurityType(types);
182 var sorted = types.Select(type => type.ToString()).OrderBy(type => type);
183 WriteLine($
"SecurityTypes: {string.Join("|
", sorted)}");
192 base.DebugMessage(message);
194 WriteLine($
"DebugMessage: {message}");
202 public override void ErrorMessage(
string message,
string stacktrace =
"")
204 base.ErrorMessage(message, stacktrace);
206 stacktrace =
string.IsNullOrEmpty(stacktrace) ? null : Environment.NewLine + stacktrace;
207 WriteLine($
"ErrorMessage: {message}{stacktrace}");
216 base.LogMessage(message);
218 WriteLine($
"LogMessage: {message}");
226 public override void RuntimeError(
string message,
string stacktrace =
"")
229 base.RuntimeError(message, stacktrace);
231 stacktrace =
string.IsNullOrEmpty(stacktrace) ? null : Environment.NewLine + stacktrace;
232 WriteLine($
"RuntimeError: {message}{stacktrace}");
241 base.SystemDebugMessage(message);
243 WriteLine($
"SystemDebugMessage: {message}");
256 if (HighFidelityLogging || _lastRuntimeStatisticsDate !=
Algorithm.
Time.Date)
260 string existingValue;
261 if (!_currentRuntimeStatistics.TryGetValue(key, out existingValue) || existingValue != value)
263 _currentRuntimeStatistics[key] = value;
264 WriteLine($
"RuntimeStatistic: {key}: {value}");
268 base.RuntimeStatistic(key, value);
270 catch (Exception exception)
282 base.AddToLogStore(message);
284 WriteLine($
"AddToLogStore: {message}");
292 base.OnSecuritiesChanged(changes);
297 .Select(security => security.Symbol.ToString())
298 .OrderBy(symbol => symbol);
300 WriteLine($
"OnSecuritiesChanged:ADD: {string.Join("|
", added)}");
306 .Select(security => security.Symbol.ToString())
307 .OrderBy(symbol => symbol);
309 WriteLine($
"OnSecuritiesChanged:REM: {string.Join("|
", removed)}");
319 if (HighFidelityLogging)
327 WriteLine($
"Slice Time: {slice.Time:o} Slice Count: {slice.Count}");
328 var data =
new Dictionary<Symbol, List<BaseData>>();
329 foreach (var kvp
in slice.Bars)
331 data.Add(kvp.Key, (
BaseData) kvp.Value);
334 foreach (var kvp
in slice.QuoteBars)
336 data.Add(kvp.Key, (
BaseData)kvp.Value);
339 foreach (var kvp
in slice.Ticks)
341 foreach (var tick
in kvp.Value)
347 foreach (var kvp
in slice.Delistings)
349 data.Add(kvp.Key, (
BaseData) kvp.Value);
352 foreach (var kvp
in slice.Splits)
354 data.Add(kvp.Key, (
BaseData) kvp.Value);
357 foreach (var kvp
in slice.SymbolChangedEvents)
359 data.Add(kvp.Key, (
BaseData) kvp.Value);
362 foreach (var kvp
in slice.Dividends)
364 data.Add(kvp.Key, (
BaseData) kvp.Value);
367 foreach (var kvp
in data.OrderBy(kvp => kvp.Key))
369 foreach (var item
in kvp.Value)
371 WriteLine($
"{Algorithm.UtcTime}: Slice: DataTime: {item.EndTime} {item}");
378 base.ProcessSynchronousEvents(forceProcess);
386 File.WriteAllText(
GetResultsPath(name), JsonConvert.SerializeObject(result));
397 var holdings =
Algorithm.
Portfolio.
Values.Where(holding => holding.Invested).Select(holding => $
"HOLDINGS:: {holding}").ToList();
398 if(holdings.Count > 0)
400 Log.
Trace($
"{Environment.NewLine}{string.Join(Environment.NewLine, holdings)}");
406 Log.
Trace($
"{Environment.NewLine}{Algorithm.Portfolio.CashBook}");
417 WriteLine($
"{kvp.Key,-15}\t{kvp.Value}");
420 var end = DateTime.UtcNow;
421 var delta = end - _testStartTime;
422 WriteLine($
"{end}: Completed regression test, took: {delta.TotalSeconds:0.0} seconds");
423 _writer.DisposeSafely();
429 while (_preInitializeLines.TryDequeue(out line))
431 Console.WriteLine(line);
452 algorithm.
Debug(msg);
453 WriteLine($
"DEBUG: {msg}");
457 algorithm.
Error(msg);
458 WriteLine($
"ERROR: {msg}");
467 WriteLine($
"DEBUG: {msg}");
472 WriteLine($
"ERROR: {msg}");
477 private void WriteLine(
string message)
488 _preInitializeLines.Enqueue(message);
492 _writer.WriteLine($
"{Algorithm.Time:O}: {message}");