Skip to content

Instantly share code, notes, and snippets.

@filipecosta90
Created February 9, 2020 01:28
Show Gist options
  • Save filipecosta90/67ba82b324a01054ba7faccfbc7be354 to your computer and use it in GitHub Desktop.
Save filipecosta90/67ba82b324a01054ba7faccfbc7be354 to your computer and use it in GitHub Desktop.
Stock Charts - Ingesting NYSE Stock Excange real-time data -- https://groups.google.com/d/msg/redistimeseries/ZhfKKqk66FY/YybDqf72AwAJ
#!/bin/bash
######################
# time-series creation
######################
# https://oss.redislabs.com/redistimeseries/commands/#tscreate
redis-cli ts.create nyse_AAPL_price RETENTION 120000 LABELS metric price name APPL
redis-cli ts.create nyse_GOOGL_price RETENTION 120000 LABELS metric price name GOOGL
redis-cli ts.create nyse_MSFT_price RETENTION 120000 LABELS metric price name MSFT
redis-cli ts.create nyse_APPL_size RETENTION 120000 LABELS metric size name APPL
redis-cli ts.create nyse_GOOGL_size RETENTION 120000 LABELS metric size name GOOGL
redis-cli ts.create nyse_MSFT_size RETENTION 120000 LABELS metric size name MSFT
#####################
# data ingestion
#####################
# https://oss.redislabs.com/redistimeseries/commands/#tsadd
# (You can also use TS.MADD to ingest multiple samples: https://oss.redislabs.com/redistimeseries/commands/#tsmadd )
# [1580503106159, "AAPL", 234.56, 1357]
redis-cli ts.add nyse_APPL_price 1580503106159 234.56
redis-cli ts.add nyse_APPL_size 1580503106159 1357
# [1580503106304, "AAPL", 232.28, 5378]
redis-cli ts.add nyse_APPL_price 1580503106304 232.28
redis-cli ts.add nyse_APPL_size 1580503106304 5378
# [1580503106235, "GOOGL", 899.56, 432]
redis-cli ts.add nyse_GOOGL_price 1580503106235 899.56
redis-cli ts.add nyse_GOOGL_size 1580503106235 432
# [1580503106256, "MSFT", 625.56, 44]
redis-cli ts.add nyse_MSFT_price 1580503106256 625.56
redis-cli ts.add nyse_MSFT_size 1580503106256 44
# [1580503106304, "AAPL", 234.78, 67888]
# Assuming this was a typo for 1580503106305, thus: [1580503106305, "AAPL", 234.78, 67888]
redis-cli ts.add nyse_APPL_price 1580503106305 234.78
redis-cli ts.add nyse_APPL_size 1580503106305 67888
#####################
# querying
#####################
# https://oss.redislabs.com/redistimeseries/commands/#tsrange
# To answer the example:
# time = minute of the time bucket ( timerange is 60 * 1000ms )
# Open = first(Price)
# Open = 234.56
echo ""
echo "Open = first(Price)"
echo "expecting Open = 234.56‬"
redis-cli ts.range nyse_APPL_price - + AGGREGATION first 60000
# time = minute of the time bucket ( timerange is 60 * 1000ms )
# High= max(Price)
# High= 234.78
echo ""
echo "High= max(Price)"
echo "expecting High= 234.7‬8"
redis-cli ts.range nyse_APPL_price - + AGGREGATION max 60000
# time = minute of the time bucket ( timerange is 60 * 1000ms )
# Low= min(Price)
# Low= 232.28
echo ""
echo "Low= min(Price)"
echo "expecting Low= 232.28"
redis-cli ts.range nyse_APPL_price - + AGGREGATION min 60000
# time = minute of the time bucket ( timerange is 60 * 1000ms )
# Close= last(Price)
# Close= 234.78
echo ""
echo "Close= last(Price)"
echo "Close= 234.78"
redis-cli ts.range nyse_APPL_price - + AGGREGATION last 60000
# time = minute of the time bucket ( timerange is 60 * 1000ms )
# Volume= sum(Size)
# Volume= ‭74623‬
echo ""
echo "Volume= sum(Size)"
echo "expecting Volume= ‭74623‬"
redis-cli ts.range nyse_APPL_size - + AGGREGATION sum 60000
###################################################################
# DOWNSAMPLING RULES
###################################################################
######################
# DOWNSAMPLING time-series creation
######################
redis-cli ts.create ds_nyse_AAPL_price_first_60s LABELS metric price name APPL downsample_time 60000 rule first
redis-cli ts.create ds_nyse_AAPL_price_max_60s LABELS metric price name APPL downsample_time 60000 rule max
redis-cli ts.create ds_nyse_AAPL_price_min_60s LABELS metric price name APPL downsample_time 60000 rule min
redis-cli ts.create ds_nyse_AAPL_price_last_60s LABELS metric price name APPL downsample_time 60000 rule last
redis-cli ts.create ds_nyse_AAPL_size_sum_60s LABELS metric size name APPL downsample_time 60000 rule sum
######################
# DOWNSAMPLING rules creation
######################
# https://oss.redislabs.com/redistimeseries/commands/#aggregation-compaction-downsampling
# TS.CREATERULE sourceKey destKey AGGREGATION aggregationType timeBucket
redis-cli ts.createrule nyse_APPL_price ds_nyse_AAPL_price_first_60s AGGREGATION first 60000
redis-cli ts.createrule nyse_APPL_price ds_nyse_AAPL_price_max_60s AGGREGATION max 60000
redis-cli ts.createrule nyse_APPL_price ds_nyse_AAPL_price_min_60s AGGREGATION min 60000
redis-cli ts.createrule nyse_APPL_price ds_nyse_AAPL_price_last_60s AGGREGATION last 60000
redis-cli ts.createrule nyse_APPL_size ds_nyse_AAPL_size_sum_60s AGGREGATION sum 60000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment