import pandas as pd import numpy as np df = pd.read_csv('candles.csv') from talib import RSI, BBANDS, MACD, ATR, EMA, SMA # group by market grouped = df.groupby('market') # for each market calculate the indicators and add them to the dataframe for market, group in grouped: # calculate the indicators print('Calculating indicators for', market) df.loc[group.index,'rsi'] = RSI(group['close'], timeperiod=14) upper, middle, lower = BBANDS(group['close'], timeperiod=20) df.loc[group.index,'bb_upper'] = upper df.loc[group.index,'bb_middle'] = middle df.loc[group.index,'bb_lower'] = lower macd, macdsignal, macdhist = MACD(group['close'], fastperiod=12, slowperiod=26, signalperiod=9) df.loc[group.index,'macd'] = macd df.loc[group.index,'macdsignal'] = macdsignal df.loc[group.index,'macdhist'] = macdhist df.loc[group.index,'atr'] = ATR(group['high'], group['low'], group['close'], timeperiod=14) df.loc[group.index,'ema'] = EMA(group['close'], timeperiod=30) df.loc[group.index,'sma'] = SMA(group['close'], timeperiod=30) # drop the rows with NaN values df = df.dropna() # save the dataframe to a new file print(df.tail()) df.to_csv('indicators.csv', index=False)