ruby-trade 0.3 → 0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/examples/slammer.rb +2 -1
- data/ruby-trade.gemspec +3 -1
- data/server/account.rb +6 -4
- data/server/order-book.rb +16 -10
- data/server/public/app.js +13 -3
- data/server/public/index.html +1 -1
- data/server/server.rb +4 -2
- metadata +19 -3
data/examples/slammer.rb
CHANGED
data/ruby-trade.gemspec
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = "ruby-trade"
|
3
|
-
s.version = "0.
|
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
|
data/server/account.rb
CHANGED
@@ -28,13 +28,15 @@ class Account
|
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
31
|
-
def on_trade order, amount
|
32
|
-
if order.side ==
|
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 -=
|
35
|
+
@cash -= price * amount
|
35
36
|
else
|
37
|
+
puts "#{@name}: got trade: #{-amount} @ #{price}"
|
36
38
|
@stock -= amount
|
37
|
-
@cash +=
|
39
|
+
@cash += price * amount
|
38
40
|
end
|
39
41
|
end
|
40
42
|
|
data/server/order-book.rb
CHANGED
@@ -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
|
-
|
70
|
-
|
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
|
-
|
76
|
-
|
77
|
-
|
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
|
-
|
94
|
-
|
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
|
-
|
100
|
-
|
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
|
109
|
+
@buy_orders.pop if orig_size == size
|
104
110
|
break
|
105
111
|
end
|
106
112
|
end
|
data/server/public/app.js
CHANGED
@@ -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
|
-
|
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
|
});
|
data/server/public/index.html
CHANGED
data/server/server.rb
CHANGED
@@ -5,7 +5,7 @@ require_relative 'exchange'
|
|
5
5
|
require_relative 'web_server'
|
6
6
|
require_relative 'common'
|
7
7
|
|
8
|
-
AccountUpdateFrequency =
|
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[
|
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.
|
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.
|
204
|
+
rubygems_version: 1.8.19
|
189
205
|
signing_key:
|
190
206
|
specification_version: 3
|
191
207
|
summary: A stock market simulation game.
|