Can anyone please help me out, this algorithm is not running:-
# This code is developed for using in the QuantConnect platform
# region imports
from AlgorithmImports import *
# endregion
class DynamicValueInvest(QCAlgorithm):
def __init__(self):
self.SetStartDate(2019,1,1)
self.SetCash(200000)
self.lookback = 252*5
self.Symbol = "SPY"
self.ROC = RateOfChange(252*5)
self.Volume = None
def CoarseSelectionFunction(self, coarse):
for i in coarse:
if i.Symbol not in self.dataDict:
self.dataDict[i.Symbol] = SymbolData(i.Symbol, self.lookback)
self.dataDict[i.Symbol].ROC.Update(i.EndTime, i.AdjustedPrice)
self.dataDict[i.Symbol].Volume = i.Volume
if self.monthly_rebalance:
# drop stocks which have no fundamental data
filteredCoarse = [x for x in coarse if (x.HasFundamentalData)]
return [i.Symbol for i in filteredCoarse]
else:
return []
def FineSelectionFunction(self, fine):
if self.monthly_rebalance:
dataReady = {symbol: symbolData for (symbol, symbolData) in self.dataDict.items() if symbolData.ROC.IsReady}
if len(dataReady) < 100:
self.filteredFine = []
else:
sortedFine = [i for i in fine if i.EarningReports.BasicAverageShares.ThreeMonths != 0 and i.Symbol in dataReady]
sortedFineSymbols = [i.Symbol for i in sortedFine]
filteredData = {symbol: symbolData for (symbol, symbolData) in dataReady.items() if symbol in sortedFineSymbols}
for i in sortedFine:
if i.Symbol in filteredData and filteredData[i.Symbol].Volume != 0:
filteredData[i.Symbol].Turnover = i.EarningReports.BasicAverageShares.ThreeMonths / filteredData[i.Symbol].Volume
for i in sortedFine:
if i.Symbol in filteredData and filteredData[i.Symbol].Volume != 0:
filteredData[i.Symbol].Turnover = i.EarningReports.BasicAverageShares.ThreeMonths / filteredData[i.Symbol].Volume
sortedByROC = sorted(filteredData.values(), key = lambda x: x.ROC.Current.Value, reverse = True)
topROC = sortedByROC[:int(len(sortedByROC)*0.2)]
bottomROC = sortedByROC[-int(len(sortedByROC)*0.2):]
HighTurnoverTopROC = sorted(topROC, key = lambda x: x.Turnover, reverse = True)
HighTurnoverBottomROC = sorted(bottomROC, key = lambda x: x.Turnover, reverse = True)
self.long = [i.Symbol for i in HighTurnoverTopROC[:int(len(HighTurnoverTopROC)*0.01)]]
self.short = [i.Symbol for i in HighTurnoverBottomROC[:int(len(HighTurnoverBottomROC)*0.01)]]
self.filteredFine = self.long + self.short
self.portfolios.append(self.filteredFine)
def OnData(self, data):
if self.monthly_rebalance and self.filteredFine:
self.filteredFine = None
self.monthly_rebalance = False
# 1/3 of the portfolio is rebalanced every month
if len(self.portfolios) == self.portfolios.maxlen:
for i in list(self.portfolios)[0]:
self.Liquidate(i)
# stocks are equally weighted and held for 3 months
short_weight = 1/len(self.short)
for i in self.short:
self.SetHoldings(i, -1/3*short_weight)
long_weight = 1/len(self.long)
for i in self.long:
self.SetHoldings(i, 1/3*long_weight)
Vladimir
Manav Trivedi
Much more than just “param” is missing. Can you share the link where you got the code from?
Manav Trivedi
It's based on value investing algorithm, and I got the code somewhere from QuantConnect, in forum or so.
Louis Szeto
Hi Manav
We should use Initialize(self) instead of __init__(self) for initializing a base algorithm in QC. I also didn't see any implementation of SymbolData class, nor AddUniverse for the coarse and fine fundamental universes. self.dataDict is also not instantiated for storing your data.
Best
Louis
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Manav Trivedi
Hi Louis, thanks for the clarification, I'll amend the changes and catch up on this if there's anything I missed. Thanks.
Vladimir
Manav Trivedi
With vague information about the code “somewhere in QuantConnect”, you can only get vague answers “someday, someone, somehow”.
It is necessary to describe the strategy in detail or close the thread.
Good luck.
Manav Trivedi
Hi Vladimir, thanks for your advice, I'll keep this in mind for any query I put from now on.
Manav Trivedi
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!