Extract settings generation, too

This commit is contained in:
Matthias 2023-10-24 19:49:24 +02:00
parent 69f032ac2e
commit bce191e86f
2 changed files with 74 additions and 72 deletions

View File

@ -65,30 +65,19 @@
<script setup lang="ts"> <script setup lang="ts">
import TradeList from '@/components/ftbot/TradeList.vue'; import TradeList from '@/components/ftbot/TradeList.vue';
import { StrategyBacktestResult, Trade } from '@/types'; import { StrategyBacktestResult } from '@/types';
import BacktestResultPeriodBreakdown from './BacktestResultPeriodBreakdown.vue'; import BacktestResultPeriodBreakdown from './BacktestResultPeriodBreakdown.vue';
import { formatObjectForTable } from '@/shared/objectToTableItems'; import { formatObjectForTable } from '@/shared/objectToTableItems';
import { computed } from 'vue'; import { computed } from 'vue';
import { import { formatPercent, formatPrice } from '@/shared/formatters';
timestampms, import { generateBacktestMetricRows, generateBacktestSettingRows } from '@/shared/backtestMetrics';
formatPercent,
formatPrice,
humanizeDurationFromSeconds,
} from '@/shared/formatters';
import { generateBacktestMetricRows } from '@/shared/backtestMetrics';
import { TableField, TableItem } from 'bootstrap-vue-next'; import { TableField, TableItem } from 'bootstrap-vue-next';
const props = defineProps({ const props = defineProps({
backtestResult: { required: true, type: Object as () => StrategyBacktestResult }, backtestResult: { required: true, type: Object as () => StrategyBacktestResult },
}); });
const formatPriceStake = (price) => {
return `${formatPrice(price, props.backtestResult.stake_currency_decimals)} ${
props.backtestResult.stake_currency
}`;
};
const backtestResultStats = computed(() => { const backtestResultStats = computed(() => {
const tmp = generateBacktestMetricRows(props.backtestResult); const tmp = generateBacktestMetricRows(props.backtestResult);
return formatObjectForTable({ value: tmp }, 'metric'); return formatObjectForTable({ value: tmp }, 'metric');
@ -96,61 +85,7 @@ const backtestResultStats = computed(() => {
const backtestResultSettings = computed(() => { const backtestResultSettings = computed(() => {
// Transpose Result into readable format // Transpose Result into readable format
const tmp = [ const tmp = generateBacktestSettingRows(props.backtestResult);
{ 'Backtesting from': timestampms(props.backtestResult.backtest_start_ts) },
{ 'Backtesting to': timestampms(props.backtestResult.backtest_end_ts) },
{
'BT execution time': humanizeDurationFromSeconds(
props.backtestResult.backtest_run_end_ts - props.backtestResult.backtest_run_start_ts,
),
},
{ 'Max open trades': props.backtestResult.max_open_trades },
{ Timeframe: props.backtestResult.timeframe },
{ 'Timeframe Detail': props.backtestResult.timeframe_detail || 'N/A' },
{ Timerange: props.backtestResult.timerange },
{ Stoploss: formatPercent(props.backtestResult.stoploss, 2) },
{ 'Trailing Stoploss': props.backtestResult.trailing_stop },
{
'Trail only when offset is reached': props.backtestResult.trailing_only_offset_is_reached,
},
{ 'Trailing Stop positive': props.backtestResult.trailing_stop_positive },
{
'Trailing stop positive offset': props.backtestResult.trailing_stop_positive_offset,
},
{ 'Custom Stoploss': props.backtestResult.use_custom_stoploss },
{ ROI: props.backtestResult.minimal_roi },
{
'Use Exit Signal':
props.backtestResult.use_exit_signal !== undefined
? props.backtestResult.use_exit_signal
: props.backtestResult.use_sell_signal,
},
{
'Exit profit only':
props.backtestResult.exit_profit_only !== undefined
? props.backtestResult.exit_profit_only
: props.backtestResult.sell_profit_only,
},
{
'Exit profit offset':
props.backtestResult.exit_profit_offset !== undefined
? props.backtestResult.exit_profit_offset
: props.backtestResult.sell_profit_offset,
},
{ 'Enable protections': props.backtestResult.enable_protections },
{
'Starting balance': formatPriceStake(props.backtestResult.starting_balance),
},
{
'Final balance': formatPriceStake(props.backtestResult.final_balance),
},
{
'Avg. stake amount': formatPriceStake(props.backtestResult.avg_stake_amount),
},
{
'Total trade volume': formatPriceStake(props.backtestResult.total_volume),
},
];
return formatObjectForTable({ value: tmp }, 'setting'); return formatObjectForTable({ value: tmp }, 'setting');
}); });

View File

@ -28,15 +28,23 @@ function getWorstPair(trades: Trade[]) {
return `${value.pair} ${formatPercent(value.profit_ratio, 2)}`; return `${value.pair} ${formatPercent(value.profit_ratio, 2)}`;
} }
function useFormatPriceStake(stake_currency_decimals: number, stake_currency: string) {
const formatPriceStake = (price) => {
return `${formatPrice(price, stake_currency_decimals)} ${stake_currency}`;
};
return formatPriceStake;
}
export function generateBacktestMetricRows(result: StrategyBacktestResult) { export function generateBacktestMetricRows(result: StrategyBacktestResult) {
const sortedTrades = getSortedTrades(result.trades); const sortedTrades = getSortedTrades(result.trades);
const bestPair = getBestPair(sortedTrades); const bestPair = getBestPair(sortedTrades);
const worstPair = getWorstPair(sortedTrades); const worstPair = getWorstPair(sortedTrades);
const pairSummary = result.results_per_pair[result.results_per_pair.length - 1]; const pairSummary = result.results_per_pair[result.results_per_pair.length - 1];
const formatPriceStake = (price) => { const formatPriceStake = useFormatPriceStake(
return `${formatPrice(price, result.stake_currency_decimals)} ${result.stake_currency}`; result.stake_currency_decimals,
}; result.stake_currency,
);
// Transpose Result into readable format // Transpose Result into readable format
const shortMetrics = const shortMetrics =
@ -179,3 +187,62 @@ export function generateBacktestMetricRows(result: StrategyBacktestResult) {
]; ];
return tmp; return tmp;
} }
export function generateBacktestSettingRows(result: StrategyBacktestResult) {
const formatPriceStake = useFormatPriceStake(
result.stake_currency_decimals,
result.stake_currency,
);
return [
{ 'Backtesting from': timestampms(result.backtest_start_ts) },
{ 'Backtesting to': timestampms(result.backtest_end_ts) },
{
'BT execution time': humanizeDurationFromSeconds(
result.backtest_run_end_ts - result.backtest_run_start_ts,
),
},
{ 'Max open trades': result.max_open_trades },
{ Timeframe: result.timeframe },
{ 'Timeframe Detail': result.timeframe_detail || 'N/A' },
{ Timerange: result.timerange },
{ Stoploss: formatPercent(result.stoploss, 2) },
{ 'Trailing Stoploss': result.trailing_stop },
{
'Trail only when offset is reached': result.trailing_only_offset_is_reached,
},
{ 'Trailing Stop positive': result.trailing_stop_positive },
{
'Trailing stop positive offset': result.trailing_stop_positive_offset,
},
{ 'Custom Stoploss': result.use_custom_stoploss },
{ ROI: result.minimal_roi },
{
'Use Exit Signal':
result.use_exit_signal !== undefined ? result.use_exit_signal : result.use_sell_signal,
},
{
'Exit profit only':
result.exit_profit_only !== undefined ? result.exit_profit_only : result.sell_profit_only,
},
{
'Exit profit offset':
result.exit_profit_offset !== undefined
? result.exit_profit_offset
: result.sell_profit_offset,
},
{ 'Enable protections': result.enable_protections },
{
'Starting balance': formatPriceStake(result.starting_balance),
},
{
'Final balance': formatPriceStake(result.final_balance),
},
{
'Avg. stake amount': formatPriceStake(result.avg_stake_amount),
},
{
'Total trade volume': formatPriceStake(result.total_volume),
},
];
}