Skip to content
Draft
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
162 changes: 162 additions & 0 deletions Algorithm.CSharp/ObjectStoreErgonomicsRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* 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.Collections.Generic;
using QuantConnect.Interfaces;

namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Regression algorithm asserting the ObjectStore ergonomics: unsupported keys are rejected with the
/// key rules stated in the error, <see cref="Storage.ObjectStore.SanitizeKey"/> converts arbitrary names
/// into supported keys, <see cref="Storage.ObjectStore.SaveText"/> is an alias of Save and reading a
/// missing key lists the available keys in the error. The Python version of this algorithm additionally
/// covers the tolerant save_json/read_json and save_dataframe helpers
/// </summary>
public class ObjectStoreErgonomicsRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
{
/// <summary>
/// Initialize the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
/// </summary>
public override void Initialize()
{
SetStartDate(2013, 10, 7);
SetEndDate(2013, 10, 11);

SetBenchmark(x => 0);

AddEquity("SPY", Resolution.Daily);
}

/// <summary>
/// End of algorithm run event handler. This method is called at the end of a backtest or live trading operation.
/// </summary>
public override void OnEndOfAlgorithm()
{
// an unsupported key, e.g. built from a user-facing name, is rejected stating the rules and the fix
const string invalidKey = "trade_log_ai_hardware_&_cloud.csv";
var errorMessage = string.Empty;
try
{
ObjectStore.Save(invalidKey, "a,b\n1,2");
}
catch (ArgumentException exception)
{
errorMessage = exception.Message;
}
if (!errorMessage.Contains(Storage.ObjectStore.SupportedKeyRules) || !errorMessage.Contains("SanitizeKey"))
{
throw new RegressionTestException($"Expected the key rules in the unsupported key error, got: '{errorMessage}'");
}

// SanitizeKey converts the arbitrary name into a supported key
var sanitized = Storage.ObjectStore.SanitizeKey(invalidKey);
if (sanitized != "trade_log_ai_hardware___cloud.csv")
{
throw new RegressionTestException($"Unexpected sanitized key: '{sanitized}'");
}
if (!Storage.ObjectStore.IsSupportedKey(sanitized) || !ObjectStore.Save(sanitized, "a,b\n1,2"))
{
throw new RegressionTestException("Expected the sanitized key to be storable");
}

// SaveText is an alias of Save
if (!ObjectStore.SaveText("ergonomics_report.txt", "The strategy went up") ||
ObjectStore.Read("ergonomics_report.txt") != "The strategy went up")
{
throw new RegressionTestException("Expected the SaveText/Read round trip to succeed");
}

// reading a missing key lists the available keys in the error
errorMessage = string.Empty;
try
{
ObjectStore.ReadBytes("ergonomics_missing.json");
}
catch (KeyNotFoundException exception)
{
errorMessage = exception.Message;
}
if (!errorMessage.Contains("Keys: [") || !errorMessage.Contains($"'{sanitized}'"))
{
throw new RegressionTestException($"Expected the available keys in the missing key error, got: '{errorMessage}'");
}

ObjectStore.Delete(sanitized);
ObjectStore.Delete("ergonomics_report.txt");
}

/// <summary>
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
/// </summary>
public bool CanRunLocally { get; } = true;

/// <summary>
/// This is used by the regression test system to indicate which languages this algorithm is written in.
/// </summary>
public List<Language> Languages { get; } = new() { Language.CSharp, Language.Python };

/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public long DataPoints => 6;

/// <summary>
/// Data Points count of the algorithm history
/// </summary>
public int AlgorithmHistoryDataPoints => 0;

/// <summary>
/// Final status of the algorithm
/// </summary>
public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed;

/// <summary>
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
/// </summary>
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
{
{"Total Orders", "0"},
{"Average Win", "0%"},
{"Average Loss", "0%"},
{"Compounding Annual Return", "0%"},
{"Drawdown", "0%"},
{"Expectancy", "0"},
{"Start Equity", "100000"},
{"End Equity", "100000"},
{"Net Profit", "0%"},
{"Sharpe Ratio", "0"},
{"Sortino Ratio", "0"},
{"Probabilistic Sharpe Ratio", "0%"},
{"Loss Rate", "0%"},
{"Win Rate", "0%"},
{"Profit-Loss Ratio", "0"},
{"Alpha", "0"},
{"Beta", "0"},
{"Annual Standard Deviation", "0"},
{"Annual Variance", "0"},
{"Information Ratio", "0"},
{"Tracking Error", "0"},
{"Treynor Ratio", "0"},
{"Total Fees", "$0.00"},
{"Estimated Strategy Capacity", "$0"},
{"Lowest Capacity Asset", ""},
{"Portfolio Turnover", "0%"},
{"Drawdown Recovery", "0"},
{"OrderListHash", "d41d8cd98f00b204e9800998ecf8427e"}
};
}
}
104 changes: 104 additions & 0 deletions Algorithm.Python/ObjectStoreErgonomicsRegressionAlgorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# 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.

