Skip to content

Tester Stats

EntriesCalculator

Calculates entry counts by hour, weekday, and month based on the deals data.

Source code in strategytester5\stats.py
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
class EntriesCalculator:
    """ Calculates entry counts by hour, weekday, and month based on the deals data. """

    def __init__(self, deals_df: pd.DataFrame):
        self.deals_df = deals_df.query(
            f"entry=={MetaTrader5Constants.DEAL_ENTRY_IN} and (type=={MetaTrader5Constants.DEAL_TYPE_SELL} or type=={MetaTrader5Constants.DEAL_TYPE_BUY})").copy()

        self.deals_df["hour"] = self.deals_df["time"].dt.hour
        self.deals_df["weekday"] = self.deals_df["time"].dt.weekday
        self.deals_df["month"] = self.deals_df["time"].dt.month

    def by_hour(self) -> pd.Series:
        """ Returns a Series with the count of entries for each hour of the day (0-23). """
        return self.deals_df.groupby("hour")["entry"].size().reindex(range(24), fill_value=0)

    def by_weekday(self) -> pd.Series:
        """ Returns a Series with the count of entries for each weekday (0=Monday, 6=Sunday). """
        return self.deals_df.groupby("weekday")["entry"].size().reindex(range(7), fill_value=0)

    def by_month(self) -> pd.Series:
        """ Returns a Series with the count of entries for each month (1-12). """
        return self.deals_df.groupby("month")["entry"].size().reindex(range(1, 13), fill_value=0)

by_hour()

Returns a Series with the count of entries for each hour of the day (0-23).

Source code in strategytester5\stats.py
532
533
534
def by_hour(self) -> pd.Series:
    """ Returns a Series with the count of entries for each hour of the day (0-23). """
    return self.deals_df.groupby("hour")["entry"].size().reindex(range(24), fill_value=0)

by_month()

Returns a Series with the count of entries for each month (1-12).

Source code in strategytester5\stats.py
540
541
542
def by_month(self) -> pd.Series:
    """ Returns a Series with the count of entries for each month (1-12). """
    return self.deals_df.groupby("month")["entry"].size().reindex(range(1, 13), fill_value=0)

by_weekday()

Returns a Series with the count of entries for each weekday (0=Monday, 6=Sunday).

Source code in strategytester5\stats.py
536
537
538
def by_weekday(self) -> pd.Series:
    """ Returns a Series with the count of entries for each weekday (0=Monday, 6=Sunday). """
    return self.deals_df.groupby("weekday")["entry"].size().reindex(range(7), fill_value=0)

PLCalculator

Calculates profit and loss statistics by hour, weekday, and month based on the deals data.

Source code in strategytester5\stats.py
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
class PLCalculator:
    """ Calculates profit and loss statistics by hour, weekday, and month based on the deals data. """

    def __init__(self, deals_df: pd.DataFrame):
        """
        Args:
            deals_df (pd.DataFrame): DataFrame containing deal records with columns like 'entry', 'type', 'profit', 'commission', and 'time'.
        """

        self.deals_df = deals_df.query(
            f"entry == {MetaTrader5Constants.DEAL_ENTRY_OUT} and (type=={MetaTrader5Constants.DEAL_TYPE_BUY} | type=={MetaTrader5Constants.DEAL_TYPE_SELL})").copy()

        self.deals_df["hour"] = self.deals_df["time"].dt.hour
        self.deals_df["weekday"] = self.deals_df["time"].dt.weekday
        self.deals_df["month"] = self.deals_df["time"].dt.month

        net = (self.deals_df["profit"] + self.deals_df["commission"])

        self.deals_df["profit"] = net.clip(lower=0.0)
        self.deals_df["loss"] = net.clip(upper=0.0)

    def loss_by_hour(self) -> pd.Series:
        """ Returns a Series with the total loss for each hour of the day (0-23). """
        return self.deals_df.groupby("hour")["loss"].sum().reindex(range(24), fill_value=0)

    def profit_by_hour(self) -> pd.Series:
        """ Returns a Series with the total profit for each hour of the day (0-23). """
        return self.deals_df.groupby("hour")["profit"].sum().reindex(range(24), fill_value=0)

    def loss_by_weekday(self) -> pd.Series:
        """ Returns a Series with the total loss for each weekday (0=Monday, 6=Sunday). """

        return self.deals_df.groupby("weekday")["loss"].sum().reindex(range(7), fill_value=0)

    def profit_by_weekday(self) -> pd.Series:
        """ Returns a Series with the total profit for each weekday (0=Monday, 6=Sunday). """
        return self.deals_df.groupby("weekday")["profit"].sum().reindex(range(7), fill_value=0)

    def loss_by_month(self) -> pd.Series:
        """ Returns a Series with the total loss for each month (1-12). """
        return self.deals_df.groupby("month")["loss"].sum().reindex(range(1, 13), fill_value=0)

    def profit_by_month(self) -> pd.Series:
        """ Returns a Series with the total profit for each month (1-12). """
        return self.deals_df.groupby("month")["profit"].sum().reindex(range(1, 13), fill_value=0)

