File size: 1,281 Bytes
1e1b538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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)