Notebook
In [ ]:
# Define a stock we want to look at along with some dates
stock = 'AAPL'

start_date = '1-1-2020'
end_date = '1-20-2020'
In [2]:
# get minute data
minute_data = get_pricing(stock, start_date, end_date, frequency='minute')
minute_data.head(5)
Out[2]:
open_price high low close_price volume price
2020-01-02 14:31:00+00:00 296.24 296.300 295.390 295.755 790025.0 295.755
2020-01-02 14:32:00+00:00 295.70 295.910 295.304 295.530 201047.0 295.530
2020-01-02 14:33:00+00:00 295.49 295.810 295.190 295.705 172261.0 295.705
2020-01-02 14:34:00+00:00 295.71 296.800 295.700 296.550 295317.0 296.550
2020-01-02 14:35:00+00:00 296.53 296.926 296.120 296.870 195182.0 296.870
In [3]:
# get daily data
daily_data = get_pricing(stock, start_date, end_date, frequency='daily')
daily_data.head(5)
Out[3]:
open_price high low close_price volume price
2020-01-02 00:00:00+00:00 296.24 300.60 295.190 300.59 26147082.0 300.59
2020-01-03 00:00:00+00:00 297.15 300.58 296.500 297.38 27192176.0 297.38
2020-01-06 00:00:00+00:00 293.79 299.96 292.750 299.78 22962018.0 299.78
2020-01-07 00:00:00+00:00 299.84 300.90 297.480 298.27 19748523.0 298.27
2020-01-08 00:00:00+00:00 297.16 304.44 297.156 303.15 25214035.0 303.15
In [5]:
# Let's calculate daily data from the minute data using 'groupby'
# First add a column for day to group on
minute_data['day'] = minute_data.index.get_level_values(level=0).date
minute_data.head(5)
Out[5]:
open_price high low close_price volume price day
2020-01-02 14:31:00+00:00 296.24 296.300 295.390 295.755 790025.0 295.755 2020-01-02
2020-01-02 14:32:00+00:00 295.70 295.910 295.304 295.530 201047.0 295.530 2020-01-02
2020-01-02 14:33:00+00:00 295.49 295.810 295.190 295.705 172261.0 295.705 2020-01-02
2020-01-02 14:34:00+00:00 295.71 296.800 295.700 296.550 295317.0 296.550 2020-01-02
2020-01-02 14:35:00+00:00 296.53 296.926 296.120 296.870 195182.0 296.870 2020-01-02
In [10]:
# Now group and apply appropriate functions
calculated_daily_data = minute_data.groupby('day').aggregate({'open_price':'first',
                                                          'high':'max',
                                                          'low':'min',
                                                          'close_price':'last',
                                                          'volume':'sum',
                                                          'price':'last'})

display(calculated_daily_data.head(5))
display(daily_data.head(5))
open_price volume low price high close_price
day
2020-01-02 296.24 26147082.0 295.190 300.59 300.60 300.59
2020-01-03 297.15 27192176.0 296.500 297.38 300.58 297.38
2020-01-06 293.79 22962018.0 292.750 299.78 299.96 299.78
2020-01-07 299.84 19748523.0 297.480 298.27 300.90 298.27
2020-01-08 297.16 25214035.0 297.156 303.15 304.44 303.15
open_price high low close_price volume price
2020-01-02 00:00:00+00:00 296.24 300.60 295.190 300.59 26147082.0 300.59
2020-01-03 00:00:00+00:00 297.15 300.58 296.500 297.38 27192176.0 297.38
2020-01-06 00:00:00+00:00 293.79 299.96 292.750 299.78 22962018.0 299.78
2020-01-07 00:00:00+00:00 299.84 300.90 297.480 298.27 19748523.0 298.27
2020-01-08 00:00:00+00:00 297.16 304.44 297.156 303.15 25214035.0 303.15

Notice that the aggregated minute data is the same as the daily data.

In [ ]: