2
$\begingroup$

I have a dataset as below and I want to see 3 days high low average, back from for each day in the same plot.

    index   closed  high    low     open
tarih                   
2018-05-18  1364    0.00000442  0.00000445  0.00000405  0.00000416
2018-05-19  1365    0.00000458  0.00000465  0.00000427  0.00000442
2018-05-20  1366    0.00000482  0.00000489  0.00000448  0.00000455
2018-05-21  1367    0.00000489  0.00000492  0.00000463  0.00000482
2018-05-22  1368    0.00000475  0.00000492  0.00000465  0.00000487
2018-05-23  1369    0.00000461  0.00000486  0.00000450  0.00000475
2018-05-24  1370    0.00000462  0.00000480  0.00000455  0.00000464

for example for 05-24 it will calculate 21,22,23 days high low average and point it to 05-24.

$\endgroup$
3
  • $\begingroup$ What you're looking for is called a rolling mean. With a bit of work it is easy to find. $\endgroup$ – Boondoggle May 24 '18 at 11:49
  • $\begingroup$ If you are using Python, the Pandas library has in-built methods for the rolling mean computation. You have to create a dataFrame with your data $\endgroup$ – ignatius Apr 3 '19 at 6:34
  • $\begingroup$ Take a look here stackoverflow.com/questions/43437657/… $\endgroup$ – bkshi Apr 3 '19 at 6:48
1
$\begingroup$

You can use the pandas rolling method for that.

back = 3
cols = ['high','low']

# calculate rolling mean on the dataframe's columns of interest
df_back_average = df[cols].rolling(back).mean()

# rename columns before concatenating the result back
df_back_average.columns = str(back) + '_back_average_' + df_back_average.columns

pd.concat([df[cols],df_back_average], axis=1)


high        low         3_back_average_high     3_back_average_low
0.000442    0.000445    NaN                     NaN
0.004580    0.004650    NaN                     NaN
0.004820    0.004890    0.003281                0.003328
0.004890    0.004920    0.004763                0.004820
0.004750    0.004920    0.004820                0.004910
0.004610    0.004860    0.004750                0.004900
0.004620    0.004800    0.004660                0.004860
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.