* t status pep8 conversion * Minor tweaks and rebase * Various minor fixes --------- Co-authored-by: Martin Molinero <martin.molinero1@gmail.com>
This commit is contained in:
@@ -19,109 +19,109 @@ from AlgorithmImports import *
|
||||
class PythonDictionaryFeatureRegressionAlgorithm(QCAlgorithm):
|
||||
'''Example algorithm showing that Slice, Securities and Portfolio behave as a Python Dictionary'''
|
||||
|
||||
def Initialize(self):
|
||||
def initialize(self):
|
||||
|
||||
self.SetStartDate(2013,10, 7) #Set Start Date
|
||||
self.SetEndDate(2013,10,11) #Set End Date
|
||||
self.SetCash(100000) #Set Strategy Cash
|
||||
self.set_start_date(2013,10, 7) #Set Start Date
|
||||
self.set_end_date(2013,10,11) #Set End Date
|
||||
self.set_cash(100000) #Set Strategy Cash
|
||||
|
||||
self.spySymbol = self.AddEquity("SPY").Symbol
|
||||
self.ibmSymbol = self.AddEquity("IBM").Symbol
|
||||
self.aigSymbol = self.AddEquity("AIG").Symbol
|
||||
self.aaplSymbol = Symbol.Create("AAPL", SecurityType.Equity, Market.USA)
|
||||
self.spy_symbol = self.add_equity("SPY").symbol
|
||||
self.ibm_symbol = self.add_equity("IBM").symbol
|
||||
self.aig_symbol = self.add_equity("AIG").symbol
|
||||
self.aapl_symbol = Symbol.create("AAPL", SecurityType.EQUITY, Market.USA)
|
||||
|
||||
dateRules = self.DateRules.On(2013, 10, 7)
|
||||
self.Schedule.On(dateRules, self.TimeRules.At(13, 0), self.TestSecuritiesDictionary)
|
||||
self.Schedule.On(dateRules, self.TimeRules.At(14, 0), self.TestPortfolioDictionary)
|
||||
self.Schedule.On(dateRules, self.TimeRules.At(15, 0), self.TestSliceDictionary)
|
||||
date_rules = self.date_rules.on(2013, 10, 7)
|
||||
self.schedule.on(date_rules, self.time_rules.at(13, 0), self.test_securities_dictionary)
|
||||
self.schedule.on(date_rules, self.time_rules.at(14, 0), self.test_portfolio_dictionary)
|
||||
self.schedule.on(date_rules, self.time_rules.at(15, 0), self.test_slice_dictionary)
|
||||
|
||||
def TestSliceDictionary(self):
|
||||
slice = self.CurrentSlice
|
||||
def test_slice_dictionary(self):
|
||||
slice = self.current_slice
|
||||
|
||||
symbols = ', '.join([f'{x}' for x in slice.keys()])
|
||||
sliceData = ', '.join([f'{x}' for x in slice.values()])
|
||||
sliceBars = ', '.join([f'{x}' for x in slice.Bars.values()])
|
||||
slice_data = ', '.join([f'{x}' for x in slice.values()])
|
||||
slice_bars = ', '.join([f'{x}' for x in slice.bars.values()])
|
||||
|
||||
if "SPY" not in slice:
|
||||
raise Exception('SPY (string) is not in Slice')
|
||||
|
||||
if self.spySymbol not in slice:
|
||||
if self.spy_symbol not in slice:
|
||||
raise Exception('SPY (Symbol) is not in Slice')
|
||||
|
||||
spy = slice.get(self.spySymbol)
|
||||
spy = slice.get(self.spy_symbol)
|
||||
if spy is None:
|
||||
raise Exception('SPY is not in Slice')
|
||||
|
||||
for symbol, bar in slice.Bars.items():
|
||||
self.Plot(symbol, 'Price', bar.Close)
|
||||
for symbol, bar in slice.bars.items():
|
||||
self.plot(symbol, 'Price', bar.close)
|
||||
|
||||
|
||||
def TestSecuritiesDictionary(self):
|
||||
symbols = ', '.join([f'{x}' for x in self.Securities.keys()])
|
||||
leverages = ', '.join([str(x.GetLastData()) for x in self.Securities.values()])
|
||||
def test_securities_dictionary(self):
|
||||
symbols = ', '.join([f'{x}' for x in self.securities.keys()])
|
||||
leverages = ', '.join([str(x.get_last_data()) for x in self.securities.values()])
|
||||
|
||||
if "IBM" not in self.Securities:
|
||||
if "IBM" not in self.securities:
|
||||
raise Exception('IBM (string) is not in Securities')
|
||||
|
||||
if self.ibmSymbol not in self.Securities:
|
||||
if self.ibm_symbol not in self.securities:
|
||||
raise Exception('IBM (Symbol) is not in Securities')
|
||||
|
||||
ibm = self.Securities.get(self.ibmSymbol)
|
||||
ibm = self.securities.get(self.ibm_symbol)
|
||||
if ibm is None:
|
||||
raise Exception('ibm is None')
|
||||
|
||||
aapl = self.Securities.get(self.aaplSymbol)
|
||||
aapl = self.securities.get(self.aapl_symbol)
|
||||
if aapl is not None:
|
||||
raise Exception('aapl is not None')
|
||||
|
||||
for symbol, security in self.Securities.items():
|
||||
self.Plot(symbol, 'Price', security.Price)
|
||||
for symbol, security in self.securities.items():
|
||||
self.plot(symbol, 'Price', security.price)
|
||||
|
||||
def TestPortfolioDictionary(self):
|
||||
symbols = ', '.join([f'{x}' for x in self.Portfolio.keys()])
|
||||
leverages = ', '.join([f'{x.Symbol}: {x.Leverage}' for x in self.Portfolio.values()])
|
||||
def test_portfolio_dictionary(self):
|
||||
symbols = ', '.join([f'{x}' for x in self.portfolio.keys()])
|
||||
leverages = ', '.join([f'{x.symbol}: {x.leverage}' for x in self.portfolio.values()])
|
||||
|
||||
if "AIG" not in self.Securities:
|
||||
if "AIG" not in self.securities:
|
||||
raise Exception('AIG (string) is not in Portfolio')
|
||||
|
||||
if self.aigSymbol not in self.Securities:
|
||||
if self.aig_symbol not in self.securities:
|
||||
raise Exception('AIG (Symbol) is not in Portfolio')
|
||||
|
||||
aig = self.Portfolio.get(self.aigSymbol)
|
||||
aig = self.portfolio.get(self.aig_symbol)
|
||||
if aig is None:
|
||||
raise Exception('aig is None')
|
||||
|
||||
aapl = self.Portfolio.get(self.aaplSymbol)
|
||||
aapl = self.portfolio.get(self.aapl_symbol)
|
||||
if aapl is not None:
|
||||
raise Exception('aapl is not None')
|
||||
|
||||
for symbol, holdings in self.Portfolio.items():
|
||||
msg = f'{symbol}: {holdings.Leverage}'
|
||||
for symbol, holdings in self.portfolio.items():
|
||||
msg = f'{symbol}: {holdings.leverage}'
|
||||
|
||||
def OnEndOfAlgorithm(self):
|
||||
def on_end_of_algorithm(self):
|
||||
|
||||
portfolioCopy = self.Portfolio.copy()
|
||||
portfolio_copy = self.portfolio.copy()
|
||||
try:
|
||||
self.Portfolio.clear() # Throws exception
|
||||
self.portfolio.clear() # Throws exception
|
||||
except Exception as e:
|
||||
self.Debug(e)
|
||||
self.debug(e)
|
||||
|
||||
bar = self.Securities.pop("SPY")
|
||||
length = len(self.Securities)
|
||||
bar = self.securities.pop("SPY")
|
||||
length = len(self.securities)
|
||||
if length != 2:
|
||||
raise Exception(f'After popping SPY, Securities should have 2 elements, {length} found')
|
||||
|
||||
securitiesCopy = self.Securities.copy()
|
||||
self.Securities.clear() # Does not throw
|
||||
securities_copy = self.securities.copy()
|
||||
self.securities.clear() # Does not throw
|
||||
|
||||
|
||||
def OnData(self, data):
|
||||
'''OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
|
||||
def on_data(self, data):
|
||||
'''on_data event is the primary entry point for your algorithm. Each new data point will be pumped in here.
|
||||
|
||||
Arguments:
|
||||
data: Slice object keyed by symbol containing the stock data
|
||||
'''
|
||||
if not self.Portfolio.Invested:
|
||||
self.SetHoldings("SPY", 1/3)
|
||||
self.SetHoldings("IBM", 1/3)
|
||||
self.SetHoldings("AIG", 1/3)
|
||||
if not self.portfolio.invested:
|
||||
self.set_holdings("SPY", 1/3)
|
||||
self.set_holdings("IBM", 1/3)
|
||||
self.set_holdings("AIG", 1/3)
|
||||
|
||||
Reference in New Issue
Block a user