From 509d55f699a2ffba833167f1eee781a0e569cd16 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Tue, 25 Jul 2023 11:56:19 +0300 Subject: [PATCH 1/3] Remove cumulative weighted price --- test/1-api.spec.js | 1 - workers/loc.api/generate-csv/csv.job.data.js | 1 - workers/loc.api/weighted.averages.report/index.js | 11 ----------- 3 files changed, 13 deletions(-) diff --git a/test/1-api.spec.js b/test/1-api.spec.js index af0f449c..7758f754 100644 --- a/test/1-api.spec.js +++ b/test/1-api.spec.js @@ -1804,7 +1804,6 @@ describe('API', () => { 'buyingAmount', 'sellingWeightedPrice', 'sellingAmount', - 'cumulativeWeightedPrice', 'cumulativeAmount', 'firstTradeMts', 'lastTradeMts' diff --git a/workers/loc.api/generate-csv/csv.job.data.js b/workers/loc.api/generate-csv/csv.job.data.js index 84d053b5..4cf1941a 100644 --- a/workers/loc.api/generate-csv/csv.job.data.js +++ b/workers/loc.api/generate-csv/csv.job.data.js @@ -1063,7 +1063,6 @@ class CsvJobData { buyingAmount: 'AMOUNT', sellingWeightedPrice: 'WEIGHTED PRICE', sellingAmount: 'AMOUNT', - cumulativeWeightedPrice: 'WEIGHTED PRICE', cumulativeAmount: 'AMOUNT', firstTradeMts: 'First Trade', lastTradeMts: 'Last Trade' diff --git a/workers/loc.api/weighted.averages.report/index.js b/workers/loc.api/weighted.averages.report/index.js index 9bd42a61..a589c574 100644 --- a/workers/loc.api/weighted.averages.report/index.js +++ b/workers/loc.api/weighted.averages.report/index.js @@ -113,9 +113,7 @@ class WeightedAveragesReport { const { tradeCount, - sumBuyingSpent = 0, sumBuyingAmount = 0, - sumSellingSpent = 0, sumSellingAmount = 0, buyingWeightedPrice = 0, sellingWeightedPrice = 0, @@ -128,9 +126,6 @@ class WeightedAveragesReport { } const cumulativeAmount = sumBuyingAmount + sumSellingAmount - const cumulativeWeightedPrice = cumulativeAmount === 0 - ? 0 - : (sumBuyingSpent + sumSellingSpent) / cumulativeAmount weightedAverages.push({ symbol, @@ -138,7 +133,6 @@ class WeightedAveragesReport { buyingAmount: sumBuyingAmount, sellingWeightedPrice, sellingAmount: sumSellingAmount, - cumulativeWeightedPrice, cumulativeAmount, firstTradeMts, lastTradeMts @@ -328,9 +322,6 @@ class WeightedAveragesReport { ? 0 : sumSellingSpent / sumSellingAmount, sellingAmount: sumSellingAmount, - cumulativeWeightedPrice: sumAmount === 0 - ? 0 - : sumSpent / sumAmount, cumulativeAmount: sumAmount }) } @@ -341,7 +332,6 @@ class WeightedAveragesReport { buyingAmount = 0, sellingWeightedPrice = 0, sellingAmount = 0, - cumulativeWeightedPrice = 0, cumulativeAmount = 0 } = val ?? {} @@ -357,7 +347,6 @@ class WeightedAveragesReport { buyingAmount, sellingWeightedPrice, sellingAmount, - cumulativeWeightedPrice, cumulativeAmount, firstTradeMts, lastTradeMts From d1d219b2849b7004086c526527bd3b21f6858c8d Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Tue, 25 Jul 2023 12:29:48 +0300 Subject: [PATCH 2/3] Add cost and sale columns to weighted averages report --- test/1-api.spec.js | 2 ++ workers/loc.api/generate-csv/csv.job.data.js | 2 ++ .../loc.api/weighted.averages.report/index.js | 29 ++++++++++++++----- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/test/1-api.spec.js b/test/1-api.spec.js index 7758f754..5cec7710 100644 --- a/test/1-api.spec.js +++ b/test/1-api.spec.js @@ -1805,6 +1805,8 @@ describe('API', () => { 'sellingWeightedPrice', 'sellingAmount', 'cumulativeAmount', + 'cost', + 'sale', 'firstTradeMts', 'lastTradeMts' ]) diff --git a/workers/loc.api/generate-csv/csv.job.data.js b/workers/loc.api/generate-csv/csv.job.data.js index 4cf1941a..87306639 100644 --- a/workers/loc.api/generate-csv/csv.job.data.js +++ b/workers/loc.api/generate-csv/csv.job.data.js @@ -1064,6 +1064,8 @@ class CsvJobData { sellingWeightedPrice: 'WEIGHTED PRICE', sellingAmount: 'AMOUNT', cumulativeAmount: 'AMOUNT', + cost: 'COST', + sale: 'SALE', firstTradeMts: 'First Trade', lastTradeMts: 'Last Trade' }, diff --git a/workers/loc.api/weighted.averages.report/index.js b/workers/loc.api/weighted.averages.report/index.js index a589c574..432b4aa2 100644 --- a/workers/loc.api/weighted.averages.report/index.js +++ b/workers/loc.api/weighted.averages.report/index.js @@ -126,6 +126,8 @@ class WeightedAveragesReport { } const cumulativeAmount = sumBuyingAmount + sumSellingAmount + const cost = buyingWeightedPrice * sumBuyingAmount + const sale = sellingWeightedPrice * sumSellingAmount weightedAverages.push({ symbol, @@ -134,6 +136,8 @@ class WeightedAveragesReport { sellingWeightedPrice, sellingAmount: sumSellingAmount, cumulativeAmount, + cost, + sale, firstTradeMts, lastTradeMts }) @@ -306,6 +310,13 @@ class WeightedAveragesReport { ? _sumSellingAmount + execAmount : _sumSellingAmount + const buyingWeightedPrice = sumBuyingAmount === 0 + ? 0 + : sumBuyingSpent / sumBuyingAmount + const sellingWeightedPrice = sumSellingAmount === 0 + ? 0 + : sumSellingSpent / sumSellingAmount + symbResMap.set(symbol, { sumSpent, sumAmount, @@ -314,15 +325,13 @@ class WeightedAveragesReport { sumSellingSpent, sumSellingAmount, - buyingWeightedPrice: sumBuyingAmount === 0 - ? 0 - : sumBuyingSpent / sumBuyingAmount, + buyingWeightedPrice, buyingAmount: sumBuyingAmount, - sellingWeightedPrice: sumSellingAmount === 0 - ? 0 - : sumSellingSpent / sumSellingAmount, + sellingWeightedPrice, sellingAmount: sumSellingAmount, - cumulativeAmount: sumAmount + cumulativeAmount: sumAmount, + cost: buyingWeightedPrice * sumBuyingAmount, + sale: sellingWeightedPrice * sumSellingAmount }) } @@ -332,7 +341,9 @@ class WeightedAveragesReport { buyingAmount = 0, sellingWeightedPrice = 0, sellingAmount = 0, - cumulativeAmount = 0 + cumulativeAmount = 0, + cost = 0, + sale = 0 } = val ?? {} const tradesBySymbol = trades.filter((t) => t.symbol === symbol) @@ -348,6 +359,8 @@ class WeightedAveragesReport { sellingWeightedPrice, sellingAmount, cumulativeAmount, + cost, + sale, firstTradeMts, lastTradeMts } From 1b5a0a4ce98d774af9ff6a3f3fb9e2bb80e64e40 Mon Sep 17 00:00:00 2001 From: Vladimir Voronkov Date: Tue, 25 Jul 2023 12:46:56 +0300 Subject: [PATCH 3/3] Change weighted averages report csv field order --- test/1-api.spec.js | 4 ++-- .../weighted-averages-report-csv-writer.js | 2 +- workers/loc.api/generate-csv/csv.job.data.js | 4 ++-- .../loc.api/weighted.averages.report/index.js | 20 +++++++++---------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/test/1-api.spec.js b/test/1-api.spec.js index 5cec7710..8fa38b9e 100644 --- a/test/1-api.spec.js +++ b/test/1-api.spec.js @@ -1802,11 +1802,11 @@ describe('API', () => { 'symbol', 'buyingWeightedPrice', 'buyingAmount', + 'cost', 'sellingWeightedPrice', 'sellingAmount', - 'cumulativeAmount', - 'cost', 'sale', + 'cumulativeAmount', 'firstTradeMts', 'lastTradeMts' ]) diff --git a/workers/loc.api/generate-csv/csv-writer/weighted-averages-report-csv-writer.js b/workers/loc.api/generate-csv/csv-writer/weighted-averages-report-csv-writer.js index 662e8842..86960164 100644 --- a/workers/loc.api/generate-csv/csv-writer/weighted-averages-report-csv-writer.js +++ b/workers/loc.api/generate-csv/csv-writer/weighted-averages-report-csv-writer.js @@ -43,7 +43,7 @@ module.exports = ( wStream.setMaxListeners(50) const headerStringifier = stringify( - { columns: ['empty', 'buy', 'empty', 'sell', 'empty', 'cumulative', 'empty'] } + { columns: ['empty', 'buy', 'empty', 'empty', 'sell', 'empty', 'empty', 'cumulative', 'empty'] } ) const resStringifier = stringify({ header: true, diff --git a/workers/loc.api/generate-csv/csv.job.data.js b/workers/loc.api/generate-csv/csv.job.data.js index 87306639..9be0f766 100644 --- a/workers/loc.api/generate-csv/csv.job.data.js +++ b/workers/loc.api/generate-csv/csv.job.data.js @@ -1061,11 +1061,11 @@ class CsvJobData { symbol: 'PAIR', buyingWeightedPrice: 'WEIGHTED PRICE', buyingAmount: 'AMOUNT', + cost: 'COST', sellingWeightedPrice: 'WEIGHTED PRICE', sellingAmount: 'AMOUNT', - cumulativeAmount: 'AMOUNT', - cost: 'COST', sale: 'SALE', + cumulativeAmount: 'AMOUNT', firstTradeMts: 'First Trade', lastTradeMts: 'Last Trade' }, diff --git a/workers/loc.api/weighted.averages.report/index.js b/workers/loc.api/weighted.averages.report/index.js index 432b4aa2..512f5fd2 100644 --- a/workers/loc.api/weighted.averages.report/index.js +++ b/workers/loc.api/weighted.averages.report/index.js @@ -133,11 +133,11 @@ class WeightedAveragesReport { symbol, buyingWeightedPrice, buyingAmount: sumBuyingAmount, + cost, sellingWeightedPrice, sellingAmount: sumSellingAmount, - cumulativeAmount, - cost, sale, + cumulativeAmount, firstTradeMts, lastTradeMts }) @@ -327,11 +327,11 @@ class WeightedAveragesReport { buyingWeightedPrice, buyingAmount: sumBuyingAmount, + cost: buyingWeightedPrice * sumBuyingAmount, sellingWeightedPrice, sellingAmount: sumSellingAmount, - cumulativeAmount: sumAmount, - cost: buyingWeightedPrice * sumBuyingAmount, - sale: sellingWeightedPrice * sumSellingAmount + sale: sellingWeightedPrice * sumSellingAmount, + cumulativeAmount: sumAmount }) } @@ -339,11 +339,11 @@ class WeightedAveragesReport { const { buyingWeightedPrice = 0, buyingAmount = 0, + cost = 0, sellingWeightedPrice = 0, sellingAmount = 0, - cumulativeAmount = 0, - cost = 0, - sale = 0 + sale = 0, + cumulativeAmount = 0 } = val ?? {} const tradesBySymbol = trades.filter((t) => t.symbol === symbol) @@ -356,11 +356,11 @@ class WeightedAveragesReport { symbol, buyingWeightedPrice, buyingAmount, + cost, sellingWeightedPrice, sellingAmount, - cumulativeAmount, - cost, sale, + cumulativeAmount, firstTradeMts, lastTradeMts }