frequi_origin/src/components/ftbot/CandleChart.vue

383 lines
10 KiB
Vue
Raw Normal View History

2020-06-17 18:38:25 +00:00
<template>
<div class="container-fluid d-flex flex-column align-items-stretch d-flex">
2020-06-19 18:37:14 +00:00
<div class="row flex-grow-1 chart-wrapper">
2020-06-17 18:38:25 +00:00
<v-chart v-if="hasData" theme="dark" autoresize :options="chartOptions" />
</div>
</div>
</template>
<script>
2020-06-20 19:18:48 +00:00
import { timestampms } from '@/shared/formatters';
2020-06-17 18:38:25 +00:00
import ECharts from 'vue-echarts';
2020-06-23 19:18:43 +00:00
import randomColor from '../../shared/randomColor';
2020-06-17 18:38:25 +00:00
import 'echarts';
2020-06-20 06:57:24 +00:00
const MARGINLEFT = '5%';
2020-06-20 18:17:46 +00:00
const MARGINRIGHT = '4%';
2020-06-20 06:35:22 +00:00
2020-06-17 18:38:25 +00:00
export default {
name: 'CandleChart',
components: { 'v-chart': ECharts },
2020-06-20 06:52:43 +00:00
props: {
pair: {
type: String,
2020-06-20 06:57:24 +00:00
required: true,
2020-06-20 06:52:43 +00:00
},
timeframe: {
type: String,
2020-06-20 06:57:24 +00:00
required: true,
},
dataset: {
type: Object,
required: true,
2020-06-20 06:52:43 +00:00
},
2020-06-23 05:09:58 +00:00
plotConfig: {
type: Object,
required: false,
default() {
return {};
},
},
2020-06-20 06:52:43 +00:00
},
2020-06-17 18:38:25 +00:00
data() {
2020-06-20 06:52:43 +00:00
return {};
2020-06-17 18:38:25 +00:00
},
computed: {
hasData() {
2020-06-23 18:07:24 +00:00
return this.dataset !== null && typeof this.dataset === 'object';
2020-06-17 18:38:25 +00:00
},
2020-06-20 06:57:24 +00:00
2020-06-17 18:38:25 +00:00
chartOptions() {
if (!this.hasData) {
return {};
}
2020-06-19 05:55:34 +00:00
const upColor = '#00da3c';
const upBorderColor = '#008F28';
const downColor = '#ec0000';
const downBorderColor = '#8A0000';
2020-06-19 18:37:14 +00:00
console.log(`Available Columns: ${this.dataset.columns}`);
2020-06-20 06:35:22 +00:00
// Find default columns (sequence might be different, depending on the strategy)
2020-06-19 18:37:14 +00:00
const colDate = this.dataset.columns.findIndex((el) => el === 'date');
const colOpen = this.dataset.columns.findIndex((el) => el === 'open');
const colHigh = this.dataset.columns.findIndex((el) => el === 'high');
const colLow = this.dataset.columns.findIndex((el) => el === 'low');
const colClose = this.dataset.columns.findIndex((el) => el === 'close');
const colVolume = this.dataset.columns.findIndex((el) => el === 'volume');
const colBuy = this.dataset.columns.findIndex((el) => el === 'buy');
const colSell = this.dataset.columns.findIndex((el) => el === 'sell');
2020-06-20 06:35:22 +00:00
// Plot data
2020-06-19 18:37:14 +00:00
const buyData = [];
const sellData = [];
2020-06-20 06:35:22 +00:00
const subPlots = {
legend: [],
grid: [],
yaxis: [],
xaxis: [],
xaxisIndex: [],
series: [],
};
2020-06-23 05:13:23 +00:00
if ('main_plot' in this.plotConfig) {
Object.entries(this.plotConfig.main_plot).forEach(([key, value]) => {
const col = this.dataset.columns.findIndex((el) => el === key);
subPlots.legend.push(key);
const sp = {
name: key,
type: 'line',
xAxisIndex: 0,
yAxisIndex: 0,
itemStyle: {
color: value.color,
},
encode: {
x: colDate,
y: col,
},
2020-06-23 18:07:24 +00:00
showSymbol: false,
2020-06-23 05:13:23 +00:00
};
subPlots.series.push(sp);
});
}
2020-06-23 05:09:58 +00:00
if ('subplots' in this.plotConfig) {
2020-06-20 06:35:22 +00:00
let plotIndex = 2;
2020-06-23 05:09:58 +00:00
Object.entries(this.plotConfig.subplots).forEach(([key, value]) => {
2020-06-20 06:35:22 +00:00
// define yaxis
subPlots.yaxis.push({
scale: true,
gridIndex: plotIndex,
name: key,
nameLocation: 'middle',
nameGap: 60,
axisLabel: { show: true },
axisLine: { show: false },
axisTick: { show: false },
splitLine: { show: false },
});
subPlots.xaxis.push({
scale: true,
gridIndex: plotIndex,
boundaryGap: false,
axisLine: { onZero: false },
axisTick: { show: false },
splitLine: { show: false },
axisLabel: { show: false },
splitNumber: 20,
});
subPlots.xaxisIndex.push(plotIndex);
subPlots.grid.push({
left: MARGINLEFT,
2020-06-20 06:57:24 +00:00
right: MARGINRIGHT,
2020-06-20 06:35:22 +00:00
bottom: `${(plotIndex - 1) * 50}px`,
height: '8%',
});
Object.entries(value).forEach(([sk, sv]) => {
2020-06-23 19:18:43 +00:00
subPlots.legend.push(sk);
2020-06-20 06:35:22 +00:00
// entries per subplot
const col = this.dataset.columns.findIndex((el) => el === sk);
if (col) {
const sp = {
2020-06-23 19:18:43 +00:00
name: sk,
2020-06-20 06:35:22 +00:00
type: 'line',
xAxisIndex: plotIndex,
yAxisIndex: plotIndex,
itemStyle: {
2020-06-23 19:18:43 +00:00
color: sv.color || randomColor(),
2020-06-20 06:35:22 +00:00
},
encode: {
x: colDate,
y: col,
},
2020-06-23 18:07:24 +00:00
showSymbol: false,
2020-06-20 06:35:22 +00:00
};
subPlots.series.push(sp);
console.log(subPlots);
} else {
console.log(`element ${sk} was not found in the columns.`);
}
});
plotIndex += 1;
});
}
2020-06-19 18:37:14 +00:00
// Generate Buy and sell array (using open rate to display marker)
for (let i = 0, len = this.dataset.data.length; i < len; i += 1) {
if (this.dataset.data[i][colBuy] === 1) {
buyData.push([this.dataset.data[i][colDate], this.dataset.data[i][colOpen]]);
}
if (this.dataset.data[i][colSell] === 1) {
sellData.push([this.dataset.data[i][colDate], this.dataset.data[i][colOpen]]);
}
}
2020-06-20 06:35:22 +00:00
// console.log(this.dataset.data);
2020-06-17 18:38:25 +00:00
return {
title: {
text: `${this.pair} - ${this.timeframe}`,
show: true,
},
2020-06-20 06:35:22 +00:00
backgroundColor: '#231202D',
2020-06-17 18:38:25 +00:00
dataset: {
source: this.dataset.data,
},
animation: false,
legend: {
2020-06-20 06:35:22 +00:00
data: ['Candles', 'Volume', 'Buy', 'Sell', ...subPlots.legend],
2020-06-17 18:38:25 +00:00
},
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'cross',
lineStyle: {
2020-06-19 05:55:34 +00:00
color: '#cccccc',
width: 1,
2020-06-17 18:38:25 +00:00
opacity: 1,
},
},
},
axisPointer: {
link: { xAxisIndex: 'all' },
label: {
backgroundColor: '#777',
},
},
xAxis: [
{
type: 'category',
scale: true,
boundaryGap: false,
axisLine: { onZero: false },
splitLine: { show: false },
splitNumber: 20,
min: 'dataMin',
max: 'dataMax',
2020-06-20 19:18:48 +00:00
axisLabel: {
formatter: (value) => {
return timestampms(value);
},
},
2020-06-17 18:38:25 +00:00
},
{
type: 'category',
gridIndex: 1,
scale: true,
boundaryGap: false,
axisLine: { onZero: false },
axisTick: { show: false },
splitLine: { show: false },
axisLabel: { show: false },
splitNumber: 20,
min: 'dataMin',
max: 'dataMax',
},
2020-06-20 06:35:22 +00:00
...subPlots.xaxis,
2020-06-17 18:38:25 +00:00
],
yAxis: [
{
scale: true,
},
{
scale: true,
gridIndex: 1,
splitNumber: 2,
axisLabel: { show: false },
axisLine: { show: false },
axisTick: { show: false },
splitLine: { show: false },
},
2020-06-20 06:35:22 +00:00
...subPlots.yaxis,
2020-06-17 18:38:25 +00:00
],
grid: [
{
2020-06-20 06:35:22 +00:00
left: MARGINLEFT,
2020-06-20 06:57:24 +00:00
right: MARGINRIGHT,
2020-06-20 18:17:46 +00:00
height: '60%',
// top: '0px',
// bottom: '150px',
2020-06-17 18:38:25 +00:00
},
{
2020-06-20 06:35:22 +00:00
// Volume
left: MARGINLEFT,
2020-06-20 06:57:24 +00:00
right: MARGINRIGHT,
2020-06-20 06:35:22 +00:00
bottom: '20%',
2020-06-19 18:37:14 +00:00
height: '80px',
2020-06-17 18:38:25 +00:00
},
2020-06-20 06:35:22 +00:00
...subPlots.grid,
2020-06-17 18:38:25 +00:00
],
dataZoom: [
{
type: 'inside',
2020-06-20 06:35:22 +00:00
xAxisIndex: [0, 1, ...subPlots.xaxisIndex],
2020-06-19 05:55:34 +00:00
start: 50,
2020-06-17 18:38:25 +00:00
end: 100,
},
{
show: true,
2020-06-20 06:35:22 +00:00
xAxisIndex: [0, 1, ...subPlots.xaxisIndex],
2020-06-17 18:38:25 +00:00
type: 'slider',
bottom: 10,
start: 10,
end: 100,
},
],
// visualMap: {
2020-06-19 05:55:34 +00:00
// // TODO: this would allow to colorize volume bars (if we'd want this)
// // Needs green / red indicator column in data.
2020-06-17 18:38:25 +00:00
// show: true,
2020-06-19 05:55:34 +00:00
// seriesIndex: 1,
// dimension: 5,
2020-06-17 18:38:25 +00:00
// pieces: [
// {
2020-06-19 05:55:34 +00:00
// max: 500000.0,
2020-06-17 18:38:25 +00:00
// color: downColor,
// },
// {
2020-06-19 05:55:34 +00:00
// min: 500000.0,
2020-06-17 18:38:25 +00:00
// color: upColor,
// },
// ],
// },
series: [
{
name: 'Candles',
type: 'candlestick',
itemStyle: {
color: upColor,
color0: downColor,
borderColor: upBorderColor,
borderColor0: downBorderColor,
},
encode: {
x: 0,
// open, close, low, high
2020-06-19 18:37:14 +00:00
y: [colOpen, colClose, colLow, colHigh],
2020-06-17 18:38:25 +00:00
},
},
{
name: 'Volume',
type: 'bar',
xAxisIndex: 1,
yAxisIndex: 1,
2020-06-19 05:55:34 +00:00
itemStyle: {
color: '#777777',
},
2020-06-17 18:38:25 +00:00
large: true,
encode: {
x: 0,
2020-06-19 18:37:14 +00:00
y: colVolume,
},
},
{
name: 'Buy',
type: 'scatter',
symbol: 'triangle',
data: buyData,
xAxisIndex: 0,
yAxisIndex: 0,
itemStyle: {
color: '#00FF00',
},
encode: {
x: 0,
y: 1,
},
},
{
name: 'Sell',
type: 'scatter',
data: sellData,
symbol: 'diamond',
xAxisIndex: 0,
yAxisIndex: 0,
itemStyle: {
color: '#FF0000',
},
encode: {
x: 0,
y: 1,
2020-06-17 18:38:25 +00:00
},
},
2020-06-20 06:35:22 +00:00
...subPlots.series,
2020-06-17 18:38:25 +00:00
],
};
},
},
};
</script>
<style scoped>
2020-06-19 18:37:14 +00:00
.chart-wrapper {
width: 100%;
height: 100%;
}
2020-06-17 18:38:25 +00:00
.echarts {
width: 100%;
2020-06-19 18:37:14 +00:00
min-height: 820px;
2020-06-17 18:38:25 +00:00
/* TODO: height calculation is not working correctly - uses min-height for now */
/* height: 600px; */
height: 100%;
}
</style>