frequi_origin/src/views/Backtesting.vue

517 lines
15 KiB
Vue
Raw Normal View History

2020-07-26 11:44:34 +00:00
<template>
2021-07-04 17:57:19 +00:00
<div class="container-fluid" style="max-height: calc(100vh - 60px)">
2021-07-04 14:08:43 +00:00
<div class="container-fluid">
<div class="row mb-2"></div>
<p v-if="!canRunBacktest">Bot must be in webserver mode to enable Backtesting.</p>
2021-07-04 14:08:43 +00:00
<div class="row w-100">
<h2 class="col-4 col-lg-3">Backtesting</h2>
<div
class="
col-12 col-lg-order-last col-lg-6
mx-md-5
d-flex
flex-wrap
justify-content-md-center justify-content-between
mb-4
"
:disabled="canRunBacktest"
2020-12-05 15:32:44 +00:00
>
2021-07-04 14:08:43 +00:00
<b-form-radio
v-model="btFormMode"
name="bt-form-radios"
button
class="mx-1 flex-samesize-items"
value="run"
:disabled="!canRunBacktest"
>Run backtest</b-form-radio
>
<b-form-radio
v-model="btFormMode"
name="bt-form-radios"
button
class="mx-1 flex-samesize-items"
value="results"
:disabled="!hasBacktestResult"
>Analyze result</b-form-radio
>
<b-form-radio
v-model="btFormMode"
name="bt-form-radios"
button
class="mx-1 flex-samesize-items"
value="visualize-summary"
:disabled="!hasBacktestResult"
>Visualize summary</b-form-radio
>
<b-form-radio
v-model="btFormMode"
name="bt-form-radios"
button
class="mx-1 flex-samesize-items"
value="visualize"
:disabled="!hasBacktestResult"
>Visualize result</b-form-radio
>
</div>
<small v-show="backtestRunning" class="text-right bt-running-label col-8 col-lg-3"
>Backtest running: {{ backtestStep }} {{ formatPercent(backtestProgress, 2) }}</small
2020-12-05 15:32:44 +00:00
>
</div>
2021-03-11 18:17:09 +00:00
</div>
2021-06-25 18:00:40 +00:00
2021-05-23 14:25:31 +00:00
<div class="d-md-flex">
2021-03-11 18:17:09 +00:00
<!-- Left bar -->
<div
2021-05-23 14:25:31 +00:00
:class="`${showLeftBar ? 'col-md-3' : ''} sticky-top sticky-offset mr-3 d-flex flex-column`"
2021-03-11 18:17:09 +00:00
>
<b-button
2021-06-25 18:00:40 +00:00
v-if="btFormMode !== 'visualize'"
2021-03-11 18:17:09 +00:00
class="align-self-start"
aria-label="Close"
size="sm"
@click="showLeftBar = !showLeftBar"
>{{ showLeftBar ? '&lt;' : '&gt;' }}</b-button
>
<transition name="fade" mode="in-out">
<BacktestResultSelect
v-if="btFormMode !== 'visualize' && showLeftBar"
:backtest-history="backtestHistory"
:selected-backtest-result-key="selectedBacktestResultKey"
@selectionChange="setBacktestResult"
/>
</transition>
2021-03-11 18:17:09 +00:00
</div>
<!-- End Left bar -->
2020-09-25 04:22:34 +00:00
2021-07-06 04:56:41 +00:00
<div v-if="btFormMode == 'run'" class="flex-fill row d-flex flex-column bt-config">
2021-05-24 09:34:48 +00:00
<div class="mb-2">
<span>Strategy</span>
<StrategySelect v-model="strategy"></StrategySelect>
</div>
2021-07-06 04:56:41 +00:00
<b-card bg-variant="light" :disabled="backtestRunning">
2020-12-05 15:32:44 +00:00
<!-- Backtesting parameters -->
2020-09-25 04:22:34 +00:00
<b-form-group
2020-12-05 15:32:44 +00:00
label-cols-lg="2"
label="Backtest params"
label-size="sm"
label-class="font-weight-bold pt-0"
class="mb-0"
2020-07-27 05:19:45 +00:00
>
<b-form-group
label-cols-sm="5"
label="Timeframe:"
label-align-sm="right"
label-for="timeframe-select"
>
<TimeframeSelect id="timeframe-select" v-model="selectedTimeframe" />
</b-form-group>
<b-form-group
label-cols-sm="5"
label="Detail Timeframe:"
label-align-sm="right"
label-for="timeframe-detail-select"
title="Detail timeframe, to simulate intra-candle results. Not setting this will not use this functionality."
>
<TimeframeSelect
id="timeframe-detail-select"
v-model="selectedDetailTimeframe"
:below-timeframe="selectedTimeframe"
/>
</b-form-group>
2020-12-05 15:32:44 +00:00
<b-form-group
label-cols-sm="5"
label="Max open trades:"
label-align-sm="right"
label-for="max-open-trades"
>
<b-form-input
id="max-open-trades"
2020-12-08 18:35:06 +00:00
v-model="maxOpenTrades"
2020-12-05 15:32:44 +00:00
placeholder="Use strategy default"
type="number"
></b-form-input>
</b-form-group>
<b-form-group
label-cols-sm="5"
label="Starting capital:"
label-align-sm="right"
label-for="starting-capital"
>
<b-form-input
id="starting-capital"
v-model="startingCapital"
type="number"
step="0.001"
></b-form-input>
</b-form-group>
2020-12-05 15:32:44 +00:00
<b-form-group
label-cols-sm="5"
label="Stake amount:"
label-align-sm="right"
label-for="stake-amount"
>
2021-02-28 08:53:51 +00:00
<div class="d-flex">
2021-03-10 15:09:55 +00:00
<b-form-checkbox
id="stake-amount-bool"
v-model="stakeAmountUnlimited"
class="col-md-6"
2021-02-28 08:53:51 +00:00
>Unlimited stake</b-form-checkbox
>
<b-form-input
id="stake-amount"
v-model="stakeAmount"
type="number"
placeholder="Use strategy default"
step="0.01"
:disabled="stakeAmountUnlimited"
></b-form-input>
</div>
2020-12-05 15:32:44 +00:00
</b-form-group>
2021-01-06 19:25:38 +00:00
<b-form-group
label-cols-sm="5"
label="Enable Protections:"
label-align-sm="right"
label-for="enable-protections"
>
<b-form-checkbox
id="enable-protections"
v-model="enableProtections"
></b-form-checkbox>
</b-form-group>
2020-12-08 18:35:06 +00:00
<!-- <b-form-group label-cols-sm="5" label="Fee:" label-align-sm="right" label-for="fee">
2020-12-05 15:32:44 +00:00
<b-form-input
id="fee"
type="number"
placeholder="Use exchange default"
step="0.01"
></b-form-input>
2020-12-08 18:35:06 +00:00
</b-form-group> -->
<hr />
<TimeRangeSelect v-model="timerange" class="mt-2"></TimeRangeSelect>
2020-09-25 04:22:34 +00:00
</b-form-group>
2020-12-05 15:32:44 +00:00
</b-card>
2021-01-06 12:13:02 +00:00
<h3 class="mt-3">Backtesting summary</h3>
2021-05-24 09:34:48 +00:00
<div
class="d-flex flex-wrap flex-md-nowrap justify-content-between justify-content-md-center"
>
2021-01-06 12:13:02 +00:00
<b-button
variant="primary"
:disabled="backtestRunning || !canRunBacktest"
2021-01-06 12:13:02 +00:00
class="mx-1"
@click="clickBacktest"
>
2020-12-05 15:32:44 +00:00
Start backtest
</b-button>
2021-01-06 12:13:02 +00:00
<b-button
variant="primary"
:disabled="backtestRunning || !canRunBacktest"
2021-01-06 12:13:02 +00:00
class="mx-1"
@click="pollBacktest"
>
2020-12-05 15:32:44 +00:00
Load backtest result
</b-button>
2021-04-05 17:59:14 +00:00
<b-button
variant="primary"
class="mx-1"
:disabled="!backtestRunning"
@click="stopBacktest"
2021-04-24 14:51:28 +00:00
>Stop Backtest</b-button
2021-04-05 17:59:14 +00:00
>
<b-button
variant="primary"
class="mx-1"
:disabled="backtestRunning || !canRunBacktest"
@click="removeBacktest"
>Reset Backtest</b-button
>
2020-12-05 15:32:44 +00:00
</div>
2020-09-25 04:22:34 +00:00
</div>
2021-03-11 18:17:09 +00:00
<BacktestResultView
v-if="hasBacktestResult && btFormMode == 'results'"
:backtest-result="selectedBacktestResult"
class="flex-fill"
2020-11-01 12:58:16 +00:00
/>
2021-03-11 18:17:09 +00:00
<div
v-if="hasBacktestResult && btFormMode == 'visualize-summary'"
class="text-center flex-fill mt-2 d-flex flex-column"
>
<TradesLogChart :trades="selectedBacktestResult.trades" class="trades-log" />
<CumProfitChart
:trades="selectedBacktestResult.trades"
profit-column="profit_abs"
class="cum-profit"
:show-title="true"
/>
</div>
2020-11-01 12:58:16 +00:00
</div>
2021-03-11 18:17:09 +00:00
2020-12-05 16:42:22 +00:00
<div
v-if="hasBacktestResult && btFormMode == 'visualize'"
2021-06-25 18:00:40 +00:00
class="container-fluid text-center w-100 mt-2"
2020-12-05 16:42:22 +00:00
>
2021-06-25 18:00:40 +00:00
<p class="row">
2021-01-20 06:55:56 +00:00
Graph will always show the latest values for the selected strategy. Strategy: {{ strategy }}
</p>
2021-06-25 18:00:40 +00:00
<div class="row text-center">
2021-01-15 19:41:32 +00:00
<PairSummary
2021-07-04 17:57:19 +00:00
class="col-md-2 overflow-auto"
style="max-height: calc(100vh - 200px)"
2021-01-15 19:41:32 +00:00
:pairlist="selectedBacktestResult.pairlist"
:trades="selectedBacktestResult.trades"
sort-method="profit"
/>
<CandleChartContainer
:available-pairs="selectedBacktestResult.pairlist"
:historic-view="!!true"
:timeframe="timeframe"
:plot-config="selectedPlotConfig"
:timerange="timerange"
:strategy="strategy"
:trades="selectedBacktestResult.trades"
2021-07-04 17:57:19 +00:00
class="col-md-10 candle-chart-container px-0 w-100 h-100"
2021-01-15 19:41:32 +00:00
>
</CandleChartContainer>
</div>
2020-07-26 17:35:56 +00:00
</div>
2020-07-26 11:44:34 +00:00
</div>
</template>
<script lang="ts">
import { Component, Vue, Watch } from 'vue-property-decorator';
import { namespace } from 'vuex-class';
import TimeRangeSelect from '@/components/ftbot/TimeRangeSelect.vue';
2020-07-26 17:35:56 +00:00
import BacktestResultView from '@/components/ftbot/BacktestResultView.vue';
2021-01-20 06:55:56 +00:00
import BacktestResultSelect from '@/components/ftbot/BacktestResultSelect.vue';
2020-09-12 17:21:35 +00:00
import CandleChartContainer from '@/components/charts/CandleChartContainer.vue';
2021-05-24 09:13:11 +00:00
import StrategySelect from '@/components/ftbot/StrategySelect.vue';
2021-03-10 15:34:09 +00:00
import ValuePair from '@/components/general/ValuePair.vue';
2020-11-01 12:58:16 +00:00
import CumProfitChart from '@/components/charts/CumProfitChart.vue';
2021-01-06 13:51:04 +00:00
import TradesLogChart from '@/components/charts/TradesLog.vue';
2020-12-05 16:42:22 +00:00
import PairSummary from '@/components/ftbot/PairSummary.vue';
2021-05-23 14:54:38 +00:00
import TimeframeSelect from '@/components/ftbot/TimeframeSelect.vue';
2020-07-26 17:35:56 +00:00
import {
BacktestPayload,
BotState,
PairHistoryPayload,
PlotConfig,
StrategyBacktestResult,
} from '@/types';
2020-07-27 05:19:45 +00:00
import { getCustomPlotConfig, getPlotConfigName } from '@/shared/storage';
2021-01-14 07:04:28 +00:00
import { formatPercent } from '@/shared/formatters';
2021-01-15 19:41:32 +00:00
import { BotStoreGetters } from '@/store/modules/ftbot';
2020-07-26 11:44:34 +00:00
const ftbot = namespace('ftbot');
@Component({
2020-11-01 12:47:57 +00:00
components: {
BacktestResultView,
2021-01-20 06:55:56 +00:00
BacktestResultSelect,
2020-11-01 12:47:57 +00:00
TimeRangeSelect,
CandleChartContainer,
2020-11-01 12:58:16 +00:00
CumProfitChart,
2021-01-06 13:51:04 +00:00
TradesLogChart,
2021-05-24 09:13:11 +00:00
StrategySelect,
2020-11-01 12:47:57 +00:00
ValuePair,
2020-12-05 16:42:22 +00:00
PairSummary,
2021-05-23 14:54:38 +00:00
TimeframeSelect,
2020-11-01 12:47:57 +00:00
},
2020-07-26 11:44:34 +00:00
})
2020-07-26 17:35:56 +00:00
export default class Backtesting extends Vue {
pollInterval: number | null = null;
2020-07-26 11:44:34 +00:00
2021-03-11 18:20:12 +00:00
showLeftBar = false;
2021-03-11 18:17:09 +00:00
2021-01-02 14:10:10 +00:00
selectedTimeframe = '';
selectedDetailTimeframe = '';
2020-09-25 04:22:34 +00:00
strategy = '';
2021-01-06 19:25:38 +00:00
timerange = '';
enableProtections = false;
maxOpenTrades = '';
2021-02-28 08:53:51 +00:00
stakeAmountUnlimited = false;
2021-01-06 19:25:38 +00:00
stakeAmount = '';
startingCapital = '';
2020-12-05 15:32:44 +00:00
btFormMode = 'run';
2020-07-26 17:35:56 +00:00
2020-07-27 05:19:45 +00:00
selectedPlotConfig: PlotConfig = getCustomPlotConfig(getPlotConfigName());
2020-07-26 11:44:34 +00:00
@ftbot.State backtestRunning!: boolean;
2021-03-11 18:20:12 +00:00
@ftbot.State backtestStep!: string;
@ftbot.State botState!: BotState;
2021-03-11 18:20:12 +00:00
@ftbot.State backtestProgress!: number;
2021-01-14 07:04:28 +00:00
@ftbot.State backtestHistory!: StrategyBacktestResult[];
2021-01-16 09:56:31 +00:00
@ftbot.State selectedBacktestResultKey!: string;
2021-01-15 19:41:32 +00:00
2020-07-27 05:19:45 +00:00
@ftbot.State history;
2021-01-16 09:56:31 +00:00
@ftbot.Getter [BotStoreGetters.selectedBacktestResult]!: StrategyBacktestResult;
@ftbot.Getter [BotStoreGetters.canRunBacktest]!: boolean;
2020-07-27 05:19:45 +00:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@ftbot.Action getPairHistory!: (payload: PairHistoryPayload) => void;
@ftbot.Action getState;
2020-07-27 05:19:45 +00:00
2020-07-26 17:35:56 +00:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2021-04-24 14:51:28 +00:00
@ftbot.Action startBacktest!: (payload: BacktestPayload) => void;
2020-07-26 11:44:34 +00:00
@ftbot.Action pollBacktest!: () => void;
@ftbot.Action removeBacktest!: () => void;
2021-04-05 17:59:14 +00:00
@ftbot.Action stopBacktest!: () => void;
2021-01-16 09:56:31 +00:00
// eslint-disable-next-line @typescript-eslint/no-unused-vars
@ftbot.Mutation setBacktestResultKey!: (key: string) => void;
2021-01-14 07:04:28 +00:00
formatPercent = formatPercent;
2020-07-26 17:35:56 +00:00
get hasBacktestResult() {
2021-01-21 06:47:51 +00:00
return this.backtestHistory ? Object.keys(this.backtestHistory).length !== 0 : false;
2020-07-26 17:35:56 +00:00
}
2020-08-08 18:21:47 +00:00
get timeframe(): string {
try {
2021-01-21 06:47:51 +00:00
return this.selectedBacktestResult.timeframe;
2020-08-08 18:21:47 +00:00
} catch (err) {
return '';
}
}
mounted() {
this.getState();
}
2021-01-16 09:56:31 +00:00
setBacktestResult(key: string) {
this.setBacktestResultKey(key);
2021-03-11 18:17:09 +00:00
// Set parameters for this result
this.strategy = this.selectedBacktestResult.strategy_name;
this.selectedTimeframe = this.selectedBacktestResult.timeframe;
this.selectedDetailTimeframe = this.selectedBacktestResult.timeframe_detail || '';
2021-03-11 18:17:09 +00:00
this.timerange = this.selectedBacktestResult.timerange;
2021-01-16 09:56:31 +00:00
}
2020-07-26 11:44:34 +00:00
clickBacktest() {
const btPayload: BacktestPayload = {
2020-07-26 17:35:56 +00:00
strategy: this.strategy,
2020-07-26 11:44:34 +00:00
timerange: this.timerange,
2021-01-06 19:25:38 +00:00
// eslint-disable-next-line @typescript-eslint/camelcase
enable_protections: this.enableProtections,
2020-07-26 11:44:34 +00:00
};
const openTradesInt = parseInt(this.maxOpenTrades, 10);
if (openTradesInt) {
2020-12-08 18:35:06 +00:00
// eslint-disable-next-line @typescript-eslint/camelcase
btPayload.max_open_trades = openTradesInt;
2020-12-08 18:35:06 +00:00
}
2021-02-28 08:53:51 +00:00
if (this.stakeAmountUnlimited) {
2020-12-08 18:35:06 +00:00
// eslint-disable-next-line @typescript-eslint/camelcase
2021-02-28 08:53:51 +00:00
btPayload.stake_amount = 'unlimited';
} else {
const stakeAmount = Number(this.stakeAmount);
if (stakeAmount) {
// eslint-disable-next-line @typescript-eslint/camelcase
btPayload.stake_amount = stakeAmount;
}
2020-12-08 18:35:06 +00:00
}
const startingCapital = Number(this.startingCapital);
if (startingCapital) {
// eslint-disable-next-line @typescript-eslint/camelcase
btPayload.dry_run_wallet = startingCapital;
}
2021-01-02 14:10:10 +00:00
if (this.selectedTimeframe) {
btPayload.timeframe = this.selectedTimeframe;
}
if (this.selectedDetailTimeframe) {
// eslint-disable-next-line @typescript-eslint/camelcase
btPayload.timeframe_detail = this.selectedDetailTimeframe;
}
2020-12-08 18:35:06 +00:00
2020-07-26 11:44:34 +00:00
this.startBacktest(btPayload);
}
@Watch('backtestRunning')
backtestRunningChanged() {
if (this.backtestRunning === true) {
2020-07-26 17:35:56 +00:00
this.pollInterval = window.setInterval(this.pollBacktest, 1000);
2020-07-26 11:44:34 +00:00
} else if (this.pollInterval) {
clearInterval(this.pollInterval);
this.pollInterval = null;
}
}
}
</script>
2021-05-24 09:34:48 +00:00
<style lang="scss" scoped>
.candle-chart-container {
2021-07-04 17:57:19 +00:00
// TODO: Rough estimate - still to fix correctly
// Applies to all "calc" usages in this file.
height: calc(100vh - 250px) !important;
}
2021-01-06 13:51:04 +00:00
.cum-profit {
height: 350px;
max-height: 350px;
}
.trades-log {
height: 350px;
max-height: 350px;
2020-11-01 12:58:16 +00:00
}
2021-01-21 06:47:51 +00:00
.bt-running-label {
position: absolute;
right: 2em;
margin-top: 1em;
}
2021-03-11 18:17:09 +00:00
.sticky-offset {
top: 2em;
}
2021-05-24 09:34:48 +00:00
.flex-samesize-items {
flex: 1 1 0;
@media md {
flex: unset;
}
}
.fade-enter-active,
.fade-leave-active {
transition: all 0.2s;
}
.fade-enter,
.fade-leave-to {
opacity: 0;
}
2021-07-06 04:56:41 +00:00
.bt-config {
@media (min-width: 992px) {
margin-left: auto;
margin-right: auto;
max-width: 75vw;
}
}
</style>