I am currently trying to do a simple implementation for stock price candle sticks. Let’s say we have a stock called XYZ. This stock receives a stream of prices (in no particular frequency), which (for example) looks like: XYZ: [10.2, 10.7, 12, 11 ….].
The objective is to record some metrics for every minute that passes to reflect the state of that stock. A candle stick has metrics like Closing price (last known price within a minute), High Price (maximum price within a minute)…etc.
One way I thought I can implement this is by using Redis TimeSeries. I considered this solution because I can create rules on the stream of prices, and every 60 seconds it would flush some aggregations (like: max, min, first..etc.) to a destination bucket.
My current implementation using Redis TimeSeries (in Python) for candle sticks for each stock price looks something like this (using stock XYZ as example again) and no labels for simplicity:
from redistimeseries.client import Client
r = Client()
r.flushdb()
# Create source & destination buckets
r.create('XYZ_PRICES') # source
r.create(closing_price)
r.create(highest_price)
# Create rules to send data from src -> closing_price & highest_price buckets
r.createrule(src, 'closing_price', 'last', bucket_size_msec=60000)
r.createrule(src, 'highest_price', 'max', bucket_size_msec=60000)
My questions are:
- Is there a way to send more than one aggregation (like max, last…etc.) in one rule, instead of creating multiple source and destination buckets for each stock?
- Is Redis TimeSeries an appropriate choice for this task? Or will it be easier to use a another solution (such as Redis streams for example)?
3
Answers
f4, thanks for trying RedisTimeSeries.
You can configure the module to create rules automatically acorrding to predefined rules.
It’s described in the documentation: https://oss.redislabs.com/redistimeseries/configuration/#compaction_policy-policy.
I Hope that will solve your issue.
I have created a package called RedisTimeseriesManager which is able to create all downsampling rules automatically.
For a full-featured implementation see https://github.com/ahmadazizi/redis_timeseries_manager/blob/main/examples/market_data.ipynb