Skip to content
This repository has been archived by the owner on Oct 27, 2022. It is now read-only.

Commit

Permalink
Merge pull request #48 from wst24365888/feat/editor
Browse files Browse the repository at this point in the history
Apply profit factor
  • Loading branch information
wst24365888 authored Oct 22, 2022
2 parents bed8f95 + 781ebf6 commit 5e664d5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
25 changes: 19 additions & 6 deletions pages/dashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,19 @@ function backtest(code: string): Backtest {
const gains = indicatorts
.applyActions(stock.closings, actions)
.map((gain) => Math.round(gain * 100));
const deviations = gains.map((gain, i) => {
if (i === 0) return 0;
return gain - gains[i - 1];
});
const positiveDeviations = deviations
.filter((deviation) => deviation > 0)
.reduce((a, b) => a + b, 0);
const negativeDeviations = deviations
.filter((deviation) => deviation < 0)
.reduce((a, b) => a + b, 0);
const profitFactor = positiveDeviations / Math.abs(negativeDeviations);
const { actionCount, winCount } = calculateStrategyActions(
actions,
stock.closings,
Expand All @@ -98,7 +111,7 @@ function backtest(code: string): Backtest {
const result = Math.round(
indicatorts.backtest(stock, [strategy])[0].gain * 100,
);
const t = { gains, winRate, actionCount, winCount, result };
const t = { gains, profitFactor, winRate, actionCount, result };
console.log(t);
return t;
}
Expand Down Expand Up @@ -128,17 +141,17 @@ function animateDashboardValue() {
backtestData.value,
{
gains: backtestDataSnapshot.gains,
profitFactor: 0,
winRate: 0,
result: 0,
actionCount: 0,
winCount: 0,
},
{
gains: backtestData.value.gains,
profitFactor: backtestData.value.profitFactor,
winRate: backtestData.value.winRate,
result: backtestData.value.result,
actionCount: backtestData.value.actionCount,
winCount: backtestData.value.winCount,
duration: 2,
ease: Power2.easeOut,
},
Expand Down Expand Up @@ -196,9 +209,6 @@ onMounted(() => {
<DashboardCard title="Total Closed Trades">
<p>{{ Math.round(backtestData.actionCount) }}</p>
</DashboardCard>
<DashboardCard title="Total Win">
<p>{{ Math.round(backtestData.winCount) }}</p>
</DashboardCard>
<DashboardCard title="Win Rate">
<p>
{{
Expand All @@ -208,6 +218,9 @@ onMounted(() => {
}}%
</p>
</DashboardCard>
<DashboardCard title="Profit Factor">
<p>{{ backtestData.profitFactor.toFixed(2) }}</p>
</DashboardCard>
<DashboardCard title="Net Profit">
<p>{{ Math.round(backtestData.result) }}%</p>
</DashboardCard>
Expand Down
2 changes: 1 addition & 1 deletion types/backtest/backtest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export interface Backtest {
gains: number[];
profitFactor: number;
winRate: number;
result: number;
actionCount: number;
winCount: number;
}

0 comments on commit 5e664d5

Please sign in to comment.