-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdatasource.tsx
232 lines (199 loc) · 6.18 KB
/
datasource.tsx
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
import {Table, Schema} from "@finos/perspective";
const FTX_URL = "wss://ftx.com/ws/";
const BINANCE_URL = "wss://fstream.binance.com/ws/btcusdt@bookTicker";
// const BITMEX_URL = "wss://www.bitmex.com/realtime";
export const SCHEMA: Schema = {
exchange: "string",
timestamp: "datetime",
price: "float",
size: "float",
side: "string",
};
interface DataSource {
exchange: string;
url: string;
websocket: WebSocket;
table: Table;
subscribed: boolean;
subscribe(): void;
unsubscribe(): void;
onmessage(event: MessageEvent): void;
send(msg: Record<string, unknown>): void;
}
/**
* A datasource that subscribes to a websocket.
*/
abstract class BaseDataSource implements DataSource {
abstract exchange: string;
abstract url: string;
abstract websocket: WebSocket;
abstract table: Table;
abstract subscribed: boolean;
abstract subscribe(): void;
abstract unsubscribe(): void;
abstract onmessage(event: MessageEvent<any>): void;
send(msg: Record<string, unknown>): void {
this.websocket.send(JSON.stringify(msg));
}
}
/**
* Subscribe to the FTX websocket API.
*/
export class FTXDataSource extends BaseDataSource {
exchange: string;
url: string;
websocket: WebSocket;
table: Table;
subscribed = false;
constructor(table: Table) {
super();
this.exchange = "FTX";
this.url = FTX_URL;
this.table = table;
this.websocket = new WebSocket(this.url);
this.websocket.onopen = this.subscribe.bind(this);
this.websocket.onclose = this.unsubscribe.bind(this);
this.websocket.onmessage = this.onmessage.bind(this);
}
subscribe(): void {
this.send({
op: "subscribe",
channel: "orderbook",
market: "BTC-PERP",
});
}
unsubscribe(): void {
this.send({
op: "unsubscribe",
channel: "orderbook",
market: "BTC-PERP",
});
}
onmessage(event: MessageEvent): void {
if (!event.data) {
return;
}
const message = JSON.parse(event.data);
const message_type = message.type;
// Upon subscribing, you will receive one snapshot of the orderbook
// (partial) with a data field containing: bids, asks, checksum, time
if (message_type === "update") {
const book = message.data;
const parsed = [];
for (const bid of book.bids) {
parsed.push(this.parse("bid", book.time, bid));
}
for (const ask of book.asks) {
parsed.push(this.parse("ask", book.time, ask));
}
if (parsed.length > 0) {
this.table.update(parsed as any);
}
} else if (message_type === "subscribed") {
this.subscribed = true;
console.debug("[FTX] successfully subscribed!");
} else if (message_type === "unsubscribed") {
this.subscribed = false;
console.debug("[FTX] successfully unsubscribed!");
} else if (message_type === "partial") {
// don't read the initial state of the book, just feed updates.
return;
} else {
throw new Error(`[FTX] Unknown message received: ${message}`);
}
}
/**
* The bids and asks are formatted like so: [
* [best price, size at price],
* [next next best price, size at price],
* ...]
*/
parse(
side: string,
timestamp: number,
row: Array<number>
): Record<string, string | number | Date | boolean> {
const price = row[0];
const size = row[1];
return {
exchange: this.exchange,
price,
size,
timestamp: new Date(timestamp * 1000),
side,
};
}
}
/**
* Subscribe to the Binance Futures websocket API. Though not implemented
* in the workspace, initializing the datasource in index.tsx is trivial
* and is left as an exercise for the reader. In practice I found that the
* datastream from the FTX API was more than granular enough for demo purposes,
* considering this isn't a real trading app but a demonstration of
* Perspective's handling of fast streaming data.
*/
export class BinanceDataSource extends BaseDataSource {
subscribed = false;
exchange: string;
url: string;
websocket: WebSocket;
table: Table;
id = 0;
constructor(table: Table) {
super();
this.exchange = "Binance";
this.url = BINANCE_URL;
this.table = table;
this.websocket = new WebSocket(this.url);
this.websocket.onopen = this.subscribe.bind(this);
this.websocket.onclose = this.unsubscribe.bind(this);
this.websocket.onmessage = this.onmessage.bind(this);
}
subscribe(): void {
this.send({
method: "subscribe",
params: ["btcusdt@bookTicker"],
id: this.id,
});
}
unsubscribe(): void {
this.send({
method: "unsubscribe",
params: ["btcusdt@bookTicker"],
id: this.id,
});
}
onmessage(event: MessageEvent<any>): void {
if (!event.data) return;
const message = JSON.parse(event.data);
const message_type = message.e;
if (message == "ping") {
this.send("pong");
return;
}
if (message_type == "bookTicker") {
if (!this.subscribed) this.subscribed = true;
const timestamp = new Date(message.E);
this.table.update([
{
exchange: this.exchange,
price: message.b,
side: "bid",
size: message.B,
timestamp,
},
{
exchange: this.exchange,
price: message.a,
side: "ask",
size: message.A,
timestamp,
},
] as any);
}
}
send(msg: Record<string, unknown> | string): void {
this.websocket.send(JSON.stringify(msg));
this.id++;
}
}