Aggregate and condense time-series values using Django Querysets?

Viewed 101

I'm currently trying to create a small crypto tracker for fun using CoinMarketCap's API. The API can be called every 5 minutes – each time returning the current exchange value, however I want to plot my graph in 30-minute intervals.

How can I condense my exchange value information of 5-minute increments down to 30-minute data points featuring the respective "Open, High, Low, Close" information? (aka first exchange value, max exchange value, min exchange value, last exchange value)


Example data:

2021-10-23 10:00:00 – price: 50
2021-10-23 10:05:00 – price: 45
2021-10-23 10:10:00 – price: 43
2021-10-23 10:15:00 – price: 47
2021-10-23 10:20:00 – price: 51
2021-10-23 10:25:00 – price: 70

2021-10-23 10:30:00 – price: 65
2021-10-23 10:35:00 – price: 55
2021-10-23 10:40:00 – price: 47
2021-10-23 10:45:00 – price: 51
2021-10-23 10:50:00 – price: 62
2021-10-23 11:55:00 – price: 50

Desired output:

2021-10-23 10:00 – open: 50, high: 70, low: 45, close: 57
2021-10-23 10:30 – open: 65, high: 62, low: 47, close: 50

Sample model:

class Crypto(models.Model):
    ticker = models.CharField(max_length=10)
    price = models.DecimalField(max_digits=7, decimal_places=2)
    percent_change = models.DecimalField(max_digits=5, decimal_places=2)
    timestamp = models.DateTimeField(auto_add_now=True)
0 Answers
Related