__init__(deals_df)

Parameters:

Name Type Description Default
deals_df DataFrame

DataFrame containing deal records with columns like 'entry', 'type', 'profit', 'commission', and 'time'.

required
Source code in strategytester5\stats.py
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
def __init__(self, deals_df: pd.DataFrame):
    """
    Args:
        deals_df (pd.DataFrame): DataFrame containing deal records with columns like 'entry', 'type', 'profit', 'commission', and 'time'.
    """

    self.deals_df = deals_df.query(
        f"entry == {MetaTrader5Constants.DEAL_ENTRY_OUT} and (type=={MetaTrader5Constants.DEAL_TYPE_BUY} | type=={MetaTrader5Constants.DEAL_TYPE_SELL})").copy()

    self.deals_df["hour"] = self.deals_df["time"].dt.hour
    self.deals_df["weekday"] = self.deals_df["time"].dt.weekday
    self.deals_df["month"] = self.deals_df["time"].dt.month

    net = (self.deals_df["profit"] + self.deals_df["commission"])

    self.deals_df["profit"] = net.clip(lower=0.0)
    self.deals_df["loss"] = net.clip(upper=0.0)

loss_by_hour()

Returns a Series with the total loss for each hour of the day (0-23).

Source code in strategytester5\stats.py
566
567
568
def loss_by_hour(self) -> pd.Series:
    """ Returns a Series with the total loss for each hour of the day (0-23). """
    return self.deals_df.groupby("hour")["loss"].sum().reindex(range(24), fill_value=0)

loss_by_month()

Returns a Series with the total loss for each month (1-12).

Source code in strategytester5\stats.py
583
584
585
def loss_by_month(self) -> pd.Series:
    """ Returns a Series with the total loss for each month (1-12). """
    return self.deals_df.groupby("month")["loss"].sum().reindex(range(1, 13), fill_value=0)

loss_by_weekday()

Returns a Series with the total loss for each weekday (0=Monday, 6=Sunday).

Source code in strategytester5\stats.py
574
575
576
577
def loss_by_weekday(self) -> pd.Series:
    """ Returns a Series with the total loss for each weekday (0=Monday, 6=Sunday). """

    return self.deals_df.groupby("weekday")["loss"].sum().reindex(range(7), fill_value=0)

profit_by_hour()

Returns a Series with the total profit for each hour of the day (0-23).

Source code in strategytester5\stats.py
570
571
572
def profit_by_hour(self) -> pd.Series:
    """ Returns a Series with the total profit for each hour of the day (0-23). """
    return self.deals_df.groupby("hour")["profit"].sum().reindex(range(24), fill_value=0)

profit_by_month()

Returns a Series with the total profit for each month (1-12).

