module RubyTrade::ConnectionClient
Public Class Methods
setup(args, parent)
click to toggle source
# File lib/client.rb, line 15 def self.setup args, parent @@username = args[:as] @@ai = args[:ai] || false @@parent = parent end
Public Instance Methods
buy(amount, args)
click to toggle source
Send a buy order
# File lib/client.rb, line 76 def buy amount, args send_order "buy", amount, args[:at] end
cash()
click to toggle source
# File lib/client.rb, line 40 def cash; @cash; end
handle_message(data)
click to toggle source
Process a message from the server
# File lib/client.rb, line 111 def handle_message data case data["action"] when "order_accept" @orders[data["local_id"]].price = data["price"] when "order_fill" update_account data @@parent.on_fill @orders[data["local_id"]], data["amount"], data["price"] when "order_partial_fill" update_account data @@parent.on_partial_fill @orders[data["local_id"]], data["amount"], data["price"] when "order_cancel" # Don't need to do anything here when "account_update" @cash, @stock = data["cash"], data["stock"] if not @connect_triggered @connect_triggered = true @@parent.on_connect end when "dividend" @cash += data["value"] @@parent.on_dividend data["value"] end end
post_init()
click to toggle source
# File lib/client.rb, line 21 def post_init # identify with the server data = { action: "identify", name: @@username, ai: @@ai }.to_json send_data_f data @buffer = "" @order_no = 0 @orders = {} @cash, @stock = 0, 0 @connect_triggered = false @@parent.child = self end
receive_data(data)
click to toggle source
Called by EM when we receive data
# File lib/client.rb, line 91 def receive_data data clean(data).each do |msg| handle_message JSON.parse msg end end
sell(amount, args)
click to toggle source
Send a sell order
# File lib/client.rb, line 81 def sell amount, args send_order "sell", amount, args[:at] end
send_data_f(data)
click to toggle source
Send data with tokens
# File lib/client.rb, line 86 def send_data_f data send_data "\x02#{data}\x03" end
send_order(side, size, price)
click to toggle source
# File lib/client.rb, line 43 def send_order side, size, price @order_no += 1 order = Order.new @order_no, side, price, size order.add_observer self @orders[@order_no] = order send_data_f({ action: "new_order", local_id: @order_no, size: size, price: price, side: side }.to_json) order end
stock()
click to toggle source
# File lib/client.rb, line 41 def stock; @stock; end
update(what, *args)
click to toggle source
# File lib/client.rb, line 62 def update what, *args case what when :cancel order = args[0] send_data_f({ action: "cancel_order", id: order.id }.to_json) else # Don't need to handle anything else end end
update_account(data)
click to toggle source
Recalculate cash/stock balances
# File lib/client.rb, line 98 def update_account data order = @orders[data["local_id"]] if order.side == "buy" @cash -= data["price"] * data["amount"] @stock += data["amount"] else @cash += data["price"] * data["amount"] @stock -= data["amount"] end end