Skip to content

Commit

Permalink
Common engine initialization (QuantConnect#5809)
Browse files Browse the repository at this point in the history
- Create Lean common engine initialization class, which will be called
  by research and Lean launcher
- Fix backtest deserialization bug due to failing to handle decimal
  value
  • Loading branch information
Martin-Molinero authored Jul 30, 2021
1 parent 2624b8c commit 41636c9
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 58 deletions.
1 change: 1 addition & 0 deletions Common/AlgorithmImports.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from QuantConnect.Orders.Fees import *
from QuantConnect.Data.Custom import *
from QuantConnect.Data.Market import *
from QuantConnect.Lean.Engine import *
from QuantConnect.Orders.Fills import *
from QuantConnect.Configuration import *
from QuantConnect.Notifications import *
Expand Down
18 changes: 9 additions & 9 deletions Common/AlphaRuntimeStatistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ public AlphaRuntimeStatistics()
/// <summary>
/// The total accumulated estimated value of trading all insights
/// </summary>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore), JsonConverter(typeof(StringDecimalJsonConverter), true)]
public decimal TotalAccumulatedEstimatedAlphaValue { get; set; }

/// <summary>
/// Score of the strategy's insights predictive power
/// </summary>
/// <remarks>See https://www.quantconnect.com/forum/discussion/6194/insight-scoring-metric/p1.
/// For performance we only truncate when the value is gotten</remarks>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore), JsonConverter(typeof(StringDecimalJsonConverter), true)]
public decimal KellyCriterionEstimate
{
get
Expand All @@ -112,7 +112,7 @@ public decimal KellyCriterionEstimate
/// </summary>
/// <remarks>See https://www.quantconnect.com/forum/discussion/6194/insight-scoring-metric/p1.
/// For performance we only truncate when the value is gotten</remarks>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore), JsonConverter(typeof(StringDecimalJsonConverter), true)]
public decimal KellyCriterionProbabilityValue
{
get
Expand All @@ -130,7 +130,7 @@ public decimal KellyCriterionProbabilityValue
/// </summary>
/// <remarks>See https://www.quantconnect.com/research/3bc40ecee68d36a9424fbd1b338eb227.
/// For performance we only truncate when the value is gotten</remarks>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore), JsonConverter(typeof(StringDecimalJsonConverter), true)]
public decimal FitnessScore
{
get
Expand All @@ -148,7 +148,7 @@ public decimal FitnessScore
/// Calculated as the sales volume with respect to the average total portfolio value.
/// </summary>
/// <remarks>For performance we only truncate when the value is gotten</remarks>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore), JsonConverter(typeof(StringDecimalJsonConverter), true)]
public decimal PortfolioTurnover
{
get
Expand All @@ -166,7 +166,7 @@ public decimal PortfolioTurnover
/// It is calculated by dividing the Portfolio Annualized Return by the Maximum Drawdown seen during the backtest.
/// </summary>
/// <remarks>For performance we only truncate when the value is gotten</remarks>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore), JsonConverter(typeof(StringDecimalJsonConverter), true)]
public decimal ReturnOverMaxDrawdown
{
get
Expand Down Expand Up @@ -200,7 +200,7 @@ public decimal SortinoRatio
/// <summary>
/// Suggested Value of the Alpha On A Monthly Basis For Licensing
/// </summary>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore), JsonConverter(typeof(StringDecimalJsonConverter), true)]
public decimal EstimatedMonthlyAlphaValue
{
get
Expand Down Expand Up @@ -235,7 +235,7 @@ public decimal EstimatedMonthlyAlphaValue
/// <summary>
/// Gets the mean estimated insight value
/// </summary>
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore), JsonConverter(typeof(StringDecimalJsonConverter), true)]
public decimal MeanPopulationEstimatedInsightValue => TotalInsightsClosed > 0 ? TotalAccumulatedEstimatedAlphaValue / TotalInsightsClosed : 0;

/// <summary>
Expand Down Expand Up @@ -292,4 +292,4 @@ private static string Invariant(IConvertible obj)
return obj.ToStringInvariant();
}
}
}
}
72 changes: 72 additions & 0 deletions Engine/Initializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.IO;
using QuantConnect.Util;
using QuantConnect.Logging;
using QuantConnect.Configuration;

namespace QuantConnect.Lean.Engine
{
/// <summary>
/// Helper class to initialize a Lean engine
/// </summary>
public static class Initializer
{
/// <summary>
/// The current Lean Engine System handlers
/// </summary>
public static LeanEngineSystemHandlers LeanEngineSystemHandlers { get; private set; }

/// <summary>
/// The current Lean Engine Algorithm handlers
/// </summary>
public static LeanEngineAlgorithmHandlers LeanEngineAlgorithmHandlers { get; private set; }

/// <summary>
/// Basic common Lean initialization
/// </summary>
public static void Start()
{
try
{
var mode = "RELEASE";
#if DEBUG
mode = "DEBUG";
#endif

Log.DebuggingEnabled = Config.GetBool("debug-mode");
Log.FilePath = Path.Combine(Config.Get("results-destination-folder"), "log.txt");
Log.LogHandler = Composer.Instance.GetExportedValueByTypeName<ILogHandler>(Config.Get("log-handler", "CompositeLogHandler"));

Log.Trace($"Engine.Main(): LEAN ALGORITHMIC TRADING ENGINE v{Globals.Version} Mode: {mode} ({(Environment.Is64BitProcess ? "64" : "32")}bit) Host: {Environment.MachineName}");
Log.Trace("Engine.Main(): Started " + DateTime.Now.ToShortTimeString());

LeanEngineSystemHandlers = LeanEngineSystemHandlers.FromConfiguration(Composer.Instance);

//Setup packeting, queue and controls system: These don't do much locally.
LeanEngineSystemHandlers.Initialize();

LeanEngineAlgorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance);
}
catch (Exception e)
{
Log.Error(e);
throw;
}
}
}
}
39 changes: 3 additions & 36 deletions Launcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ static Program()

