ruby-trade 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -19,7 +19,8 @@ class Slammer
19
19
  end
20
20
 
21
21
  def self.setup_next_shot
22
- time_gap = MinTime + rand(TimeVariance)
22
+ #time_gap = MinTime + rand(TimeVariance)
23
+ time_gap = 10
23
24
  puts "Firing in #{time_gap} seconds..."
24
25
 
25
26
  this = self
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "ruby-trade"
3
- s.version = "0.3"
3
+ s.version = "0.4"
4
4
  s.date = "2013-11-09"
5
5
  s.summary = "A stock market simulation game."
6
6
  s.description = ""
@@ -9,4 +9,6 @@ Gem::Specification.new do |s|
9
9
  s.files = `git ls-files`.split("\n")
10
10
  s.require_paths = ["lib"]
11
11
  s.license = "MIT"
12
+
13
+ s.add_runtime_dependency "em-zeromq"
12
14
  end
@@ -28,13 +28,15 @@ class Account
28
28
  end
29
29
  end
30
30
 
31
- def on_trade order, amount
32
- if order.side == :buy
31
+ def on_trade order, price, amount
32
+ if order.side == "buy"
33
+ puts "#{@name}: got trade: #{amount} @ #{price}"
33
34
  @stock += amount
34
- @cash -= order.price * amount
35
+ @cash -= price * amount
35
36
  else
37
+ puts "#{@name}: got trade: #{-amount} @ #{price}"
36
38
  @stock -= amount
37
- @cash += order.price * amount
39
+ @cash += price * amount
38
40
  end
39
41
  end
40
42
 
@@ -66,15 +66,18 @@ private
66
66
  # buy order is at least equal to the ask, at least one trade will trigger
67
67
  if order.size > next_order.size
68
68
  # Took out the entire order, keep going
69
- next_order.fill! next_order.price, next_order.size
70
- order.fill! next_order.price, next_order.size
69
+ price, size = next_order.price, next_order.size
70
+ next_order.fill! price, size
71
+ order.fill! price, size
71
72
  @sell_orders.pop
72
73
  @last = next_order.price
73
74
  else
74
75
  # This one is enough to fill the sent one, no need to add it
75
- next_order.fill! next_order.price, order.size
76
- order.fill! next_order.price, order.size
77
- @sell_orders.pop if next_order.size == order.size
76
+ orig_size = next_order.size
77
+ price, size = next_order.price, order.size
78
+ next_order.fill! price, size
79
+ order.fill! price, size
80
+ @sell_orders.pop if size == orig_size
78
81
  @last = next_order.price
79
82
  break
80
83
  end
@@ -90,17 +93,20 @@ private
90
93
  while not @buy_orders.empty? and order.price <= (next_order = @buy_orders.next).price
91
94
  if order.size > next_order.size
92
95
  # Took out the entire order, keep going
93
- next_order.fill! next_order.price, next_order.size
94
- order.fill! next_order.price, next_order.size
96
+ price, size = next_order.price, next_order.size
97
+ next_order.fill! price, size
98
+ order.fill! price, size
95
99
  @buy_orders.pop
96
100
  @last = next_order.price
97
101
  else
98
102
  # This one is enough to fill the sent one, no need to add it
99
- next_order.fill! next_order.price, order.size
100
- order.fill! next_order.price, order.size
103
+ orig_size = next_order.size
104
+ price, size = next_order.price, order.size
105
+ next_order.fill! price, size
106
+ order.fill! price, size
101
107
 
102
108
  @last = next_order.price
103
- @buy_orders.pop if next_order.size == order.size
109
+ @buy_orders.pop if orig_size == size
104
110
  break
105
111
  end
106
112
  end
@@ -1,7 +1,7 @@
1
1
  /*jslint browser: true, indent: 2, nomen: true, plusplus: true, newcap: true, regexp: true, sloppy: true */
2
2
  /*global $, SockJS, console, WebSocket*/
