Skip to content

Commit 8967f1b

Browse files
Merge pull request #7 from vacuumlabs/implement-price-chart
implement price chart
2 parents 28668c0 + 39ebdae commit 8967f1b

File tree

2 files changed

+47
-47
lines changed

2 files changed

+47
-47
lines changed

src/lib/chart.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ let sidebarWidth = 300;
2828
export function initChart() {
2929

3030
let script = document.createElement("script");
31-
script.setAttribute("src", "https://unpkg.com/lightweight-charts/dist/lightweight-charts.standalone.production.js");
31+
// IMPORTANT: Do not change the package version as there are some breaking changes
32+
script.setAttribute("src", "https://unpkg.com/lightweight-charts@^4/dist/lightweight-charts.standalone.production.js");
3233
document.body.appendChild(script);
3334

3435
script.addEventListener("load", scriptLoaded, false);
@@ -196,10 +197,13 @@ export async function loadCandles(_resolution, _start, _end, prepend, productOve
196197

197198
// console.log('start, end', start, end, new Date(start).toString(), new Date(end).toString());
198199

199-
const url_start = encodeURIComponent(new Date(start).toUTCString());
200-
const url_end = encodeURIComponent(new Date(end).toUTCString());
200+
const url_start = Math.floor(start / 1000);
201+
const url_end = Math.floor(end / 1000);
201202

202-
const response = await fetch(`https://api.exchange.coinbase.com/products/${_product}/candles?granularity=${_resolution}&start=${url_start}&end=${url_end}`);
203+
// TODO: update this based on the used backend, we're using mock price provider which expects in minutes
204+
const resolution_mins = Number(_resolution) / 60;
205+
206+
const response = await fetch(`http://localhost:3000/products/${_product}/candles?granularity=${resolution_mins}m&start=${url_start}&end=${url_end}`);
203207
const json = await response.json();
204208

205209
if (!json || !Array.isArray(json)) {
@@ -212,26 +216,28 @@ export async function loadCandles(_resolution, _start, _end, prepend, productOve
212216
for (const item of json) {
213217
prepend_set.push({
214218
time: correctedTime(item[0]),
215-
low: item[1],
219+
open: item[1],
216220
high: item[2],
217-
open: item[3],
221+
low: item[3],
218222
close: item[4]
219223
});
220224
}
221-
prepend_set.reverse();
225+
// If backend returns prices sorted by decreasing timestamp, apply reverse function
226+
// prepend_set.reverse();
222227
candles = prepend_set.concat(candles);
223228
} else {
224229
candles = [];
225230
for (const item of json) {
226231
candles.push({
227232
time: correctedTime(item[0]),
228-
low: item[1],
233+
open: item[1],
229234
high: item[2],
230-
open: item[3],
235+
low: item[3],
231236
close: item[4]
232237
});
233238
}
234-
candles.reverse();
239+
// If backend returns prices sorted by decreasing timestamp, apply reverse function
240+
// candles.reverse();
235241
}
236242

237243
//console.log('data', data);
@@ -260,15 +266,15 @@ export function onNewPrice(price, timestamp, _product) {
260266

261267
timestamp = correctedTime(timestamp/1000);
262268

263-
const resolution = get(chartResolution);
269+
const resolution = Number(get(chartResolution));
264270

265271
if (timestamp >= lastCandle.time + resolution) {
266272
// new candle
267273
let candle = {
268274
time: parseInt(resolution * parseInt(timestamp/resolution)),
269-
low: price,
270-
high: price,
271275
open: price,
276+
high: price,
277+
low: price,
272278
close: price
273279
}
274280
candles.push(candle);

src/lib/stream.js

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -76,47 +76,45 @@ let s;
7676

7777
function getPrice(product_id) {
7878

79-
// fetch('https://api.exchange.coinbase.com/products/' + product_id + '/ticker')
80-
// .then((res) => { return res.json() })
81-
// .then((json) => {
82-
// console.log('price: ', price);
79+
fetch('http://localhost:3000/products/' + product_id + '/ticker')
80+
.then((res) => { return res.json() })
81+
.then((json) => {
82+
lastMessageReceived = Date.now();
8383

84-
// lastMessageReceived = Date.now();
84+
const price = json.price;
8585

86-
// const price = json.price;
86+
prices.update((x) => {
87+
x[product_id] = price * 1;
88+
return x;
89+
});
8790

88-
// prices.update((x) => {
89-
// x[product_id] = price * 1;
90-
// return x;
91-
// });
91+
// update chart
92+
onNewPrice(price, Date.now(), product_id);
9293

93-
// // update chart
94-
// onNewPrice(price, Date.now(), product_id);
95-
96-
// if (product_id == get(productId)) {
97-
// setTitle(product_id, price);
98-
// }
94+
if (product_id == get(productId)) {
95+
setTitle(product_id, price);
96+
}
9997

100-
// });
98+
});
10199

102100
}
103101

104-
// let poller;
105-
// export function initWebsocket() {
102+
let poller;
103+
export function initWebsocket() {
106104

107-
// // Poll
108-
// clearInterval(poller);
105+
// Poll
106+
clearInterval(poller);
109107

110-
// for (const product_id of ['ETH-USD', 'BTC-USD']) {
111-
// getPrice(product_id);
112-
// }
108+
for (const product_id of ['ETH-USD']) {
109+
getPrice(product_id);
110+
}
113111

114-
// // Poll for prices every 5 sec
115-
// poller = setInterval(() => {
116-
// for (const product_id of ['ETH-USD', 'BTC-USD']) {
117-
// getPrice(product_id);
118-
// }
119-
// }, 5000);
112+
// Poll for prices every 5 sec
113+
poller = setInterval(() => {
114+
for (const product_id of ['ETH-USD']) {
115+
getPrice(product_id);
116+
}
117+
}, 5000);
120118

121119
// // Stream
122120

@@ -218,8 +216,4 @@ function getPrice(product_id) {
218216
// console.log('Websocket error', e);
219217
// }
220218

221-
// }
222-
223-
export function initWebsocket() {
224-
225219
}

0 commit comments

Comments
 (0)