let pricePrecision = 1; let priceStep = 1; let quantityPrecision = 1; for (let index =…
- User
let pricePrecision = 1; let priceStep = 1; let quantityPrecision = 1;
for (let index = 0; index <= rowsCount; index++) { const microPrice = startMicroPrice - index * zoomedTickSize;
if (microPrice < 0) continue; maxVolume = Math.max(maxVolume, rows[microPrice]?.bid || 0, rows[microPrice]?.ask || 0);}
ask, bid, microPrice указаны как целочисленные. Чтобы их перевести в реальные цифры нужно добавить запятую в соответствии с pricePrecision и quantityPrecision
- Assistant
let pricePrecision = 1; let priceStep = 1; let quantityPrecision = 1;
for (let index = 0; index <= rowsCount; index++) { const microPrice = (startMicroPrice - index * zoomedTickSize) / Math.pow(10, pricePrecision);
if (microPrice < 0) continue;
const bid = rows[microPrice]?.bid / Math.pow(10, pricePrecision); const ask = rows[microPrice]?.ask / Math.pow(10, pricePrecision);
maxVolume = Math.max(maxVolume, bid || 0, ask || 0); }
- User
Object is possibly 'undefined'.ts(2532) (property) bid?: number | undefined
- Assistant
The
Object is possibly 'undefined'error is occurring because therows[microPrice]object may be undefined. To fix this error, you can add a check to ensure that the object exists before accessing its properties.Here's an updated code snippet that includes the check:
let pricePrecision = 1; let priceStep = 1; let quantityPrecision = 1; for (let index = 0; index <= rowsCount; index++) { const microPrice = (startMicroPrice - index * zoomedTickSize) / Math.pow(10, pricePrecision); if (microPrice < 0) continue; const row = rows[microPrice]; const bid = row?.bid / Math.pow(10, pricePrecision); const ask = row?.ask / Math.pow(10, pricePrecision); maxVolume = Math.max(maxVolume, bid || 0, ask || 0); }In this code, we store the
rows[microPrice]object in a separate variable calledrow. Then we can safely access thebidandaskproperties using the optional chaining operator?.. This will prevent the error from occurring if therowobject is undefined. - User
'row.bid' is possibly 'undefined'.ts(18048) (property) bid?: number | undefined
- Assistant
The
row.bidproperty may be undefined, which is causing therow.bid is possibly undefinederror. To handle this, you can add an extra check to ensure that thebidproperty exists before performing any operations on it.Here's an updated code snippet that includes the check:
let pricePrecision = 1; let priceStep = 1; let quantityPrecision = 1; for (let index = 0; index <= rowsCount; index++) { const microPrice = (startMicroPrice - index * zoomedTickSize) / Math.pow(10, pricePrecision); if (microPrice < 0) continue; const row = rows[microPrice]; const bid = row?.bid; const ask = row?.ask; if (typeof bid === 'number' && typeof ask === 'number') { const bidValue = bid / Math.pow(10, pricePrecision); const askValue = ask / Math.pow(10, pricePrecision); maxVolume = Math.max(maxVolume, bidValue, askValue); } }In this code, we first assign the
row.bidandrow.askproperties to separate variables (bidand ). Then we check if and are valid numbers using the operator. If they are valid numbers, we proceed with performing operations on them. - User
rows[microPrice] = cup[microPrice] || {}; const bid = row?.bid * microPrice / priceDivider; const ask = row?.ask * microPrice / priceDivider;
maxVolume = Math.max(maxVolume, (ask || bid || 0) / quantityDivider);'row.bid' is possibly 'undefined'.ts(18048) (property) bid?: number | undefined
- Assistant
Apologies for the confusion in my previous responses. To properly handle the possibility of
row.bidbeingundefined, you can update the code as follows:let pricePrecision = 1; let priceStep = 1; let quantityPrecision = 1; for (let index = 0; index <= rowsCount; index++) { const microPrice = (startMicroPrice - index * zoomedTickSize) / Math.pow(10, pricePrecision); if (microPrice < 0) continue; rows[microPrice] = rows[microPrice] || {}; const row = rows[microPrice]; const bid = row.bid !== undefined ? row.bid * microPrice / priceDivider : undefined; const ask = row.ask !== undefined ? row.ask * microPrice / priceDivider : undefined; maxVolume = Math.max(maxVolume, (ask || bid || 0) / quantityDivider); }In this updated code, I've made the following changes:
- User
draw: ( ctx: CanvasRenderingContext2D, size: CanvasSize, dpiScale: number, bestBidPrice: number, bestAskPrice: number, maxVolume: number, pricePrecision: number, quantityPrecision: number, priceStep: number, zoom: number, rowCount: number, camera: number, cellHeight: number, zoomedBestMicroPrice: any, darkMode: boolean, cup: {[key: number]: CupItem, dollarsVolume: boolean}, ) => { const quantity = ((side === "bid" ? item?.bid || 0 : item?.ask || 0) / quantityDivider) * (microPrice / priceDivider);
если dollarsVolume то делать умножение с (microPrice / priceDivider);, иначе const quantity = (side === "bid" ? item?.bid || 0 : item?.ask || 0) / quantityDivider
- Assistant
Based on your code snippet, it seems that you have a conditional requirement to calculate the
quantitydifferently based on whetherdollarsVolumeis true or false.Here's an updated
drawfunction that includes the necessary logic:draw: ( ctx: CanvasRenderingContext2D, size: CanvasSize, dpiScale: number, bestBidPrice: number, bestAskPrice: number, maxVolume: number, pricePrecision: number, quantityPrecision: number, priceStep: number, zoom: number, rowCount: number, camera: number, cellHeight: number, zoomedBestMicroPrice: any, darkMode: boolean, cup: { [key: number]: CupItem }, dollarsVolume: boolean ) => { // Other code... const quantityDivider = Math.pow(10, quantityPrecision); const priceDivider = Math.pow(10, pricePrecision); // Other code... const quantity = dollarsVolume ? (side === "bid" ? (item?.bid || 0) : (item?.ask || 0)) * microPrice / priceDivider : (side === "bid" ? (item?.bid || 0) : (item?.ask || 0)) / quantityDivider; // Other code... }; - User
cupDrawer.draw( context, canvasSize, dpiScale, event.data.bestBidPrice, event.data.bestAskPrice, event.data.maxVolume, event.data.pricePrecision, event.data.quantityPrecision, event.data.priceStep, event.data.aggregation, rowsOnScreenCount, event.data.camera, realCellHeight, { buy: parseInt((Math.floor(event.data.bestBidPrice / zoomedTickSize) * zoomedTickSize).toFixed(0)), sell: parseInt((Math.ceil(event.data.bestAskPrice / zoomedTickSize) * zoomedTickSize).toFixed(0)), }, darkMode, event.data.cup, settings.dollarsVolume, );
const cupDrawer = { clear: (ctx: CanvasRenderingContext2D, size: CanvasSize) => { ctx.clearRect(0, 0, size.width, size.height); }, draw: ( ctx: CanvasRenderingContext2D, size: CanvasSize, dpiScale: number, bestBidPrice: number, bestAskPrice: number, maxVolume: number, pricePrecision: number, quantityPrecision: number, priceStep: number, zoom: number, rowCount: number, camera: number, cellHeight: number, zoomedBestMicroPrice: any, darkMode: boolean, cup: {[key: number]: CupItem, dollarsVolume: boolean}, ) => {
- Assistant
Based on the provided code snippets, it seems that the
dollarsVolumevariable is not defined within thedrawfunction scope. To fix this issue, you should include thedollarsVolumeparameter in the function signature and use it accordingly. - User
если dollarsVolume то maxVolume = Math.max(maxVolume, (ask || bid || 0) / quantityDivider) * (microPrice / priceDivider); если нет, то * (microPrice / priceDivider) делать не нужно
- Assistant
I see that you have updated the condition for calculating
maxVolumebased ondollarsVolume. You can modify the code as follows:const quantity = dollarsVolume ? ((side === "bid" ? (item?.bid || 0) : (item?.ask || 0)) / quantityDivider) * (microPrice / priceDivider) : (side === "bid" ? (item?.bid || 0) : (item?.ask || 0)) / quantityDivider; maxVolume = dollarsVolume ? Math.max(maxVolume, (ask || bid || 0) / quantityDivider) * (microPrice / priceDivider) : Math.max(maxVolume, (ask || bid || 0) / quantityDivider);In this updated code, the
quantitycalculation uses the condition to determine whether to multiply by(microPrice / priceDivider).Similarly, the
maxVolumecalculation is now conditioned ondollarsVolume. IfdollarsVolumeistrue, it performs the multiplication of(microPrice / priceDivider). IfdollarsVolumeisfalse, it calculatesmaxVolumewithout the multiplication.Remember to ensure that , , , , and are correctly defined before using them in the calculation. Also, make sure that and variables are defined earlier in your code if they are used in this context.