DEV Community

Spectar FX Gold
Spectar FX Gold

Posted on

Checking a consistency rule in Python from an MT5 history export

Many funded-account programmes have a consistency rule: no single day may make up more than a set share of the total gain. The share differs by firm (30%, 40%, 50%...), and so does the definition of a "day" and of "gain". Read your own firm's wording first; the code below only does the arithmetic on numbers you give it.

This post builds a small script that reads a MetaTrader 5 history export, groups closed trades by server day, and reports the best day's share of the total gain.

1. Export the history

In MT5: Toolbox > History > right click > Report > Open XML (MS Office Excel 2007) or save as HTML and copy the deals table into a CSV. For this example we assume a CSV with these columns:

time,pnl,commission,swap
2026.11.24 09:41:12,182.40,-7.00,0.00
2026.11.24 15:02:55,-64.10,-7.00,0.00
2026.11.25 10:11:03,95.00,-7.00,-2.10
Enter fullscreen mode Exit fullscreen mode

time is in broker server time, which is what most rules use for "the day".

2. Group by server day

import csv
from collections import defaultdict
from datetime import datetime

def daily_net(path):
    days = defaultdict(float)
    with open(path, newline="", encoding="utf-8") as f:
        for row in csv.DictReader(f):
            t = datetime.strptime(row["time"], "%Y.%m.%d %H:%M:%S")
            net = float(row["pnl"]) + float(row["commission"]) + float(row["swap"])
            days[t.date()] += net
    return dict(sorted(days.items()))
Enter fullscreen mode Exit fullscreen mode

Commission and swap are included, because they change the balance like any other loss. If your firm counts gross results only, drop them.

3. Best-day share

def best_day_share(days):
    total = sum(days.values())
    if total <= 0:
        return None, total  # rule is usually only checked on a positive total
    best_day, best = max(days.items(), key=lambda kv: kv[1])
    return (best_day, best, best / total), total

def report(path, limit=0.40):
    days = daily_net(path)
    res, total = best_day_share(days)
    for d, v in days.items():
        print(f"{d}  {v:10.2f}")
    print(f"total      {total:10.2f}")
    if res is None:
        print("total is not positive; share not defined")
        return
    d, best, share = res
    print(f"best day {d}: {best:.2f} = {share:.1%} of total (limit {limit:.0%})")
    if share > limit:
        # extra total needed so that `best` falls to the limit
        needed = best / limit - total
        print(f"over the limit; total would have to grow by {needed:.2f} with no new best day")

report("history.csv", limit=0.40)
Enter fullscreen mode Exit fullscreen mode

The last line is the useful one: if the best day is 600 and the total is 1,000 with a 40% limit, the total would have to reach 1,500, so another 500 spread over other days, without any of them becoming the new best day.

4. Things that trip people up

  • Which days count. Some firms count only positive days in the denominator, some count the net of all days. Change total accordingly.
  • Open trades. The script uses closed deals only. A trade still open at the reset belongs to the day it is closed.
  • Server midnight. Your local midnight is not the broker's. Keep the timestamps as exported.
  • Rounding. Firms may round the share; check whether 40.0% passes or fails.

If you would rather not run code, the same arithmetic is in a free browser calculator (no sign-up): spectarfx.com/dv.

This is arithmetic on your own figures, not trading advice. Trading involves a high risk of loss.

  • Spectar FX team

Top comments (0)