pep8 conversion of python algo #8 (#7946)

* pep8 conversion

* Minor tweaks

---------

Co-authored-by: Martin Molinero <martin.molinero1@gmail.com>
This commit is contained in:
Louis Szeto
2024-04-19 03:08:34 +08:00
committed by GitHub
parent e8362c12a3
commit 7e5b8d6243
26 changed files with 806 additions and 804 deletions
@@ -14,53 +14,53 @@
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
### 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)
def initialize(self):
self.set_start_date(2014, 6, 5)
self.set_end_date(2014, 6, 6)
self.aapl = self.AddData(CustomData, "AAPL", Resolution.Minute).Symbol
self.spy = self.AddData(CustomData, "SPY", Resolution.Minute).Symbol
self.aapl = self.add_data(CustomData, "AAPL", Resolution.MINUTE).symbol
self.spy = self.add_data(CustomData, "SPY", Resolution.MINUTE).symbol
def OnEndOfAlgorithm(self):
aaplHistory = self.History(CustomData, self.aapl, self.StartDate, self.EndDate, Resolution.Minute,
fillForward=False, extendedMarketHours=False, dataNormalizationMode=DataNormalizationMode.Raw).droplevel(0, axis=0)
spyHistory = self.History(CustomData, self.spy, self.StartDate, self.EndDate, Resolution.Minute,
fillForward=False, extendedMarketHours=False, dataNormalizationMode=DataNormalizationMode.Raw).droplevel(0, axis=0)
def on_end_of_algorithm(self):
aapl_history = self.history(CustomData, self.aapl, self.start_date, self.end_date, Resolution.MINUTE,
fill_forward=False, extended_market_hours=False, data_normalization_mode=DataNormalizationMode.RAW).droplevel(0, axis=0)
spy_history = self.history(CustomData, self.spy, self.start_date, self.end_date, Resolution.MINUTE,
fill_forward=False, extended_market_hours=False, data_normalization_mode=DataNormalizationMode.RAW).droplevel(0, axis=0)
if aaplHistory.size == 0 or spyHistory.size == 0:
if aapl_history.size == 0 or spy_history.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):
if not aapl_history.equals(spy_history):
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(
def get_source(self, config, date, is_live_mode):
return TradeBar().get_source(
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)),
# Symbol.create(config.mapped_symbol, SecurityType.EQUITY, config.market)),
Symbol.create("AAPL", SecurityType.EQUITY, config.market)),
date,
isLiveMode)
is_live_mode)
def Reader(self, config, line, date, isLiveMode):
tradeBar = TradeBar.ParseEquity(config, line, date)
def reader(self, config, line, date, is_live_mode):
trade_bar = TradeBar.parse_equity(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
data.time = trade_bar.time
data.value = trade_bar.value
data.close = trade_bar.close
data.open = trade_bar.open
data.high = trade_bar.high
data.low = trade_bar.low
data.volume = trade_bar.volume
return data