Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Algorithm/QCAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public partial class QCAlgorithm : MarshalByRefObject, IAlgorithm
private TimeSpan? _warmupTimeSpan;
private int? _warmupBarCount;
private Dictionary<string, string> _parameters = new Dictionary<string, string>();
private bool _brokerageDataSet;
private bool _deploymentDetailsSet;
private SecurityDefinitionSymbolResolver _securityDefinitionSymbolResolver;

private SecurityDefinitionSymbolResolver SecurityDefinitionSymbolResolver
Expand Down Expand Up @@ -750,11 +750,11 @@ public ConcurrentQueue<string> ErrorMessages
public ObjectStore ObjectStore { get; private set; }

/// <summary>
/// Gets a read-only view of the brokerage data shared by the brokerage, data queue handler or any other component,
/// Gets a read-only view of the deployment details shared by the brokerage, data queue handler or any other component,
/// for example account information. Usually empty when not running in live mode
/// </summary>
[DocumentationAttribute(LiveTrading)]
public ReadOnlyExtendedDictionary<string, string> BrokerageData { get; private set; } = new();
public ReadOnlyExtendedDictionary<string, string> DeploymentDetails { get; private set; } = new();

/// <summary>
/// The current statistics for the running algorithm.
Expand Down Expand Up @@ -927,22 +927,22 @@ public ReadOnlyExtendedDictionary<string, string> GetParameters()
}

/// <summary>
/// Sets the brokerage data read-only view. Can only be set once, it's shared by the engine
/// Sets the deployment details read-only view. Can only be set once, it's shared by the engine
/// </summary>
/// <param name="brokerageData">The brokerage data</param>
/// <param name="deploymentDetails">The deployment details</param>
[DocumentationAttribute(LiveTrading)]
public void SetBrokerageData(ReadOnlyExtendedDictionary<string, string> brokerageData)
public void SetDeploymentDetails(ReadOnlyExtendedDictionary<string, string> deploymentDetails)
{
if (brokerageData == null)
if (deploymentDetails == null)
{
throw new ArgumentNullException(nameof(brokerageData));
throw new ArgumentNullException(nameof(deploymentDetails));
}
if (_brokerageDataSet && !ReferenceEquals(BrokerageData, brokerageData))
if (_deploymentDetailsSet && !ReferenceEquals(DeploymentDetails, deploymentDetails))
{
throw new InvalidOperationException("QCAlgorithm.SetBrokerageData(): the brokerage data has already been set, it can only be set once");
throw new InvalidOperationException("QCAlgorithm.SetDeploymentDetails(): the deployment details have already been set, they can only be set once");
}
BrokerageData = brokerageData;
_brokerageDataSet = true;
DeploymentDetails = deploymentDetails;
_deploymentDetailsSet = true;
}

/// <summary>
Expand Down
10 changes: 5 additions & 5 deletions AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,10 @@ public Exception RunTimeError
public ObjectStore ObjectStore => _baseAlgorithm.ObjectStore;

/// <summary>
/// Gets a read-only view of the brokerage data shared by the brokerage, data queue handler or any other component,
/// Gets a read-only view of the deployment details shared by the brokerage, data queue handler or any other component,
/// for example account information. Usually empty when not running in live mode
/// </summary>
public ReadOnlyExtendedDictionary<string, string> BrokerageData => _baseAlgorithm.BrokerageData;
public ReadOnlyExtendedDictionary<string, string> DeploymentDetails => _baseAlgorithm.DeploymentDetails;

/// <summary>
/// Returns the current Slice object
Expand Down Expand Up @@ -1171,10 +1171,10 @@ public void SetFinishedWarmingUp()
public void SetParameters(Dictionary<string, string> parameters) => _baseAlgorithm.SetParameters(parameters);

/// <summary>
/// Sets the brokerage data read-only view
/// Sets the deployment details read-only view
/// </summary>
/// <param name="brokerageData">The brokerage data</param>
public void SetBrokerageData(ReadOnlyExtendedDictionary<string, string> brokerageData) => _baseAlgorithm.SetBrokerageData(brokerageData);
/// <param name="deploymentDetails">The deployment details</param>
public void SetDeploymentDetails(ReadOnlyExtendedDictionary<string, string> deploymentDetails) => _baseAlgorithm.SetDeploymentDetails(deploymentDetails);

/// <summary>
/// Tries to convert a PyObject into a C# object
Expand Down
10 changes: 5 additions & 5 deletions Common/AlgorithmConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ public class AlgorithmConfiguration
public IReadOnlyDictionary<string, string> Parameters { get; set; }

/// <summary>
/// The brokerage data used by the live algorithm, if any
/// The deployment details of the live algorithm, if any
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public IReadOnlyDictionary<string, string> BrokerageData { get; set; }
public IReadOnlyDictionary<string, string> DeploymentDetails { get; set; }

