-
-
Notifications
You must be signed in to change notification settings - Fork 663
/
redis.py
203 lines (133 loc) · 6.32 KB
/
redis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
'''
Copyright (C) 2017-2024 Bryant Moscon - bmoscon@gmail.com
Please see the LICENSE file for the terms and conditions
associated with this software.
'''
from collections import defaultdict
from redis import asyncio as aioredis
from yapic import json
from cryptofeed.backends.backend import BackendBookCallback, BackendCallback, BackendQueue
class RedisCallback(BackendQueue):
def __init__(self, host='127.0.0.1', port=6379, socket=None, key=None, none_to='None', numeric_type=float, **kwargs):
"""
setting key lets you override the prefix on the
key used in redis. The defaults are related to the data
being stored, i.e. trade, funding, etc
"""
prefix = 'redis://'
if socket:
prefix = 'unix://'
port = None
self.redis = f"{prefix}{host}" + f":{port}" if port else ""
self.key = key if key else self.default_key
self.numeric_type = numeric_type
self.none_to = none_to
self.running = True
class RedisZSetCallback(RedisCallback):
def __init__(self, host='127.0.0.1', port=6379, socket=None, key=None, numeric_type=float, score_key='timestamp', **kwargs):
"""
score_key: str
the value at this key will be used to store the data in the ZSet in redis. The
default is timestamp. If you wish to look up the data by a different value,
use this to change it. It must be a numeric value.
"""
self.score_key = score_key
super().__init__(host=host, port=port, socket=socket, key=key, numeric_type=numeric_type, **kwargs)
async def writer(self):
conn = aioredis.from_url(self.redis)
while self.running:
async with self.read_queue() as updates:
async with conn.pipeline(transaction=False) as pipe:
for update in updates:
pipe = pipe.zadd(f"{self.key}-{update['exchange']}-{update['symbol']}", {json.dumps(update): update[self.score_key]}, nx=True)
await pipe.execute()
await conn.close()
await conn.connection_pool.disconnect()
class RedisStreamCallback(RedisCallback):
async def writer(self):
conn = aioredis.from_url(self.redis)
while self.running:
async with self.read_queue() as updates:
async with conn.pipeline(transaction=False) as pipe:
for update in updates:
if 'delta' in update:
update['delta'] = json.dumps(update['delta'])
elif 'book' in update:
update['book'] = json.dumps(update['book'])
elif 'closed' in update:
update['closed'] = str(update['closed'])
pipe = pipe.xadd(f"{self.key}-{update['exchange']}-{update['symbol']}", update)
await pipe.execute()
await conn.close()
await conn.connection_pool.disconnect()
class RedisKeyCallback(RedisCallback):
async def writer(self):
conn = aioredis.from_url(self.redis)
while self.running:
async with self.read_queue() as updates:
update = list(updates)[-1]
if update:
await conn.set(f"{self.key}-{update['exchange']}-{update['symbol']}", json.dumps(update))
await conn.close()
await conn.connection_pool.disconnect()
class TradeRedis(RedisZSetCallback, BackendCallback):
default_key = 'trades'
class TradeStream(RedisStreamCallback, BackendCallback):
default_key = 'trades'
class FundingRedis(RedisZSetCallback, BackendCallback):
default_key = 'funding'
class FundingStream(RedisStreamCallback, BackendCallback):
default_key = 'funding'
class BookRedis(RedisZSetCallback, BackendBookCallback):
default_key = 'book'
def __init__(self, *args, snapshots_only=False, snapshot_interval=1000, score_key='receipt_timestamp', **kwargs):
self.snapshots_only = snapshots_only
self.snapshot_interval = snapshot_interval
self.snapshot_count = defaultdict(int)
super().__init__(*args, score_key=score_key, **kwargs)
class BookStream(RedisStreamCallback, BackendBookCallback):
default_key = 'book'
def __init__(self, *args, snapshots_only=False, snapshot_interval=1000, **kwargs):
self.snapshots_only = snapshots_only
self.snapshot_interval = snapshot_interval
self.snapshot_count = defaultdict(int)
super().__init__(*args, **kwargs)
class BookSnapshotRedisKey(RedisKeyCallback, BackendBookCallback):
default_key = 'book'
def __init__(self, *args, snapshot_interval=1000, score_key='receipt_timestamp', **kwargs):
kwargs['snapshots_only'] = True
self.snapshot_interval = snapshot_interval
self.snapshot_count = defaultdict(int)
super().__init__(*args, score_key=score_key, **kwargs)
class TickerRedis(RedisZSetCallback, BackendCallback):
default_key = 'ticker'
class TickerStream(RedisStreamCallback, BackendCallback):
default_key = 'ticker'
class OpenInterestRedis(RedisZSetCallback, BackendCallback):
default_key = 'open_interest'
class OpenInterestStream(RedisStreamCallback, BackendCallback):
default_key = 'open_interest'
class LiquidationsRedis(RedisZSetCallback, BackendCallback):
default_key = 'liquidations'
class LiquidationsStream(RedisStreamCallback, BackendCallback):
default_key = 'liquidations'
class CandlesRedis(RedisZSetCallback, BackendCallback):
default_key = 'candles'
class CandlesStream(RedisStreamCallback, BackendCallback):
default_key = 'candles'
class OrderInfoRedis(RedisZSetCallback, BackendCallback):
default_key = 'order_info'
class OrderInfoStream(RedisStreamCallback, BackendCallback):
default_key = 'order_info'
class TransactionsRedis(RedisZSetCallback, BackendCallback):
default_key = 'transactions'
class TransactionsStream(RedisStreamCallback, BackendCallback):
default_key = 'transactions'
class BalancesRedis(RedisZSetCallback, BackendCallback):
default_key = 'balances'
class BalancesStream(RedisStreamCallback, BackendCallback):
default_key = 'balances'
class FillsRedis(RedisZSetCallback, BackendCallback):
default_key = 'fills'
class FillsStream(RedisStreamCallback, BackendCallback):
default_key = 'fills'