Source code in strategytester5\stats.py
587
588
589
def profit_by_month(self) -> pd.Series:
    """ Returns a Series with the total profit for each month (1-12). """
    return self.deals_df.groupby("month")["profit"].sum().reindex(range(1, 13), fill_value=0)

profit_by_weekday()

Returns a Series with the total profit for each weekday (0=Monday, 6=Sunday).

Source code in strategytester5\stats.py
579
580
581
def profit_by_weekday(self) -> pd.Series:
    """ Returns a Series with the total profit for each weekday (0=Monday, 6=Sunday). """
    return self.deals_df.groupby("weekday")["profit"].sum().reindex(range(7), fill_value=0)

TesterStats

Computes various statistics from the tester results, including trade performance metrics, drawdowns, and more.

This class is responsible fo calculating all the stats you see in the HTML report

Source code in strategytester5\stats.py
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
class TesterStats:
    """ Computes various statistics from the tester results, including trade performance metrics, drawdowns, and more.

    This class is responsible fo calculating all the stats you see in the HTML report"""

    def __init__(self,
                 deals: list,
                 initial_deposit: float,
                 balance_curve: np.ndarray,
                 equity_curve: np.ndarray,
                 margin_level_curve: np.ndarray,
                 ticks: int,
                 symbols: int
                 ):

        """ Initializes the TesterStats object with the provided data and computes all statistics.

        Args:
            deals (list): List of deal records from the tester.
            initial_deposit (float): The initial deposit amount used in the test.
            balance_curve (np.ndarray): Array representing the balance curve over time.
            equity_curve (np.ndarray): Array representing the equity curve over time.
            margin_level_curve (np.ndarray): Array representing the margin level curve over time.
            ticks (int): Total number of ticks processed during the test.
            symbols (int): Total number of unique symbols traded during the test.
        """

        self.deals = deals
        self.initial_deposit = float(initial_deposit)
        self.balance_curve = np.ascontiguousarray(np.asarray(balance_curve, dtype=np.float64)).reshape(-1)
        self.equity_curve = np.ascontiguousarray(np.asarray(equity_curve, dtype=np.float64)).reshape(-1)
        self.margin_level_curve = np.ascontiguousarray(np.asarray(margin_level_curve, dtype=np.float64)).reshape(-1)
        self.ticks = ticks
        self.symbols = symbols

        self._profits: list[float] = []
        self._losses: list[float] = []  # negative profits (losses)
        self._returns = np.diff(self.equity_curve)

        self._total_trades = 0
        self._total_long_trades = 0
        self._total_short_trades = 0
        self._long_trades_won = 0
        self._short_trades_won = 0

        self._max_consec_win_count = 0
        self._max_consec_win_money = 0.0
        self._max_consec_loss_count = 0
        self._max_consec_loss_money = 0.0

        self._max_profit_streak_money = 0.0
        self._max_profit_streak_count = 0
        self._max_loss_streak_money = 0.0
        self._max_loss_streak_count = 0

        self._win_streaks: list[int] = []
        self._loss_streaks: list[int] = []
        self._trade_returns = []  # per-trade returns in *fraction* (e.g., 0.01 = +1%)

        self.eps = 1e-10

        self._compute()

        y = self.balance_curve.astype(float)
        x = np.arange(len(y), dtype=float)

        if len(x) == 0 or len(y) == 0:
            self.lr_res = None
        else:
            self.lr_res = linregress(x, y)

    def _compute(self):
        cur_win_count = 0
        cur_win_money = 0.0
        cur_loss_count = 0
        cur_loss_money = 0.0

        for d in self.deals:
            if getattr(d, "entry", None) != MetaTrader5Constants.DEAL_ENTRY_OUT:
                continue

            self._total_trades += 1

            d_type = d.type
            if d_type == MetaTrader5Constants.DEAL_TYPE_BUY:
                self._total_long_trades += 1
            elif d_type == MetaTrader5Constants.DEAL_TYPE_SELL:
                self._total_short_trades += 1

            profit = d.profit + d.commission

            # ---- per-trade return (percent change per trade) for AHPR/GHPR :contentReference[oaicite:10]{index=10}
            bal_after = getattr(d, "balance", None)
            if bal_after is not None:
                bal_after = float(bal_after)
                bal_before = bal_after - profit
                if bal_before > self.eps:
                    self._trade_returns.append(profit / bal_before)

            if profit > 0.0:

                self._profits.append(profit)

                if cur_loss_count > 0:
                    self._loss_streaks.append(cur_loss_count)
                    cur_loss_count = 0
                    cur_loss_money = 0.0

                cur_win_count += 1
                cur_win_money += profit

                if cur_win_count > self._max_consec_win_count:
                    self._max_consec_win_count = cur_win_count
                    self._max_consec_win_money = cur_win_money

                if cur_win_money > self._max_profit_streak_money:
                    self._max_profit_streak_money = cur_win_money
                    self._max_profit_streak_count = cur_win_count

                if d_type == MetaTrader5Constants.DEAL_TYPE_BUY:
                    self._long_trades_won += 1
                elif d_type == MetaTrader5Constants.DEAL_TYPE_SELL:
                    self._short_trades_won += 1

            else:
                self._losses.append(profit)  # negative or zero

                if cur_win_count > 0:
                    self._win_streaks.append(cur_win_count)
                    cur_win_count = 0
                    cur_win_money = 0.0

                cur_loss_count += 1
                cur_loss_money += profit  # negative accumulation

                if cur_loss_count > self._max_consec_loss_count:
                    self._max_consec_loss_count = cur_loss_count
                    self._max_consec_loss_money = cur_loss_money

                if cur_loss_money < self._max_loss_streak_money:
                    self._max_loss_streak_money = cur_loss_money
                    self._max_loss_streak_count = cur_loss_count

        # flush last streaks (important!)
        if cur_win_count > 0:
            self._win_streaks.append(cur_win_count)
        if cur_loss_count > 0:
            self._loss_streaks.append(cur_loss_count)

    @property
    def total_trades(self) -> int:
        """ Total number of trades opened and closed during the test. """
        return self._total_trades

    @property
    def total_deals(self) -> int:
        """The total number of deal records, including both entries and exits. Note that the first deal is usually the initial deposit and is not a real trade."""
        return len(self.deals) - 1

    @property
    def total_short_trades(self) -> int:
        """ Total number of short (SELL) trades closed during the test. """
        return self._total_short_trades

    @property
    def total_long_trades(self) -> int:
        """ Total number of long (BUY) trades closed during the test. """
        return self._total_long_trades

    @property
    def short_trades_won(self) -> int:
        """ Number of short (SELL) trades that were profitable (profit > 0) at closing. """
        return self._short_trades_won

    @property
    def long_trades_won(self) -> int:
        """ Number of long (BUY) trades that were profitable (profit > 0) at closing. """
        return self._long_trades_won

    @property
    def profit_trades(self) -> int:
        """ Number of trades that were profitable (profit > 0) at closing. """
        return len(self._profits) if self._profits else 0

    @property
    def loss_trades(self) -> int:
        """ Number of trades that were not profitable (profit <= 0) at closing. """
        return len(self._losses) if self._losses else 0

    @property
    def largest_profit_trade(self) -> float:
        """ Largest profit from a single trade. """
        return np.max(self._profits) if self._profits else 0

    @property
    def largest_loss_trade(self) -> float:
        """ Largest loss from a single trade. """
        return np.min(self._losses) if self._losses else 0

    @property
    def average_profit_trade(self) -> float:
        """ Average profit from profitable trades. """
        return np.mean(self._profits) if self._profits else 0

    @property
    def average_loss_trade(self) -> float:
        """ Average loss from unprofitable trades. """
        return np.mean(self._losses) if self._losses else 0

    # ---------- streak metrics ----------

    @property
    def maximum_consecutive_wins_count(self) -> int:
        """ Maximum number of consecutive winning trades. """
        return self._max_consec_win_count

    @property
    def maximum_consecutive_wins_money(self) -> float:
        """ Maximum money won from consecutive winning trades. """
        return self._max_consec_win_money

    @property
    def maximum_consecutive_losses_count(self) -> int:
        """ Maximum number of consecutive losing trades. """
        return self._max_consec_loss_count

    @property
    def maximum_consecutive_losses_money(self) -> float:
        """ Maximum money lost from consecutive losing trades. """
        # show as absolute money if you prefer; MT5 shows total loss (negative) in brackets
        return self._max_consec_loss_money

    @property
    def maximal_consecutive_profit_count(self) -> int:
        """ Maximum number of consecutive profitable trades. """
        return self._max_profit_streak_count

    @property
    def maximal_consecutive_profit_money(self) -> float:
        """ Maximum money won from consecutive profitable trades. """
        return self._max_profit_streak_money

    @property
    def maximal_consecutive_loss_count(self) -> int:
        """ Maximum number of consecutive losing trades. """
        return self._max_loss_streak_count

    @property
    def maximal_consecutive_loss_money(self) -> float:
        """ Maximum money lost from consecutive losing trades. """
        return self._max_loss_streak_money

    @property
    def average_consecutive_wins(self) -> float:
        """ Average profit from consecutive winning trades. """
        return np.mean(self._win_streaks) if self._win_streaks else 0

    @property
    def average_consecutive_losses(self) -> float:
        """ Average loss from consecutive losing trades. """
        return np.mean(self._loss_streaks) if self._loss_streaks else 0

    @property
    def gross_profit(self) -> float:
        """ Total profit from all profitable trades. """
        return np.sum(self._profits) if self._profits else 0.0

    @property
    def gross_loss(self) -> float:
        """ Total loss from all unprofitable trades. """
        return np.sum(self._losses) if self._losses else 0.0

    @property
    def net_profit(self) -> float:
        """ Net profit (gross profit - gross loss). """
        return self.gross_profit - np.abs(self.gross_loss)

    @property
    def profit_factor(self) -> float:
        """ Profit factor (gross profit / gross loss). """
        return self.gross_profit / (self.gross_loss + self.eps)

    @property
    def recovery_factor(self) -> float:
        """ Recovery factor (net profit / maximal equity drawdown). """
        return self.net_profit / (self.equity_drawdown_maximal + self.eps)

    @property
    def expected_payoff(self) -> int:
        """ Expected payoff (net profit / total trades). """
        return (self.net_profit / self.total_trades) if self.total_trades > 0 else 0

    # ---------- drawdowns ----------
    @staticmethod
    def _abs_drawdown(initial: float, curve: np.ndarray) -> float:
        if curve.size == 0:
            return 0.0
        min_val = float(np.min(curve))
        dd = initial - min_val
        return dd if dd > 0.0 else 0.0

    @property
    def balance_drawdown_absolute(self) -> float:
        """ Absolute drawdown for balance curve. """
        # AbsoluteDrawDown = InitialDeposit - MinimalBalance (below initial) :contentReference[oaicite:12]{index=12}
        return self._abs_drawdown(self.initial_deposit, self.balance_curve)

    @property
    def equity_drawdown_absolute(self) -> float:
        """ Absolute drawdown for equity curve. """
        return self._abs_drawdown(self.initial_deposit, self.equity_curve)

    @property
    def balance_drawdown_maximal(self) -> float:
        """ MT5 maximal drawdown uses local-high, next local low definition, which can be different from absolute drawdown. """
        return float(_max_dd_money_and_pct_nb(self.balance_curve)[0])

    def _validate_baleq_values(self, value: float) -> float:
        if abs(value) > self.initial_deposit:
            return np.nan

        return value

    @property
    def balance_drawdown_relative(self) -> float:
        """ MT5 relative drawdown uses local high, next local low (max %)"""
        return self._validate_baleq_values(float(_max_dd_money_and_pct_nb(self.balance_curve)[1]))

    @property
    def equity_drawdown_maximal(self) -> float:
        """ MT5 maximal drawdown uses local-high, next local low definition, which can be different from absolute drawdown. """
        return self._validate_baleq_values(float(_max_dd_money_and_pct_nb(self.equity_curve)[0]))

    @property
    def equity_drawdown_relative(self) -> float:
        """ MT5 relative drawdown uses local high, next local low (max %)"""
        return self._validate_baleq_values(float(_max_dd_money_and_pct_nb(self.equity_curve)[1]))

    @property
    def sharpe_ratio(self) -> float:
        """(Return - 0) / std(Return)"""
        r = np.asarray(self._trade_returns, dtype=np.float64)

        if r.size < 2:
            return 0.0

        std = float(np.std(r))
        return float(np.mean(r) / np.maximum(std, self.eps))

    # ---------- Z-score (runs test over win/loss sequence) ----------

    @property
    def z_score(self) -> float:

        """ Build win/loss sequence from CLOSED trades:"""

        seq = []
        for d in self.deals:
            if getattr(d, "entry", None) != MetaTrader5Constants.DEAL_ENTRY_OUT:
                continue
            seq.append(1 if float(getattr(d, "profit", 0.0)) > 0.0 else 0)

        n = len(seq)
        if n < 2:
            return 0.0

        n1 = sum(seq)  # wins
        n2 = n - n1  # losses
        if n1 == 0 or n2 == 0:
            return 0.0

        # number of runs
        R = 1
        for i in range(1, n):
            if seq[i] != seq[i - 1]:
                R += 1

        ER = 1.0 + (2.0 * n1 * n2) / (n1 + n2)
        VR = (2.0 * n1 * n2 * (2.0 * n1 * n2 - n1 - n2)) / (((n1 + n2) ** 2) * (n1 + n2 - 1.0))
        if VR <= self.eps:
            return 0.0
        return float((R - ER) / np.sqrt(VR))

    # ---------- AHPR / GHPR ----------

    @property
    def ahpr_factor(self) -> float:
        """ AHPR = (1 + r1) * (1 + r2) * ... * (1 + rn)^(1/n) - 1, where r_i are per-trade returns in fraction (e.g., 0.01 = +1%) """
        r = np.asarray(self._trade_returns, dtype=np.float64)
        if r.size == 0:
            return 1.0
        return float(1.0 + np.mean(r))

    @property
    def ahpr_percent(self) -> float:
        """ AHPR in percent form. """
        return float((self.ahpr_factor - 1.0) * 100.0)

    @property
    def ghpr_factor(self) -> float:
        """ GHPR = ((1 + r1) * (1 + r2) * ... * (1 + rn))^(1/n) - 1, where r_i are per-trade returns in fraction (e.g., 0.01 = +1%) """
        r = np.asarray(self._trade_returns, dtype=np.float64)
        if r.size == 0:
            return 1.0
        return float(np.prod(1.0 + r) ** (1.0 / r.size))

    @property
    def ghpr_percent(self) -> float:
        """ GHPR in percent form. """
        return float((self.ghpr_factor - 1.0) * 100.0)

    @property
    def lr_correlation(self) -> float:
        """ Correlation coefficient (r-value) from linear regression of balance curve over time. """
        return np.nan if not self.lr_res else self.lr_res.rvalue

    @property
    def lr_standard_error(self) -> float:
        """ Standard error of the estimate from linear regression of balance curve over time. """
        return np.nan if not self.lr_res else self.lr_res.stderr

    @property
    def on_tester_results(self) -> float:
        return 0.0

    @property
    def margin_level(self) -> float:
        """ Minimum margin level (%) during the test. """
        return np.min(self.margin_level_curve) if len(self.margin_level_curve) > 0 else np.nan

    @staticmethod
    def holding_time_calculator(entry_time: pd.Series, exit_time: pd.Series) -> dict:
        """ Calculates holding time statistics (min, max, average) for trades based on entry and exit times of positions. """
        durations = exit_time - entry_time

        if durations.empty:
            return {"min": None, "max": None, "avg": None, "count": 0}

        return {
            "time": exit_time,
            "durations": durations,
            "count": int(len(durations)),
            "min": durations.min(),
            "max": durations.max(),
            "avg": durations.mean(),
        }