3
- $(function () {
4
- var plot,
3
+ function connect() {
4
+ var plot, refreshTimer, connectTimer,
5
5
  NumPeriods = 300,
6
6
  UpdateInterval = 100,
7
7
  sock = new WebSocket("ws://" + window.location.host + "/ws"),
@@ -15,6 +15,7 @@ $(function () {
15
15
 
16
16
  sock.onopen = function () {
17
17
  console.log("Connected to WS server.");
18
+ clearTimeout(connectTimer);
18
19
  };
19
20
 
20
21
  sock.onmessage = function (data) {
@@ -29,6 +30,8 @@ $(function () {
29
30
  case "accounts":
30
31
  $("#leaderboard tbody").html(
31
32
  $.map(data.accounts, function (obj) {
33
+ obj[1] = obj[1].toFixed(2);
34
+ obj[3] = obj[3].toFixed(2);
32
35
  return "<tr><td>" + obj.join("</td><td>") + "</td></tr>";
33
36
  }).join("")
34
37
  );
@@ -38,6 +41,9 @@ $(function () {
38
41
 
39
42
  sock.onclose = function () {
40
43
  console.log("Disconnected from WS server.");
44
+ // reconnect in 10 seconds
45
+ clearInterval(refreshTimer);
46
+ connectTimer = setTimeout(connect, 10000);
41
47
  };
42
48
 
43
49
  function getData() {
@@ -67,7 +73,7 @@ $(function () {
67
73
  });
68
74
 
69
75
  // Push last, update graph
70
- setInterval(function () {
76
+ refreshTimer = setInterval(function () {
71
77
  time++;
72
78
  ticks.bids.push([time, level1.bid > 0 ? level1.bid : null]);
73
79
  ticks.asks.push([time, level1.ask > 0 ? level1.ask : null]);
@@ -88,4 +94,8 @@ $(function () {
88
94
  plot.setupGrid();
89
95
  plot.draw();
90
96
  }, UpdateInterval);
97
+ }
98
+
99
+ $(function () {
100
+ connect();
91
101
  });
@@ -44,8 +44,8 @@
44
44
  <tr>
45
45
  <th>Name</th>
46
46
  <th>Value</th>
47
- <th>Cash</th>
48
47
  <th>Stock</th>
48
+ <th>Cash</th>
49
49
  </tr>
50
50
  </thead>
51
51
  <tbody></tbody>
@@ -5,7 +5,7 @@ require_relative 'exchange'
5
5
  require_relative 'web_server'
6
6
  require_relative 'common'
7
7
 
8
- AccountUpdateFrequency = 30
8
+ AccountUpdateFrequency = 5
9
9
 
10
10
  class OrderServer < EM::Connection
11
11
  include LineCleaner
@@ -26,7 +26,7 @@ class OrderServer < EM::Connection
26
26
  }.map { |account|
27
27
  [account.name, account.net_value(level1[:last]), account.stock, account.cash]
28
28
  }.sort_by { |row|
29
- row[3]
29
+ -row[1]
30
30
  })
31
31
  end
32
32
  end
@@ -50,6 +50,7 @@ class OrderServer < EM::Connection
50
50
  when :fill
51
51
  order, price, amount = args
52
52
  @my_orders.delete order.id
53
+ @account.on_trade order, price, amount
53
54
  send_data_f({
54
55
  action: "order_fill",
55
56
  amount: amount,
@@ -59,6 +60,7 @@ class OrderServer < EM::Connection
59
60
  @@parent.tick @@exchange
60
61
  when :partial_fill
61
62
  order, price, amount = args
63
+ @account.on_trade order, price, amount
62
64
  send_data_f({
63
65
  action: "order_partial_fill",
64
66
  amount: amount,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-trade
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.3'
4
+ version: '0.4'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,23 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
  date: 2013-11-09 00:00:00.000000000 Z
13
- dependencies: []
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: em-zeromq
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  description: ''
15
31
  email: rob@robbritton.com
16
32
  executables: []
@@ -185,7 +201,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
185
201
  version: '0'
186
202
  requirements: []
187
203
  rubyforge_project:
188
- rubygems_version: 1.8.23
204
+ rubygems_version: 1.8.19
189
205
  signing_key:
190
206
  specification_version: 3
191
207
  summary: A stock market simulation game.