Hey Guys,
For a project i need to create my own rating.
Based on coarse and fine selection I want to create a rating (0-100) on which the trades are made.
I thought of creating an DataFrame with initializing and adding my weighted indicators.
For example:
def Initialize(self):
columns = ['SYMBOL', 'VOL', 'RATING']
self.data = pd.DataFrame(columns=columns)
def CoarseSelectionFunction(self, coarse):
selected = [x for x in coarse if (x.HasFundamentalData) and (float(x.Price) > 5)]
for x in selected:
v = lambda x: x.DollarVolume
z = lambda x: x.Symbol.Value
try:
if v > 20000000:
self.data.append(pd.DataFrame({'VOL': [1], 'SYMBOL': z}, index=[z]))
else:
self.data.append(pd.DataFrame({'VOL': [v/20000000], 'SYMBOL': z}, index=[z]))
except:
self.data.append(pd.DataFrame({'VOL': [0], 'SYMBOL': v}, index=[z]))
return [x.Symbol for x in selected]
def FineSelectionFunction(self, fine):
for x in fine:
m = lambda x: x.MarketCap
z = lambda x: x.Symbol
try:
if m >= 10000000000:
self.data.loc[z, "SIZE"] = 1
elif m <= 1000000000:
self.data.loc[z, "SIZE"] = 0
else:
self.data.loc[z, "SIZE"] = ((m - 1000000000) / 9000000000)
except:
self.data.loc[z, "SIZE"] = 0
Since I get an Attribute error when accessing Symbol in FineSelection, I'm wondering wether my approach with Lambda is even correct?
Rainer Gabel
forgot to add "SIZE" as a columm..
Shile Wen
Hi Rainier,
We don't need a lambda function (which doesn't get us the desired result anyway), we can just get the value directly. Furthermore, we get the Attribute error because we are trying to access an index using a function. After that, we also need to add "SIZE" as a field initially when we add the rows to the DataFrame. Please see the attached backtest for the fixes, and I've also made a few stylistic changes and other bugfixes.
Best,
Shile Wen
Rainer Gabel
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!