Hi

I have tried to work out a way to loop over a list of fundamental ratios, and get it nice and smooth into a pandas DataFrame, so I can implement a multivariable linear regression model on the data. My lack of data manipulation skills in pandas unfortunately stops me from making it work. I have worked out a way to do it without a loop, but i takes up a lot of space, and it is not flexible at all (see code). 

  1. qb = QuantBook()
  2. from sklearn.decomposition import FactorAnalysis
  3. tickers = ("AAPL", "MSFT")
  4. symbols = [qb.AddEquity(ticker, Resolution.Daily).Symbol for ticker in tickers]
  5. start_time = datetime(2017, 1, 2)
  6. end_time = datetime(2020, 1, 2)
  7. funda_start = datetime(2020, 1, 1)
  8. funda_end = datetime(2020, 1, 2)
  9. fundamentals = ["ValuationRatios.SustainableGrowthRate", "ValuationRatios.PayoutRatio"]
  10. pe_ratios = qb.GetFundamental(symbols, "ValuationRatios.SustainableGrowthRate", funda_start, funda_end)
  11. pay_ratio = qb.GetFundamental(symbols, "ValuationRatios.PayoutRatio", funda_start, funda_end)
  12. dataframe = pd.concat([pe_ratios, pay_ratio], axis=0)
  13. dataframe = dataframe.transpose()
  14. dataframe.columns = fundamentals
  15. returns = qb.History(symbols, start_time, end_time, Resolution.Daily)['close']
  16. returns = returns.to_frame()
  17. returns.reset_index(["time"], inplace=True)
  18. final_returns = returns.groupby(level=0)
  19. final_returns = final_returns.agg(lambda x: x.iloc[-1]).close
  20. final_returns = final_returns.to_frame()
  21. final_data = pd.concat([dataframe, final_returns], axis=1)
  22. y = final_data.close
  23. X = dataframe
  24. from sklearn import linear_model
  25. regr = linear_model.LinearRegression()
  26. regr.fit(X, y)
  27. print(regr.coef_)
+ Expand

 

The problem I am encountering is that a loop over the list, and appending to a empty DataFrame does not work (see code)

  1. qb = QuantBook()
  2. tickers = ("AAPL", "MSFT")
  3. symbols = [qb.AddEquity(ticker, Resolution.Daily).Symbol for ticker in tickers]
  4. start_time = datetime(2017, 1, 2)
  5. end_time = datetime(2020, 1, 2)
  6. funda_start = datetime(2020, 1, 1)
  7. funda_end = datetime(2020, 1, 2)
  8. fundamentals = ["ValuationRatios.SustainableGrowthRate", "ValuationRatios.PayoutRatio"]
  9. ratios = pd.DataFrame()
  10. for fundamental in fundamentals:
  11. ratio = qb.GetFundamental(symbols, fundamental, funda_start, funda_end)
  12. ratio1 = ratio.transpose()
  13. ratio1.set_axis([fundamental], axis=1)
  14. ratios.append(ratio1)
  15. ratios
+ Expand

 

I was wondering if any knew how to solve this problem, as it becomes more irritating, the more variables is being used in the program

Have a nice day

Lucas

 

Author

Lucas

February 2022