from AlgorithmImports import *
from decimal import Decimal

### <summary>
### Regression algorithm asserting the ObjectStore ergonomics: unsupported keys are rejected with the
### key rules stated in the error, sanitize_key converts arbitrary names into supported keys, save_text
### is an alias of save, reading a missing key lists the available keys in the error, and the tolerant
### save_json/read_json and save_dataframe helpers handle datetime/date/Decimal/Symbol and data frames
### out of the box.
### </summary>
class ObjectStoreErgonomicsRegressionAlgorithm(QCAlgorithm):

def initialize(self):
self.set_start_date(2013, 10, 7)
self.set_end_date(2013, 10, 11)

self.set_benchmark(lambda x: 0)

self.spy = self.add_equity("SPY", Resolution.DAILY).symbol

def on_end_of_algorithm(self):
# an unsupported key, e.g. built from a user-facing name, is rejected stating the rules and the fix.
# note: BaseException because AlgorithmImports shadows the builtin Exception with System.Exception
invalid_key = "trade_log_ai_hardware_&_cloud.csv"
error = None
try:
self.object_store.save(invalid_key, "a,b\n1,2")
except BaseException as e:
error = str(e)
if error is None or "keys may only contain" not in error or "SanitizeKey" not in error:
raise AssertionError(f"Expected the key rules in the unsupported key error, got: '{error}'")

# sanitize_key converts the arbitrary name into a supported key
sanitized = self.object_store.sanitize_key(invalid_key)
if sanitized != "trade_log_ai_hardware___cloud.csv":
raise AssertionError(f"Unexpected sanitized key: '{sanitized}'")
if not self.object_store.save(sanitized, "a,b\n1,2"):
raise AssertionError("Expected the sanitized key to be storable")

# save_json tolerates datetime/date/Decimal/Symbol values and non-string dictionary keys
trade_log = {
"time": datetime(2013, 10, 11, 16, 0, 0),
"date": date(2013, 10, 11),
"qty": Decimal("1.5"),
"symbol": self.spy,
"fills": [{"price": Decimal("167.42"), self.spy: 10}]
}
if not self.object_store.save_json("ergonomics/trade_log.json", trade_log):
raise AssertionError("Expected save_json to succeed")
data = self.object_store.read_json("ergonomics/trade_log.json")
expected = {
"time": "2013-10-11T16:00:00",
"date": "2013-10-11",
"qty": 1.5,
"symbol": "SPY",
"fills": [{"price": 167.42, "SPY": 10}]
}
if data != expected:
raise AssertionError(f"Unexpected read_json round trip result: {data}")

# read_json returns the given default when the key is missing
if self.object_store.read_json("ergonomics/missing.json") is not None:
raise AssertionError("Expected read_json of a missing key to return None")
if self.object_store.read_json("ergonomics/missing.json", {"warmed_up": False}) != {"warmed_up": False}:
raise AssertionError("Expected read_json of a missing key to return the given default")

# save_dataframe stores a pandas DataFrame as CSV
frame = pd.DataFrame({"close": [167.42, 168.0]}, index=pd.to_datetime(["2013-10-10", "2013-10-11"]))
if not self.object_store.save_dataframe("ergonomics/history.csv", frame):
raise AssertionError("Expected save_dataframe to succeed")
csv = self.object_store.read("ergonomics/history.csv")
if "close" not in csv or "2013-10-10" not in csv:
raise AssertionError(f"Unexpected save_dataframe content: {csv}")

# save_text is an alias of save
if not self.object_store.save_text("ergonomics/report.txt", "The strategy went up"):
raise AssertionError("Expected save_text to succeed")
if self.object_store.read("ergonomics/report.txt") != "The strategy went up":
raise AssertionError("Expected the save_text/read round trip to succeed")

# reading a missing key lists the available keys in the error
error = None
try:
self.object_store.read_bytes("ergonomics/missing.json")
except BaseException as e:
error = str(e)
if error is None or "Keys: [" not in error or f"'{sanitized}'" not in error:
raise AssertionError(f"Expected the available keys in the missing key error, got: '{error}'")

for key in [sanitized, "ergonomics/trade_log.json", "ergonomics/history.csv", "ergonomics/report.txt"]:
self.object_store.delete(key)
Loading
Loading