Skip to content

Commit

Permalink
Private cloud tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Molinero committed Nov 8, 2024
1 parent d5b2e4d commit f75973d
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 24 deletions.
6 changes: 3 additions & 3 deletions Api/ApiConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ApiConnection(int userId, string token)
{
_token = token;
_userId = userId.ToStringInvariant();
Client = new RestClient("https://www.quantconnect.com/api/v2/");
Client = new RestClient(Globals.Api);
}

/// <summary>
Expand Down Expand Up @@ -112,15 +112,15 @@ public async Task<Tuple<bool, T>> TryRequestAsync<T>(RestRequest request)

if (!restsharpResponse.IsSuccessful)
{
Log.Error($"ApiConnect.TryRequest(): Content: {restsharpResponse.Content}");
Log.Error($"ApiConnect.TryRequest({request.Resource}): Content: {restsharpResponse.Content}");
}

responseContent = restsharpResponse.Content;
result = JsonConvert.DeserializeObject<T>(responseContent, _jsonSettings);

if (result == null || !result.Success)
{
Log.Debug($"ApiConnection.TryRequest(): Raw response: '{responseContent}'");
Log.Debug($"ApiConnection.TryRequest({request.Resource}): Raw response: '{responseContent}'");
return new Tuple<bool, T>(false, result);
}
}
Expand Down
28 changes: 17 additions & 11 deletions Common/Api/Backtest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using QuantConnect.Statistics;
using System.Collections.Generic;
using QuantConnect.Optimizer.Parameters;
using QuantConnect.Util;

namespace QuantConnect.Api
{
Expand Down Expand Up @@ -52,6 +53,16 @@ public class ResearchGuide
/// </summary>
public class BasicBacktest : RestResponse
{
/// <summary>
/// Backtest error message
/// </summary>
public string Error { get; set; }

/// <summary>
/// Backtest error stacktrace
/// </summary>
public string Stacktrace { get; set; }

/// <summary>
/// Assigned backtest Id
/// </summary>
Expand All @@ -70,6 +81,7 @@ public class BasicBacktest : RestResponse
/// <summary>
/// Backtest creation date and time
/// </summary>
[JsonConverter(typeof(DateTimeJsonConverter), DateFormat.UI)]
public DateTime Created { get; set; }

/// <summary>
Expand Down Expand Up @@ -103,7 +115,6 @@ public class BasicBacktest : RestResponse
/// </summary>
public class Backtest : BasicBacktest
{

/// <summary>
/// Note on the backtest attached by the user
/// </summary>
Expand All @@ -114,16 +125,6 @@ public class Backtest : BasicBacktest
/// </summary>
public bool Completed { get; set; }

/// <summary>
/// Backtest error message
/// </summary>
public string Error { get; set; }

/// <summary>
/// Backtest error stacktrace
/// </summary>
public string StackTrace { get; set; }

/// <summary>
/// Organization ID
/// </summary>
Expand Down Expand Up @@ -185,6 +186,11 @@ public class Backtest : BasicBacktest
/// </summary>
public string NodeName { get; set; }

/// <summary>
/// The associated project id
/// </summary>
public int ProjectId { get; set; }

/// <summary>
/// End date of out of sample data
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion Common/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static Globals()
/// <summary>
/// The base api url address to use
/// </summary>
public static string Api { get; } = "https://www.quantconnect.com/api/v2/";
public static string Api { get; set; }

/// <summary>
/// The user Id
Expand Down Expand Up @@ -96,6 +96,7 @@ public static void Reset ()
ProjectId = Config.GetInt("project-id");
UserToken = Config.Get("api-access-token");
OrganizationID = Config.Get("job-organization-id");
Api = Config.Get("api-url", "https://www.quantconnect.com/api/v2/");
ResultsDestinationFolder = Config.Get("results-destination-folder", Directory.GetCurrentDirectory());
}

Expand Down
6 changes: 6 additions & 0 deletions Common/Isolator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ private bool MonitorTask(Task task,
memoryCap *= 1024 * 1024;
var spikeLimit = memoryCap*2;

if (memoryCap <= 0)
{
memoryCap = int.MaxValue;
spikeLimit = int.MaxValue;
}

while (!task.IsCompleted && !CancellationTokenSource.IsCancellationRequested && utcNow < end)
{
// if over 80% allocation force GC then sample
Expand Down
4 changes: 2 additions & 2 deletions Common/Optimizer/Parameters/ParameterSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public class ParameterSet
/// </summary>
/// <param name="id">Unique identifier</param>
/// <param name="value">Combination of optimization parameters</param>
public ParameterSet(int id, Dictionary<string, string> value)
public ParameterSet(int id, IReadOnlyDictionary<string, string> value)
{
Id = id;
Value = value?.ToReadOnlyDictionary();
Value = value;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Common/Packets/AlgorithmNodePacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public string AlgorithmId
/// The maximum amount of RAM (in MB) this algorithm is allowed to utilize
/// </summary>
public int RamAllocation {
get { return Controls.RamAllocation; }
get { return Controls?.RamAllocation ?? 0; }
}

/// <summary>
Expand Down
7 changes: 6 additions & 1 deletion Engine/Results/BaseResultsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public abstract class BaseResultsHandler
/// </summary>
public const string PortfolioMarginKey = "Portfolio Margin";

/// <summary>
/// String message saying: Portfolio Margin
/// </summary>
public const string AssetsSalesVolumeKey = "Assets Sales Volume";

/// <summary>
/// The main loop update interval
/// </summary>
Expand Down Expand Up @@ -720,7 +725,7 @@ protected virtual void SampleSalesVolume(DateTime time)
foreach (var holding in Algorithm.Portfolio.Values.Where(y => y.TotalSaleVolume != 0)
.OrderByDescending(x => x.TotalSaleVolume).Take(30))
{
Sample("Assets Sales Volume", $"{holding.Symbol.Value}", 0, SeriesType.Treemap, new ChartPoint(time, holding.TotalSaleVolume),
Sample(AssetsSalesVolumeKey, $"{holding.Symbol.Value}", 0, SeriesType.Treemap, new ChartPoint(time, holding.TotalSaleVolume),
AlgorithmCurrencySymbol);
}
}
Expand Down
5 changes: 1 addition & 4 deletions Engine/Setup/BacktestingSetupHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ public bool Setup(SetupHandlerParameters parameters)
throw new ArgumentException("Expected BacktestNodePacket but received " + parameters.AlgorithmNodePacket.GetType().Name);
}

Log.Trace($"BacktestingSetupHandler.Setup(): Setting up job: UID: {job.UserId.ToStringInvariant()}, " +
$"PID: {job.ProjectId.ToStringInvariant()}, Version: {job.Version}, Source: {job.RequestSource}"
);
BaseSetupHandler.Setup(parameters);

if (algorithm == null)
{
Expand Down Expand Up @@ -164,7 +162,6 @@ public bool Setup(SetupHandlerParameters parameters)

//Algorithm is backtesting, not live:
algorithm.SetAlgorithmMode(job.AlgorithmMode);
algorithm.SetDeploymentTarget(job.DeploymentTarget);

//Set the source impl for the event scheduling
algorithm.Schedule.SetEventSchedule(parameters.RealTimeHandler);
Expand Down
18 changes: 18 additions & 0 deletions Engine/Setup/BaseSetupHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,24 @@ public static class BaseSetupHandler
/// </summary>
public static TimeSpan AlgorithmCreationTimeout { get; } = TimeSpan.FromSeconds(Config.GetDouble("algorithm-creation-timeout", 90));

/// <summary>
/// Primary entry point to setup a new algorithm
/// </summary>
/// <param name="parameters">The parameters object to use</param>
/// <returns>True on successfully setting up the algorithm state, or false on error.</returns>
public static bool Setup(SetupHandlerParameters parameters)
{
var algorithm = parameters.Algorithm;
var job = parameters.AlgorithmNodePacket;

algorithm?.SetDeploymentTarget(job.DeploymentTarget);

Log.Trace($"BaseSetupHandler.Setup({job.DeploymentTarget}): UID: {job.UserId.ToStringInvariant()}, " +
$"PID: {job.ProjectId.ToStringInvariant()}, Version: {job.Version}, Source: {job.RequestSource}"
);
return true;
}

/// <summary>
/// Will first check and add all the required conversion rate securities
/// and later will seed an initial value to them.
Expand Down
2 changes: 1 addition & 1 deletion Engine/Setup/BrokerageSetupHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public bool Setup(SetupHandlerParameters parameters)
return false;
}

BaseSetupHandler.Setup(parameters);

// attach to the message event to relay brokerage specific initialization messages
EventHandler<BrokerageMessageEvent> brokerageOnMessage = (sender, args) =>
Expand Down Expand Up @@ -243,7 +244,6 @@ public bool Setup(SetupHandlerParameters parameters)

//Algorithm is live, not backtesting:
algorithm.SetAlgorithmMode(liveJob.AlgorithmMode);
algorithm.SetDeploymentTarget(liveJob.DeploymentTarget);

//Initialize the algorithm's starting date
algorithm.SetDateTime(DateTime.UtcNow);
Expand Down

0 comments on commit f75973d

Please sign in to comment.