Files
quantconnect--lean/Common/Api/Nodes.cs
T
Andreas Sundebo e2de241c2b
Build & Test Lean / build (push) Has been cancelled
Regression Tests / build (push) Has been cancelled
Feature 5090 add api optimization methods (#6108)
* Move Optimizer-related DTOs and JSON converters into Common/Optimizer

* Add REST methods for Optimization

* Move OptimizationStatus into Common

* Change optimizationId parameter type to string

* Update Optimization and add lightweight optimization object

* Rename lightweight optimization to BaseOptimization and remove unneccessary properties

* Remove snapshotId from Optimization, add ParameterSet to Backtest

* Add missing IApi.cs method signatures

* Move ParameterSet into Common

* Replace Backtest with OptimizationBacktest

* Update UpdateOptimization to not include null or empty name and layout params in the request

* Change Objective targetTemplate regex pattern from ['(.+)'] to (.+) to prevent escaping target strings without whitespace

* Return Estimate object when calling EstimateOptimization

* Use DefaultNamingStrategy when serializing constraint operators

* Revert "Change Objective targetTemplate regex pattern from ['(.+)'] to (.+) to prevent escaping target strings without whitespace"

This reverts commit fbe7de0fd77dccc62e8c52c42a4ec9c18347acb2.

* Update Api method signatures

* Add unit tests

* Fix XML comment referring to the old class name

* Fix XML summary for OptimizationResponseWrapper

* Address review feedback
- Remove unused testOrganizationId
- Change NodeType from string to NodeType enum
- Clarify unit types for Estimate time and balance
- Simplify JsonConverter classes

* Add accessors to Common/Api classes

* Define performance metrics names in PerformanceMetrics class

* Remove unnecessary branching logic from GetSeriesValues method

* Add crefs and examples to XML comments in the Api class

* Revert "Change NodeType from string to NodeType enum"

* Remove layout param from UpdateOptimization method

* Backtest property ParameterSet should be of type ParameterSet

* Add asserts for deserialization in OptimizationBacktestJsonConverterTests

* Replace the three target-related properties with Criterion

* Add serialization and deserialization tests for Optimization

* Remove Optimization Serialization test

* Add EstimateDeserialization test

* Add asserts for integration tests

* Address self review

* Revert test case

* Update Nodes.cs

* Update Nodes.cs

* Set Aborted status when Optimization fails to start

* Add ParameterSetJsonConverter and ParameterSetJsonConverterTests

Co-authored-by: Martin-Molinero <martin@quantconnect.com>
2021-12-22 13:14:56 -03:00

258 lines
7.6 KiB
C#

/*
* 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.Collections.Generic;
using Newtonsoft.Json;
namespace QuantConnect.Api
{
/// <summary>
/// Node class built for API endpoints nodes/read and nodes/create.
/// Converts JSON properties from API response into data members for the class.
/// Contains all relevant information on a Node to interact through API endpoints.
/// </summary>
public class Node
{
/// <summary>
/// The nodes cpu clock speed in GHz
/// </summary>
[JsonProperty(PropertyName = "speed")]
public decimal Speed { get; set; }
/// <summary>
/// The monthly and yearly prices of the node in US dollars,
/// see <see cref="NodePrices"/> for type.
/// </summary>
[JsonProperty(PropertyName = "price")]
public NodePrices Prices { get; set; }
/// <summary>
/// CPU core count of node
/// </summary>
[JsonProperty(PropertyName = "cpu")]
public int CpuCount { get; set; }
/// <summary>
/// Size of RAM in Gigabytes
/// </summary>
[JsonProperty(PropertyName = "ram")]
public decimal Ram { get; set; }
/// <summary>
/// Name of the node
/// </summary>
[JsonProperty(PropertyName = "name")]
public string Name { get; set; }
/// <summary>
/// Node type identifier for configuration
/// </summary>
[JsonProperty(PropertyName = "sku")]
public string SKU { get; set; }
/// <summary>
/// String description of the node
/// </summary>
[JsonProperty(PropertyName = "description")]
public string Description { get; set; }
/// <summary>
/// User currently using the node
/// </summary>
[JsonProperty(PropertyName = "usedBy")]
public string UsedBy { get; set; }
/// <summary>
/// Project the node is being used for
/// </summary>
[JsonProperty(PropertyName = "projectName")]
public string ProjectName { get; set; }
/// <summary>
/// Boolean if the node is currently busy
/// </summary>
[JsonProperty(PropertyName = "busy")]
public bool Busy { get; set; }
/// <summary>
/// Full ID of node
/// </summary>
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
}
/// <summary>
/// Rest api response wrapper for node/read, contains sets of node lists for each
/// target environment. List are composed of <see cref="Node"/> objects.
/// </summary>
public class NodeList : RestResponse
{
/// <summary>
/// Collection of backtest nodes
/// </summary>
[JsonProperty(PropertyName = "backtest")]
public List<Node> BacktestNodes { get; set; }
/// <summary>
/// Collection of research nodes
/// </summary>
[JsonProperty(PropertyName = "research")]
public List<Node> ResearchNodes { get; set; }
/// <summary>
/// Collection of live nodes
/// </summary>
[JsonProperty(PropertyName = "live")]
public List<Node> LiveNodes { get; set; }
}
/// <summary>
/// Rest api response wrapper for node/create, reads in the nodes information into a
/// node object
/// </summary>
public class CreatedNode : RestResponse
{
/// <summary>
/// The created node from node/create
/// </summary>
[JsonProperty("node")]
public Node Node { get; set; }
}
/// <summary>
/// Class for generating a SKU for a node with a given configuration
/// Every SKU is made up of 3 variables:
/// - Target environment (L for live, B for Backtest, R for Research)
/// - CPU core count
/// - Dedicated RAM (GB)
/// </summary>
public class SKU
{
/// <summary>
/// The number of CPU cores in the node
/// </summary>
public int Cores { get; set; }
/// <summary>
/// Size of RAM in GB of the Node
/// </summary>
public int Memory { get; set; }
/// <summary>
/// Target environment for the node
/// </summary>
public NodeType Target { get; set; }
/// <summary>
/// Constructs a SKU object out of the provided node configuration
/// </summary>
/// <param name="cores">Number of cores</param>
/// <param name="memory">Size of RAM in GBs</param>
/// <param name="target">Target Environment Live/Backtest/Research</param>
public SKU(int cores, int memory, NodeType target)
{
Cores = cores;
Memory = memory;
Target = target;
}
/// <summary>
/// Generates the SKU string for API calls based on the specifications of the node
/// </summary>
/// <returns>String representation of the SKU</returns>
public override string ToString()
{
string result = "";
switch (Target)
{
case NodeType.Backtest:
result += "B";
break;
case NodeType.Research:
result += "R";
break;
case NodeType.Live:
result += "L";
break;
}
if (Cores == 0)
{
result += "-MICRO";
}
else
{
result += Cores + "-" + Memory;
}
return result;
}
}
/// <summary>
/// NodeTypes enum for all possible options of target environments
/// Used in conjuction with SKU class as a NodeType is a required parameter for SKU
/// </summary>
public enum NodeType
{
/// A node for running backtests
Backtest, //0
/// A node for running research
Research, //1
/// A node for live trading
Live //2
}
/// <summary>
/// Class for deserializing node prices from node object
/// </summary>
public class NodePrices
{
/// <summary>
/// The monthly price of the node in US dollars
/// </summary>
[JsonProperty(PropertyName = "monthly")]
public int Monthly { get; set; }
/// <summary>
/// The yearly prices of the node in US dollars
/// </summary>
[JsonProperty(PropertyName = "yearly")]
public int Yearly { get; set; }
}
/// <summary>
/// Supported optimization nodes
/// </summary>
public static class OptimizationNodes
{
/// <summary>
/// 2 CPUs 8 GB ram
/// </summary>
public static string O2_8 => "O2-8";
/// <summary>
/// 4 CPUs 12 GB ram
/// </summary>
public static string O4_12 => "O4-12";
/// <summary>
/// 8 CPUs 16 GB ram
/// </summary>
public static string O8_16 => "O8-16";
}
}