Back

For loop help

Hi there, 

I am trying to create my own funamental indicator for stocks named ROIC, which involves taking 3 existing funamental data and then for each company dividing one by the addition e.g ROIC = a/b+c. 

However, I am having trouble with the for loop involved in this, if someone could help me, it would be much appreciated. 

Update Backtest







def FineSelectionFunction(self, fine):
if self.flag1:
self.flag1 = 0


filtered_fine = [x for x in fine if x.FinancialStatements.IncomeStatement.EBIT.ThreeMonths
and x.FinancialStatements.BalanceSheet.InvestedCapital.ThreeMonths
and x.FinancialStatements.BalanceSheet.PreferredStock.ThreeMonths]


ROIC = [for x in filtered_fine: (x.FinancialStatements.IncomeStatement.EBIT.ThreeMonths/FinancialStatements.BalanceSheet.InvestedCapital.ThreeMonths+FinancialStatements.BalanceSheet.PreferredStock.ThreeMonths)]

sorted_by_ROIC = sorted(ROIC, reverse=True)

# The FinancialStatements is the property of elements in filtered_fine, so you need to basically say "from the data in filtered_fine, take x, y and z and do this function, and return the answer as sortedbyROIC.
sortedByROIC = sorted(filtered_fine, key=lambda x: (x.FinancialStatements.IncomeStatement.EBIT.ThreeMonths/FinancialStatements.BalanceSheet.InvestedCapital.ThreeMonths+FinancialStatements.BalanceSheet.PreferredStock.ThreeMonths), reverse=True)
#financial data comes in quaters i.e Three Month periods, therefore make sure it is three months so that there is no 0 on denominator.

return [i.Symbol for i in sortedByROIC]

num_stocks = floor(len(filtered_fine)/self.num_portfolios)

stock_dict = {}

for i,ele in enumerate(ROIC):
rank1 = i

score = [ceil(rank1/num_stocks)]
score = sum(score)
stock_dict[ele] = score

self.sorted_stock = sorted(stock_dict.items(), key=lambda d:d[1],reverse=True)
sorted_symbol = [self.sorted_stock[i][0] for i in range(len(self.sorted_stock))]
topFine = sorted_symbol[:self.numberOfSymbolsFine]


else:
return []

0

Sorry, I had to add the code seperately. 

0

In the first comment, you added an unrelated notebook.
Could you please share/attach the backtest with the FineSelectionFunction method above?
By the way, what do you mean by "having trouble with the for loop involved in this"?

0

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.


Update Backtest





0

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.


Loading...

This discussion is closed