From 20d3b80d4182eb62d84a99098aa31d96b9f33c8e Mon Sep 17 00:00:00 2001 From: Matthias Date: Sat, 30 Jul 2022 07:09:48 +0200 Subject: [PATCH] Fix cumProfitChart being empty part of #879 --- src/components/charts/CumProfitChart.vue | 81 ++++++++++++------------ 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/src/components/charts/CumProfitChart.vue b/src/components/charts/CumProfitChart.vue index 10f6333e..783ae58b 100644 --- a/src/components/charts/CumProfitChart.vue +++ b/src/components/charts/CumProfitChart.vue @@ -52,53 +52,54 @@ export default defineComponent({ const botList = ref([]); const cumulativeData = ref<{ date: number; profit: any }[]>([]); - watch( - () => props.trades, - () => { - botList.value = []; - const res: CumProfitData[] = []; - const resD: CumProfitDataPerDate = {}; - const closedTrades = props.trades - .slice() - .sort((a, b) => (a.close_timestamp > b.close_timestamp ? 1 : -1)); - let profit = 0.0; + const calcCumulativeData = () => { + console.log('trades'); + botList.value = []; + const res: CumProfitData[] = []; + const resD: CumProfitDataPerDate = {}; + const closedTrades = props.trades + .slice() + .sort((a, b) => (a.close_timestamp > b.close_timestamp ? 1 : -1)); + let profit = 0.0; - for (let i = 0, len = closedTrades.length; i < len; i += 1) { - const trade = closedTrades[i]; + for (let i = 0, len = closedTrades.length; i < len; i += 1) { + const trade = closedTrades[i]; - if (trade.close_timestamp && trade[props.profitColumn]) { - profit += trade[props.profitColumn]; - if (!resD[trade.close_timestamp]) { - // New timestamp - resD[trade.close_timestamp] = { profit, [trade.botId]: profit }; + if (trade.close_timestamp && trade[props.profitColumn]) { + profit += trade[props.profitColumn]; + if (!resD[trade.close_timestamp]) { + // New timestamp + resD[trade.close_timestamp] = { profit, [trade.botId]: profit }; + } else { + // Add to existing profit + resD[trade.close_timestamp].profit += trade[props.profitColumn]; + if (resD[trade.close_timestamp][trade.botId]) { + resD[trade.close_timestamp][trade.botId] += trade[props.profitColumn]; } else { - // Add to existing profit - resD[trade.close_timestamp].profit += trade[props.profitColumn]; - if (resD[trade.close_timestamp][trade.botId]) { - resD[trade.close_timestamp][trade.botId] += trade[props.profitColumn]; - } else { - resD[trade.close_timestamp][trade.botId] = profit; - } - } - // console.log(trade.close_date, profit); - res.push({ date: trade.close_timestamp, profit, [trade.botId]: profit }); - if (!botList.value.includes(trade.botId)) { - botList.value.push(trade.botId); + resD[trade.close_timestamp][trade.botId] = profit; } } + // console.log(trade.close_date, profit); + res.push({ date: trade.close_timestamp, profit, [trade.botId]: profit }); + if (!botList.value.includes(trade.botId)) { + botList.value.push(trade.botId); + } } - // console.log(resD); + } + // console.log(resD); - cumulativeData.value = Object.entries(resD).map(([k, v]) => { - const obj = { date: parseInt(k, 10), profit: v.profit }; - // TODO: The below could allow "lines" per bot" - // this.botList.forEach((botId) => { - // obj[botId] = v[botId]; - // }); - return obj; - }); - }, - ); + cumulativeData.value = Object.entries(resD).map(([k, v]) => { + const obj = { date: parseInt(k, 10), profit: v.profit }; + // TODO: The below could allow "lines" per bot" + // this.botList.forEach((botId) => { + // obj[botId] = v[botId]; + // }); + return obj; + }); + }; + watch([props.trades], calcCumulativeData); + // Initial call + calcCumulativeData(); const chartOptions = computed((): EChartsOption => { const chartOptionsLoc: EChartsOption = {