How to Resample Stock Data and Prepare a Candlestick Chart with pandas, Python Programming for Finance p.4

138.6K views
January 17, 2017
by
sentdex
YouTube video player
How to Resample Stock Data and Prepare a Candlestick Chart with pandas, Python Programming for Finance p.4

TL;DR

Use pandas to resample daily stock data into 10-day periods, applying OHLC to adjusted close prices and sum to trading volume. The tutorial explains why resampling differs from a 10-day moving average, how it reduces dataset size, and how to reset the date index before preparing data for Matplotlib’s candlestick_ohlc. Read on for the exact workflow and operation choices.

Transcript

what's going on everybody Welcome to part four of our python for finance tutorial what we're going to be talking about in this tutorial is resampling our data since it's a really useful operation that we can do with pandas also because of res sample we can also create a Candlestick all in one tutorial so it'll be great so what we're going to do is ... Read More

Key Insights

  • 👻 Resampling data allows for easier analysis by adjusting the frequency of data.
  • 🍹 Different operations like mean and sum can be applied during the resampling process.
  • 📈 Candlestick graphs provide valuable visual representations of price movements.
  • 📈 Resampled data can be used to create Candlestick graphs, condensing the price data.
  • ❓ Resampling can be applied to multiple columns of a dataframe simultaneously.
  • 👻 The frequency of resampling can be customized, allowing for flexibility in analyzing data.
  • 💦 Resampling is particularly useful when working with large datasets or when focusing on specific time periods.

Explore YouTube Video Summarizer or Get YouTube Transcript Extractor

Questions & Answers

Q: How do you resample stock data with pandas for a candlestick chart?

Select the adjusted close column, call resample with a 10-day window using "10D," and apply OHLC to generate open, high, low, and close values. Resample the volume column over the same 10-day periods using sum, then reset the OHLC dataframe’s index so the date becomes a column for candlestick_ohlc.

Q: What does resampling data in pandas do?

Resampling changes time-based data into a different frequency and applies an operation within each resulting period. For example, irregular doorway counts can be grouped hourly and summed to show how many people entered during each hour.

Q: How can daily stock data be resampled into 10-day periods?

Apply resample("10D") to the relevant dataframe column. In the tutorial, adjusted close prices are aggregated with OHLC, while volume is aggregated separately with sum.

Q: Why use OHLC when resampling adjusted close prices?

OHLC produces an open, high, low, and close value for every 10-day period. The tutorial creates these values from adjusted close because companies with stock splits may require price data rebuilt from the adjusted close series.

Q: How should trading volume be resampled?

Resample the volume column over 10 days and apply sum. The tutorial prefers sum because it represents the total volume across the period rather than the average 10-day volume.

Q: Is 10-day resampling the same as a 10-day moving average?

No. Resampling takes each 10-day period and calculates one aggregate value, so it significantly reduces the dataset’s size. A moving average instead uses a rolling window.

Q: What time frequencies can pandas resampling use?

The tutorial demonstrates 10-day periods but notes that resampling can also use minutes, days, weekly, biweekly, and monthly frequencies. It can therefore condense millisecond tick data into minute, daily, or 10-day data when the original detail is unnecessary.

Q: Why must the dataframe index be reset before using candlestick_ohlc?

The OHLC dataframe initially stores the date as its index, but candlestick_ohlc expects date, open, high, low, and close values together. Resetting the index in place converts the date into a column, after which it can be converted to Matplotlib’s date format.

Summary & Key Takeaways

  • Resampling is a useful operation in pandas that allows you to change the frequency of data, such as converting minute data into hourly or daily data.

  • Resampling can be done to either increase or decrease the frequency of data, and various operations like mean, sum, or open-high-low-close (OHLC) can be applied during the resampling process.

  • The tutorial provides an example of resampling stock price and volume data into 10-day periods and demonstrates how to create a Candlestick graph using the resampled data.


Read in Other Languages (beta)

Share This Summary 📚

Explore More Summaries from sentdex 📚