@@ -28,7 +28,8 @@ let sidebarWidth = 300;
28
28
export function initChart ( ) {
29
29
30
30
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" ) ;
32
33
document . body . appendChild ( script ) ;
33
34
34
35
script . addEventListener ( "load" , scriptLoaded , false ) ;
@@ -196,10 +197,13 @@ export async function loadCandles(_resolution, _start, _end, prepend, productOve
196
197
197
198
// console.log('start, end', start, end, new Date(start).toString(), new Date(end).toString());
198
199
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 ) ;
201
202
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 } ` ) ;
203
207
const json = await response . json ( ) ;
204
208
205
209
if ( ! json || ! Array . isArray ( json ) ) {
@@ -212,26 +216,28 @@ export async function loadCandles(_resolution, _start, _end, prepend, productOve
212
216
for ( const item of json ) {
213
217
prepend_set . push ( {
214
218
time : correctedTime ( item [ 0 ] ) ,
215
- low : item [ 1 ] ,
219
+ open : item [ 1 ] ,
216
220
high : item [ 2 ] ,
217
- open : item [ 3 ] ,
221
+ low : item [ 3 ] ,
218
222
close : item [ 4 ]
219
223
} ) ;
220
224
}
221
- prepend_set . reverse ( ) ;
225
+ // If backend returns prices sorted by decreasing timestamp, apply reverse function
226
+ // prepend_set.reverse();
222
227
candles = prepend_set . concat ( candles ) ;
223
228
} else {
224
229
candles = [ ] ;
225
230
for ( const item of json ) {
226
231
candles . push ( {
227
232
time : correctedTime ( item [ 0 ] ) ,
228
- low : item [ 1 ] ,
233
+ open : item [ 1 ] ,
229
234
high : item [ 2 ] ,
230
- open : item [ 3 ] ,
235
+ low : item [ 3 ] ,
231
236
close : item [ 4 ]
232
237
} ) ;
233
238
}
234
- candles . reverse ( ) ;
239
+ // If backend returns prices sorted by decreasing timestamp, apply reverse function
240
+ // candles.reverse();
235
241
}
236
242
237
243
//console.log('data', data);
@@ -260,15 +266,15 @@ export function onNewPrice(price, timestamp, _product) {
260
266
261
267
timestamp = correctedTime ( timestamp / 1000 ) ;
262
268
263
- const resolution = get ( chartResolution ) ;
269
+ const resolution = Number ( get ( chartResolution ) ) ;
264
270
265
271
if ( timestamp >= lastCandle . time + resolution ) {
266
272
// new candle
267
273
let candle = {
268
274
time : parseInt ( resolution * parseInt ( timestamp / resolution ) ) ,
269
- low : price ,
270
- high : price ,
271
275
open : price ,
276
+ high : price ,
277
+ low : price ,
272
278
close : price
273
279
}
274
280
candles . push ( candle ) ;
0 commit comments