Search code examples
pythonpandasdataframematplotlibta-lib

Where is a price indicator calculated using Pandas supposed to be 'put'?


first of all thanks for all the love you show to newcomers like me.

Now to the code.

I have this seemingly simple code that someone wrote to for calculating the Moving Averages of price, but to understand it and follow it I have one question that I need to answer regarding the definition of where.

def ma(Data, lookback, what, where):
    
    for i in range(len(Data)):
            try:
                Data[i, where] = (Data[i - lookback + 1:i + 1, what].mean())
        
            except IndexError:
                pass
    return Data

#1 The Data variable is the OHLC array
#2 The what variable is the closing price column
#3 The where variable is where to put the indicator
#4 The lookback variable is the moving average's period

But when it comes to #3, I don't understand. What is meant where to put the indicator? I want to compute the data, and use that data. I don't want to put the indicator anywhere, just to have it for reference.

This is the dataframe I want to use as Data:

                         open          high           low         close
hr                                                                      
2021-02-01 01:00  32835.839519  33438.836060  32835.839519  33389.974454
2021-02-01 02:00  33400.422170  33663.000232  33260.343246  33569.816646
2021-02-01 03:00  33581.206956  33633.454861  33437.081539  33466.178320
2021-02-01 04:00  33473.881292  33680.926267  33473.881292  33512.310031
2021-02-01 05:00  33496.550836  33692.662741  33442.771302  33692.295982
2021-02-01 06:00  33674.363254  33931.304243  33372.640454  33372.640454
2021-02-01 07:00  33466.277250  33718.499099  33410.173005  33543.580113
2021-02-01 08:00  33691.622420  34155.668712  33577.491467  34155.668712
2021-02-01 09:00  34541.989582  34541.989582  34246.591606  34255.313422
2021-02-01 10:00  34254.311054  34318.042807  34009.930110  34043.516502
2021-02-01 11:00  34102.209535  34181.236131  33960.033182  34181.236131
2021-02-01 12:00  34196.688991  34196.688991  33677.872302  33677.872302
2021-02-01 13:00  33658.073761  33831.043558  33616.263612  33616.263612
2021-02-01 14:00  33506.144521  33698.288934  33298.355558  33458.379708
2021-02-01 15:00  33449.472954  33540.119075  33137.424801  33137.424801
2021-02-01 16:00  33129.661923  33444.487621  33129.661923  33444.487621
2021-02-01 17:00  33427.510914  33966.758995  33328.452664  33966.758995
2021-02-01 18:00  33883.647812  33883.647812  33692.664177  33753.657270
2021-02-01 19:00  33568.840250  33904.003721  33568.840250  33817.161851
2021-02-01 20:00  33716.826906  33857.417730  33663.339958  33757.230256
2021-02-01 21:00  33608.964863  33692.950108  33508.713475  33553.086495
2021-02-01 22:00  33475.494546  33629.985564  33423.225607  33423.225607
2021-02-01 23:00  33396.069559  33614.188165  33361.882597  33405.990357
2021-02-02 00:00  33423.242711  33912.467287  33423.242711  33626.927090
2021-02-02 01:00  33578.032588  33578.032588  33470.631802  33470.631802