ahpr_factor property

AHPR = (1 + r1) * (1 + r2) * ... * (1 + rn)^(1/n) - 1, where r_i are per-trade returns in fraction (e.g., 0.01 = +1%)

ahpr_percent property

AHPR in percent form.

average_consecutive_losses property

Average loss from consecutive losing trades.

average_consecutive_wins property

Average profit from consecutive winning trades.

average_loss_trade property

Average loss from unprofitable trades.

average_profit_trade property

Average profit from profitable trades.

balance_drawdown_absolute property

Absolute drawdown for balance curve.

balance_drawdown_maximal property

MT5 maximal drawdown uses local-high, next local low definition, which can be different from absolute drawdown.

balance_drawdown_relative property

MT5 relative drawdown uses local high, next local low (max %)

equity_drawdown_absolute property

Absolute drawdown for equity curve.

equity_drawdown_maximal property

MT5 maximal drawdown uses local-high, next local low definition, which can be different from absolute drawdown.

equity_drawdown_relative property

MT5 relative drawdown uses local high, next local low (max %)

expected_payoff property

Expected payoff (net profit / total trades).

ghpr_factor property

GHPR = ((1 + r1) * (1 + r2) * ... * (1 + rn))^(1/n) - 1, where r_i are per-trade returns in fraction (e.g., 0.01 = +1%)

