forked from dbharath/HFT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trade.html
executable file
·190 lines (168 loc) · 5.58 KB
/
trade.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>
Trade Desk
</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.js"></script>
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script type="text/javascript" src="/underscore-min.js"></script>
<link href="/style.css" rel="stylesheet" type="text/css">
</head>
<script type="text/javascript">
$.fn.serializeObject = function()
{
var o = {};
var a = this.serializeArray();
$.each(a, function() {
var value = Number(this.value) || this.value || "";
if (value == "true") value = 1;
if (value == "false") value = 0;
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(value);
} else {
o[this.name] = value;
}
});
return o;
};
$(document).ready(function() {
var socket = io.connect("http://50.17.88.172:4000");
socket.on('tickers', function(data){
var ticker, o, tickers= eval(data);
while(ticker = tickers.shift()) {
o = '<option value="'+ticker+'">'+ticker+'</option>';
$("#ticker").append(o);
}
$("#order").removeClass("hide");
});
socket.on('quote', function(data) {
var data = $("<pre>" + data + "</pre><hr />");
$("#quotes").append(data);
$("html, body").animate({ scrollTop: $(document).height() }, 100);
$(data).show("slide", { direction: "up" }, 250);
$(data).effect("highlight", {}, 1500);
});
socket.on("portfolio", function(data){
eval('data=('+data+')');
var template = _.template($("#portfolio-template").html());
var value=data.port.position.cash;
for (name in data.port.position) {
if (name != "cash") {
value += data.port.position[name] * data.port.LastPrice[name]
}
}
$("#networth").text(value);
$("#position-summary").html(template(data.port));
template = _.template($("#orderbook-template").html());
$("#order-summary").html(template(data.port.orderBook));
if (data.event == "orderfilled") {
template = _.template($("#filled-template").html());
data.data.buy = data.data.buy ? "Bought" : "Sold";
$("#msg").append(template(data.data)).fadeIn();
var tid = setTimeout(function(){
$("#msg").fadeOut(function(){
$("#msg").empty();
});
clearTimeout(tid);
}, 3000);
}
$(".cancel").unbind();
$(".cancel").click(function(){
var id = $(this).parent().find(".order-id:first").text();
socket.emit("cancel_order", id);
});
});
$("#submit").click(function(){
var order = $("#order").serializeObject();
if (order.ticker && order.size && order.price && order.type) {
socket.emit("incoming_order", order);
} else {
alert("Fill all the fields to order, please.");
}
});
$("body").click(function(){
$("#msg").fadeOut();
});
});
</script>
<body>
<div id="msg" class="hide"> </div>
<div id="control" class="hide">
<h1>Now you can play around. </h1>
<h2>Net worth: <span id="networth"></span></h2>
<hr />
<form id="order">
<p><span>Ticker:</span>
<select name="ticker" id="ticker">
</select>
</p>
<p><span>Order Type:</span>
<select name="type">
<option value="LIMIT">limit</option>
<option value="STOP">stop</option>
<option value="MARKET">market</option>
</select>
</p>
<p>
[<span>Long/Buy:</span><input type="radio" name="buy" value="true">]
</p>
<p>
[<span>Short/Sell:</span><input type="radio" name="buy" value="false">]
</p>
<p><span>Shares:</span><input type="number" name="size" min="1"></p>
<p><span>Price:</span><input type="number" name="price" min="1"/></p>
<input id="submit" type="button" value="Place Order">
</form>
<hr />
<div id="position-summary"> </div>
<hr />
<div id="order-summary"> </div>
</div>
<div id="quotes"></div>
<script type="text/template" id="portfolio-template">
<h2>Position Summary:</h2>
<table>
<% _.each(position, function(pos, ticker) {
%> <tr>
<td><%= ticker %>:</td>
<td><%= pos %></td>
</tr> <% }); %>
</table>
</script>
<script type="text/template" id="orderbook-template">
<h2>Open Orders:</h2>
<table>
<% _.each(orders, function(order) {
%> <tr>
<td><strong><%= order.ticker %></strong></td>
<td>Size: <%= order.size %></td>
<td>
<% if (order.buy) { %>
BUY
<% } else { %>
SELL
<% } %>
</td>
<td>Price: <%= order.price %></td>
<td>Type: <%= order.type %></td>
<td>
<button class="cancel">Cancel</button>
<data class="order-id"><%= order.id %></data>
</td>
</tr> <% }); %>
</table>
</script>
<script type="text/template" id="filled-template">
<p>
<%= buy %> <%= shares %> shares of <%= ticker %>, @
$<%= price %>
</p>
</script>
</body>
</html>