Add average time

This commit is contained in:
2025-06-01 14:59:55 +07:00
parent faafedbdd9
commit 8f8bdf8d69
3 changed files with 91 additions and 3 deletions

View File

@@ -24,6 +24,63 @@ const BacktestRowDetails: React.FC<IBacktestRowDetailsProps> = ({
config
} = backtest;
// Helper function to calculate position open time in hours
const calculateOpenTimeInHours = (position: any) => {
const openDate = new Date(position.open.date);
let closeDate: Date | null = null;
// Determine close date based on realized P&L (matching backend logic)
if (position.profitAndLoss?.realized != null) {
if (position.profitAndLoss.realized > 0) {
// Profitable close = Take Profit
closeDate = new Date(position.takeProfit1.date);
} else {
// Loss or breakeven close = Stop Loss
closeDate = new Date(position.stopLoss.date);
}
}
if (closeDate) {
const diffInMs = closeDate.getTime() - openDate.getTime();
return diffInMs / (1000 * 60 * 60); // Convert to hours
}
return 0;
};
// Calculate average open time for winning positions
const getAverageOpenTimeWinning = () => {
const winningPositions = positions.filter((p) => {
const realized = p.profitAndLoss?.realized ?? 0;
return realized > 0;
});
if (winningPositions.length === 0) return "0.00";
const totalHours = winningPositions.reduce((sum, position) => {
return sum + calculateOpenTimeInHours(position);
}, 0);
const averageHours = totalHours / winningPositions.length;
return averageHours.toFixed(2);
};
// Calculate average open time for losing positions
const getAverageOpenTimeLosing = () => {
const losingPositions = positions.filter((p) => {
const realized = p.profitAndLoss?.realized ?? 0;
return realized <= 0;
});
if (losingPositions.length === 0) return "0.00";
const totalHours = losingPositions.reduce((sum, position) => {
return sum + calculateOpenTimeInHours(position);
}, 0);
const averageHours = totalHours / losingPositions.length;
return averageHours.toFixed(2);
};
return (
<>
<div className="grid grid-flow-row">
@@ -81,8 +138,14 @@ const BacktestRowDetails: React.FC<IBacktestRowDetailsProps> = ({
"SL: " + backtest.optimizedMoneyManagement?.stopLoss.toFixed(2) + "% TP: " + backtest.optimizedMoneyManagement?.takeProfit.toFixed(2) + "%"
}
></CardText>
<CardText
title="Avg Open Time (Winning)"
content={getAverageOpenTimeWinning() + " hours"}
></CardText>
<CardText
title="Avg Open Time (Losing)"
content={getAverageOpenTimeLosing() + " hours"}
></CardText>
</div>
<div>
<figure>