Vinay Kumar
Vinay Kumar

Reputation: 300

Resampling in Python

I recently appeared for a test in which I was asked to build a screener for a Data Sheet. The question was as follows :

Build a Screener on the data sheet attached. 
The strategy workflow is as follows:-
•   Convert 1 minute time frame data into 15 minutes
•   If low of candle is less than previous low we enter short position, with 2 exit criteria’s
    o   Exit at end of day
    o   Exit if the high of previous candle is broken

I am having trouble with resampling of data.
I am a beginner, Please help me !

Link to Github Repository

Upvotes: 2

Views: 374

Answers (1)

izbid
izbid

Reputation: 108

To resample, you need to first ensure that your dataframe has an index of type DateTimeIndex. In your own case you need to downsample(ie lower frequency), after which you need to aggregate the values across the new sampling frequency(15mins in your case). Here is a working code.

#read data as csv
df = pd.read_csv('data.csv',index_col = 'Time')

#convert df index to DataTimeIndex
df.index = pd.to_datetime(df.index)

#downsample and aggregate
df.resample('15T').sum()

result:

    Open    High    Low Close   Volume
Time                    
2020-08-22 09:15:00 67651.75    68489.75    66555.80    67449.95    20526750
2020-08-22 09:30:00 66925.60    67568.40    66227.60    66917.05    13935600
2020-08-22 09:45:00 66661.35    67223.20    66065.30    66685.30    11484225
2020-08-22 10:00:00 65943.20    66399.60    65396.70    65902.50    8253600
2020-08-22 10:15:00 66893.50    67397.70    66409.60    66904.75    8384775
2020-08-22 10:30:00 66306.30    66784.25    65789.65    66274.60    7927350
2020-08-22 10:45:00 66410.70    66873.80    65964.20    66424.20    7811550
2020-08-22 11:00:00 65391.45    65818.80    64933.00    65408.95    7302525
2020-08-22 11:15:00 62587.45    63031.15    62059.35    62522.10    6503775
2020-08-22 11:30:00 62369.40    62891.20    61854.70    62387.40    7074825
2020-08-22 11:45:00 63602.35    64068.20    63132.15    63613.05    7082175
2020-08-22 12:00:00 63347.25    63814.55    62903.80    63342.15    6986250
2020-08-22 12:15:00 62588.20    63128.45    62165.75    62655.05    7644375
2020-08-22 12:30:00 64288.35    64769.35    63759.40    64241.20    7598400
2020-08-22 12:45:00 61430.25    61916.45    60898.85    61379.00    8495775
2020-08-22 13:00:00 61137.65    61740.60    60630.45    61213.70    10142250
2020-08-22 13:15:00 61139.60    61723.20    60493.55    61092.30    9513900
2020-08-22 13:30:00 62049.05    62659.50    61437.50    62044.85    10065750
2020-08-22 13:45:00 64004.35    64515.00    63334.60    63936.95    7864125
2020-08-22 14:00:00 63347.80    63923.20    62694.10    63284.55    9224025
2020-08-22 14:15:00 61649.90    62177.70    60951.70    61551.35    8542350
2020-08-22 14:30:00 61993.75    62647.80    61423.70    62058.45    9870600
2020-08-22 14:45:00 62134.75    62697.90    61474.25    62062.55    10302600
2020-08-22 15:00:00 62679.55    63249.90    62063.75    62676.35    12184050
2020-08-22 15:15:00 62727.55    63091.80    62329.15    62717.75    11147250

Upvotes: 4

Related Questions