Same issue: I would like to get a custom timeframe of data. Just taking the last 180 bars from 1m charts and calling it 12 bars of 15m charts does not represent how we chart the data in contemporary charting systems. Typical systems align the bars to the start of the day or start of the month or week or... actually I don't even really know. But telling me to take the previous bars and simply transposing the last 15 and calling it a 15 minute bar does not sit well.
Maybe I am confused but it seems like I should be able to enter '15m', '30m', '1h', '2h', '1w', '1M', '1y', etc. into the history function to get the data I am trying to curate.
Because of this thread and my anticipation of the need for the 15m and 1h charts, I made this algo. Of course, if this is already made or integrated, please let me know so I can use the proper implementation.
Known buggies/issues:
- Assumes start of day is at 13:30 and therefore may not account for daylight savings time. This needs further development.
- Need to evaluate each timeframe to ensure that is defined as expected: for example, the minute and hourly is based on the start of day which is 31 minutes into the hour. A 15m chart would have its first period from 9:31 to 9:45, second period from 9:46 to 10:00, etc.
- Also, I don't think 7, 31, and 365 are the proper constants for week month and year, but I will leave these as is for now.
- Only tested using a single symbol. I would not suggest trying on a series of symbols. Probably the simple solution is to just iterate over the multiple symbols. That is how I would implement the functionality.
- Probably not optimized, especially if you are trying to take multiple timeframes on the same symbol. There is likely much optimization that could be accomplished in the future and more so out of our control here.
- I am kinda burned on this project, so I am probably missing stuff, take it as it is, let me/us know how to make it better.
Check the source of Historic Custom Timeframe and take all the code from def historic_custom_timeframe and below. You will also need the first line of code to import pandas as pd
You will be able to get your data as exemplified in the top portion of the code like this:
hourly_chart_100_bars = historic_custom_timeframe(data,sid(24),'1h',100)
fifteenMinute_chart_30_bars = historic_custom_timeframe(data,sid(24),'15m',30)