-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEVENT_BAR_HANDLE_REALTIME_DATA_02.py
More file actions
143 lines (130 loc) · 4.5 KB
/
EVENT_BAR_HANDLE_REALTIME_DATA_02.py
File metadata and controls
143 lines (130 loc) · 4.5 KB
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
import logging
import datetime
import json
from redis import Redis
from redisTimeseriesData import RealTimeBars
from redisPubsub import RedisSubscriber, RedisPublisher
from pubsubKeys import PUBSUB_KEYS
from redisUtil import RedisTimeFrame
class EventBarHandleRealtimeData:
'''A 1-Minute Bar happened. Save the data. And get 2 min and 5 min data for analysis later.'''
def __init__(self, pubDataCheck=None, pubDataSave=None):
self.rtb = RealTimeBars()
self.publisher = RedisPublisher(
PUBSUB_KEYS.EVENT_BAR_FILTER_VSA) if pubDataCheck is None else pubDataCheck
self.subscriber = RedisSubscriber(
PUBSUB_KEYS.EVENT_BAR_CANDIDATE, None, self.AddBar)
def makePubData(self, symbol: str, timeframe: str, data: list):
return {
"symbol": symbol,
"period": timeframe,
"data": data
}
def isTimeInterval(self, timeframe:str) -> bool:
timeIntervals = {
# RedisTimeFrame.MIN2: 2,
# RedisTimeFrame.MIN15: 15
RedisTimeFrame.MIN5: 5
}
min = timeIntervals.get(timeframe, -1)
if min <= 0:
return False
moment = datetime.datetime.now()
# for the minute 5 data, you have to be at minute 6.
# so you need to compensate for that.
if moment.minute % min == 1:
return True
return False
def publish2Min(self, symbol: str, timeframe: str):
data2 = self.rtb.RedisGetRealtimeData(None, symbol, timeframe)
if isinstance(data2, dict):
arrLen = len(data2['data']) if 'data' in data2.keys() else 0
if data2 is not None and arrLen >= 3:
self.publisher.publish(data2)
else:
logging.info(
f"EventBarHandleRealtimeData.publish2Min: Not Enough {symbol} {timeframe} {arrLen}")
def AddBar(self, data=None):
try:
symbol: str = ''
if data is None:
symbol = 'FANG'
else:
symbol = data['S']
self.rtb.RedisAddBar(data)
self.rtb.RedisAddBarAggregation(data)
if self.isTimeInterval(RedisTimeFrame.MIN2):
self.publish2Min(symbol, RedisTimeFrame.MIN2)
if self.isTimeInterval(RedisTimeFrame.MIN5):
self.publish2Min(symbol, RedisTimeFrame.MIN5)
if self.isTimeInterval(RedisTimeFrame.MIN15):
self.publish2Min(symbol, RedisTimeFrame.MIN15)
except Exception as e:
logging.error(
f"Error EVENT_BAR_CANDIDATE.EventBarHandleRealtimeData.AddBar {e} {data} ")
def start(self):
try:
self.subscriber.start()
except KeyboardInterrupt:
self.subscriber.stop()
except Exception as e:
logging.error(e)
@staticmethod
def run():
logging.info("EVENT_BAR_HANDLE_REALTIME_DATA.EventBarHandleRealtimeData.run")
eventBarCandidate = EventBarHandleRealtimeData()
eventBarCandidate.start()
if __name__ == "__main__":
logging.info("EVENT_BAR_HANDLE_REALTIME_DATA.EventBarHandleRealtimeData.run")
ebc = EventBarHandleRealtimeData()
#data2 = ebc.rtb.RedisGetRealtimeData(None, 'MSFT', RedisTimeFrame.MIN2)
ebc.AddBar()
# ebc.run()
# input: { 'c': 136.02, 'h': 136.06, 'l': 136.0, 'o': 136.04, 'S': 'ALLE', 't': 1627493640000000000, 'v': 712})
# output:
# {
# "symbol": "ALLE",
# "period": "1MIN",
# "data": [
# {
# "timestamp": 000010,
# "close": 136.02,
# "high": 136.06,
# "low": 136.0,
# "open": 136.04,
# "volume": 712
# },
# {
# "timestamp": 000009,
# "close": 136.02,
# "high": 136.06,
# "low": 136.0,
# "open": 136.04,
# "volume": 712
# },
# {
# "timestamp": 000008,
# "close": 136.02,
# "high": 136.06,
# "low": 136.0,
# "open": 136.04,
# "volume": 712
# },
# {
# "timestamp": 000007,
# "close": 136.02,
# "high": 136.06,
# "low": 136.0,
# "open": 136.04,
# "volume": 712
# },
# {
# "timestamp": 000006,
# "close": 136.02,
# "high": 136.06,
# "low": 136.0,
# "open": 136.04,
# "volume": 712
# }
# ]
# }