/// <summary>
/// Backtest maximum end date
Expand Down Expand Up @@ -102,10 +102,10 @@ public class AlgorithmConfiguration
public AlgorithmConfiguration(string name, ISet<string> tags, string accountCurrency, BrokerageName brokerageName,
AccountType accountType, IReadOnlyDictionary<string, string> parameters, DateTime startDate, DateTime endDate,
DateTime? outOfSampleMaxEndDate, int outOfSampleDays = 0, int tradingDaysPerYear = 0,
IReadOnlyDictionary<string, string> brokerageData = null)
IReadOnlyDictionary<string, string> deploymentDetails = null)
{
Name = name;
BrokerageData = brokerageData;
DeploymentDetails = deploymentDetails;
Tags = tags;
OutOfSampleMaxEndDate = outOfSampleMaxEndDate;
TradingDaysPerYear = tradingDaysPerYear;
Expand Down Expand Up @@ -149,7 +149,7 @@ public static AlgorithmConfiguration Create(IAlgorithm algorithm, BacktestNodePa
// use value = 252 like default for backwards compatibility
algorithm?.Settings?.TradingDaysPerYear ?? 252,
// only included when set, live mode. We take a snapshot since the algorithm's instance can be updated later on
algorithm.BrokerageData?.Count > 0 ? new Dictionary<string, string>(algorithm.BrokerageData) : null);
algorithm.DeploymentDetails?.Count > 0 ? new Dictionary<string, string>(algorithm.DeploymentDetails) : null);
}
}
}
10 changes: 5 additions & 5 deletions Common/Interfaces/IAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,10 @@ InsightManager Insights
ObjectStore ObjectStore { get; }

/// <summary>
/// Gets a read-only view of the brokerage data shared by the brokerage, data queue handler or any other component,
/// Gets a read-only view of the deployment details shared by the brokerage, data queue handler or any other component,
/// for example account information. Usually empty when not running in live mode
/// </summary>
ReadOnlyExtendedDictionary<string, string> BrokerageData { get; }
ReadOnlyExtendedDictionary<string, string> DeploymentDetails { get; }

/// <summary>
/// Returns the current Slice object
Expand Down Expand Up @@ -481,10 +481,10 @@ InsightManager Insights
void SetParameters(Dictionary<string, string> parameters);

/// <summary>
/// Sets the brokerage data read-only view
/// Sets the deployment details read-only view
/// </summary>
/// <param name="brokerageData">The brokerage data</param>
void SetBrokerageData(ReadOnlyExtendedDictionary<string, string> brokerageData);
/// <param name="deploymentDetails">The deployment details</param>
void SetDeploymentDetails(ReadOnlyExtendedDictionary<string, string> deploymentDetails);

