move heikin ashi formatter to its own file

This commit is contained in:
Lemuel 2022-03-11 18:57:27 +08:00
parent b786ac3749
commit 7362e8d5e2
4 changed files with 37 additions and 39 deletions

View File

@ -9,7 +9,7 @@ import { Component, Vue, Prop, Watch } from 'vue-property-decorator';
import { Trade, PairHistory, PlotConfig } from '@/types'; import { Trade, PairHistory, PlotConfig } from '@/types';
import randomColor from '@/shared/randomColor'; import randomColor from '@/shared/randomColor';
import { roundTimeframe } from '@/shared/timemath'; import { roundTimeframe } from '@/shared/timemath';
import { heikinAshiDataset } from '@/shared/formatters'; import heikinashi from '@/shared/heikinashi';
import ECharts from 'vue-echarts'; import ECharts from 'vue-echarts';
import { use } from 'echarts/core'; import { use } from 'echarts/core';
@ -353,7 +353,7 @@ export default class CandleChart extends Vue {
const options: EChartsOption = { const options: EChartsOption = {
dataset: { dataset: {
source: this.heikinAshi source: this.heikinAshi
? heikinAshiDataset(this.datasetColumns, this.dataset.data) ? heikinashi(this.datasetColumns, this.dataset.data)
: this.dataset.data, : this.dataset.data,
}, },
grid: [ grid: [

View File

@ -112,41 +112,6 @@ export function humanizeDurationFromSeconds(duration: number): string {
return humanizeDuration(duration * 1000); return humanizeDuration(duration * 1000);
} }
export function heikinAshiDataset(columns: string[], data: Array<number[]>) {
const openIdx = columns.indexOf('open');
const closeIdx = columns.indexOf('close');
const highIdx = columns.indexOf('high');
const lowIdx = columns.indexOf('low');
// Prevent mutation of original data
const dataCopy = data.map((original) => original.slice());
return dataCopy.map((candle, idx, candles) => {
if (idx === 0) {
const close = (candle[openIdx] + candle[highIdx] + candle[lowIdx] + candle[closeIdx]) / 4;
const open = (candle[openIdx] + candle[closeIdx]) / 2;
candle[openIdx] = open;
candle[closeIdx] = close;
return candle;
}
const prevCandle = candles[idx - 1];
const close = (candle[openIdx] + candle[highIdx] + candle[lowIdx] + candle[closeIdx]) / 4;
const open = (prevCandle[openIdx] + prevCandle[closeIdx]) / 2;
const high = Math.max(candle[highIdx], candle[openIdx], candle[closeIdx]);
const low = Math.min(candle[lowIdx], candle[openIdx], candle[closeIdx]);
candle[openIdx] = open;
candle[closeIdx] = close;
candle[highIdx] = high;
candle[lowIdx] = low;
return candle;
});
}
export default { export default {
formatPrice, formatPrice,
formatPercent, formatPercent,
@ -155,5 +120,4 @@ export default {
timestampToDateString, timestampToDateString,
dateStringToTimeRange, dateStringToTimeRange,
setTimezone, setTimezone,
heikinAshiDataset,
}; };

34
src/shared/heikinashi.ts Normal file
View File

@ -0,0 +1,34 @@
export default function heikinAshiDataset(columns: string[], data: Array<number[]>) {
const openIdx = columns.indexOf('open');
const closeIdx = columns.indexOf('close');
const highIdx = columns.indexOf('high');
const lowIdx = columns.indexOf('low');
// Prevent mutation of original data
const dataCopy = data.map((original) => original.slice());
return dataCopy.map((candle, idx, candles) => {
if (idx === 0) {
const close = (candle[openIdx] + candle[highIdx] + candle[lowIdx] + candle[closeIdx]) / 4;
const open = (candle[openIdx] + candle[closeIdx]) / 2;
candle[openIdx] = open;
candle[closeIdx] = close;
return candle;
}
const prevCandle = candles[idx - 1];
const close = (candle[openIdx] + candle[highIdx] + candle[lowIdx] + candle[closeIdx]) / 4;
const open = (prevCandle[openIdx] + prevCandle[closeIdx]) / 2;
const high = Math.max(candle[highIdx], candle[openIdx], candle[closeIdx]);
const low = Math.min(candle[lowIdx], candle[openIdx], candle[closeIdx]);
candle[openIdx] = open;
candle[closeIdx] = close;
candle[highIdx] = high;
candle[lowIdx] = low;
return candle;
});
}

View File

@ -186,7 +186,7 @@ export interface PairHistory {
timeframe: string; timeframe: string;
timeframe_ms: number; timeframe_ms: number;
columns: string[]; columns: string[];
data: number[]; data: Array<number[]>;
length: number; length: number;
/** Number of buy signals in this response */ /** Number of buy signals in this response */
buy_signals: number; buy_signals: number;