Improve backtesting logic

This commit is contained in:
Matthias 2021-02-28 20:05:07 +01:00
parent afbaa41044
commit efa49e494d
4 changed files with 12 additions and 25 deletions

View File

@ -22,7 +22,7 @@ import { StrategyBacktestResult } from '@/types';
import { Component, Emit, Prop, Vue } from 'vue-property-decorator';
@Component({})
export default class Template extends Vue {
export default class BacktestResultSelect extends Vue {
@Prop({ required: true }) backtestHistory!: StrategyBacktestResult[];
@Prop({ required: false, default: '' }) selectedBacktestResultKey!: string;

View File

@ -112,7 +112,7 @@ export default {
[BotStoreGetters.refreshRequired](state: FtbotStateType): boolean {
return state.refreshRequired;
},
[BotStoreGetters.selectedBacktestResult](state): StrategyBacktestResult {
[BotStoreGetters.selectedBacktestResult](state: FtbotStateType): StrategyBacktestResult {
return state.backtestHistory[state.selectedBacktestResultKey];
},
},
@ -202,9 +202,12 @@ export default {
state.backtestResult = backtestResult;
// TODO: Properly identify duplicates to avoid pushing the same multiple times
Object.entries(backtestResult.strategy).forEach(([key, strat]) => {
const xxx = `${key}_${strat.total_trades}_${strat.profit_total.toFixed(3)}`;
state.backtestHistory[xxx] = strat;
state.selectedBacktestResultKey = xxx;
console.log(key, strat);
const stratKey = `${key}_${strat.total_trades}_${strat.profit_total.toFixed(3)}`;
// state.backtestHistory[stratKey] = strat;
state.backtestHistory = { ...state.backtestHistory, ...{ [stratKey]: strat } };
state.selectedBacktestResultKey = stratKey;
});
},
resetBacktestHistory(state: FtbotStateType) {
@ -608,18 +611,7 @@ export default {
const result = await api.get('/backtest');
commit('updateBacktestRunning', result.data.running);
if (result.data.running === false && result.data.backtest_result) {
// TODO: This should be aligned in the backend, which would allow us to remove this whole block
const backtestresult = result.data.backtest_result;
for (let i = 0, len = backtestresult.strategy_comparison.length; i < len; i += 1) {
const { key } = backtestresult.strategy_comparison[i];
for (let j = 0, len = backtestresult.strategy[key].trades.length; j < len; j += 1) {
// eslint-disable-next-line @typescript-eslint/camelcase
backtestresult.strategy[key].trades[j].profit_ratio =
backtestresult.strategy[key].trades[j].profit_percent;
}
}
commit('updateBacktestResult', backtestresult);
commit('updateBacktestResult', result.data.backtest_result);
}
},
async removeBacktest({ commit }) {

View File

@ -11,6 +11,7 @@ import {
PlotConfigStorage,
ProfitInterface,
BacktestResult,
StrategyBacktestResult,
} from '@/types';
export interface FtbotStateType {
@ -45,7 +46,7 @@ export interface FtbotStateType {
backtestRunning: boolean;
backtestResult?: BacktestResult;
selectedBacktestResultKey: string;
backtestHistory: Record<string, BacktestResult>;
backtestHistory: Record<string, StrategyBacktestResult>;
}
const state: FtbotStateType = {
version: '',

View File

@ -227,13 +227,7 @@ import CumProfitChart from '@/components/charts/CumProfitChart.vue';
import TradesLogChart from '@/components/charts/TradesLog.vue';
import PairSummary from '@/components/ftbot/PairSummary.vue';
import {
BacktestPayload,
BacktestResult,
PairHistoryPayload,
PlotConfig,
StrategyBacktestResult,
} from '@/types';
import { BacktestPayload, PairHistoryPayload, PlotConfig, StrategyBacktestResult } from '@/types';
import { getCustomPlotConfig, getPlotConfigName } from '@/shared/storage';
import { formatPercent } from '@/shared/formatters';