Skip to content

Instantly share code, notes, and snippets.

@hn4002
Last active June 5, 2024 20:12
Show Gist options
  • Select an option

  • Save hn4002/5cb07e894f20b5b1b8438cf1263297d4 to your computer and use it in GitHub Desktop.

Select an option

Save hn4002/5cb07e894f20b5b1b8438cf1263297d4 to your computer and use it in GitHub Desktop.
Convert OHLCV 1-min data to other intraday timeframe
import math
import pandas
# ======================================================================================================================
def convert_data_for_timeframe(price_data, target_timeframe):
"""
Convert 1-min price data to a new timeframe.
Reference Links
* https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.resample.html
* https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases
:param price_data: List of objects. Each object has the following fields: open, high, low, close, volume, datetime. Example:
{
"open": 168.99,
"high": 169.0908,
"low": 168.96,
"close": 169.05,
"volume": 108369,
"datetime": 1683821460000
},
:param target_timeframe: The new timeframe. Example: "5m", "15m", "60m"
:return:
"""
# Some basic validation
if not target_timeframe.endswith("m"):
return None
# Convert list of objects to panda DataFrame
df = pandas.DataFrame.from_records(price_data)
# Convert unix time in ms to python datetime
df["date"] = pandas.to_datetime(df['datetime'], unit='ms', origin='unix')
# Make the python datetime as index
df = df.set_index("date")
aggregation_dict = {
'open': 'first',
'high': 'max',
'low': 'min',
'close': 'last',
'volume': 'sum',
}
# get panda timeframe. panda timeframe examples: 5min, 15min, 60min
panda_time_frame = target_timeframe.replace("m", "min")
# Resample the data to the new timeframe using the aggregation functions
df2 = df.resample(panda_time_frame).agg(aggregation_dict)
# Get the list of objects from panda DataFrame
bars2 = df2.reset_index().values.tolist()
out_candles = []
for bar in bars2:
candle = {}
candle["datetime"] = int(bar[0].timestamp() * 1000)
# candle["_debug"] = bar[0].strftime('%Y-%m-%dT%H:%M:%S')
candle["open"] = bar[1]
candle["high"] = bar[2]
candle["low"] = bar[3]
candle["close"] = bar[4]
candle["volume"] = bar[5]
if math.isnan(candle["open"]) or math.isnan(candle["high"]) or math.isnan(candle["low"]) or math.isnan(candle["close"]):
# Skip it
continue
out_candles.append(candle)
return out_candles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment