intrinio-realtime 4.0.0 → 4.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/intrinio-realtime.rb +574 -571
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62489e9518d298632d43fb8504c9debffe45dd6d1b46b596e7d5cdc5cfb77627
|
4
|
+
data.tar.gz: 4d7473efe7d699734c16116e27ab1a44653d21654a1c8412efdc56075f6decc5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d0ef6392d70888aa915f432da2a0fd9eae3df8043245a4e3b12d4ed4c72d71cec34da888ad8f73767e7622d1266d04dd9c4c593d7e202446ad8389cb33686078
|
7
|
+
data.tar.gz: f33a8657e2d413bf1a195a8ff4678429ad68736f87f894312d1fd8084881a9f719bece3802acbe10a4e15d590269d7e34604f5204903b77bbd15c20d3e0f3d2b
|
data/lib/intrinio-realtime.rb
CHANGED
@@ -1,571 +1,574 @@
|
|
1
|
-
require 'logger'
|
2
|
-
require 'uri'
|
3
|
-
#require 'http'
|
4
|
-
require 'net/http'
|
5
|
-
require 'eventmachine'
|
6
|
-
require 'websocket-client-simple'
|
7
|
-
|
8
|
-
module Intrinio
|
9
|
-
module Realtime
|
10
|
-
HEARTBEAT_TIME = 3
|
11
|
-
SELF_HEAL_BACKOFFS = [0, 100, 500, 1000, 2000, 5000].freeze
|
12
|
-
REALTIME = "REALTIME".freeze
|
13
|
-
MANUAL = "MANUAL".freeze
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
client.
|
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
|
-
@channels =
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
@logger
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
@
|
149
|
-
@
|
150
|
-
@
|
151
|
-
@
|
152
|
-
@
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
@channels.
|
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
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
EM.cancel_timer(@
|
209
|
-
@
|
210
|
-
@
|
211
|
-
@
|
212
|
-
@
|
213
|
-
@
|
214
|
-
@
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
@
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
#
|
313
|
-
#
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
http
|
331
|
-
http.
|
332
|
-
|
333
|
-
request.
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
return fatal("
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
url
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
url = "#{url}
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
@
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
@
|
399
|
-
|
400
|
-
|
401
|
-
ws
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
me.send
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
me.send :
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
channels
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
return
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
1
|
+
require 'logger'
|
2
|
+
require 'uri'
|
3
|
+
#require 'http'
|
4
|
+
require 'net/http'
|
5
|
+
require 'eventmachine'
|
6
|
+
require 'websocket-client-simple'
|
7
|
+
|
8
|
+
module Intrinio
|
9
|
+
module Realtime
|
10
|
+
HEARTBEAT_TIME = 3
|
11
|
+
SELF_HEAL_BACKOFFS = [0, 100, 500, 1000, 2000, 5000].freeze
|
12
|
+
REALTIME = "REALTIME".freeze
|
13
|
+
MANUAL = "MANUAL".freeze
|
14
|
+
DELAYED_SIP = "DELAYED_SIP".freeze
|
15
|
+
PROVIDERS = [REALTIME, MANUAL, DELAYED_SIP].freeze
|
16
|
+
ASK = "Ask".freeze
|
17
|
+
BID = "Bid".freeze
|
18
|
+
|
19
|
+
def self.connect(options, on_trade, on_quote)
|
20
|
+
EM.run do
|
21
|
+
client = ::Intrinio::Realtime::Client.new(options, on_trade, on_quote)
|
22
|
+
client.connect()
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Trade
|
27
|
+
def initialize(symbol, price, size, timestamp, total_volume)
|
28
|
+
@symbol = symbol
|
29
|
+
@price = price
|
30
|
+
@size = size
|
31
|
+
@timestamp = timestamp
|
32
|
+
@total_volume = total_volume
|
33
|
+
end
|
34
|
+
|
35
|
+
def symbol
|
36
|
+
@symbol
|
37
|
+
end
|
38
|
+
|
39
|
+
def price
|
40
|
+
@price
|
41
|
+
end
|
42
|
+
|
43
|
+
def size
|
44
|
+
@size
|
45
|
+
end
|
46
|
+
|
47
|
+
def timestamp
|
48
|
+
@timestamp
|
49
|
+
end
|
50
|
+
|
51
|
+
def total_volume
|
52
|
+
@total_volume
|
53
|
+
end
|
54
|
+
|
55
|
+
def to_s
|
56
|
+
[@symbol, @price, @size, @timestamp, @total_volume].join(",")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
class Quote
|
61
|
+
def initialize(type, symbol, price, size, timestamp)
|
62
|
+
@type = type
|
63
|
+
@symbol = symbol
|
64
|
+
@price = price
|
65
|
+
@size = size
|
66
|
+
@timestamp = timestamp
|
67
|
+
end
|
68
|
+
|
69
|
+
def type
|
70
|
+
@type
|
71
|
+
end
|
72
|
+
|
73
|
+
def symbol
|
74
|
+
@symbol
|
75
|
+
end
|
76
|
+
|
77
|
+
def price
|
78
|
+
@price
|
79
|
+
end
|
80
|
+
|
81
|
+
def size
|
82
|
+
@size
|
83
|
+
end
|
84
|
+
|
85
|
+
def timestamp
|
86
|
+
@timestamp
|
87
|
+
end
|
88
|
+
|
89
|
+
def to_s
|
90
|
+
[@symbol, @type, @price, @size, @timestamp].join(",")
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
class Client
|
95
|
+
|
96
|
+
def initialize(options, on_trade, on_quote)
|
97
|
+
raise "Options parameter is required" if options.nil? || !options.is_a?(Hash)
|
98
|
+
@stop = false
|
99
|
+
@messages = Queue.new
|
100
|
+
raise "Unable to create queue." if @messages.nil?
|
101
|
+
@on_trade = on_trade
|
102
|
+
@on_quote = on_quote
|
103
|
+
|
104
|
+
@api_key = options[:api_key]
|
105
|
+
raise "API Key was formatted invalidly." if @api_key && !valid_api_key?(@api_key)
|
106
|
+
|
107
|
+
unless @api_key
|
108
|
+
@username = options[:username]
|
109
|
+
@password = options[:password]
|
110
|
+
raise "API Key or Username and password are required" if @username.nil? || @username.empty? || @password.nil? || @password.empty?
|
111
|
+
end
|
112
|
+
|
113
|
+
@provider = options[:provider]
|
114
|
+
unless @provider
|
115
|
+
@provider = REALTIME
|
116
|
+
end
|
117
|
+
raise "Provider must be 'REALTIME' or 'MANUAL'" unless PROVIDERS.include?(@provider)
|
118
|
+
|
119
|
+
@ip_address = options[:ip_address]
|
120
|
+
raise "Missing option ip_address while in MANUAL mode." if @provider == MANUAL and (@ip_address.nil? || @ip_address.empty?)
|
121
|
+
|
122
|
+
@trades_only = options[:trades_only]
|
123
|
+
if @trades_only.nil?
|
124
|
+
@trades_only = false
|
125
|
+
end
|
126
|
+
|
127
|
+
@thread_quantity = options[:threads]
|
128
|
+
unless @thread_quantity
|
129
|
+
@thread_quantity = 4
|
130
|
+
end
|
131
|
+
|
132
|
+
@threads = []
|
133
|
+
|
134
|
+
@channels = []
|
135
|
+
@channels = parse_channels(options[:channels]) if options[:channels]
|
136
|
+
bad_channels = @channels.select{|x| !x.is_a?(String)}
|
137
|
+
raise "Invalid channels to join: #{bad_channels}" unless bad_channels.empty?
|
138
|
+
|
139
|
+
if options[:logger] == false
|
140
|
+
@logger = nil
|
141
|
+
elsif !options[:logger].nil?
|
142
|
+
@logger = options[:logger]
|
143
|
+
else
|
144
|
+
@logger = Logger.new($stdout)
|
145
|
+
@logger.level = Logger::INFO
|
146
|
+
end
|
147
|
+
|
148
|
+
@ready = false
|
149
|
+
@joined_channels = []
|
150
|
+
@heartbeat_timer = nil
|
151
|
+
@selfheal_timer = nil
|
152
|
+
@selfheal_backoffs = Array.new(SELF_HEAL_BACKOFFS)
|
153
|
+
@ws = nil
|
154
|
+
end
|
155
|
+
|
156
|
+
def provider
|
157
|
+
@provider
|
158
|
+
end
|
159
|
+
|
160
|
+
def join(*channels)
|
161
|
+
channels = parse_channels(channels)
|
162
|
+
nonconforming = channels.select{|x| !x.is_a?(String)}
|
163
|
+
return error("Invalid channels to join: #{nonconforming}") unless nonconforming.empty?
|
164
|
+
|
165
|
+
@channels.concat(channels)
|
166
|
+
@channels.uniq!
|
167
|
+
debug "Joining channels #{channels}"
|
168
|
+
|
169
|
+
refresh_channels()
|
170
|
+
end
|
171
|
+
|
172
|
+
def leave(*channels)
|
173
|
+
channels = parse_channels(channels)
|
174
|
+
nonconforming = channels.find{|x| !x.is_a?(String)}
|
175
|
+
return error("Invalid channels to leave: #{nonconforming}") unless nonconforming.empty?
|
176
|
+
|
177
|
+
channels.each{|c| @channels.delete(c)}
|
178
|
+
debug "Leaving channels #{channels}"
|
179
|
+
|
180
|
+
refresh_channels()
|
181
|
+
end
|
182
|
+
|
183
|
+
def leave_all
|
184
|
+
@channels = []
|
185
|
+
debug "Leaving all channels"
|
186
|
+
refresh_channels()
|
187
|
+
end
|
188
|
+
|
189
|
+
def connect
|
190
|
+
raise "Must be run from within an EventMachine run loop" unless EM.reactor_running?
|
191
|
+
return warn("Already connected!") if @ready
|
192
|
+
debug "Connecting..."
|
193
|
+
|
194
|
+
catch :fatal do
|
195
|
+
begin
|
196
|
+
@closing = false
|
197
|
+
@ready = false
|
198
|
+
refresh_token()
|
199
|
+
refresh_websocket()
|
200
|
+
rescue StandardError => e
|
201
|
+
error("Connection error: #{e} \n#{e.backtrace.join("\n")}")
|
202
|
+
try_self_heal()
|
203
|
+
end
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
def disconnect
|
208
|
+
EM.cancel_timer(@heartbeat_timer) if @heartbeat_timer
|
209
|
+
EM.cancel_timer(@selfheal_timer) if @selfheal_timer
|
210
|
+
@ready = false
|
211
|
+
@closing = true
|
212
|
+
@channels = []
|
213
|
+
@joined_channels = []
|
214
|
+
@ws.close() if @ws
|
215
|
+
@stop = true
|
216
|
+
sleep(2)
|
217
|
+
@threads.each { |thread|
|
218
|
+
if !thread.nil? && (!thread.pending_interrupt? || thread.status == "run" || thread.status == "Sleeping")
|
219
|
+
then thread.join(7)
|
220
|
+
elsif !thread.nil?
|
221
|
+
then thread.kill
|
222
|
+
end
|
223
|
+
}
|
224
|
+
@threads = []
|
225
|
+
@stop = false
|
226
|
+
info "Connection closed"
|
227
|
+
end
|
228
|
+
|
229
|
+
def on_trade(on_trade)
|
230
|
+
@on_trade = on_trade
|
231
|
+
end
|
232
|
+
|
233
|
+
def on_quote(on_quote)
|
234
|
+
@on_quote = on_quote
|
235
|
+
end
|
236
|
+
|
237
|
+
private
|
238
|
+
|
239
|
+
def queue_message(message)
|
240
|
+
@messages.enq(message)
|
241
|
+
end
|
242
|
+
|
243
|
+
def parse_uint64(data)
|
244
|
+
data.map { |i| [sprintf('%02x',i)].pack('H2') }.join.unpack('Q<').first
|
245
|
+
end
|
246
|
+
|
247
|
+
def parse_int32(data)
|
248
|
+
data.map { |i| [sprintf('%02x',i)].pack('H2') }.join.unpack('l<').first
|
249
|
+
end
|
250
|
+
|
251
|
+
def parse_uint32(data)
|
252
|
+
data.map { |i| [sprintf('%02x',i)].pack('H2') }.join.unpack('V').first
|
253
|
+
end
|
254
|
+
|
255
|
+
def parse_float32(data)
|
256
|
+
data.map { |i| [sprintf('%02x',i)].pack('H2') }.join.unpack('e').first
|
257
|
+
end
|
258
|
+
|
259
|
+
def parse_trade(data, start_index, symbol_length)
|
260
|
+
symbol = data[start_index + 2, symbol_length].map!{|c| c.chr}.join
|
261
|
+
price = parse_float32(data[start_index + 2 + symbol_length, 4])
|
262
|
+
size = parse_uint32(data[start_index + 6 + symbol_length, 4])
|
263
|
+
timestamp = parse_uint64(data[start_index + 10 + symbol_length, 8])
|
264
|
+
total_volume = parse_uint32(data[start_index + 18 + symbol_length, 4])
|
265
|
+
return Trade.new(symbol, price, size, timestamp, total_volume)
|
266
|
+
end
|
267
|
+
|
268
|
+
def parse_quote(data, start_index, symbol_length, msg_type)
|
269
|
+
type = case when msg_type == 1 then ASK when msg_type == 2 then BID end
|
270
|
+
symbol = data[start_index + 2, symbol_length].map!{|c| c.chr}.join
|
271
|
+
price = parse_float32(data[start_index + 2 + symbol_length, 4])
|
272
|
+
size = parse_uint32(data[start_index + 6 + symbol_length, 4])
|
273
|
+
timestamp = parse_uint64(data[start_index + 10 + symbol_length, 8])
|
274
|
+
return Quote.new(type, symbol, price, size, timestamp)
|
275
|
+
end
|
276
|
+
|
277
|
+
def handle_message(data, start_index)
|
278
|
+
msg_type = data[start_index]
|
279
|
+
symbol_length = data[start_index + 1]
|
280
|
+
case msg_type
|
281
|
+
when 0 then
|
282
|
+
trade = parse_trade(data, start_index, symbol_length)
|
283
|
+
@on_trade.call(trade)
|
284
|
+
return start_index + 22 + symbol_length
|
285
|
+
when 1 || 2 then
|
286
|
+
quote = parse_quote(data, start_index, symbol_length, msg_type)
|
287
|
+
@on_quote.call(quote)
|
288
|
+
return start_index + 18 + symbol_length
|
289
|
+
end
|
290
|
+
return start_index
|
291
|
+
end
|
292
|
+
|
293
|
+
def handle_data
|
294
|
+
Thread.current.priority -= 1
|
295
|
+
me = self
|
296
|
+
pop = nil
|
297
|
+
until @stop do
|
298
|
+
begin
|
299
|
+
pop = nil
|
300
|
+
data = nil
|
301
|
+
pop = @messages.deq
|
302
|
+
unless pop.nil?
|
303
|
+
begin
|
304
|
+
data = pop.unpack('C*')
|
305
|
+
rescue StandardError => ex
|
306
|
+
me.send :error, "Error unpacking data from queue: #{ex} #{pop}"
|
307
|
+
next
|
308
|
+
end
|
309
|
+
if !data then me.send :error, "Cannot process data. Data is nil. #{pop}" end
|
310
|
+
start_index = 1
|
311
|
+
count = data[0]
|
312
|
+
# These are grouped (many) messages.
|
313
|
+
# The first byte tells us how many there are.
|
314
|
+
# From there, check the type and symbol length at index 0 of each chunk to know how many bytes each message has.
|
315
|
+
count.times {start_index = handle_message(data, start_index)}
|
316
|
+
end
|
317
|
+
if pop.nil? then sleep(0.1) end
|
318
|
+
rescue StandardError => e
|
319
|
+
me.send :error, "Error handling message from queue: #{e} #{pop} : #{data} ; count: #{count} ; start index: #{start_index}"
|
320
|
+
rescue Exception => e
|
321
|
+
#me.send :error, "General error handling message from queue: #{e} #{pop} : #{data} ; count: #{count} ; start index: #{start_index}"
|
322
|
+
end
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
def refresh_token
|
327
|
+
@token = nil
|
328
|
+
|
329
|
+
uri = URI.parse(auth_url)
|
330
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
331
|
+
http.use_ssl = true if (auth_url.include?("https"))
|
332
|
+
http.start
|
333
|
+
request = Net::HTTP::Get.new(uri.request_uri)
|
334
|
+
request.add_field("Client-Information", "IntrinioRealtimeRubySDKv4.0")
|
335
|
+
|
336
|
+
unless @api_key
|
337
|
+
request.basic_auth(@username, @password)
|
338
|
+
end
|
339
|
+
|
340
|
+
response = http.request(request)
|
341
|
+
|
342
|
+
return fatal("Unable to authorize") if response.code == "401"
|
343
|
+
return fatal("Could not get auth token") if response.code != "200"
|
344
|
+
|
345
|
+
@token = response.body
|
346
|
+
debug "Token refreshed"
|
347
|
+
end
|
348
|
+
|
349
|
+
def auth_url
|
350
|
+
url = ""
|
351
|
+
|
352
|
+
case @provider
|
353
|
+
when REALTIME then url = "https://realtime-mx.intrinio.com/auth"
|
354
|
+
when DELAYED_SIP then url = "https://realtime-delayed-sip.intrinio.com/auth"
|
355
|
+
when MANUAL then url = "http://" + @ip_address + "/auth"
|
356
|
+
end
|
357
|
+
|
358
|
+
url = api_auth_url(url) if @api_key
|
359
|
+
|
360
|
+
url
|
361
|
+
end
|
362
|
+
|
363
|
+
def api_auth_url(url)
|
364
|
+
if url.include? "?"
|
365
|
+
url = "#{url}&"
|
366
|
+
else
|
367
|
+
url = "#{url}?"
|
368
|
+
end
|
369
|
+
|
370
|
+
"#{url}api_key=#{@api_key}"
|
371
|
+
end
|
372
|
+
|
373
|
+
def socket_url
|
374
|
+
case @provider
|
375
|
+
when REALTIME then "wss://realtime-mx.intrinio.com/socket/websocket?vsn=1.0.0&token=#{@token}"
|
376
|
+
when DELAYED_SIP then "wss://realtime-delayed-sip.intrinio.com/socket/websocket?vsn=1.0.0&token=#{@token}"
|
377
|
+
when MANUAL then "ws://" + @ip_address + "/socket/websocket?vsn=1.0.0&token=#{@token}"
|
378
|
+
end
|
379
|
+
end
|
380
|
+
|
381
|
+
def refresh_websocket
|
382
|
+
me = self
|
383
|
+
|
384
|
+
@ws.close() unless @ws.nil?
|
385
|
+
@ready = false
|
386
|
+
@joined_channels = []
|
387
|
+
|
388
|
+
@stop = true
|
389
|
+
sleep(2)
|
390
|
+
@threads.each { |thread|
|
391
|
+
if !thread.nil? && (!thread.pending_interrupt? || thread.status == "run" || thread.status == "Sleeping")
|
392
|
+
then thread.join(7)
|
393
|
+
elsif !thread.nil?
|
394
|
+
then thread.kill
|
395
|
+
end
|
396
|
+
}
|
397
|
+
@threads = []
|
398
|
+
@stop = false
|
399
|
+
@thread_quantity.times {@threads << Thread.new{handle_data}}
|
400
|
+
|
401
|
+
@ws = ws = WebSocket::Client::Simple.connect(socket_url)
|
402
|
+
me.send :info, "Connection opening"
|
403
|
+
|
404
|
+
ws.on :open do
|
405
|
+
me.send :info, "Connection established"
|
406
|
+
me.send :ready, true
|
407
|
+
if [REALTIME, MANUAL].include?(me.send(:provider))
|
408
|
+
me.send :refresh_channels
|
409
|
+
end
|
410
|
+
me.send :start_heartbeat
|
411
|
+
me.send :stop_self_heal
|
412
|
+
end
|
413
|
+
|
414
|
+
ws.on :message do |frame|
|
415
|
+
data_message = frame.data
|
416
|
+
#me.send :debug, "Message: #{data_message}"
|
417
|
+
begin
|
418
|
+
unless data_message.nil?
|
419
|
+
then me.send :queue_message, data_message
|
420
|
+
end
|
421
|
+
rescue StandardError => e
|
422
|
+
me.send :error, "Error adding message to queue: #{data_message} #{e}"
|
423
|
+
end
|
424
|
+
end
|
425
|
+
|
426
|
+
ws.on :close do |e|
|
427
|
+
me.send :ready, false
|
428
|
+
me.send :info, "Connection closing...: #{e}"
|
429
|
+
me.send :try_self_heal
|
430
|
+
end
|
431
|
+
|
432
|
+
ws.on :error do |e|
|
433
|
+
me.send :ready, false
|
434
|
+
me.send :error, "Connection error: #{e}"
|
435
|
+
me.send :try_self_heal
|
436
|
+
end
|
437
|
+
end
|
438
|
+
|
439
|
+
def refresh_channels
|
440
|
+
return unless @ready
|
441
|
+
debug "Refreshing channels"
|
442
|
+
|
443
|
+
# Join new channels
|
444
|
+
new_channels = @channels - @joined_channels
|
445
|
+
new_channels.each do |channel|
|
446
|
+
#msg = join_message(channel)
|
447
|
+
#@ws.send(msg.to_json)
|
448
|
+
msg = join_binary_message(channel)
|
449
|
+
@ws.send(msg)
|
450
|
+
info "Joined #{channel}"
|
451
|
+
end
|
452
|
+
|
453
|
+
# Leave old channels
|
454
|
+
old_channels = @joined_channels - @channels
|
455
|
+
old_channels.each do |channel|
|
456
|
+
#msg = leave__message(channel)
|
457
|
+
#@ws.send(msg.to_json)
|
458
|
+
msg = leave_binary_message(channel)
|
459
|
+
@ws.send(msg)
|
460
|
+
info "Left #{channel}"
|
461
|
+
end
|
462
|
+
|
463
|
+
@channels.uniq!
|
464
|
+
@joined_channels = Array.new(@channels)
|
465
|
+
debug "Current channels: #{@channels}"
|
466
|
+
end
|
467
|
+
|
468
|
+
def start_heartbeat
|
469
|
+
EM.cancel_timer(@heartbeat_timer) if @heartbeat_timer
|
470
|
+
@heartbeat_timer = EM.add_periodic_timer(HEARTBEAT_TIME) do
|
471
|
+
if msg = heartbeat_msg()
|
472
|
+
@ws.send(msg)
|
473
|
+
debug "Heartbeat #{msg}"
|
474
|
+
end
|
475
|
+
end
|
476
|
+
end
|
477
|
+
|
478
|
+
def heartbeat_msg
|
479
|
+
""
|
480
|
+
end
|
481
|
+
|
482
|
+
def stop_heartbeat
|
483
|
+
EM.cancel_timer(@heartbeat_timer) if @heartbeat_timer
|
484
|
+
end
|
485
|
+
|
486
|
+
def try_self_heal
|
487
|
+
return if @closing
|
488
|
+
debug "Attempting to self-heal"
|
489
|
+
|
490
|
+
time = @selfheal_backoffs.first
|
491
|
+
@selfheal_backoffs.delete_at(0) if @selfheal_backoffs.count > 1
|
492
|
+
|
493
|
+
EM.cancel_timer(@heartbeat_timer) if @heartbeat_timer
|
494
|
+
EM.cancel_timer(@selfheal_timer) if @selfheal_timer
|
495
|
+
|
496
|
+
@selfheal_timer = EM.add_timer(time/1000) do
|
497
|
+
connect()
|
498
|
+
end
|
499
|
+
end
|
500
|
+
|
501
|
+
def stop_self_heal
|
502
|
+
EM.cancel_timer(@selfheal_timer) if @selfheal_timer
|
503
|
+
@selfheal_backoffs = Array.new(SELF_HEAL_BACKOFFS)
|
504
|
+
end
|
505
|
+
|
506
|
+
def ready(val)
|
507
|
+
@ready = val
|
508
|
+
end
|
509
|
+
|
510
|
+
def debug(message)
|
511
|
+
message = "IntrinioRealtime | #{message}"
|
512
|
+
@logger.debug(message) rescue
|
513
|
+
nil
|
514
|
+
end
|
515
|
+
|
516
|
+
def info(message)
|
517
|
+
message = "IntrinioRealtime | #{message}"
|
518
|
+
@logger.info(message) rescue
|
519
|
+
nil
|
520
|
+
end
|
521
|
+
|
522
|
+
def error(message)
|
523
|
+
message = "IntrinioRealtime | #{message}"
|
524
|
+
@logger.error(message) rescue
|
525
|
+
nil
|
526
|
+
end
|
527
|
+
|
528
|
+
def fatal(message)
|
529
|
+
message = "IntrinioRealtime | #{message}"
|
530
|
+
@logger.fatal(message) rescue
|
531
|
+
EM.stop_event_loop
|
532
|
+
throw :fatal
|
533
|
+
nil
|
534
|
+
end
|
535
|
+
|
536
|
+
def parse_channels(channels)
|
537
|
+
channels.flatten!
|
538
|
+
channels.uniq!
|
539
|
+
channels.compact!
|
540
|
+
channels
|
541
|
+
end
|
542
|
+
|
543
|
+
def join_binary_message(channel)
|
544
|
+
if (channel == "lobby") && (@trades_only == false)
|
545
|
+
return [74, 0, 36, 70, 73, 82, 69, 72, 79, 83, 69].pack('C*') #74, not trades only, "$FIREHOSE"
|
546
|
+
elsif (channel == "lobby") && (@trades_only == true)
|
547
|
+
return [74, 1, 36, 70, 73, 82, 69, 72, 79, 83, 69].pack('C*') #74, trades only, "$FIREHOSE"
|
548
|
+
else
|
549
|
+
bytes = [74, 0]
|
550
|
+
if (@trades_only == true)
|
551
|
+
bytes[1] = 1
|
552
|
+
end
|
553
|
+
return bytes.concat(channel.bytes).pack('C*')
|
554
|
+
end
|
555
|
+
end
|
556
|
+
|
557
|
+
def leave_binary_message(channel)
|
558
|
+
if channel == "lobby"
|
559
|
+
return [76, 36, 70, 73, 82, 69, 72, 79, 83, 69].pack('C*') #74, not trades only, "$FIREHOSE"
|
560
|
+
else
|
561
|
+
bytes = [76]
|
562
|
+
return bytes.concat(channel.bytes).pack('C*')
|
563
|
+
end
|
564
|
+
end
|
565
|
+
|
566
|
+
def valid_api_key?(api_key)
|
567
|
+
return false unless api_key.is_a?(String)
|
568
|
+
return false if api_key.empty?
|
569
|
+
true
|
570
|
+
end
|
571
|
+
|
572
|
+
end
|
573
|
+
end
|
574
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: intrinio-realtime
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Intrinio
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-01-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: eventmachine
|
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
93
|
- !ruby/object:Gem::Version
|
94
94
|
version: '0'
|
95
95
|
requirements: []
|
96
|
-
rubygems_version: 3.
|
96
|
+
rubygems_version: 3.1.6
|
97
97
|
signing_key:
|
98
98
|
specification_version: 4
|
99
99
|
summary: Intrinio provides real-time stock prices from its Multi-Exchange feed, via
|