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
143 changes: 143 additions & 0 deletions Algorithm.CSharp/ObjectStoreKeySanitizationRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* 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 System.Linq;
using QuantConnect.Interfaces;

namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// Regression algorithm asserting the ObjectStore key rules: an unsupported key is sanitized and used on every
/// save and read, and a save through it replaces the object stored under its sanitized name
/// </summary>
public class ObjectStoreKeySanitizationRegressionAlgorithm : 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 sanitized and used on every save and read
const string invalidKey = "trade_log_ai_hardware_&_cloud.csv";
const string sanitizedKey = "trade_log_ai_hardware___cloud.csv";
if (!ObjectStore.Save(invalidKey, "a,b\n1,2"))
{
throw new RegressionTestException("Expected the unsupported key to be sanitized and saved");
}
var keys = ObjectStore.Keys.ToList();
if (!keys.Contains(sanitizedKey) || !keys.Contains(invalidKey))
{
throw new RegressionTestException($"Expected the keys to hold '{sanitizedKey}' and '{invalidKey}', keys: {string.Join(", ", keys)}");
}
if (!ObjectStore.ContainsKey(invalidKey) || !ObjectStore.Keys.Contains(invalidKey)
|| ObjectStore.Read(invalidKey) != "a,b\n1,2" || ObjectStore.Read(sanitizedKey) != "a,b\n1,2")
{
throw new RegressionTestException("Expected the object to be read through both the original and the sanitized key");
}

// a later save through the same key updates the object
if (!ObjectStore.Save(invalidKey, "a,b\n3,4") || ObjectStore.Read(sanitizedKey) != "a,b\n3,4")
{
throw new RegressionTestException("Expected the second save through the unsupported key to update the object");
}

// a save through the unsupported key replaces the object under its sanitized name, whoever wrote it
if (!ObjectStore.Save(sanitizedKey, "c,d\n5,6") || !ObjectStore.Save(invalidKey, "e,f\n7,8") || ObjectStore.Read(sanitizedKey) != "e,f\n7,8")
{
throw new RegressionTestException("Expected the save through the unsupported key to replace the object under its sanitized name");
}

ObjectStore.Delete(sanitizedKey);
if (ObjectStore.ContainsKey(invalidKey) || ObjectStore.Keys.ToList().Contains(invalidKey))
{
throw new RegressionTestException("Expected the delete of the sanitized key to remove the object the unsupported key reads");
}
}

/// <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"}
};
}
}
55 changes: 55 additions & 0 deletions Algorithm.Python/ObjectStoreKeySanitizationRegressionAlgorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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 *

### <summary>
### Regression algorithm asserting the ObjectStore key rules: an unsupported key is sanitized and used on every
### save and read, and a save through it replaces the object stored under its sanitized name
### </summary>
class ObjectStoreKeySanitizationRegressionAlgorithm(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.add_equity("SPY", Resolution.DAILY)

def on_end_of_algorithm(self):
# an unsupported key, e.g. built from a user-facing name, is sanitized and used on every save and read
invalid_key = "trade_log_ai_hardware_&_cloud.csv"
sanitized_key = "trade_log_ai_hardware___cloud.csv"
if not self.object_store.save(invalid_key, "a,b\n1,2"):
raise AssertionError("Expected the unsupported key to be sanitized and saved")
keys = list(self.object_store.keys)
if sanitized_key not in keys or invalid_key not in keys:
raise AssertionError(f"Expected the keys to hold '{sanitized_key}' and '{invalid_key}', keys: {keys}")
if not self.object_store.contains_key(invalid_key) or invalid_key not in self.object_store.keys \
or self.object_store.read(invalid_key) != "a,b\n1,2" \
or self.object_store.read(sanitized_key) != "a,b\n1,2":
raise AssertionError("Expected the object to be read through both the original and the sanitized key")

# a later save through the same key updates the object
if not self.object_store.save(invalid_key, "a,b\n3,4") or self.object_store.read(sanitized_key) != "a,b\n3,4":
raise AssertionError("Expected the second save through the unsupported key to update the object")

# a save through the unsupported key replaces the object under its sanitized name, whoever wrote it
if not self.object_store.save(sanitized_key, "c,d\n5,6") or not self.object_store.save(invalid_key, "e,f\n7,8") \
or self.object_store.read(sanitized_key) != "e,f\n7,8":
raise AssertionError("Expected the save through the unsupported key to replace the object under its sanitized name")

self.object_store.delete(sanitized_key)
if self.object_store.contains_key(invalid_key) or invalid_key in list(self.object_store.keys):
raise AssertionError("Expected the delete of the sanitized key to remove the object the unsupported key reads")
2 changes: 1 addition & 1 deletion Algorithm/QCAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3321,7 +3321,7 @@ public void SetApi(IApi api)
public void SetObjectStore(IObjectStore objectStore)
{
// an already wrapped store, like the Python one, is used as is
ObjectStore = objectStore as ObjectStore ?? new ObjectStore(objectStore);
ObjectStore = objectStore as ObjectStore ?? new ObjectStore(objectStore, Debug);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,7 @@ public void SetCurrentSlice(Slice slice)
/// Sets the object store
/// </summary>
/// <param name="objectStore">The object store</param>
public void SetObjectStore(IObjectStore objectStore) => _baseAlgorithm.SetObjectStore(new PythonObjectStore(objectStore));
public void SetObjectStore(IObjectStore objectStore) => _baseAlgorithm.SetObjectStore(new PythonObjectStore(objectStore, _baseAlgorithm.Debug));

/// <summary>
/// Determines if the Symbol is shortable at the brokerage
Expand Down
Loading
Loading