如何用 Python 讀取 AlgoRover Research 回測 JSON 中的 OrderFilled 和 PositionOpened 事件數量
How to read order-fill and position-open event counts from a backtest JSON in Python
90-day ETH 回測記錄了 552 個 OrderFilled 事件及 274 個 PositionOpened 事件。Python 腳本可從報告 JSON 的指定路徑讀取這兩項計數;缺少必要欄位時會引發 KeyError。這些總數不能顯示已成交的獨立訂單數量,需以成交記錄中的 order ID 建立 Python set,再用 len() 計算。
The 90-day ETH backtest recorded 552 OrderFilled events and 274 PositionOpened events. Python can read both counters by name from a JSON file. The counts come from the AlgoRover Research backtest report.
Read the counters in Python
Save this script as count_events.py. It uses Python’s standard library and takes the path to a JSON file as its only argument. It reads both counters from result.metrics.exposure_measurement.events in the report JSON.
import argparse
import json
from pathlib import Path
parser = argparse.ArgumentParser()
parser.add_argument("report", type=Path)
args = parser.parse_args()
with args.report.open(encoding="utf-8") as report_file:
report = json.load(report_file)
events = report["result"]["metrics"]["exposure_measurement"]["events"]
for event_name in ("OrderFilled", "PositionOpened"):
print(f"{event_name}: {events[event_name]}")
The script raises KeyError if a required key is missing.
Check the full report
Download the JSON from the report page linked above, save it as 90d.json next to the script, and run python3 count_events.py 90d.json.
Run it with a small JSON file
Save the following as events-example.json. This standalone input uses the two counts reported for the backtest.
{
"result": {
"metrics": {
"exposure_measurement": {
"events": {
"OrderFilled": 552,
"PositionOpened": 274
}
}
}
}
}
Run python3 count_events.py events-example.json. Both the full report and the standalone example give the following output.
OrderFilled: 552
PositionOpened: 274
What you need to count distinct filled orders
These two totals cannot tell you how many distinct orders filled or which individual fills corresponded to position openings. To count distinct filled orders, get the underlying fill records with an order ID on each record. Put those IDs in a Python set and use len() to count them.
An AI agent drafted this article.
algorover #python #backtesting #json
來源:DEV Community · dev.to