2020-08-24 17:28:46 +00:00
|
|
|
<template>
|
2022-04-19 18:07:59 +00:00
|
|
|
<v-chart v-if="trades" :option="chartOptions" autoresize :theme="settingsStore.chartTheme" />
|
2020-08-24 17:28:46 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
import ECharts from 'vue-echarts';
|
2021-05-24 14:39:02 +00:00
|
|
|
import { EChartsOption } from 'echarts';
|
2020-08-24 17:28:46 +00:00
|
|
|
|
2021-05-24 15:45:20 +00:00
|
|
|
import { use } from 'echarts/core';
|
|
|
|
import { CanvasRenderer } from 'echarts/renderers';
|
|
|
|
import { LineChart, BarChart } from 'echarts/charts';
|
2021-08-08 17:55:25 +00:00
|
|
|
import {
|
|
|
|
DatasetComponent,
|
|
|
|
DataZoomComponent,
|
|
|
|
LegendComponent,
|
|
|
|
TitleComponent,
|
|
|
|
TooltipComponent,
|
|
|
|
} from 'echarts/components';
|
2020-08-24 17:28:46 +00:00
|
|
|
|
2021-09-05 07:30:55 +00:00
|
|
|
import { ClosedTrade, CumProfitData, CumProfitDataPerDate } from '@/types';
|
2022-08-13 13:35:31 +00:00
|
|
|
import { defineComponent, computed, ComputedRef } from 'vue';
|
2022-04-19 18:07:59 +00:00
|
|
|
import { useSettingsStore } from '@/stores/settings';
|
2023-04-19 18:35:41 +00:00
|
|
|
import { dataZoomPartial } from '@/shared/charts/chartZoom';
|
2020-08-24 17:28:46 +00:00
|
|
|
|
2021-08-08 17:55:25 +00:00
|
|
|
use([
|
|
|
|
BarChart,
|
|
|
|
LineChart,
|
|
|
|
|
|
|
|
CanvasRenderer,
|
|
|
|
|
|
|
|
DatasetComponent,
|
|
|
|
DataZoomComponent,
|
|
|
|
LegendComponent,
|
|
|
|
TitleComponent,
|
|
|
|
TooltipComponent,
|
|
|
|
]);
|
2021-05-24 15:45:20 +00:00
|
|
|
|
2020-08-24 17:28:46 +00:00
|
|
|
// Define Column labels here to avoid typos
|
|
|
|
const CHART_PROFIT = 'Profit';
|
|
|
|
|
2022-04-17 07:00:28 +00:00
|
|
|
export default defineComponent({
|
|
|
|
name: 'CumProfitChart',
|
2020-08-24 17:28:46 +00:00
|
|
|
components: {
|
|
|
|
'v-chart': ECharts,
|
|
|
|
},
|
2022-04-17 07:00:28 +00:00
|
|
|
props: {
|
|
|
|
trades: { required: true, type: Array as () => ClosedTrade[] },
|
|
|
|
showTitle: { default: true, type: Boolean },
|
2023-02-27 20:34:05 +00:00
|
|
|
profitColumn: { default: 'profit_abs', type: String },
|
2022-04-17 07:00:28 +00:00
|
|
|
},
|
|
|
|
setup(props) {
|
2022-04-19 18:07:59 +00:00
|
|
|
const settingsStore = useSettingsStore();
|
2022-08-13 13:35:31 +00:00
|
|
|
// const botList = ref<string[]>([]);
|
|
|
|
// const cumulativeData = ref<{ date: number; profit: any }[]>([]);
|
2022-04-17 07:00:28 +00:00
|
|
|
|
2022-08-13 13:35:31 +00:00
|
|
|
const cumulativeData: ComputedRef<{ date: number; profit: number }[]> = computed(() => {
|
2022-07-30 05:09:48 +00:00
|
|
|
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;
|
2022-04-17 07:00:28 +00:00
|
|
|
|
2022-07-30 05:09:48 +00:00
|
|
|
for (let i = 0, len = closedTrades.length; i < len; i += 1) {
|
|
|
|
const trade = closedTrades[i];
|
2022-04-21 04:37:57 +00:00
|
|
|
|
2022-07-30 05:09:48 +00:00
|
|
|
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];
|
2022-04-17 07:00:28 +00:00
|
|
|
} else {
|
2022-07-30 05:09:48 +00:00
|
|
|
resD[trade.close_timestamp][trade.botId] = profit;
|
2022-04-17 07:00:28 +00:00
|
|
|
}
|
2021-09-04 14:41:44 +00:00
|
|
|
}
|
2022-07-30 05:09:48 +00:00
|
|
|
res.push({ date: trade.close_timestamp, profit, [trade.botId]: profit });
|
2021-09-04 14:41:44 +00:00
|
|
|
}
|
2022-07-30 05:09:48 +00:00
|
|
|
}
|
|
|
|
// console.log(resD);
|
2022-04-17 07:00:28 +00:00
|
|
|
|
2022-08-13 13:35:31 +00:00
|
|
|
return Object.entries(resD).map(([k, v]) => {
|
2022-07-30 05:09:48 +00:00
|
|
|
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;
|
|
|
|
});
|
2022-08-13 13:35:31 +00:00
|
|
|
});
|
2020-08-24 17:28:46 +00:00
|
|
|
|
2022-04-17 07:00:28 +00:00
|
|
|
const chartOptions = computed((): EChartsOption => {
|
|
|
|
const chartOptionsLoc: EChartsOption = {
|
|
|
|
title: {
|
|
|
|
text: 'Cumulative Profit',
|
|
|
|
show: props.showTitle,
|
|
|
|
},
|
|
|
|
backgroundColor: 'rgba(0, 0, 0, 0)',
|
|
|
|
dataset: {
|
|
|
|
dimensions: ['date', 'profit'],
|
|
|
|
source: cumulativeData.value,
|
2020-08-24 17:28:46 +00:00
|
|
|
},
|
2022-04-17 07:00:28 +00:00
|
|
|
tooltip: {
|
|
|
|
trigger: 'axis',
|
|
|
|
axisPointer: {
|
|
|
|
type: 'line',
|
|
|
|
label: {
|
|
|
|
backgroundColor: '#6a7985',
|
|
|
|
},
|
2020-08-24 17:28:46 +00:00
|
|
|
},
|
|
|
|
},
|
2022-04-17 07:00:28 +00:00
|
|
|
legend: {
|
|
|
|
data: [CHART_PROFIT],
|
|
|
|
right: '5%',
|
2020-08-24 17:28:46 +00:00
|
|
|
},
|
2022-04-17 07:00:28 +00:00
|
|
|
useUTC: false,
|
|
|
|
xAxis: {
|
|
|
|
type: 'time',
|
2020-08-24 17:28:46 +00:00
|
|
|
},
|
2022-04-17 07:00:28 +00:00
|
|
|
yAxis: [
|
|
|
|
{
|
|
|
|
type: 'value',
|
|
|
|
name: CHART_PROFIT,
|
|
|
|
splitLine: {
|
|
|
|
show: false,
|
|
|
|
},
|
|
|
|
nameRotate: 90,
|
|
|
|
nameLocation: 'middle',
|
|
|
|
nameGap: 40,
|
2020-10-04 17:24:21 +00:00
|
|
|
},
|
2022-04-17 07:00:28 +00:00
|
|
|
],
|
|
|
|
grid: {
|
|
|
|
bottom: 80,
|
2020-08-24 17:28:46 +00:00
|
|
|
},
|
2022-04-17 07:00:28 +00:00
|
|
|
dataZoom: [
|
|
|
|
{
|
|
|
|
type: 'inside',
|
|
|
|
// xAxisIndex: [0],
|
|
|
|
start: 0,
|
2023-04-19 18:35:41 +00:00
|
|
|
|
2022-04-17 07:00:28 +00:00
|
|
|
end: 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// xAxisIndex: [0],
|
|
|
|
bottom: 10,
|
|
|
|
start: 0,
|
|
|
|
end: 100,
|
2023-04-19 18:35:41 +00:00
|
|
|
...dataZoomPartial,
|
2022-04-17 07:00:28 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
series: [
|
|
|
|
{
|
|
|
|
type: 'line',
|
|
|
|
name: CHART_PROFIT,
|
|
|
|
animation: true,
|
|
|
|
step: 'end',
|
|
|
|
lineStyle: {
|
2022-04-19 18:07:59 +00:00
|
|
|
color: settingsStore.chartTheme === 'dark' ? '#c2c2c2' : 'black',
|
2022-04-17 07:00:28 +00:00
|
|
|
},
|
|
|
|
itemStyle: {
|
2022-04-19 18:07:59 +00:00
|
|
|
color: settingsStore.chartTheme === 'dark' ? '#c2c2c2' : 'black',
|
2022-04-17 07:00:28 +00:00
|
|
|
},
|
|
|
|
// symbol: 'none',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
// TODO: maybe have profit lines per bot?
|
|
|
|
// this.botList.forEach((botId: string) => {
|
|
|
|
// console.log('bot', botId);
|
|
|
|
// chartOptionsLoc.series.push({
|
|
|
|
// type: 'line',
|
|
|
|
// name: botId,
|
|
|
|
// animation: true,
|
|
|
|
// step: 'end',
|
|
|
|
// lineStyle: {
|
2022-04-19 18:07:59 +00:00
|
|
|
// color: settingsStore.chartTheme === 'dark' ? '#c2c2c2' : 'black',
|
2022-04-17 07:00:28 +00:00
|
|
|
// },
|
2022-04-19 18:07:59 +00:00
|
|
|
// itemStylesettingsStore.chartTheme === 'dark' ? '#c2c2c2' : 'black',
|
2022-04-17 07:00:28 +00:00
|
|
|
// },
|
|
|
|
// // symbol: 'none',
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
return chartOptionsLoc;
|
|
|
|
});
|
|
|
|
|
2022-04-19 18:07:59 +00:00
|
|
|
return { settingsStore, cumulativeData, chartOptions };
|
2022-04-17 07:00:28 +00:00
|
|
|
},
|
|
|
|
});
|
2020-08-24 17:28:46 +00:00
|
|
|
</script>
|
|
|
|
|
2020-08-25 17:45:35 +00:00
|
|
|
<style scoped>
|
|
|
|
.echarts {
|
|
|
|
width: 100%;
|
|
|
|
height: 100%;
|
2021-05-26 19:00:39 +00:00
|
|
|
min-height: 150px;
|
2020-08-25 17:45:35 +00:00
|
|
|
}
|
|
|
|
</style>
|