static void Main(string[] args)
{
//Initialize:
var mode = "RELEASE";
#if DEBUG
mode = "DEBUG";
#endif

if (OS.IsWindows)
{
Console.OutputEncoding = System.Text.Encoding.UTF8;
Expand All @@ -65,29 +59,12 @@ static void Main(string[] args)
}

var liveMode = Config.GetBool("live-mode");
Log.DebuggingEnabled = Config.GetBool("debug-mode");
Log.FilePath = Path.Combine(Config.Get("results-destination-folder"), "log.txt");
Log.LogHandler = Composer.Instance.GetExportedValueByTypeName<ILogHandler>(Config.Get("log-handler", "CompositeLogHandler"));

//Name thread for the profiler:
Thread.CurrentThread.Name = "Algorithm Analysis Thread";
Log.Trace($"Engine.Main(): LEAN ALGORITHMIC TRADING ENGINE v{Globals.Version} Mode: {mode} ({(Environment.Is64BitProcess ? "64" : "32")}bit) Host: {Environment.MachineName}");
Log.Trace("Engine.Main(): Started " + DateTime.Now.ToShortTimeString());

//Import external libraries specific to physical server location (cloud/local)

try
{
leanEngineSystemHandlers = LeanEngineSystemHandlers.FromConfiguration(Composer.Instance);
}
catch (CompositionException compositionException)
{
Log.Error("Engine.Main(): Failed to load library: " + compositionException);
throw;
}

//Setup packeting, queue and controls system: These don't do much locally.
leanEngineSystemHandlers.Initialize();
Initializer.Start();
leanEngineSystemHandlers = Initializer.LeanEngineSystemHandlers;
leanEngineAlgorithmHandlers = Initializer.LeanEngineAlgorithmHandlers;

//-> Pull job from QuantConnect job queue, or, pull local build:
string assemblyPath;
Expand All @@ -100,16 +77,6 @@ static void Main(string[] args)
throw new ArgumentException(jobNullMessage);
}

try
{
leanEngineAlgorithmHandlers = LeanEngineAlgorithmHandlers.FromConfiguration(Composer.Instance);
}
catch (CompositionException compositionException)
{
Log.Error("Engine.Main(): Failed to load library: " + compositionException);
throw;
}

// if the job version doesn't match this instance version then we can't process it
// we also don't want to reprocess redelivered jobs
if (job.Redelivered)
Expand Down
3 changes: 0 additions & 3 deletions Research/QuantBook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ public class QuantBook : QCAlgorithm

static QuantBook()
{
Logging.Log.LogHandler =
Composer.Instance.GetExportedValueByTypeName<ILogHandler>(Config.Get("log-handler", "CompositeLogHandler"));

//Determine if we are in a Python Notebook
try
{
Expand Down
8 changes: 3 additions & 5 deletions Research/QuantConnect.csx
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ using QuantConnect.Securities.Equity;
using QuantConnect.Securities.Forex;
using QuantConnect.Securities.Interfaces;
using QuantConnect.Configuration;
using QuantConnect.Lean.Engine;

// Loads up a connection to our API for use in the research environment
Api api = new Api();
api.Initialize(Config.GetInt("job-user-id", 1),
Config.Get("api-access-token", "default"),
Config.Get("data-folder"));
Initializer.Start();
Api api = (Api)Initializer.LeanEngineSystemHandlers.Api;
10 changes: 5 additions & 5 deletions Research/start.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@

from AlgorithmImports import *

# Start an instance of an API class
api = Api()
api.Initialize(Config.GetInt("job-user-id", 1),
Config.Get("api-access-token", "default"),
Config.Get("data-folder"))
# Used by pythonNet
AddReference("Fasterflect")

Initializer.Start()
api = Initializer.LeanEngineSystemHandlers.Api

get_ipython().run_line_magic('matplotlib', 'inline')

0 comments on commit 41636c9

Please sign in to comment.