Home > Mobile >  Python OHLC convert weekly to biweekly
Python OHLC convert weekly to biweekly

Time:12-18

I have weekly data from Yahoo Finance

                  Open        High         Low       Close   Adj Close        Volume
Date                                                                                
1999-12-27    0.901228    0.918527    0.888393    0.917969    0.782493  1.638112e 08
2000-01-03    0.936384    1.004464    0.848214    0.888393    0.757282  3.055203e 09
2000-01-10    0.910714    0.912946    0.772321    0.896763    0.764417  3.345742e 09
2000-01-17    0.901786    1.084821    0.896763    0.993862    0.847186  3.383878e 09
2000-01-24    0.968192    1.019531    0.898438    0.907366    0.773455  2.068674e 09
2000-01-31    0.901786    0.982143    0.843750    0.964286    0.821975  2.384424e 09
2000-02-07    0.964286    1.045759    0.945871    0.970982    0.827682  1.664309e 09
2000-02-14    0.976004    1.070871    0.969866    0.993304    0.846710  1.754469e 09
2000-02-21    0.983259    1.063616    0.952567    0.985491    0.840050  1.520971e 09
2000-02-28    0.983259    1.179129    0.967634    1.142857    0.974192  2.408918e 09
2000-03-06    1.125000    1.152902    1.055804    1.122768    0.957068  1.280126e 09
2000-03-13    1.090402    1.129464    1.017857    1.116071    0.951359  1.859290e 09
2000-03-20    1.102679    1.342634    1.085938    1.238281    1.055533  2.306293e 09
2000-03-27    1.228795    1.292411    1.119978    1.212612    1.033652  1.541019e 09
2000-04-03    1.209821    1.245536    1.042411    1.176339    1.002732  1.948621e 09
2000-04-10    1.175781    1.185268    0.936384    0.998884    0.851467  2.892669e 09
2000-04-17    0.977679    1.162946    0.973772    1.061384    0.904743  2.042757e 09
2000-04-24    1.026786    1.149554    1.024554    1.107701    0.944224  1.778358e 09
2000-05-01    1.114955    1.127232    0.987165    1.010045    0.860980  1.636018e 09

We need to resample it to biweekly, i.e. once in 2 weeks.

yahoo finance only provided the data for these intervals :

[1m, 2m, 5m, 15m, 30m, 60m, 90m, 1h, 1d, 5d, 1wk, 1mo, 3mo]

need something between 1 week and 1 Month i.e. 2 weeks.

Please let me know how to resample

CodePudding user response:

I would just resample to 2-week increments and calculate each value. The opening price is the first, the high is the maximum, the low is the minimum, the close is the last, the adjusted close is also the last, and the volume is the total. See https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.resample.html

df['Date'] = pd.to_datetime(df['Date']) 
df.set_index('Date', inplace=True)

d_ohlcv = {'Open': 'first',
           'High': 'max',
           'Low': 'min',
           'Close': 'last',
           'Adj Close': 'last',
           'Volume': 'sum'}

df.resample('2W').agg(d_ohlcv).head(8)
    Open    High    Low     Close   Adj Close   Volume
Date                        
2000-01-02  0.901228    0.918527    0.888393    0.917969    0.782493    1.638112e 08
2000-01-16  0.936384    1.004464    0.772321    0.896763    0.764417    6.400945e 09
2000-01-30  0.901786    1.084821    0.896763    0.907366    0.773455    5.452552e 09
2000-02-13  0.901786    1.045759    0.843750    0.970982    0.827682    4.048733e 09
2000-02-27  0.976004    1.070871    0.952567    0.985491    0.840050    3.275440e 09
2000-03-12  0.983259    1.179129    0.967634    1.122768    0.957068    3.689044e 09
2000-03-26  1.090402    1.342634    1.017857    1.238281    1.055533    4.165583e 09
2000-04-09  1.228795    1.292411    1.042411    1.176339    1.002732    3.489640e 09
  • Related