visierl / mdi

Market Divergence Index (MDI) Calculator Project
Other
1 stars 0 forks source link

How about using pandas? #1

Open alexispetit opened 6 years ago

alexispetit commented 6 years ago

Hi there, I've just come across your library and thought you could benefit from using pandas. Here's an example of how to calculate the MDI using the data you provided:

import pandas as pd 

# Load data
path = 'test_data/multiple_markets/'
names = ('date', 'open', 'high', 'low', 'close', 'volume', 'open_interest')
ZC = pd.read_csv(path + 'ZC_REV.csv', header=None, names=names, parse_dates=[0], index_col=0)
KW = pd.read_csv(path + 'KW_REV.csv', header=None, names=names, parse_dates=[0], index_col=0)

def signal_to_noise(df, n):
    signal = df['close'].diff(n).abs()
    noise = df['close'].diff().abs().rolling(n).sum()
    return signal / noise

# Calculate SNR and MDI
snr = pd.concat([signal_to_noise(ZC, n=100).to_frame('ZC'), 
                           signal_to_noise(KW, n=100).to_frame('KW')], axis=1)
mdi = snr.mean(axis=1)
mdi.to_csv('MDI.csv')
visierl commented 6 years ago

That looks promising, I will play with pandas a bit and see where it goes. Thanks for the suggestion and the sample code.