분석레포트 로직 수정
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { PaperSummaryDto, PaperTradeDto } from './backendApi';
|
||||
import type { PaperSummaryDto, PaperTradeDto, BacktestAnalysis } from './backendApi';
|
||||
|
||||
export interface PaperPerformanceMetrics {
|
||||
mddPct: number;
|
||||
@@ -126,3 +126,43 @@ export function computePaperMetrics(
|
||||
profitFactor: Math.min(profitFactor, 99),
|
||||
};
|
||||
}
|
||||
|
||||
/** 실시간 매매 체결 이력 → 백테스트 분석 DTO (ledger·라운드트립 기준) */
|
||||
export function buildAnalysisFromPaperTrades(
|
||||
trades: PaperTradeDto[],
|
||||
initialCapital = 10_000_000,
|
||||
): BacktestAnalysis {
|
||||
const sorted = [...trades].sort((a, b) =>
|
||||
(a.createdAt ?? '').localeCompare(b.createdAt ?? ''),
|
||||
);
|
||||
const metrics = computePaperMetrics(sorted, { initialCapital } as PaperSummaryDto);
|
||||
const curve = buildEquityCurve(sorted, initialCapital);
|
||||
const finalEquity = curve.length > 0 ? curve[curve.length - 1] : initialCapital;
|
||||
const { wins, total, grossProfit, grossLoss } = roundTripStats(sorted);
|
||||
const profitLossRatio = grossLoss > 0 ? grossProfit / grossLoss : grossProfit > 0 ? 99 : 0;
|
||||
|
||||
return {
|
||||
initialCapital,
|
||||
finalEquity,
|
||||
totalReturnPct: initialCapital > 0 ? (finalEquity - initialCapital) / initialCapital : 0,
|
||||
totalProfitLoss: finalEquity - initialCapital,
|
||||
grossProfit,
|
||||
grossLoss,
|
||||
avgReturnPct: total > 0 ? (finalEquity - initialCapital) / initialCapital / total : 0,
|
||||
profitLossRatio: Math.min(profitLossRatio, 99),
|
||||
numberOfPositions: total,
|
||||
numberOfWinning: wins,
|
||||
numberOfLosing: Math.max(0, total - wins),
|
||||
numberOfBreakEven: 0,
|
||||
winRate: total > 0 ? wins / total : 0,
|
||||
maxDrawdownPct: metrics.mddPct / 100,
|
||||
maxRunupPct: 0,
|
||||
sharpeRatio: metrics.sharpeRatio,
|
||||
sortinoRatio: 0,
|
||||
calmarRatio: 0,
|
||||
valueAtRisk95: 0,
|
||||
expectedShortfall: 0,
|
||||
buyAndHoldReturnPct: 0,
|
||||
vsBuyAndHold: 0,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user