Skip to content

Commit

Permalink
fix(py): fix the issue where NaN values in the price cause computed m…
Browse files Browse the repository at this point in the history
…etrics to become NaN. (#175)
  • Loading branch information
nkaz001 committed Jan 16, 2025
1 parent cc1fc2f commit e93a0f8
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions py-hftbacktest/hftbacktest/stats/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(self, name: str = None, book_size: float | None = None):
self.book_size = book_size

def compute(self, df: pl.DataFrame, context: Dict[str, Any]) -> Mapping[str, Any]:
equity = df['equity_wo_fee'] - df['fee']
equity = (df['equity_wo_fee'] - df['fee']).drop_nans()
pnl = equity[-1] - equity[0]

if self.book_size is not None:
Expand Down Expand Up @@ -99,7 +99,7 @@ def compute(self, df: pl.DataFrame, context: Dict[str, Any]) -> Mapping[str, Any
c = get_num_samples_per_day(df['timestamp']) * self.trading_days_per_year

with np.errstate(divide='ignore'):
return {self.name: np.divide(pnl.mean(), pnl.std()) * np.sqrt(c)}
return {self.name: np.divide(pnl.drop_nans().mean(), pnl.drop_nans().std()) * np.sqrt(c)}


class Sortino(Metric):
Expand All @@ -125,9 +125,9 @@ def compute(self, df: pl.DataFrame, context: Dict[str, Any]) -> Mapping[str, Any
pnl = equity.diff()
c = get_num_samples_per_day(df['timestamp']) * self.trading_days_per_year

dr = np.sqrt((np.minimum(0, pnl) ** 2).mean())
dr = np.sqrt((np.minimum(0, pnl) ** 2).drop_nans().mean())
with np.errstate(divide='ignore'):
return {self.name: np.divide(pnl.mean(), dr) * np.sqrt(c)}
return {self.name: np.divide(pnl.drop_nans().mean(), dr) * np.sqrt(c)}


class ReturnOverMDD(Metric):
Expand Down

0 comments on commit e93a0f8

Please sign in to comment.