Skip to content Skip to sidebar Skip to footer

Python: How To Code An Exponential Moving Average?

I want to do calculations on three columns of a dataframe df. In order to do that I want run a price of assets (cryptocurrencies) list in a three column table in order to calculate

Solution 1:

pandas.stats.moments.ewma from the original answer has been deprecated.

Instead you can use pandas.DataFrame.ewm as documented here.


Below is a complete snippet with random data that builds a dataframe with calculated ewmas from specified columns.

Code:

# importsimport pandas as pd
import numpy as np

np.random.seed(123)

rows = 50
df = pd.DataFrame(np.random.randint(90,110,size=(rows, 3)), columns=['BTC', 'ETH', 'DASH'])
datelist = pd.date_range(pd.datetime(2017, 1, 1).strftime('%Y-%m-%d'), periods=rows).tolist()
df['dates'] = datelist 
df = df.set_index(['dates'])
df.index = pd.to_datetime(df.index)

defewmas(df, win, keepSource):
    """Add exponentially weighted moving averages for all columns in a dataframe.

    Arguments: 
    df -- pandas dataframe
    win -- length of ewma estimation window
    keepSource -- True or False for keep or drop source data in output dataframe

    """

    df_temp = df.copy()

    # Manage existing column names
    colNames = list(df_temp.columns.values).copy()
    removeNames = colNames.copy()

    i = 0for col in colNames:

        # Make new names for ewmas
        ewmaName = colNames[i] + '_ewma_' + str(win)   

        # Add ewmas#df_temp[ewmaName] = pd.stats.moments.ewma(df[colNames[i]], span = win)
        df_temp[ewmaName] = df[colNames[i]].ewm(span = win, adjust=True).mean()

        i = i + 1# Remove estimates with insufficient window length
    df_temp = df_temp.iloc[win:]

    # Remove or keep source dataif keepSource == False:
        df_temp = df_temp.drop(removeNames,1)

    return df_temp

# Test run
df_new = ewmas(df = df, win = 22, keepSource = True)
print(df_new.tail())

Output:

BTCETHDASHBTC_ewma_22ETH_ewma_22DASH_ewma_22dates2017-02-15   91969898.752431100.08105297.9267872017-02-16  10010210298.862445100.25027098.2859732017-02-17  1001079798.962634100.84474998.1727122017-02-18  1031029199.317826100.94638497.5416842017-02-19   991049199.289894101.21475596.966758

Plot using df_new[['BTC', 'BTC_ewma_22']].plot():

enter image description here

Solution 2:

In your loop for i,column in enumerate(column_by_search): you iterate over the elements in your column_by_search list, that is column takes on the values "BTC", "ETH", "DASH" in turn. Thus, len(column) will give you the length of the string "BTC", which is 3 in fact.

Try df[column] instead, that will return a list with the elements in the desired column and you can iterate over it.

Post a Comment for "Python: How To Code An Exponential Moving Average?"