ghpr_percent property

GHPR in percent form.

gross_loss property

Total loss from all unprofitable trades.

gross_profit property

Total profit from all profitable trades.

largest_loss_trade property

Largest loss from a single trade.

largest_profit_trade property

Largest profit from a single trade.

long_trades_won property

Number of long (BUY) trades that were profitable (profit > 0) at closing.

loss_trades property

Number of trades that were not profitable (profit <= 0) at closing.

lr_correlation property

Correlation coefficient (r-value) from linear regression of balance curve over time.

lr_standard_error property

Standard error of the estimate from linear regression of balance curve over time.

margin_level property

Minimum margin level (%) during the test.

maximal_consecutive_loss_count property

Maximum number of consecutive losing trades.

maximal_consecutive_loss_money property

Maximum money lost from consecutive losing trades.

maximal_consecutive_profit_count property

Maximum number of consecutive profitable trades.

maximal_consecutive_profit_money property

Maximum money won from consecutive profitable trades.

maximum_consecutive_losses_count property

Maximum number of consecutive losing trades.

maximum_consecutive_losses_money property

Maximum money lost from consecutive losing trades.

maximum_consecutive_wins_count property

Maximum number of consecutive winning trades.

maximum_consecutive_wins_money property

Maximum money won from consecutive winning trades.

