Ant*_*pin 3 python math finance python-3.x pandas
我想对数据帧的三列进行计算df.为了做到这一点,我想在三列表中运行资产(加密货币)列表的价格,以便在有足够的数据后计算它们的指数移动平均值.
def calculateAllEMA(self,values_array):
    df = pd.DataFrame(values_array, columns=['BTC', 'ETH', 'DASH'])
    column_by_search = ["BTC", "ETH", "DASH"]
    print(df)
    for i,column in enumerate(column_by_search):
        ema=[]
        # over and over for each day that follows day 23 to get the full range of EMA
        for j in range(0, len(column)-24):
            # Add the closing prices for the first 22 days together and divide them by 22.
            EMA_yesterday = column.iloc[1+j:22+j].mean()
            k = float(2)/(22+1)
            # getting the first EMA day by taking the following day’s (day 23) closing price multiplied by k, then multiply the previous day’s moving average by (1-k) and add the two.
            ema.append(column.iloc[23 + j]*k+EMA_yesterday*(1-k))
        print("ema")
        print(ema)
        mean_exp[i] = ema[-1]
    return mean_exp
然而,当我打印出来的时候len(column)-24我得到-21(-24 + 3?).因此,我无法通过循环.我如何应对此错误以获得资产的指数移动平均线?
我试图将这个链接从iexplain.com应用于指数移动平均线的伪代码.
如果你有任何更容易的想法,我很乐意听到它.
这是我用来计算错误的数据:
        BTC     ETH    DASH
0   4044.59  294.40  196.97
1   4045.25  294.31  196.97
2   4044.59  294.40  196.97
3   4045.25  294.31  196.97
4   4044.59  294.40  196.97
5   4045.25  294.31  196.97
6   4044.59  294.40  196.97
7   4045.25  294.31  196.97
8   4045.25  294.31  196.97
9   4044.59  294.40  196.97
10  4045.25  294.31  196.97
11  4044.59  294.40  196.97
12  4045.25  294.31  196.97
13  4045.25  294.32  197.07
14  4045.25  294.31  196.97
15  4045.41  294.46  197.07
16  4045.25  294.41  197.07
17  4045.41  294.41  197.07
18  4045.41  294.47  197.07
19  4045.25  294.41  197.07
20  4045.25  294.32  197.07
21  4045.43  294.35  197.07
22  4045.41  294.46  197.07
23  4045.25  294.41  197.07
您可以pandas.stats.moments.ewma按照此处的说明使用.
以下是我对可能的解决方案的建议:
带有随机值的数据框应符合您的描述:
# imports
import 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)
def ewmas(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 = 0
    for 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 data
    if 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())
以下代码段生成原始数据框的副本,并在给定定义窗口长度的情况下添加具有ewma的列:
             BTC  ETH   DASH  BTC_ewma_22  ETH_ewma_22    DASH_ewma_22
dates                                                             
2017-02-15   91   96    98    98.752431    100.081052     97.926787
2017-02-16  100  102   102    98.862445    100.250270     98.285973
2017-02-17  100  107    97    98.962634    100.844749     98.172712
2017-02-18  103  102    91    99.317826    100.946384     97.541684
2017-02-19   99  104    91    99.289894    101.214755     96.966758
我们不得不做一些调整.pandas.DataFrame.ewm如果没有足够的观察值来填充您的估算窗口,则不会引发错误.因此,您应该删除与窗口长度相对应的第一个观察值.
# imports
import 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)
def ewmas(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 = 0
    for 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 data
    if 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())
根据您是否要将源数据保存在新数据框中,可以将其删除,如下所示:
             BTC  ETH   DASH  BTC_ewma_22  ETH_ewma_22    DASH_ewma_22
dates                                                             
2017-02-15   91   96    98    98.752431    100.081052     97.926787
2017-02-16  100  102   102    98.862445    100.250270     98.285973
2017-02-17  100  107    97    98.962634    100.844749     98.172712
2017-02-18  103  102    91    99.317826    100.946384     97.541684
2017-02-19   99  104    91    99.289894    101.214755     96.966758
这是包含在函数中的整个过程:
# imports
import 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)
def ewmas(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 = 0
    for 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 data
    if 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())
这是一个测试运行:
             BTC  ETH   DASH  BTC_ewma_22  ETH_ewma_22    DASH_ewma_22
dates                                                             
2017-02-15   91   96    98    98.752431    100.081052     97.926787
2017-02-16  100  102   102    98.862445    100.250270     98.285973
2017-02-17  100  107    97    98.962634    100.844749     98.172712
2017-02-18  103  102    91    99.317826    100.946384     97.541684
2017-02-19   99  104    91    99.289894    101.214755     96.966758
小智 0
在循环中,for i,column in enumerate(column_by_search):您迭代列表中的元素column_by_search,即列依次采用值“BTC”、“ETH”、“DASH”。因此,len(column)将为您提供字符串“BTC”的长度,实际上是 3。
请尝试df[column],这将返回一个列表,其中包含所需列中的元素,您可以对其进行迭代。
| 归档时间: | 
 | 
| 查看次数: | 6117 次 | 
| 最近记录: |