How to deal with minute frequency data

Hello everyone, I'm looking to gain more resolution in the plotting of my charts from pricing data obtained by the function get_pricing. Plotting with pyplot causes weird breaks, making the plot uninterpretable. Based on older posts, a workaround was found to define x_axis = range(0,len(price)). This works for general visualization purposes but I would like to retain timestamps.

Is there a straightforward way to execute this? I am new to Python2.7 but have general Matlab experience. I'm looking for something that can explain the code rather than providing me with a specific answer to my question.

1
Notebook previews are currently unavailable.
2 responses

The original code

SPY_price = SPY['price']
plt.plot(range(0,len(SPY_price.index)), SPY_price.values);



simply starts with a series of prices for SPY. The index values are the minute timestamps. The data values are the prices. We want the minutes to be on the x axis and the price values to be on the y axis. However, pyplot is sometimes too smart. It knows that the x axis is a time so it tries to make all the minutes equal-distant. This is what causes the big gaps during non-trading times. The work around is to simply number the x axis with integers (lose the times). That's what this code does

range(0,len(SPY_price.index)



That simply makes a list of integers from 0 to the length of the data we have. To plot we simply give the plot method a list of x values and a list of y values. In this case the range of integers for x and the prices for y.

However, as you noticed we now have the x tick labels as simply integers. You want to put the times back in. The x tick labels can be over-written with any string values that are desired. Use the '.xticks' method to set the labels. The format is a series of positions along the x axis, followed by the corresponding desired labels.

plt.xticks(range(0,len(SPY_price.index),390), SPY_price[::390].index.strftime('%b-%d'))



That will change all the x labels to the string value of the index (ie the datetime). Moreover, with some fancy slicing, it only places a label every 390 ticks. This is simply to make the scale more readable. There are 390 minutes in a typical trading day so it puts a label every day.

See attached notebook.

1