/// <summary>
/// Determines if the Symbol is shortable at the brokerage
Expand Down
4 changes: 2 additions & 2 deletions Engine/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ public void Run(AlgorithmNodePacket job, AlgorithmManager manager, string assemb

algorithm.ProjectId = job.ProjectId;

// share the brokerage data with the algorithm right away so it's available during initialization
algorithm.SetBrokerageData(AlgorithmHandlers.Results.BrokerageData);
// share the deployment details with the algorithm right away so it's available during initialization
algorithm.SetDeploymentDetails(AlgorithmHandlers.Results.DeploymentDetails);

// Set algorithm in ILeanManager
SystemHandlers.LeanManager.SetAlgorithm(algorithm);
Expand Down
26 changes: 13 additions & 13 deletions Engine/Results/BaseResultsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,14 @@ protected Bar CurrentAlgorithmEquity
protected Dictionary<string, string> State { get; set; }

/// <summary>
/// Brokerage data shared with the user and the algorithm, see <see cref="AddBrokerageData"/>
/// Deployment details shared with the user and the algorithm, see <see cref="AddDeploymentDetail"/>
/// </summary>
private readonly Dictionary<string, string> _brokerageData = new();
private readonly Dictionary<string, string> _deploymentDetails = new();

/// <summary>
/// Read only view of the brokerage data, see <see cref="AddBrokerageData"/>. Shared with the algorithm
/// Read only view of the deployment details, see <see cref="AddDeploymentDetail"/>. Shared with the algorithm
/// </summary>
public ReadOnlyExtendedDictionary<string, string> BrokerageData { get; }
public ReadOnlyExtendedDictionary<string, string> DeploymentDetails { get; }

/// <summary>
/// The handler responsible for communicating messages to listeners
Expand Down Expand Up @@ -338,7 +338,7 @@ protected BaseResultsHandler()
Messages = new ConcurrentQueue<Packet>();
RuntimeStatistics = new Dictionary<string, string>();
// same instance, so any entries added later are visible through the view
BrokerageData = new ReadOnlyExtendedDictionary<string, string>(_brokerageData, copy: false);
DeploymentDetails = new ReadOnlyExtendedDictionary<string, string>(_deploymentDetails, copy: false);
StartTime = DateTime.UtcNow;
CompileId = "";
AlgorithmId = "";
Expand Down Expand Up @@ -557,32 +557,32 @@ public virtual void SetAlgorithm(IAlgorithm algorithm, decimal startingPortfolio
}

/// <summary>
/// Adds or updates a brokerage data entry. Key value pairs the brokerage, data queue handler or any other component
/// Adds or updates a deployment detail entry. Key value pairs the brokerage, data queue handler or any other component
/// wants to share with the user, through the results, and the algorithm, for example account information.
/// Sensitive data, like credentials, should never be added
/// </summary>
/// <param name="key">The brokerage data key</param>
/// <param name="value">The brokerage data value</param>
public virtual void AddBrokerageData(string key, string value)
/// <param name="key">The deployment detail key</param>
/// <param name="value">The deployment detail value</param>
public virtual void AddDeploymentDetail(string key, string value)
{
if (string.IsNullOrEmpty(key))
{
return;
}
lock (_brokerageData)
lock (_deploymentDetails)
{
_brokerageData[key] = value ?? string.Empty;
_deploymentDetails[key] = value ?? string.Empty;
}
}

/// <summary>
/// Creates the algorithm configuration to include in the results, taking a snapshot of the current brokerage data
/// Creates the algorithm configuration to include in the results, taking a snapshot of the current deployment details
/// </summary>
/// <param name="backtestNodePacket">The associated backtest node packet if any</param>
/// <returns>A new <see cref="AlgorithmConfiguration"/> instance</returns>
protected AlgorithmConfiguration CreateAlgorithmConfiguration(BacktestNodePacket backtestNodePacket = null)
{
lock (_brokerageData)
lock (_deploymentDetails)
{
return AlgorithmConfiguration.Create(Algorithm, backtestNodePacket);
}
Expand Down
61 changes: 61 additions & 0 deletions Engine/Results/DeploymentDetailsHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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.Threading;
using QuantConnect.Util;
using QuantConnect.Logging;

namespace QuantConnect.Lean.Engine.Results
{
/// <summary>
/// Helper to share deployment details with the user and the algorithm, see <see cref="IResultHandler.AddDeploymentDetail"/>
/// </summary>
public static class DeploymentDetailsHelper
{
private static int _missingResultHandlerLogged;

/// <summary>
/// Adds or updates a deployment detail entry on the result handler loaded in the <see cref="Composer"/>, if any.
/// Key value pairs the brokerage, data queue handler or any other component wants to share with the user,
/// through the results, and the algorithm, for example account information.
/// Sensitive data, like credentials, should never be added
/// </summary>
/// <remarks>Will never throw, callers are not expected to handle any failure sharing a deployment detail</remarks>
/// <param name="key">The deployment detail key</param>
/// <param name="value">The deployment detail value</param>
public static void Add(string key, string value)
{
try
{
var resultHandler = Composer.Instance.GetPart<IResultHandler>();
if (resultHandler == null)
{
// we only log this once, else we would spam for every entry
if (Interlocked.Exchange(ref _missingResultHandlerLogged, 1) == 0)
{
Log.Error($"DeploymentDetailsHelper.Add(): no result handler was found, deployment details will be ignored");
}
return;
}
resultHandler.AddDeploymentDetail(key, value);
}
catch (Exception exception)
{
Log.Error(exception, $"Failed to add deployment detail '{key}'");
}
}
}
}
12 changes: 6 additions & 6 deletions Engine/Results/IResultHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,18 @@ bool IsActive
void RuntimeStatistic(string key, string value);

/// <summary>
/// Adds or updates a brokerage data entry. Key value pairs the brokerage, data queue handler or any other component
/// Adds or updates a deployment detail entry. Key value pairs the brokerage, data queue handler or any other component
/// wants to share with the user, through the results, and the algorithm, for example account information.
/// Sensitive data, like credentials, should never be added
/// </summary>
/// <param name="key">The brokerage data key</param>
/// <param name="value">The brokerage data value</param>
void AddBrokerageData(string key, string value);
/// <param name="key">The deployment detail key</param>
/// <param name="value">The deployment detail value</param>
void AddDeploymentDetail(string key, string value);

/// <summary>
/// Read only view of the brokerage data, see <see cref="AddBrokerageData"/>. Shared with the algorithm
/// Read only view of the deployment details, see <see cref="AddDeploymentDetail"/>. Shared with the algorithm
/// </summary>
ReadOnlyExtendedDictionary<string, string> BrokerageData { get; }
ReadOnlyExtendedDictionary<string, string> DeploymentDetails { get; }

/// <summary>
/// Send a new order event.
Expand Down
Loading
Loading