2

I have a multi-index dataframe that looks like this:

                      LAST PRICE     HIGH      LOW  PX CLOSE 1D  
date                                                                           
2010-01-04 SPX Index      1132.99  1133.87  1116.56      1115.10       
           CL1 Comdty       81.51    81.79    79.66        79.36         
2010-01-05 SPX Index      1136.52  1136.63  1129.66      1132.99       
           CL1 Comdty       81.77    82.00    80.95        81.51         
2010-01-06 SPX Index      1137.14  1139.19  1133.95      1136.52       
           CL1 Comdty       83.18    83.52    80.85        81.77       
2010-01-07 SPX Index      1141.69  1142.46  1131.32      1137.14      
           CL1 Comdty       82.66    83.36    82.26        83.18       
2010-01-08 SPX Index      1144.98  1145.39  1136.22      1141.69        
           CL1 Comdty       82.75    83.47    81.80        82.66   

I would like to add new columns with simple and exponential moving averages for 'LAST PRICE'. How do I do this so that the calculations are correct per underlying? I believe I need to use groupby but I can't get the syntax correct.

thanks in advance

1 Answer 1

2

please refer to ewm documentation for more information

Use unstack

df['PX CLOSE 1D'].unstack().ewm(halflife=1).mean()

            CL1 Comdty    SPX Index
date                               
2010-01-04   79.360000  1115.100000
2010-01-05   80.793333  1127.026667
2010-01-06   81.351429  1132.451429
2010-01-07   82.326667  1134.952000
2010-01-08   82.498710  1138.429677
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.