Building Python Functions to Combine Lower-Timeframe Candles
Summary
This tutorial translates a JavaScript candle aggregation routine into Python for creating trading intervals that an exchange or data provider does not supply. It infers the source interval from the final two records, checks that the target interval is an integral multiple that fits within an hour or day, and groups source bars at time-aligned boundaries. For each group, it carries forward the first open and timestamp, tracks the highest high and lowest low, uses the last close, and sums volume. The sample applies the function to generate four-hour bars from exchange data.
The evidence is an illustrative chart comparison and example code, not a systematic validation of accuracy or strategy performance. The article cautions that the implementation is for learning and may need adjustment and testing for specific uses. Its time-boundary calculation relies on a timezone offset, so users should verify its behavior for their data timestamps, timezone conventions, and incomplete final groups before relying on the output.
Key ideas
- The function infers the source candle interval from the timestamps of the last two records.
- The requested target interval must be an integer multiple of the source interval and fit an hourly or daily cycle.
- Aggregated candles retain the first open and time, extreme high and low, final close, and summed volume.
- The example demonstrates four-hour candles, but the code should be checked against the data's timestamp and timezone conventions.
Tags
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.