from sklearn.model_selection import train_test_split from sklearn.ensemble import RandomForestClassifier from sklearn.metrics import accuracy_score, classification_report from datetime import datetime from datetime import timedelta import pandas as pd import numpy as np class TransdimensionalTachyonCompensator(QCAlgorithm): month = 0 def Initialize(self): self.SetStartDate(2020, 1, 1) # Set Start Date self.SetEndDate(2020, 10, 27) self.SetCash(100000) # Set Strategy Cash self.trading_symbols = [] self.UniverseSettings.Resolution = Resolution.Minute self.AddUniverse(self.CoarseSelectionFunction) self.lookback_period = 60 self.spy = self.AddEquity("SPY").Symbol # self.Train(self.DateRules.MonthStart(), self.TimeRules.AfterMarketOpen(self.spy, 0), self.train) self.Schedule.On(self.DateRules.MonthStart(), self.TimeRules.AfterMarketOpen(self.spy, 0), self.train) def CoarseSelectionFunction(self, coarse): if self.Time.month == self.month: return Universe.Unchanged self.month = self.Time.month sortedByDollarVolume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True) self.trading_symbols = [ x.Symbol for x in sortedByDollarVolume if x.HasFundamentalData ][:20] return self.trading_symbols def train(self): if not self.trading_symbols: return price_history = self.History(self.trading_symbols , self.lookback_period , Resolution.Daily) symbol1 = [self.trading_symbols[0] for i in range(self.lookback_period)] symbol2 = [self.trading_symbols[1] for i in range(self.lookback_period)] symbol3 = [self.trading_symbols[2] for i in range(self.lookback_period)] symbol4 = [self.trading_symbols[3] for i in range(self.lookback_period)] symbol5 = [self.trading_symbols[4] for i in range(self.lookback_period)] symbol6 = [self.trading_symbols[5] for i in range(self.lookback_period)] symbol7 = [self.trading_symbols[6] for i in range(self.lookback_period)] symbol8 = [self.trading_symbols[7] for i in range(self.lookback_period)] symbol9 = [self.trading_symbols[8] for i in range(self.lookback_period)] symbol10 = [self.trading_symbols[9] for i in range(self.lookback_period)] symbol11 = [self.trading_symbols[10] for i in range(self.lookback_period)] symbol12 = [self.trading_symbols[11] for i in range(self.lookback_period)] symbol13 = [self.trading_symbols[12] for i in range(self.lookback_period)] symbol14 = [self.trading_symbols[13] for i in range(self.lookback_period)] symbol15 = [self.trading_symbols[14] for i in range(self.lookback_period)] symbol16 = [self.trading_symbols[15] for i in range(self.lookback_period)] symbol17 = [self.trading_symbols[16] for i in range(self.lookback_period)] symbol18 = [self.trading_symbols[17] for i in range(self.lookback_period)] symbol19 = [self.trading_symbols[18] for i in range(self.lookback_period)] symbol20 = [self.trading_symbols[19] for i in range(self.lookback_period)] close_list = [] open_list = [] high_list = [] low_list = [] volume_list = [] date_list = [ str(price_history.index[i][1]).split(" ")[0] for i in range(price_history.shape[0]) ] for i in range(price_history.shape[0]): close_list.append(price_history.close.iloc[i]) open_list.append(price_history.open.iloc[i]) high_list.append(price_history.high.iloc[i]) low_list.append(price_history.low.iloc[i]) volume_list.append(price_history.volume.iloc[i]) d = {'symbol': symbol1 + symbol2 + symbol3 + symbol4 + symbol5 + symbol6 + symbol7 + symbol8 + symbol9 \ + symbol10 + symbol11 + symbol12 + symbol13 + symbol14 + symbol15 + symbol16 + symbol17 + symbol18 + \ symbol19 + symbol20, 'datetime': date_list, 'close': close_list, 'high': high_list, 'low': low_list, 'open': open_list, 'volume': volume_list } self.price_data = pd.DataFrame(data=d) self.price_data = self.price_data[['symbol','datetime','close','high','low','open','volume']] # sort the values by symbol and then date self.price_data.sort_values(by = ['symbol','datetime'], inplace = True) # calculate the change in price self.price_data['change_in_price'] = self.price_data['close'].diff() # identify rows where the symbol changes mask = self.price_data['symbol'] != self.price_data['symbol'].shift(1) # For those rows, let's make the value null self.price_data['change_in_price'] = np.where(mask == True, np.nan, self.price_data['change_in_price']) # print the rows that have a null value, should only be 5 self.price_data[self.price_data.isna().any(axis = 1)] # Calculate the 14 day RSI n = 14 # First make a copy of the data frame twice up_df, down_df = self.price_data[['symbol','change_in_price']].copy(), self.price_data[['symbol','change_in_price']].copy() # For up days, if the change is less than 0 set to 0. up_df.loc['change_in_price'] = up_df.loc[(up_df['change_in_price'] < 0), 'change_in_price'] = 0 # For down days, if the change is greater than 0 set to 0. down_df.loc['change_in_price'] = down_df.loc[(down_df['change_in_price'] > 0), 'change_in_price'] = 0 # We need change in price to be absolute. down_df['change_in_price'] = down_df['change_in_price'].abs() # Calculate the EWMA (Exponential Weighted Moving Average), meaning older values are given less weight compared to newer values. ewma_up = up_df.groupby('symbol')['change_in_price'].transform(lambda x: x.ewm(span = n).mean()) ewma_down = down_df.groupby('symbol')['change_in_price'].transform(lambda x: x.ewm(span = n).mean())

Hi guys, I've been trying to apply a lambda transform to my "change in price" column values, sorted by symbol values. 

However, I'm being thrown the error: 

"2020-04-01 09:31:00 Runtime Error: In Scheduled Event 'MonthStart: SPY: 0 min after MarketOpen', TypeError : Cannot get managed object
  at train in main.py:line 160 :: ewma_up = up_df.groupby(\'symbol\')[\'change_in_price\'].transform(lambda x: x.ewm(span = n).mean())
 TypeError : Cannot get managed object"

This does not seem to be a common issue as well, and the solutions provided in other threads do not help for me. 

Any help will be greatly appreciated, thank you!