Solution

  • The where refers to the column name in which the result will be stored. In pandas you commonly don't return a list from a function, but just add a column/Series to a DataFrame, which downstream functions can use as they see fit.

    In this example I save the result to the where='close-3-day-avg' column. Note I modified the function ma a bit as I could not get it working in the form that it was:

    import pandas as pd
    pd.set_option('display.max_columns', None)
    pd.set_option('display.width', 2000)
    
    def ma(Data, lookback, what, where):
        Data[where] = 0.0
        for i in range(len(Data)):
            Data[where].loc[i] = Data[what].loc[i - lookback + 1:i].mean()
        return Data
    
    data = """                open          high           low         close
    hr
    2021-02-01 01:00  32835.839519  33438.836060  32835.839519  33389.974454
    2021-02-01 02:00  33400.422170  33663.000232  33260.343246  33569.816646
    2021-02-01 03:00  33581.206956  33633.454861  33437.081539  33466.178320
    2021-02-01 04:00  33473.881292  33680.926267  33473.881292  33512.310031
    2021-02-01 05:00  33496.550836  33692.662741  33442.771302  33692.295982
    2021-02-01 06:00  33674.363254  33931.304243  33372.640454  33372.640454
    2021-02-01 07:00  33466.277250  33718.499099  33410.173005  33543.580113
    2021-02-01 08:00  33691.622420  34155.668712  33577.491467  34155.668712
    2021-02-01 09:00  34541.989582  34541.989582  34246.591606  34255.313422
    2021-02-01 10:00  34254.311054  34318.042807  34009.930110  34043.516502
    2021-02-01 11:00  34102.209535  34181.236131  33960.033182  34181.236131
    2021-02-01 12:00  34196.688991  34196.688991  33677.872302  33677.872302
    2021-02-01 13:00  33658.073761  33831.043558  33616.263612  33616.263612
    2021-02-01 14:00  33506.144521  33698.288934  33298.355558  33458.379708
    2021-02-01 15:00  33449.472954  33540.119075  33137.424801  33137.424801
    2021-02-01 16:00  33129.661923  33444.487621  33129.661923  33444.487621
    2021-02-01 17:00  33427.510914  33966.758995  33328.452664  33966.758995
    2021-02-01 18:00  33883.647812  33883.647812  33692.664177  33753.657270
    2021-02-01 19:00  33568.840250  33904.003721  33568.840250  33817.161851
    2021-02-01 20:00  33716.826906  33857.417730  33663.339958  33757.230256
    2021-02-01 21:00  33608.964863  33692.950108  33508.713475  33553.086495
    2021-02-01 22:00  33475.494546  33629.985564  33423.225607  33423.225607
    2021-02-01 23:00  33396.069559  33614.188165  33361.882597  33405.990357
    2021-02-02 00:00  33423.242711  33912.467287  33423.242711  33626.927090
    2021-02-02 01:00  33578.032588  33578.032588  33470.631802  33470.631802"""
    
    rows = [line.split('  ') for line in data.split('\n')[2:]]
    df = pd.DataFrame(rows, columns=['hr', 'open', 'high', 'low', 'close']) # .set_index('hr')
    df[['open', 'high', 'low', 'close']] = df[['open', 'high', 'low', 'close']].apply(pd.to_numeric)
    print(ma(df, lookback=3, what='close', where='close-3-day-avg'))
    

    Result (note the result is in the new column):

                      hr          open          high           low         close  close-3-day-avg
    0   2021-02-01 01:00  32835.839519  33438.836060  32835.839519  33389.974454     33389.974454
    1   2021-02-01 02:00  33400.422170  33663.000232  33260.343246  33569.816646     33479.895550
    2   2021-02-01 03:00  33581.206956  33633.454861  33437.081539  33466.178320     33475.323140
    3   2021-02-01 04:00  33473.881292  33680.926267  33473.881292  33512.310031     33516.101666
    4   2021-02-01 05:00  33496.550836  33692.662741  33442.771302  33692.295982     33556.928111
    5   2021-02-01 06:00  33674.363254  33931.304243  33372.640454  33372.640454     33525.748822
    6   2021-02-01 07:00  33466.277250  33718.499099  33410.173005  33543.580113     33536.172183
    7   2021-02-01 08:00  33691.622420  34155.668712  33577.491467  34155.668712     33690.629760
    8   2021-02-01 09:00  34541.989582  34541.989582  34246.591606  34255.313422     33984.854082
    9   2021-02-01 10:00  34254.311054  34318.042807  34009.930110  34043.516502     34151.499545
    10  2021-02-01 11:00  34102.209535  34181.236131  33960.033182  34181.236131     34160.022018
    11  2021-02-01 12:00  34196.688991  34196.688991  33677.872302  33677.872302     33967.541645
    12  2021-02-01 13:00  33658.073761  33831.043558  33616.263612  33616.263612     33825.124015
    13  2021-02-01 14:00  33506.144521  33698.288934  33298.355558  33458.379708     33584.171874
    14  2021-02-01 15:00  33449.472954  33540.119075  33137.424801  33137.424801     33404.022707
    15  2021-02-01 16:00  33129.661923  33444.487621  33129.661923  33444.487621     33346.764043
    16  2021-02-01 17:00  33427.510914  33966.758995  33328.452664  33966.758995     33516.223806
    17  2021-02-01 18:00  33883.647812  33883.647812  33692.664177  33753.657270     33721.634629
    18  2021-02-01 19:00  33568.840250  33904.003721  33568.840250  33817.161851     33845.859372
    19  2021-02-01 20:00  33716.826906  33857.417730  33663.339958  33757.230256     33776.016459
    20  2021-02-01 21:00  33608.964863  33692.950108  33508.713475  33553.086495     33709.159534
    21  2021-02-01 22:00  33475.494546  33629.985564  33423.225607  33423.225607     33577.847453
    22  2021-02-01 23:00  33396.069559  33614.188165  33361.882597  33405.990357     33460.767486
    23  2021-02-02 00:00  33423.242711  33912.467287  33423.242711  33626.927090     33485.381018
    24  2021-02-02 01:00  33578.032588  33578.032588  33470.631802  33470.631802     33501.183083