Hi guys!
I am trying to create a strategy, which would sort stocks by their market cap, by choosing only top 20% of them. I've been following a simmilar code which I found in tutorials, regarding the “book-to-Market Value Anomaly”, by adjusting it to my strategy.
https://www.quantconnect.com/tutorials/strategy-library/book-to-market-value-anomaly
Unfortunately after running my code i get this error "
Runtime Error: property is read-only
at FineSelectionFunction
i.MarketCap = float(i.MarketCap)
at Python.Runtime.PythonException.ThrowLastAsClrException()
at Python.Runtime.Dispatcher.TrueDispatch(Object[] args)
at Python.Runtime.Dispatcher.Dispatch(Object[] args)
at __System_Func`2\[\[System_Collections_Generic_IEnumerable`1\[\[QuantConnect_Data_Fundamental_FineFundamental\ in main.py: line 40
Do you know what could be causing this? And do you see any other issues in my code, which I could be facing in the near future?
def CoarseSelectionFunction(self, coarse):
''' Drop stocks which have no fundamental data or have low price '''
return [x.Symbol for x in coarse if x.HasFundamentalData and x.Price > 5]
def FineSelectionFunction(self, fine):
''' Selects the stocks by lowest market cap '''
sorted_market_cap = sorted([x for x in fine if x.MarketCap > 0],
key=lambda x: x.MarketCap)
for i in fine:
i.MarketCap = float(i.MarketCap)
top_market_cap = sorted(fine, key = lambda x:x.MarketCap, reverse=True)[:int(len(fine)*0.2)]
self.sorted_by_mc = [i.Symbol for i in top_market_cap]
total_market_cap = np.sum([i.MarketCap for i in top_market_cap])
self.weights = {}
for i in top_market_cap:
self.weights[str(i.Symbol)] = i.MarketCap/total_market_cap
return self.sorted_by_mc
Nico Xenox
Hey Sebastian Wozniczka,
you're trying to change the values of the fine selection, thats why you receive this error.
I added the code:
I changed the len(fine) to len([i for i in fine]) because that didnt work either. Also if you try to access the symbol in self.weights later in your code I would suggest just adding the QC symbol and not the str(symbol). This will make your life easier.
Hope it helps ;)
Sebastian Wozniczka
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!