Improve memory structure for backtest result

This commit is contained in:
Matthias 2023-07-30 09:47:59 +02:00
parent 46ca166863
commit 094208cfd2
3 changed files with 24 additions and 8 deletions

View File

@ -3,14 +3,15 @@
<h3>Available results:</h3> <h3>Available results:</h3>
<b-list-group class="ms-2"> <b-list-group class="ms-2">
<b-list-group-item <b-list-group-item
v-for="[key, strat] in Object.entries(backtestHistory)" v-for="[key, result] in Object.entries(backtestHistory)"
:key="key" :key="key"
button button
:active="key === selectedBacktestResultKey" :active="key === selectedBacktestResultKey"
class="d-flex justify-content-between align-items-center py-1 pe-1" class="d-flex justify-content-between align-items-center py-1 pe-1"
@click="setBacktestResult(key)" @click="setBacktestResult(key)"
> >
{{ key }} {{ strat.total_trades }} {{ formatPercent(strat.profit_total) }} {{ key }} {{ result.strategy.total_trades }}
{{ formatPercent(result.strategy.profit_total) }}
<b-button <b-button
class="ms-2" class="ms-2"
size="sm" size="sm"
@ -26,12 +27,12 @@
<script setup lang="ts"> <script setup lang="ts">
import { formatPercent } from '@/shared/formatters'; import { formatPercent } from '@/shared/formatters';
import { StrategyBacktestResult } from '@/types'; import { BacktestResultInMemory } from '@/types';
defineProps({ defineProps({
backtestHistory: { backtestHistory: {
required: true, required: true,
type: Object as () => Record<string, StrategyBacktestResult>, type: Object as () => Record<string, BacktestResultInMemory>,
}, },
selectedBacktestResultKey: { required: false, default: '', type: String }, selectedBacktestResultKey: { required: false, default: '', type: String },
}); });

View File

@ -43,6 +43,7 @@ import {
PairlistEvalResponse, PairlistEvalResponse,
PairlistsPayload, PairlistsPayload,
PairlistsResponse, PairlistsResponse,
BacktestResultInMemory,
} from '@/types'; } from '@/types';
import axios, { AxiosResponse } from 'axios'; import axios, { AxiosResponse } from 'axios';
import { defineStore } from 'pinia'; import { defineStore } from 'pinia';
@ -102,7 +103,7 @@ export function createBotSubStore(botId: string, botName: string) {
backtestTradeCount: 0, backtestTradeCount: 0,
backtestResult: undefined as BacktestResult | undefined, backtestResult: undefined as BacktestResult | undefined,
selectedBacktestResultKey: '', selectedBacktestResultKey: '',
backtestHistory: {} as Record<string, StrategyBacktestResult>, backtestHistory: {} as Record<string, BacktestResultInMemory>,
backtestHistoryList: [] as BacktestHistoryEntry[], backtestHistoryList: [] as BacktestHistoryEntry[],
sysInfo: {} as SysInfoResponse, sysInfo: {} as SysInfoResponse,
}; };
@ -114,7 +115,8 @@ export function createBotSubStore(botId: string, botName: string) {
stakeCurrencyDecimals: (state) => state.botState?.stake_currency_decimals || 3, stakeCurrencyDecimals: (state) => state.botState?.stake_currency_decimals || 3,
canRunBacktest: (state) => state.botState?.runmode === RunModes.WEBSERVER, canRunBacktest: (state) => state.botState?.runmode === RunModes.WEBSERVER,
isWebserverMode: (state) => state.botState?.runmode === RunModes.WEBSERVER, isWebserverMode: (state) => state.botState?.runmode === RunModes.WEBSERVER,
selectedBacktestResult: (state) => state.backtestHistory[state.selectedBacktestResultKey], selectedBacktestResult: (state) =>
state.backtestHistory[state.selectedBacktestResultKey]?.strategy || {},
shortAllowed: (state) => state.botState?.short_allowed || false, shortAllowed: (state) => state.botState?.short_allowed || false,
openTradeCount: (state) => state.openTrades.length, openTradeCount: (state) => state.openTrades.length,
isTrading: (state) => isTrading: (state) =>
@ -898,11 +900,16 @@ export function createBotSubStore(botId: string, botName: string) {
this.backtestResult = backtestResult; this.backtestResult = backtestResult;
// TODO: Properly identify duplicates to avoid pushing the same multiple times // TODO: Properly identify duplicates to avoid pushing the same multiple times
Object.entries(backtestResult.strategy).forEach(([key, strat]) => { Object.entries(backtestResult.strategy).forEach(([key, strat]) => {
console.log(key, strat); const metadata = backtestResult.metadata[key];
console.log(key, strat, metadata);
const stratKey = `${key}_${strat.total_trades}_${strat.profit_total.toFixed(3)}`; const stratKey = `${key}_${strat.total_trades}_${strat.profit_total.toFixed(3)}`;
const btResult: BacktestResultInMemory = {
metadata,
strategy: strat,
};
// this.backtestHistory[stratKey] = strat; // this.backtestHistory[stratKey] = strat;
this.backtestHistory = { ...this.backtestHistory, ...{ [stratKey]: strat } }; this.backtestHistory = { ...this.backtestHistory, ...{ [stratKey]: btResult } };
this.selectedBacktestResultKey = stratKey; this.selectedBacktestResultKey = stratKey;
}); });
}, },

View File

@ -188,6 +188,14 @@ export interface BacktestMetadata {
run_id: string; run_id: string;
} }
/**
* Represents the in-memory result of a backtest.
*/
export interface BacktestResultInMemory {
strategy: StrategyBacktestResult;
metadata: BacktestMetadata;
}
export interface BacktestResult { export interface BacktestResult {
strategy: Record<string, StrategyBacktestResult>; strategy: Record<string, StrategyBacktestResult>;
strategy_comparison: Array<Record<string, string | number>>; strategy_comparison: Array<Record<string, string | number>>;