简体   繁体   中英

How to convert real world 5-day daily stock data to weekly pattern to get weekly moving average

I have Indian Stock Exchange Data and a code to give simple moving average by using .rolling_mean but I want to do the same with weekly data. I tried many approaches given over internet and blogs but nothing seems to work with my usecase.

I am using Python 3.7 and Pandas 1.3.0 .

Here are the codes which I have tried:

df1 = df.copy()
df1.set_index('DATE',drop=True,inplace=True)
logic = {'OPEN'  : 'first',
         'HIGH'  : 'max',
         'LOW'   : 'min',
         'CLOSE' : 'last',}

offset = pd.offsets.timedelta(days=-6)
f = pd.read_clipboard(parse_dates=['DATE'], index_col=['DATE'])
f.resample('W', loffset=offset).apply(logic)

And this one:

f = df.copy()

f['DATE'] = pd.to_datetime(f['DATE'])
f.set_index('DATE', inplace=True)
f.sort_index(inplace=True)

def take_first(array_like):
    return array_like[0]

def take_last(array_like):
    return array_like[-1]

output = f.resample('W',                                 # Weekly resample
                    how={'OPEN': take_first, 
                         'HIGH': 'max',
                         'LOW': 'min',
                         'CLOSE': take_last,}, 
                    loffset=pd.offsets.timedelta(days=-6))  # to put the labels to Monday

output = output[['OPEN', 'HIGH', 'LOW', 'CLOSE']]

Both give the same error as :

AttributeError: module 'pandas.tseries.offsets' has no attribute 'timedelta'

Can use Anchored offsets to resample Weekly anchored to Mondays then can Resample.aggregate to perform operations:

logic = {'Open': 'first',
         'High': 'max',
         'Low': 'min',
         'Close': 'last'}
output = df.resample('W-MON').agg(logic)

Some Sample Data:

import yfinance as yf

msft = yf.Ticker("MSFT")
df = msft.history(start='2021-06-20', end="2021-07-20")
                  Open        High  ...  Dividends  Stock Splits
Date                                ...                         
2021-06-21  259.820007  263.519989  ...          0             0
2021-06-22  262.720001  265.790009  ...          0             0
2021-06-23  265.989990  266.829987  ...          0             0
2021-06-24  266.160004  267.850006  ...          0             0
2021-06-25  266.230011  267.250000  ...          0             0
2021-06-28  266.190002  268.899994  ...          0             0
2021-06-29  268.869995  271.649994  ...          0             0
2021-06-30  270.690002  271.359985  ...          0             0
2021-07-01  269.609985  271.839996  ...          0             0

Sample Output:

                  Open        High         Low       Close
Date                                                      
2021-06-21  259.820007  263.519989  257.920013  262.630005
2021-06-28  262.720001  268.899994  262.399994  268.720001
2021-07-05  268.869995  278.000000  267.980011  277.649994
2021-07-12  278.029999  280.690002  274.299988  277.320007
2021-07-19  277.519989  284.100006  275.000000  276.140015

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM