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 "

  1. Runtime Error: property is read-only
  2. at FineSelectionFunction
  3. i.MarketCap = float(i.MarketCap)
  4. at Python.Runtime.PythonException.ThrowLastAsClrException()
  5. at Python.Runtime.Dispatcher.TrueDispatch(Object[] args)
  6. at Python.Runtime.Dispatcher.Dispatch(Object[] args)
  7. 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?

  1. def CoarseSelectionFunction(self, coarse):
  2. ''' Drop stocks which have no fundamental data or have low price '''
  3. return [x.Symbol for x in coarse if x.HasFundamentalData and x.Price > 5]
  4. def FineSelectionFunction(self, fine):
  5. ''' Selects the stocks by lowest market cap '''
  6. sorted_market_cap = sorted([x for x in fine if x.MarketCap > 0],
  7. key=lambda x: x.MarketCap)
  8. for i in fine:
  9. i.MarketCap = float(i.MarketCap)
  10. top_market_cap = sorted(fine, key = lambda x:x.MarketCap, reverse=True)[:int(len(fine)*0.2)]
  11. self.sorted_by_mc = [i.Symbol for i in top_market_cap]
  12. total_market_cap = np.sum([i.MarketCap for i in top_market_cap])
  13. self.weights = {}
  14. for i in top_market_cap:
  15. self.weights[str(i.Symbol)] = i.MarketCap/total_market_cap
  16. return self.sorted_by_mc
+ Expand

Author

Sebastian Wozniczka

October 2022