Class: Stockfighter::Websockets

Inherits:
Object
  • Object
show all
Defined in:
lib/stockfighter/websockets.rb

Constant Summary collapse

WS_URL =
"https://www.stockfighter.io/ob/api/ws"

Instance Method Summary collapse

Constructor Details

#initialize(key:, account:, symbol:, venue:) ⇒ Websockets

Returns a new instance of Websockets.



9
10
11
12
13
14
# File 'lib/stockfighter/websockets.rb', line 9

def initialize(key:, account:, symbol:, venue:)
  @account = 
  @venue = venue
  @quote_callbacks = []
  @execution_callbacks = []
end

Instance Method Details

#add_execution_callback(&block) ⇒ Object



98
99
100
# File 'lib/stockfighter/websockets.rb', line 98

def add_execution_callback(&block)
  @execution_callbacks << block
end

#add_quote_callback(&block) ⇒ Object



94
95
96
# File 'lib/stockfighter/websockets.rb', line 94

def add_quote_callback(&block)
  @quote_callbacks << block
end

#startObject



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
# File 'lib/stockfighter/websockets.rb', line 16

def start() 

  EM.epoll
  EM.run do

    EM.error_handler{ |e|
      abort("Error raised during event loop: #{e}")
    }

    tickertape = WebSocket::EventMachine::Client.connect(:uri => "#{WS_URL}/#{@account}/venues/#{@venue}/tickertape", :ssl => true)
    tickertape.onopen do
      puts "tickertape websocket: connected"
    end

    tickertape.onmessage do |msg|
      incoming = JSON.parse(msg)
      if not incoming["ok"]
        raise "tickertape websocket: Error response received: #{msg}"
      end
      if incoming.key?('quote')
        quote = incoming['quote']
        @quote_callbacks.each { |callback|
            callback.call(quote)
        }
      else
        raise "tickertape websocket: TODO: Unhandled message type: #{msg}"
      end
    end

    tickertape.onerror do |e|
      puts "tickertape websocket: Error #{e}"
    end

    tickertape.onping do |msg|
      puts "tickertape websocket: Received ping: #{msg}"
    end

    tickertape.onpong do |msg|
      puts "tickertape websocket: Received pong: #{msg}"
    end

    tickertape.onclose do
      raise "tickertape websocket: Disconnected"
    end

    executions = WebSocket::EventMachine::Client.connect(:uri => "#{WS_URL}/#{@account}/venues/#{@venue}/executions", :ssl => true)
    executions.onopen do
      puts "executions websocket: Connected"
    end

    executions.onmessage do |msg|
      execution = JSON.parse(msg)
      if not execution["ok"]
        raise "execution websocket: Error response received: #{msg}"
      end
      @execution_callbacks.each { |callback|
        callback.call(execution)
      }
    end

    executions.onerror do |e|
      puts "executions websocket: Error: #{e}"
    end

    executions.onping do |msg|
      puts "executions websocket: Received ping: #{msg}"
    end

    executions.onpong do |msg|
      puts "executions websocket: Received pong: #{msg}"
    end

    executions.onclose do
      raise "executions websocket: Disconnected"
    end
  end
end