skip to Main Content

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:

  1. 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?
  2. 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


    1. There is no option to send more than one aggregation to a downsample series since each timestamp can hold a single. You can utilize labels to query all series at once.
    2. RedisTimeSeries would be a good solution as it will downsample your data at insertion so querying it would be super fast. It also uses double-delta compression which means your data will require lesser memory than some other solutions. You can even use retention to retire the source data if all you care about is the candlesticks.
    r.create('XYZ_PRICES', retention_msecs=300000, labels={'name':'xyz', 'type:src'})
     
    r.create(opeing_price, labels={'name':'xyz', 'type:opening'})
    r.create(closing_price, labels={'name':'xyz', 'type:closing'})
    r.create(highest_price, labels={'name':'xyz', 'type:highest'})
    r.create(lowest_price, labels={'name':'xyz', 'type:lowest'})
    
    r.createrule(src, 'opening_price', 'first', bucket_size_msec=60000)
    r.createrule(src, 'closing_price', 'last', bucket_size_msec=60000)
    r.createrule(src, 'lowest_price', 'min', bucket_size_msec=60000)
    r.createrule(src, 'highest_price', 'max', bucket_size_msec=60000)
    
    Login or Signup to reply.
  1. 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.

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search