import json
from decimal import Decimal as D


LIMIT = D("100.15")
SIZE, DEADLINE = 10, 500
OPPORTUNITIES = [
    (0, D("100.04"), 4), (180, D("100.11"), 3),
    (240, D("100.18"), 10), (420, D("100.14"), 3),
    (830, D("100.08"), 2),
]


def fill_on_timeline(start_ms):
    executed, cash = 0, D(0)
    for time, price, available in OPPORTUNITIES:
        if start_ms <= time <= DEADLINE and price <= LIMIT:
            quantity = min(available, SIZE - executed)
            executed += quantity
            cash += price * quantity
    average = float(cash / executed) if executed else None
    return [start_ms, executed, SIZE - executed, average, float(cash)]


def run():
    rows = [fill_on_timeline(delay) for delay in (0, 100, 200, 600)]
    assert [row[1] for row in rows] == [10, 6, 3, 0]
    assert rows[0][3] == 100.091 and rows[1][3] == 100.125
    assert rows[-1][3] is None
    return rows


if __name__ == "__main__":
    print(json.dumps(run()))
