Files
quantconnect--lean/Algorithm.Python/HistoryWithCustomDataSourceRegressionAlgorithm.py
T
Jhonathan Abreu 5758b65099
Build & Test Lean / build (push) Has been cancelled
Regression Tests / build (push) Has been cancelled
Research Regression Tests / build (push) Has been cancelled
Added configuration parameters to Python QCAlgorithm.History() method that takes custom data source type (#6448)
* Add new Python QCAlgorithm.History() method with all parameters and type

* Add regression algorithms

* Using all parameters in History()

* Use private methods to reuse History() code

* Use private methods to reuse History() code

* Add unit tests for QCAlgorithm.Python.History()

* Add unit tests for QCAlgorithm.Python.History()

* Add unit tests for QCAlgorithm.Python.History()

* Add unit tests for QCAlgorithm.Python.History()

* Add unit tests for QCAlgorithm.Python.History()

* Add unit tests for QCAlgorithm.Python.History()

* Asserting history count
2022-07-05 13:14:08 -03:00

67 lines
3.2 KiB
Python

# 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 test illustrating how history from custom data sources can be requested. The <see cref="QCAlgorithm.History"/> method used in this
### example also allows to specify other parameters than just the resolution, such as the data normalization mode, the data mapping mode, etc.
### </summary>
class HistoryWithCustomDataSourceRegressionAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2014, 6, 5)
self.SetEndDate(2014, 6, 6)
self.aapl = self.AddData(CustomData, "AAPL", Resolution.Minute).Symbol
self.spy = self.AddData(CustomData, "SPY", Resolution.Minute).Symbol
def OnEndOfAlgorithm(self):
aaplHistory = self.History(CustomData, self.aapl, self.StartDate, self.EndDate, Resolution.Minute,
fillForward=False, extendedMarket=False, dataNormalizationMode=DataNormalizationMode.Raw).droplevel(0, axis=0)
spyHistory = self.History(CustomData, self.spy, self.StartDate, self.EndDate, Resolution.Minute,
fillForward=False, extendedMarket=False, dataNormalizationMode=DataNormalizationMode.Raw).droplevel(0, axis=0)
if aaplHistory.size == 0 or spyHistory.size == 0:
raise Exception("At least one of the history results is empty")
# Check that both resutls contain the same data, since CustomData fetches APPL data regardless of the symbol
if not aaplHistory.equals(spyHistory):
raise Exception("Histories are not equal")
class CustomData(PythonData):
'''Custom data source for the regression test algorithm, which returns AAPL equity data regardless of the symbol requested.'''
def GetSource(self, config, date, isLiveMode):
return TradeBar().GetSource(
SubscriptionDataConfig(
config,
CustomData,
# Create a new symbol as equity so we find the existing data files
# Symbol.Create(config.MappedSymbol, SecurityType.Equity, config.Market)),
Symbol.Create("AAPL", SecurityType.Equity, config.Market)),
date,
isLiveMode)
def Reader(self, config, line, date, isLiveMode):
tradeBar = TradeBar.ParseEquity(config, line, date)
data = CustomData()
data.Time = tradeBar.Time
data.Value = tradeBar.Value
data.Close = tradeBar.Close
data.Open = tradeBar.Open
data.High = tradeBar.High
data.Low = tradeBar.Low
data.Volume = tradeBar.Volume
return data