I got a "Building a comprehensive set of Technical Indicators in Python for quantitative trading" code sample from:
However, groupby method (for Simple Moving Average-SMA) is resolved based on it's 'symbol' (AAPL or NFLX):
all_data['SMA_5'] = all_data.groupby('symbol')['Close'].transform(lambda x: x.rolling(window = 5).mean())
all_data['SMA_15'] = all_data.groupby('symbol')['Close'].transform(lambda x: x.rolling(window = 15).mean())
If I have my own DataFrame without the symbol column (only open, close, high, low and volume), is there a way to skip it and perform this method (together with mentioned transform method call)? Calling the method this way seem to require it's symbol column.
Thank you!
I have tried erasing the ('symbol') requirement, however the method does not seem to respond to this type of call. I would like to receive an SMA for a DataFrame that only have open, close, high, low and volume columns using groupby.
CodePudding user response:
Try this for 5 day moving average
all_data['SMA_5'] = all_data['column_name'].ewm(span=5).mean()
CodePudding user response:
If you don't need to group by "symbol" and just want to apply the transformation on your whole data, just remove the whole groupby call:
all_data['SMA_5'] = all_data['Close'].transform(lambda x: x.rolling(window = 5).mean())