Module: RubyTrade::Client::ClassMethods

Defined in:
lib/client.rb

Instance Method Summary collapse

Instance Method Details

#buy(*args) ⇒ Object



158
# File 'lib/client.rb', line 158

def buy *args; @@child.buy(*args); end

#cashObject



160
# File 'lib/client.rb', line 160

def cash; @@child.cash; end

#child=(child) ⇒ Object

hook so we can call child methods



146
147
148
# File 'lib/client.rb', line 146

def child= child
  @@child = child
end

#connect_to(server, args) ⇒ Object



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
# File 'lib/client.rb', line 163

def connect_to server, args
  feed_port = args[:feed_port] || DEFAULT_FEED_PORT
  order_port = args[:order_port] || DEFAULT_ORDER_PORT

  if not args[:as]
    raise "Need to specify a username: connect_to \"...\", as: \"username\""
  end

  @zmq_context = EM::ZeroMQ::Context.new 1

  EM.run do
    @feed = @zmq_context.socket ZMQ::SUB

    puts "Listening to feed on #{server}:#{feed_port}"
    @feed.connect "tcp://#{server}:#{feed_port}"
    @feed.subscribe

    @feed.on :message do |part|
      begin
        self.process_message JSON.parse part.copy_out_string
      ensure
        part.close
      end
    end

    ConnectionClient.setup args, self

    puts "Connecting to order server #{server}:#{order_port}"
    EM.connect server, order_port, ConnectionClient

    Signal.trap("INT") { EM.stop }
    Signal.trap("TERM") { EM.stop }
  end
end

#on_connect(*args) ⇒ Object



139
# File 'lib/client.rb', line 139

def on_connect *args; end

#on_dividend(*args) ⇒ Object



143
# File 'lib/client.rb', line 143

def on_dividend *args; end

#on_fill(*args) ⇒ Object



141
# File 'lib/client.rb', line 141

def on_fill *args; end

#on_partial_fill(*args) ⇒ Object



142
# File 'lib/client.rb', line 142

def on_partial_fill *args; end

#on_tick(*args) ⇒ Object



140
# File 'lib/client.rb', line 140

def on_tick *args; end

#process_message(data) ⇒ Object

Called when we receive feed data



151
152
153
154
155
156
# File 'lib/client.rb', line 151

def process_message data
  case data["action"]
  when "tick"
    self.on_tick data["level1"]
  end
end

#sell(*args) ⇒ Object



159
# File 'lib/client.rb', line 159

def sell *args; @@child.sell(*args); end

#stockObject



161
# File 'lib/client.rb', line 161

def stock; @@child.stock; end