pythonpandasdatetimealgorithmic-tradingstockquotes

Partail for-loop in pandas dataframe using datetime


I have 1-min ticker information of Apple in a dataframe as so:

             Local time   Open    High     Low   Close  Volume
0   2018-04-19 15:00:00 46.707  46.708  46.687  46.687  0.0150
1   2018-04-19 15:01:00 46.688  46.688  46.667  46.688  0.0320
2   2018-04-19 15:02:00 46.687  46.728  46.677  46.728  0.0091
3   2018-04-19 15:03:00 46.727  46.728  46.708  46.717  0.0332
4   2018-04-19 15:04:00 46.708  46.718  46.677  46.677  0.0243

I have converted the "Local time" column into datetime using pd.to_datetime(df['Local time']). I want to go through each day individually to backtest a strategy. But I do not know how to loop through chunks of the df one at a time defined by a change in date. I tried using some for loops but they did not work since the number of minutes traded is apparently different on some days (not 390):

index = 390 #Number of traded minutes on most days
rows = 286155 #number of rows in the dataset
for x in range(286155/390):
    index = index * x
    index2 = index * (x-1)
    for y in df[index2:index]:
        '''Strategy to be Executed for that day'''

How can I achieve what I want to do?


Solution

  • As suggested by @Ben.T:

    for dt, df in data.groupby(data["Local time"].dt.date):
        print(f"\n[{dt}]")
        print(df.head())
        # do stuff here
    
    [2021-04-16]
               Local time  Value
    0 2021-04-16 00:00:00  28.15
    1 2021-04-16 00:01:00  25.33
    2 2021-04-16 00:02:00  82.04
    3 2021-04-16 00:03:00  17.81
    4 2021-04-16 00:04:00  80.71
    
    [2021-04-17]
                  Local time  Value
    1440 2021-04-17 00:00:00  67.72
    1441 2021-04-17 00:01:00  52.91
    1442 2021-04-17 00:02:00  26.40
    1443 2021-04-17 00:03:00  69.11
    1444 2021-04-17 00:04:00  91.88
    
    [2021-04-18]
                  Local time  Value
    2880 2021-04-18 00:00:00  13.03
    2881 2021-04-18 00:01:00  53.42
    2882 2021-04-18 00:02:00   9.28
    2883 2021-04-18 00:03:00  77.74
    2884 2021-04-18 00:04:00  24.91