Module: RubyTrade::Client::ClassMethods

Defined in:
lib/client.rb

Instance Method Summary collapse

Instance Method Details

#buy(*args) ⇒ Object



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

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

#cashObject



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

def cash; @@child.cash; end

#child=(child) ⇒ Object

hook so we can call child methods



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

def child= child
  @@child = child
end

#connect_to(server, args) ⇒ Object



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

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



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

def on_connect *args; end

#on_fill(*args) ⇒ Object



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

def on_fill *args; end

#on_partial_fill(*args) ⇒ Object



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

def on_partial_fill *args; end

#on_tick(*args) ⇒ Object



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

def on_tick *args; end

#process_message(data) ⇒ Object

Called when we receive feed data



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

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

#sell(*args) ⇒ Object



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

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

#stockObject



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

def stock; @@child.stock; end