Skip to content

Commit 09ca345

Browse files
Custom data type universe - python support (#8618)
* Cache registered custom python security and universe data types Use this cache to get the correct config for history requests since pythonnet will always pass PythonData and we lose reference to the actual Python type * Use local repo data for unit tests * Move unit tests to algorithm history tests * Use UniverseManager instead of CacheCustomPythonDataType * Add regression algorithms * Update regression algorithms to solve issues * Update regression test and History * Updated source path to avoid issues with linux --------- Co-authored-by: Jhonathan Abreu <[email protected]>
1 parent 49265bd commit 09ca345

File tree

4 files changed

+393
-4
lines changed

4 files changed

+393
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
/*
2+
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3+
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
using System;
17+
using System.Collections.Generic;
18+
using System.IO;
19+
using System.Linq;
20+
using QuantConnect.Data;
21+
using QuantConnect.Interfaces;
22+
23+
namespace QuantConnect.Algorithm.CSharp
24+
{
25+
/// <summary>
26+
/// Adds a universe with a custom data type and retrieves historical data
27+
/// while preserving the custom data type.
28+
/// </summary>
29+
public class PersistentCustomDataUniverseRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
30+
{
31+
private Symbol _universeSymbol;
32+
private bool _dataReceived;
33+
34+
public override void Initialize()
35+
{
36+
SetStartDate(2018, 6, 1);
37+
SetEndDate(2018, 6, 19);
38+
39+
var universe = AddUniverse<StockDataSource>("my-stock-data-source", Resolution.Daily, UniverseSelector);
40+
_universeSymbol = universe.Symbol;
41+
RetrieveHistoricalData();
42+
}
43+
44+
private IEnumerable<Symbol> UniverseSelector(IEnumerable<BaseData> data)
45+
{
46+
foreach (var item in data.OfType<StockDataSource>())
47+
{
48+
yield return item.Symbol;
49+
}
50+
}
51+
52+
private void RetrieveHistoricalData()
53+
{
54+
var history = History<StockDataSource>(_universeSymbol, new DateTime(2018, 1, 1), new DateTime(2018, 6, 1), Resolution.Daily).ToList();
55+
if (history.Count == 0)
56+
{
57+
throw new RegressionTestException($"No historical data received for the symbol {_universeSymbol}.");
58+
}
59+
60+
// Ensure all values are of type StockDataSource
61+
foreach (var item in history)
62+
{
63+
if (item is not StockDataSource)
64+
{
65+
throw new RegressionTestException($"Unexpected data type in history. Expected StockDataSource but received {item.GetType().Name}.");
66+
}
67+
}
68+
}
69+
70+
public override void OnData(Slice slice)
71+
{
72+
if (!slice.ContainsKey(_universeSymbol))
73+
{
74+
throw new RegressionTestException($"No data received for the universe symbol: {_universeSymbol}.");
75+
}
76+
if (!_dataReceived)
77+
{
78+
RetrieveHistoricalData();
79+
}
80+
_dataReceived = true;
81+
}
82+
83+
public override void OnEndOfAlgorithm()
84+
{
85+
if (!_dataReceived)
86+
{
87+
throw new RegressionTestException("No data was received after the universe selection.");
88+
}
89+
}
90+
91+
92+
/// <summary>
93+
/// Our custom data type that defines where to get and how to read our backtest and live data.
94+
/// </summary>
95+
public class StockDataSource : BaseData
96+
{
97+
public List<string> Symbols { get; set; }
98+
99+
public StockDataSource()
100+
{
101+
Symbols = new List<string>();
102+
}
103+
104+
public override DateTime EndTime
105+
{
106+
get { return Time + Period; }
107+
set { Time = value - Period; }
108+
}
109+
110+
public TimeSpan Period
111+
{
112+
get { return QuantConnect.Time.OneDay; }
113+
}
114+
115+
public override SubscriptionDataSource GetSource(SubscriptionDataConfig config, DateTime date, bool isLiveMode)
116+
{
117+
var source = Path.Combine("..", "..", "..", "Tests", "TestData", "daily-stock-picker-backtest.csv");
118+
return new SubscriptionDataSource(source);
119+
}
120+
121+
public override BaseData Reader(SubscriptionDataConfig config, string line, DateTime date, bool isLiveMode)
122+
{
123+
if (string.IsNullOrWhiteSpace(line) || !char.IsDigit(line[0]))
124+
{
125+
return null;
126+
}
127+
128+
var stocks = new StockDataSource { Symbol = config.Symbol };
129+
130+
try
131+
{
132+
var csv = line.ToCsv();
133+
stocks.Time = DateTime.ParseExact(csv[0], "yyyyMMdd", null);
134+
stocks.Symbols.AddRange(csv[1..]);
135+
}
136+
catch (FormatException)
137+
{
138+
return null;
139+
}
140+
141+
return stocks;
142+
}
143+
}
144+
145+
/// <summary>
146+
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
147+
/// </summary>
148+
public bool CanRunLocally { get; } = true;
149+
150+
/// <summary>
151+
/// This is used by the regression test system to indicate which languages this algorithm is written in.
152+
/// </summary>
153+
public List<Language> Languages { get; } = new() { Language.CSharp, Language.Python };
154+
155+
/// <summary>
156+
/// Data Points count of all timeslices of algorithm
157+
/// </summary>
158+
public long DataPoints => 8767;
159+
160+
/// <summary>
161+
/// Data Points count of the algorithm history
162+
/// </summary>
163+
public int AlgorithmHistoryDataPoints => 298;
164+
165+
/// <summary>
166+
/// Final status of the algorithm
167+
/// </summary>
168+
public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed;
169+
170+
/// <summary>
171+
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
172+
/// </summary>
173+
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
174+
{
175+
{"Total Orders", "0"},
176+
{"Average Win", "0%"},
177+
{"Average Loss", "0%"},
178+
{"Compounding Annual Return", "0%"},
179+
{"Drawdown", "0%"},
180+
{"Expectancy", "0"},
181+
{"Start Equity", "100000"},
182+
{"End Equity", "100000"},
183+
{"Net Profit", "0%"},
184+
{"Sharpe Ratio", "0"},
185+
{"Sortino Ratio", "0"},
186+
{"Probabilistic Sharpe Ratio", "0%"},
187+
{"Loss Rate", "0%"},
188+
{"Win Rate", "0%"},
189+
{"Profit-Loss Ratio", "0"},
190+
{"Alpha", "0"},
191+
{"Beta", "0"},
192+
{"Annual Standard Deviation", "0"},
193+
{"Annual Variance", "0"},
194+
{"Information Ratio", "-3.9"},
195+
{"Tracking Error", "0.045"},
196+
{"Treynor Ratio", "0"},
197+
{"Total Fees", "$0.00"},
198+
{"Estimated Strategy Capacity", "$0"},
199+
{"Lowest Capacity Asset", ""},
200+
{"Portfolio Turnover", "0%"},
201+
{"OrderListHash", "d41d8cd98f00b204e9800998ecf8427e"}
202+
};
203+
}
204+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
2+
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
from datetime import timedelta
15+
from AlgorithmImports import *
16+
17+
### <summary>
18+
### Adds a universe with a custom data type and retrieves historical data
19+
### while preserving the custom data type.
20+
### </summary>
21+
class PersistentCustomDataUniverseRegressionAlgorithm(QCAlgorithm):
22+
23+
def Initialize(self):
24+
self.set_start_date(2018, 6, 1)
25+
self.set_end_date(2018, 6, 19)
26+
27+
universe = self.add_universe(StockDataSource, "my-stock-data-source", Resolution.DAILY, self.universe_selector)
28+
self._universe_symbol = universe.symbol
29+
self.retrieve_historical_data()
30+
self._data_received = False
31+
32+
def universe_selector(self, data):
33+
return [x.symbol for x in data]
34+
35+
def retrieve_historical_data(self):
36+
history = list(self.history[StockDataSource](self._universe_symbol, datetime(2018, 1, 1), datetime(2018, 6, 1), Resolution.DAILY))
37+
if (len(history) == 0):
38+
raise RegressionTestException(f"No historical data received for symbol {self._universe_symbol}.")
39+
40+
# Ensure all values are of type StockDataSource
41+
for item in history:
42+
if not isinstance(item, StockDataSource):
43+
raise RegressionTestException(f"Unexpected data type in history. Expected StockDataSource but received {type(item).__name__}.")
44+
45+
def OnData(self, slice: Slice):
46+
if self._universe_symbol not in slice:
47+
raise RegressionTestException(f"No data received for the universe symbol: {self._universe_symbol}.")
48+
if (not self._data_received):
49+
self.retrieve_historical_data()
50+
self._data_received = True
51+
52+
def OnEndOfAlgorithm(self) -> None:
53+
if not self._data_received:
54+
raise RegressionTestException("No data was received after the universe selection.")
55+
56+
class StockDataSource(PythonData):
57+
58+
def get_source(self, config: SubscriptionDataConfig, date: datetime, is_live: bool) -> SubscriptionDataSource:
59+
source = "../../../Tests/TestData/daily-stock-picker-backtest.csv"
60+
return SubscriptionDataSource(source)
61+
62+
def reader(self, config: SubscriptionDataConfig, line: str, date: datetime, is_live: bool) -> BaseData:
63+
if not (line.strip() and line[0].isdigit()): return None
64+
65+
stocks = StockDataSource()
66+
stocks.symbol = config.symbol
67+
68+
try:
69+
csv = line.split(',')
70+
stocks.time = datetime.strptime(csv[0], "%Y%m%d")
71+
stocks.end_time = stocks.time + self.period
72+
stocks["Symbols"] = csv[1:]
73+
74+
except ValueError:
75+
return None
76+
77+
return stocks
78+
@property
79+
def period(self) -> timedelta:
80+
return timedelta(days=1)

Algorithm/QCAlgorithm.History.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -1132,18 +1132,22 @@ private IEnumerable<SubscriptionDataConfig> GetMatchingSubscriptions(Symbol symb
11321132
}
11331133
else
11341134
{
1135-
resolution = GetResolution(symbol, resolution, type);
1136-
11371135
// If type was specified and not a lean data type and also not abstract, we create a new subscription
11381136
if (type != null && !LeanData.IsCommonLeanDataType(type) && !type.IsAbstract)
11391137
{
11401138
// we already know it's not a common lean data type
11411139
var isCustom = Extensions.IsCustomDataType(symbol, type);
11421140
var entry = MarketHoursDatabase.GetEntry(symbol, new[] { type });
11431141

1142+
// Retrieve the associated data type from the universe if available, otherwise, use the provided type
1143+
var dataType = UniverseManager.TryGetValue(symbol, out var universe) ? universe.DataType : type;
1144+
1145+
// Determine resolution using the data type
1146+
resolution = GetResolution(symbol, resolution, dataType);
1147+
11441148
// we were giving a specific type let's fetch it
11451149
return new[] { new SubscriptionDataConfig(
1146-
type,
1150+
dataType,
11471151
symbol,
11481152
resolution.Value,
11491153
entry.DataTimeZone,
@@ -1152,11 +1156,12 @@ private IEnumerable<SubscriptionDataConfig> GetMatchingSubscriptions(Symbol symb
11521156
UniverseSettings.ExtendedMarketHours,
11531157
true,
11541158
isCustom,
1155-
LeanData.GetCommonTickTypeForCommonDataTypes(type, symbol.SecurityType),
1159+
LeanData.GetCommonTickTypeForCommonDataTypes(dataType, symbol.SecurityType),
11561160
true,
11571161
UniverseSettings.GetUniverseNormalizationModeOrDefault(symbol.SecurityType))};
11581162
}
11591163

1164+
resolution = GetResolution(symbol, resolution, type);
11601165
return SubscriptionManager
11611166
.LookupSubscriptionConfigDataTypes(symbol.SecurityType, resolution.Value, symbol.IsCanonical())
11621167
.Where(tuple => SubscriptionDataConfigTypeFilter(type, tuple.Item1))

0 commit comments

Comments
 (0)