Module: RubyTrade::ConnectionClient

Includes:
LineCleaner
Defined in:
lib/client.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.setup(args, parent) ⇒ Object



15
16
17
18
19
# File 'lib/client.rb', line 15

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



76
77
78
# File 'lib/client.rb', line 76

def buy amount, args
  send_order "buy", amount, args[:at]
end

#cashObject



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

def cash; @cash; end

#handle_message(data) ⇒ Object

Process a message from the server



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 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"
     data
    @@parent.on_fill @orders[data["local_id"]], data["amount"], data["price"]
  when "order_partial_fill"
     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_initObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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) ⇒ Object

Called by EM when we receive data



91
92
93
94
95
# 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) ⇒ Object

Send a sell order



81
82
83
# File 'lib/client.rb', line 81

def sell amount, args
  send_order "sell", amount, args[:at]
end

#send_data_f(data) ⇒ Object

Send data with tokens



86
87
88
# File 'lib/client.rb', line 86

def send_data_f data
  send_data "\x02#{data}\x03"
end

#send_order(side, size, price) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# 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

#stockObject



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

def stock; @stock; end

#update(what, *args) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object

Recalculate cash/stock balances



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/client.rb', line 98

def  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