net_profit property

Net profit (gross profit - gross loss).

profit_factor property

Profit factor (gross profit / gross loss).

profit_trades property

Number of trades that were profitable (profit > 0) at closing.

recovery_factor property

Recovery factor (net profit / maximal equity drawdown).

sharpe_ratio property

(Return - 0) / std(Return)

short_trades_won property

Number of short (SELL) trades that were profitable (profit > 0) at closing.

total_deals property

The total number of deal records, including both entries and exits. Note that the first deal is usually the initial deposit and is not a real trade.

total_long_trades property

Total number of long (BUY) trades closed during the test.

total_short_trades property

Total number of short (SELL) trades closed during the test.

total_trades property

Total number of trades opened and closed during the test.

z_score property

Build win/loss sequence from CLOSED trades:

__init__(deals, initial_deposit, balance_curve, equity_curve, margin_level_curve, ticks, symbols)

Initializes the TesterStats object with the provided data and computes all statistics.

Parameters:

Name Type Description Default
deals list

List of deal records from the tester.

required
initial_deposit float

The initial deposit amount used in the test.

required
balance_curve ndarray

Array representing the balance curve over time.

required
equity_curve ndarray

Array representing the equity curve over time.

required
margin_level_curve ndarray

Array representing the margin level curve over time.

