2020-08-24 17:28:46 +00:00
|
|
|
<template>
|
2021-09-04 14:41:44 +00:00
|
|
|
<v-chart v-if="trades" :option="chartOptions" autoresize :theme="getChartTheme" />
|
2020-08-24 17:28:46 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
import { Component, Vue, Prop } from 'vue-property-decorator';
|
2020-12-28 19:34:54 +00:00
|
|
|
import { Getter } from 'vuex-class';
|
2020-08-24 17:28:46 +00:00
|
|
|
|
|
|
|
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';
|
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';
|
|
|
|
const CHART_TRADE_COUNT = 'Trade Count';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
components: {
|
|
|
|
'v-chart': ECharts,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
export default class CumProfitChart extends Vue {
|
2020-08-25 17:52:07 +00:00
|
|
|
@Prop({ required: true }) trades!: ClosedTrade[];
|
2020-08-24 17:28:46 +00:00
|
|
|
|
2020-08-31 15:47:26 +00:00
|
|
|
@Prop({ default: true, type: Boolean }) showTitle!: boolean;
|
|
|
|
|
2020-11-01 12:58:46 +00:00
|
|
|
@Prop({ default: 'close_profit_abs' }) profitColumn!: string;
|
|
|
|
|
2020-12-28 19:34:54 +00:00
|
|
|
@Getter getChartTheme!: string;
|
|
|
|
|
2021-09-04 14:41:44 +00:00
|
|
|
botList: string[] = [];
|
|
|
|
|
2020-08-24 17:28:46 +00:00
|
|
|
get cumulativeData() {
|
2021-09-04 14:41:44 +00:00
|
|
|
this.botList = [];
|
2020-08-29 09:32:26 +00:00
|
|
|
const res: CumProfitData[] = [];
|
2021-09-05 07:30:55 +00:00
|
|
|
const resD: CumProfitDataPerDate = {};
|
2021-01-06 13:51:28 +00:00
|
|
|
const closedTrades = this.trades
|
|
|
|
.slice()
|
|
|
|
.sort((a, b) => (a.close_timestamp > b.close_timestamp ? 1 : -1));
|
2020-08-24 17:28:46 +00:00
|
|
|
let profit = 0.0;
|
2021-09-04 14:41:44 +00:00
|
|
|
|
2020-08-24 17:28:46 +00:00
|
|
|
for (let i = 0, len = closedTrades.length; i < len; i += 1) {
|
|
|
|
const trade = closedTrades[i];
|
2021-09-04 14:41:44 +00:00
|
|
|
|
2020-11-01 12:58:46 +00:00
|
|
|
if (trade.close_timestamp && trade[this.profitColumn]) {
|
|
|
|
profit += trade[this.profitColumn];
|
2021-09-04 14:41:44 +00:00
|
|
|
if (!resD[trade.close_timestamp]) {
|
|
|
|
// New timestamp
|
|
|
|
resD[trade.close_timestamp] = { profit, [trade.botId]: profit };
|
|
|
|
} else {
|
|
|
|
// Add to existing profit
|
2021-10-13 17:33:05 +00:00
|
|
|
resD[trade.close_timestamp].profit += trade[this.profitColumn];
|
2021-09-04 14:41:44 +00:00
|
|
|
if (resD[trade.close_timestamp][trade.botId]) {
|
2021-10-13 17:33:05 +00:00
|
|
|
resD[trade.close_timestamp][trade.botId] += trade[this.profitColumn];
|
2021-09-04 14:41:44 +00:00
|
|
|
} else {
|
|
|
|
resD[trade.close_timestamp][trade.botId] = profit;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:20:02 +00:00
|
|
|
// console.log(trade.close_date, profit);
|
2021-09-04 14:41:44 +00:00
|
|
|
res.push({ date: trade.close_timestamp, profit, [trade.botId]: profit });
|
|
|
|
if (!this.botList.includes(trade.botId)) {
|
|
|
|
this.botList.push(trade.botId);
|
|
|
|
}
|
2020-08-24 17:28:46 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-04 14:41:44 +00:00
|
|
|
// console.log(resD);
|
|
|
|
|
|
|
|
return 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;
|
|
|
|
});
|
2020-08-24 17:28:46 +00:00
|
|
|
}
|
|
|
|
|
2021-05-24 14:39:02 +00:00
|
|
|
get chartOptions(): EChartsOption {
|
2021-09-04 14:41:44 +00:00
|
|
|
const chartOptionsLoc: EChartsOption = {
|
2020-08-24 17:28:46 +00:00
|
|
|
title: {
|
|
|
|
text: 'Cumulative Profit',
|
2020-08-31 15:47:26 +00:00
|
|
|
show: this.showTitle,
|
2020-08-24 17:28:46 +00:00
|
|
|
},
|
2021-05-25 20:06:43 +00:00
|
|
|
backgroundColor: 'rgba(0, 0, 0, 0)',
|
2020-08-24 17:28:46 +00:00
|
|
|
dataset: {
|
|
|
|
dimensions: ['date', 'profit'],
|
|
|
|
source: this.cumulativeData,
|
|
|
|
},
|
|
|
|
tooltip: {
|
|
|
|
trigger: 'axis',
|
|
|
|
axisPointer: {
|
|
|
|
type: 'line',
|
|
|
|
label: {
|
|
|
|
backgroundColor: '#6a7985',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
legend: {
|
|
|
|
data: [CHART_PROFIT],
|
|
|
|
right: '5%',
|
|
|
|
},
|
|
|
|
useUTC: false,
|
|
|
|
xAxis: {
|
|
|
|
type: 'time',
|
|
|
|
},
|
|
|
|
yAxis: [
|
|
|
|
{
|
|
|
|
type: 'value',
|
|
|
|
name: CHART_PROFIT,
|
|
|
|
splitLine: {
|
|
|
|
show: false,
|
|
|
|
},
|
|
|
|
nameRotate: 90,
|
|
|
|
nameLocation: 'middle',
|
2021-02-10 09:30:27 +00:00
|
|
|
nameGap: 40,
|
2020-08-24 17:28:46 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
grid: {
|
|
|
|
bottom: 80,
|
|
|
|
},
|
|
|
|
dataZoom: [
|
|
|
|
{
|
|
|
|
type: 'inside',
|
|
|
|
// xAxisIndex: [0],
|
|
|
|
start: 0,
|
|
|
|
end: 100,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
show: true,
|
|
|
|
// xAxisIndex: [0],
|
|
|
|
type: 'slider',
|
|
|
|
bottom: 10,
|
|
|
|
start: 0,
|
|
|
|
end: 100,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
series: [
|
|
|
|
{
|
|
|
|
type: 'line',
|
|
|
|
name: CHART_PROFIT,
|
2021-01-05 18:29:57 +00:00
|
|
|
animation: true,
|
2021-07-01 04:34:55 +00:00
|
|
|
step: 'end',
|
2020-10-04 17:24:21 +00:00
|
|
|
lineStyle: {
|
2020-12-28 19:34:54 +00:00
|
|
|
color: this.getChartTheme === 'dark' ? '#c2c2c2' : 'black',
|
2020-10-04 17:24:21 +00:00
|
|
|
},
|
|
|
|
itemStyle: {
|
2020-12-28 19:34:54 +00:00
|
|
|
color: this.getChartTheme === 'dark' ? '#c2c2c2' : 'black',
|
2020-10-04 17:24:21 +00:00
|
|
|
},
|
2020-08-24 17:28:46 +00:00
|
|
|
// symbol: 'none',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
2021-09-04 14:41:44 +00:00
|
|
|
// 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: {
|
|
|
|
// color: this.getChartTheme === 'dark' ? '#c2c2c2' : 'black',
|
|
|
|
// },
|
|
|
|
// itemStyle: {
|
|
|
|
// color: this.getChartTheme === 'dark' ? '#c2c2c2' : 'black',
|
|
|
|
// },
|
|
|
|
// // symbol: 'none',
|
|
|
|
// });
|
|
|
|
// });
|
|
|
|
return chartOptionsLoc;
|
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>
|