"""Synthetic accounting example for the Quanition Magic Number guide.
Run: python3 magic-number-example.py
No MT5 connection, trading, third-party packages, or files are required.
Amounts are invented USD values, not account or product results.
"""
from datetime import datetime
from decimal import Decimal as D

# time, magic, deal type, entry, profit, swap, commission, fee
DEALS = [
    ('2026-10-07T10:00:00', 101, 'SELL', 'OUT', '40', '-1', '-1', '0'),
    ('2026-10-08T10:00:00', 202, 'BUY', 'OUT', '-20', '-1', '-1', '0'),
    ('2026-10-14T10:00:00', 101, 'BUY', 'IN', '0', '0', '-2', '-0.5'),
    ('2026-10-15T10:00:00', 101, 'SELL', 'OUT', '120', '-3', '-2', '-0.5'),
    ('2026-10-16T10:00:00', 202, 'BUY', 'OUT', '-40', '-1', '-1', '0'),
    ('2026-10-19T10:00:00', 101, 'BUY', 'IN', '0', '0', '-2', '0'),
    ('2026-10-19T11:00:00', 0, 'BALANCE', '', '1000', '0', '0', '0'),
    ('2026-10-19T11:01:00', 0, 'COMMISSION', '', '-5', '0', '0', '0'),
]
# Snapshot at 2026-10-20 12:00:00 in the same assumed server clock.
# magic, POSITION_PROFIT, POSITION_SWAP
POSITIONS = [(101, '-80', '-1'), (202, '10', '-0.5')]


def history(start, end):
    """Start-inclusive/end-exclusive, trading deals only, by deal magic."""
    result = {}
    for time, magic, kind, entry, *money in DEALS:
        if kind not in ('BUY', 'SELL') or not start <= time < end:
            continue
        row = result.setdefault(magic, [D('0'), 0])
        row[0] += sum(map(D, money), D('0'))
        row[1] += entry in ('OUT', 'INOUT', 'OUT_BY')
    return result


def floating():
    result = {}
    for magic, profit, swap in POSITIONS:
        result[magic] = result.get(magic, D('0')) + D(profit) + D(swap)
    return result


if __name__ == '__main__':
    prior = history('2026-10-06T12:00:00', '2026-10-13T12:00:00')
    recent = history('2026-10-13T12:00:00', '2026-10-20T12:00:00')
    current = floating()
    # Pin the hand-worked example. These checks do not test the MT5 product.
    assert prior == {101: [D('38'), 1], 202: [D('-22'), 1]}
    assert recent == {101: [D('110.0'), 1], 202: [D('-42'), 1]}
    assert current == {101: D('-81'), 202: D('9.5')}
    assert sum(row[0] for row in recent.values()) == D('68')
    assert history('2026-10-19T10:00:00', '2026-10-19T10:00:01') == {101: [D('-2'), 0]}
    assert history('2026-10-19T11:00:00', '2026-10-20T00:00:00') == {}
    assert datetime.fromisoformat('2026-10-20T12:00:00') - datetime.fromisoformat('2026-10-13T12:00:00') == datetime.fromisoformat('2026-10-13T12:00:00') - datetime.fromisoformat('2026-10-06T12:00:00')
    print('SYNTHETIC USD EXAMPLE - no account or product performance')
    print('Magic   Prior 7d   Recent 7d   Closing deals   Open now')
    for magic in sorted(recent):
        print(f'{magic:<7} {prior[magic][0]:>8.2f} {recent[magic][0]:>11.2f} {recent[magic][1]:>15} {current[magic]:>10.2f}')
    print('Recent history total: +68.00; open snapshot: -71.50')
    print('Excluded cash items: deposit +1000.00; separate commission -5.00')
    print('Checks passed: totals, equal windows, entry cost, cash exclusion.')
