Module: RubyTrade::ConnectionClient
- Defined in:
- lib/client.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#buy(amount, args) ⇒ Object
Send a buy order.
- #cash ⇒ Object
-
#clean(data) ⇒ Object
Strip off begin/end transmission tokens.
-
#handle_message(data) ⇒ Object
Process a message from the server.
- #post_init ⇒ Object
-
#receive_data(data) ⇒ Object
Called by EM when we receive data.
-
#sell(amount, args) ⇒ Object
Send a sell order.
-
#send_data_f(data) ⇒ Object
Send data with tokens.
- #send_order(side, size, price) ⇒ Object
- #stock ⇒ Object
- #update(what, *args) ⇒ Object
-
#update_account(data) ⇒ Object
Recalculate cash/stock balances.
Class Method Details
.setup(args, parent) ⇒ Object
12 13 14 15 16 |
# File 'lib/client.rb', line 12 def self.setup args, parent @@username = args[:as] @@ai = args[:ai] || false @@parent = parent end |
Instance Method Details
#buy(amount, args) ⇒ Object
Send a buy order
72 73 74 |
# File 'lib/client.rb', line 72 def buy amount, args send_order "buy", amount, args[:at] end |
#cash ⇒ Object
36 |
# File 'lib/client.rb', line 36 def cash; @cash; end |
#clean(data) ⇒ Object
Strip off begin/end transmission tokens
87 88 89 90 91 92 93 |
# File 'lib/client.rb', line 87 def clean data if data.length > 2 data[1..-2].split("\x03\x02") else [] end end |
#handle_message(data) ⇒ Object
Process a message from the server
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/client.rb', line 116 def 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 end end |
#post_init ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/client.rb', line 18 def post_init # identify with the server data = { action: "identify", name: @@username, ai: @@ai }.to_json send_data_f data @order_no = 0 @orders = {} @cash, @stock = 0, 0 @connect_triggered = false @@parent.child = self end |
#receive_data(data) ⇒ Object
Called by EM when we receive data
96 97 98 99 100 |
# File 'lib/client.rb', line 96 def receive_data data clean(data).each do |msg| JSON.parse msg end end |
#sell(amount, args) ⇒ Object
Send a sell order
77 78 79 |
# File 'lib/client.rb', line 77 def sell amount, args send_order "sell", amount, args[:at] end |
#send_data_f(data) ⇒ Object
Send data with tokens
82 83 84 |
# File 'lib/client.rb', line 82 def send_data_f data send_data "\x02#{data}\x03" end |
#send_order(side, size, price) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/client.rb', line 39 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 ⇒ Object
37 |
# File 'lib/client.rb', line 37 def stock; @stock; end |
#update(what, *args) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/client.rb', line 58 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) ⇒ Object
Recalculate cash/stock balances
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/client.rb', line 103 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 |