-
Notifications
You must be signed in to change notification settings - Fork 257
/
index.html
282 lines (256 loc) · 7.01 KB
/
index.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>SignificantTrades for Bitmex - v1.1</title>
<script src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-114936513-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-114936513-1');
</script>
</head>
<body>
<div id="root"></div>
<style>
.streamingTable
{
background-color: rgba(0, 0, 0, 0);
border-collapse: collapse;
border-spacing: 0;
font-family: 'Open Sans', sans-serif;
font-size: 14px;
}
.streamingTable .streamingListRow td
{
padding: 5px;
white-space: nowrap;
border-left: 1px solid #ddd;
text-align: right;
}
.streamingListRow
{
border-top: 1px solid #bbb;
}
.streamingTable .Buy
{
color: #4aa165;
}
.streamingTable tr.Dark.Buy
{
color: white;
background-color: #4aa165;
}
.streamingTable .Sell
{
color: #d16547;
}
.streamingTable tr.Dark.Sell
{
color: white;
background-color: #d16547;
}
.streamingListRow:nth-child(odd) {
background: rgba(195,195,195,0.25);
}
.streamingListRow:nth-child(even) {
background: #fff;;
}
.symbolInput
{
width: 120px;
margin-right: 5px;
}
.priceInput
{
width: 170px;
margin-right: 5px;
}
</style>
<script type="text/babel">
function formatAMPM(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
seconds = seconds < 10 ? '0'+seconds : seconds;
var strTime = hours + ':' + minutes + ':' + seconds + ' ' + ampm;
return strTime;
}
function average(arr, priceFilter) {
var sumSizeArr = {}, sumPriceArr= {}, results = [], timestamp, side;
for (var i = 0; i < arr.length; i++) {
var item = arr[i];
var key = item.timestamp + item.side;
if (!(key in sumSizeArr)) {
sumSizeArr[key] = {};
sumSizeArr[key].sizeSum = item.size;
} else {
sumSizeArr[key].sizeSum += arr[i].size;
}
}
for (var i = 0; i < arr.length; i++) {
var item = arr[i];
var key = item.timestamp + item.side;
var sizeItem = sumSizeArr[key];
if (!(key in sumPriceArr)) {
sumPriceArr[key] = {};
sumPriceArr[key].symbol = item.symbol;
sumPriceArr[key].priceSum = item.price * item.size / sizeItem.sizeSum;
sumPriceArr[key].sizeSum = item.size;
sumPriceArr[key].timestamp = formatAMPM(new Date(item.timestamp));
sumPriceArr[key].side = item.side;
} else {
sumPriceArr[key].priceSum += item.price * item.size / sizeItem.sizeSum;
sumPriceArr[key].sizeSum += item.size;
}
}
for(key in sumPriceArr) {
var item = sumPriceArr[key];
if (item.sizeSum >= priceFilter)
{
results.push({
symbol: item.symbol,
price:item.priceSum.toFixed(2),
size: item.sizeSum,
timestamp: item.timestamp,
side: item.side
});
}
}
return results;
}
class TableRow extends React.Component {
render() {
const {data} = this.props;
const row = data.map((data, i) =>
<tr className={"streamingListRow " + (data.size > 99999 ? 'Dark ' : '') + (data.side == "Sell" ? 'Sell' : 'Buy')} key={i}>
<td width="90">{data.price}</td>
<td width="80">{data.size > 999 ? (data.size/1000).toFixed(0) + 'k' : data.size}</td>
<td width="100">{data.timestamp}</td>
</tr>
);
return (
<tbody>{row}</tbody>
);
}
}
class Table extends React.Component {
constructor(props) {
super(props);
}
render() {
return (
<table className="streamingTable">
<TableRow data={this.props.data} />
</table>
);
}
}
class ShowData extends React.Component {
constructor(props) {
super(props);
this.state = {data: []};
this.instance = this;
}
initWebSocketClient(symbol, price) {
if (symbol == null)
{
return null
}
var context = this.instance
var streamingData = null;
var priceFilter = isNaN(price) ? 0 : price;
if (context.socket != null)
{
context.socket.close();
context.socket = null;
}
context.socket = new WebSocket("wss://www.bitmex.com/realtime?subscribe=trade:" + symbol.toUpperCase());
context.socket.onmessage = function(event) {
let obj = JSON.parse(event.data);
if (obj.data == null)
{
return;
}
let data = average(obj.data, priceFilter);
if (streamingData == null)
{
streamingData =data;
}
else
{
if (streamingData.length > 100)
{
streamingData.splice(streamingData.length - 20);
}
streamingData = data.concat(streamingData);
}
context.setStreamData({data: streamingData});
};
}
setStreamData(data){
this.setState(data);
}
render() {
return (
<div>
<Table data={this.state.data}/>
</div>);
}
}
class SetSymbolView extends React.Component {
constructor() {
super();
this.onSymbol = this.onSymbol.bind(this);
this.onKeyPress = this.onKeyPress.bind(this);
}
onKeyPress(e) {
if (e.key === 'Enter') {
this.onSymbol();
}
}
onSymbol() {
this.dataComponent.initWebSocketClient(this.refs.symbol.value, parseInt(this.refs.minPrice.value));
}
render() {
return (
<div>
<input
ref="symbol"
type="text"
name="symbolBox"
className="symbolInput"
placeholder="Enter symbol here..."
onKeyPress={this.onKeyPress}
/>
<input
ref="minPrice"
type="number"
name="minPriceBox"
className="priceInput"
placeholder="Min order size threshold..."
min="0"
onKeyPress={this.onKeyPress}
/>
<button onClick={this.onSymbol}>Submit</button>
<p/>
<div>
<ShowData ref={instance => { this.dataComponent = instance; }}/>
</div>
</div>
);
}
}
ReactDOM.render(<SetSymbolView />, document.getElementById('root'));
</script>
</body>
</html>