required
ticks int

Total number of ticks processed during the test.

required
symbols int

Total number of unique symbols traded during the test.

required
Source code in strategytester5\stats.py
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def __init__(self,
             deals: list,
             initial_deposit: float,
             balance_curve: np.ndarray,
             equity_curve: np.ndarray,
             margin_level_curve: np.ndarray,
             ticks: int,
             symbols: int
             ):

    """ Initializes the TesterStats object with the provided data and computes all statistics.

    Args:
        deals (list): List of deal records from the tester.
        initial_deposit (float): The initial deposit amount used in the test.
        balance_curve (np.ndarray): Array representing the balance curve over time.
        equity_curve (np.ndarray): Array representing the equity curve over time.
        margin_level_curve (np.ndarray): Array representing the margin level curve over time.
        ticks (int): Total number of ticks processed during the test.
        symbols (int): Total number of unique symbols traded during the test.
    """

    self.deals = deals
    self.initial_deposit = float(initial_deposit)
    self.balance_curve = np.ascontiguousarray(np.asarray(balance_curve, dtype=np.float64)).reshape(-1)
    self.equity_curve = np.ascontiguousarray(np.asarray(equity_curve, dtype=np.float64)).reshape(-1)
    self.margin_level_curve = np.ascontiguousarray(np.asarray(margin_level_curve, dtype=np.float64)).reshape(-1)
    self.ticks = ticks
    self.symbols = symbols

    self._profits: list[float] = []
    self._losses: list[float] = []  # negative profits (losses)
    self._returns = np.diff(self.equity_curve)

    self._total_trades = 0
    self._total_long_trades = 0
    self._total_short_trades = 0
    self._long_trades_won = 0
    self._short_trades_won = 0

    self._max_consec_win_count = 0
    self._max_consec_win_money = 0.0
    self._max_consec_loss_count = 0
    self._max_consec_loss_money = 0.0

    self._max_profit_streak_money = 0.0
    self._max_profit_streak_count = 0
    self._max_loss_streak_money = 0.0
    self._max_loss_streak_count = 0

    self._win_streaks: list[int] = []
    self._loss_streaks: list[int] = []
    self._trade_returns = []  # per-trade returns in *fraction* (e.g., 0.01 = +1%)

    self.eps = 1e-10

    self._compute()

    y = self.balance_curve.astype(float)
    x = np.arange(len(y), dtype=float)

    if len(x) == 0 or len(y) == 0:
        self.lr_res = None
    else:
        self.lr_res = linregress(x, y)

holding_time_calculator(entry_time, exit_time) staticmethod

Calculates holding time statistics (min, max, average) for trades based on entry and exit times of positions.

Source code in strategytester5\stats.py
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
@staticmethod
def holding_time_calculator(entry_time: pd.Series, exit_time: pd.Series) -> dict:
    """ Calculates holding time statistics (min, max, average) for trades based on entry and exit times of positions. """
    durations = exit_time - entry_time

    if durations.empty:
        return {"min": None, "max": None, "avg": None, "count": 0}

    return {
        "time": exit_time,
        "durations": durations,
        "count": int(len(durations)),
        "min": durations.min(),
        "max": durations.max(),
        "avg": durations.mean(),
    }