tradingview / charting-library-tutorial

This tutorial explains step by step how to connect your data to the Charting Library
MIT License
397 stars 306 forks source link

getBars are returning wrong periodParams #75

Open lucasbara opened 1 year ago

lucasbara commented 1 year ago

Hello,

I'm having the following problem, when the user selects a custom range in a TradingView chart, the getBars function it's getting wrong periodParams(from, to)

getBars: async ({ exchange, full_name, name, session, timezone }, resolution, { firstDataRequest, from, to }, onResult, _onError) => { const isUSExchange = usExchanges.includes(exchange); const websocket = isUSExchange ? obookWebSocket : multiWebSocket; if (firstDataRequest) { const { currency: currency_code, isin } = await getStockInfoOnClient(exchange, name); currency = currency_code; ISIN = isin; id = name; } switch (resolution) { case "1D": const res = await getChartData(Number(full_name), dayjs.unix(from).format("YYYY-MM-DD"), dayjs.unix(to).format("YYYY-MM-DD")); const beforeMarketOpenDiff = getCurrentSessionInterval(session.split(","), timezone) !== -1 ? 0 : dayjs().day() === 1 ? 3 : 1; if ( firstDataRequest && !dayjs .utc(res[res.length - 1]?.[0]) .add(beforeMarketOpenDiff, "days") .isSame(dayjs.utc(), "day") ) { onHistoryResult = null; current = { time: undefined, open: undefined, high: undefined, low: undefined, close: undefined }; updateHistory = isUSExchange ? ({ data }) => { const parsedData = JSON.parse(data); const LT: [{ o: number; h: number; l: number; c: number }, number][] = parsedData.LT; if (LT) { const [openRaw, closeRaw] = getMainSession(session.split(",")).split("-"); const open = dayjs.tz(openRaw, "HHmm", timezone).subtract(beforeMarketOpenDiff, "days"); const close = dayjs.tz(closeRaw, "HHmm", timezone).subtract(beforeMarketOpenDiff, "days"); const hasOldData = dayjs(LT[0][1]).isBefore(open); current = (hasOldData ? LT.filter(([_, time]) => dayjs(time).isBetween(open, close)) : LT).reduce<Current>((prev, [{ o, h, l, c }, time]) => { return { time, open: prev.open ?? o, high: Math.max(prev.high ?? h, h), low: Math.min(prev.low ?? l, l), close: c, }; }, current); } } : ({ data }) => { const parsedData = JSON.parse(data); const ts: [number, number, number, number, number][] = parsedData.ts; if (parsedData.ISIN === ISIN && ts) { const currentDate = dayjs(); current = ts .filter(([time]) => dayjs(time).isSame(currentDate, "day")) .reduce<Current>( (prev, [time, open, high, low, close]) => ({ time, open: prev.open ?? open, high: Math.max(prev.high ?? high, high), low: Math.min(prev.low ?? low, low), close, }), current ); } }; websocket.addEventListener("message", updateHistory); const onOpenTS = isUSExchange ? onOpenUSTS : onOpenDailyTS; websocket.readyState === 1 ? onOpenTS() : websocket.addEventListener("open", onOpenTS); } onResult( res.map(([time, open, high, low, close]) => ({ time, open, high, low, close })), { noData: !res.length && dayjs.unix(to).diff(dayjs.unix(from), "days") > 1 } ); break; case "1": if (firstDataRequest) { onHistoryResult = onResult; websocket.addEventListener("message", isUSExchange ? tsUSMessage : tsMessage); websocket.removeEventListener("message", updateHistory); const onOpenTS = isUSExchange ? onOpenUSTS : onOpenWeeklyTS; if (websocket.readyState === 1) onOpenTS(); else { websocket.addEventListener("open", onOpenTS); onResult([], { noData: true }); } } else onResult([], { noData: true }); break; } },

image