73 lines
2.4 KiB
Python
73 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
import csv, json, sys, argparse
|
|
from collections import defaultdict
|
|
|
|
def read_csv(path):
|
|
with open(path, newline='') as f:
|
|
reader = csv.DictReader(f)
|
|
return list(reader)
|
|
|
|
def parse_date(dt):
|
|
# dt is like '2024-06-15 12:34:56+00'
|
|
return dt.split(' ')[0] # just the date part
|
|
|
|
def main():
|
|
p = argparse.ArgumentParser()
|
|
p.add_argument('--trades', required=True)
|
|
p.add_argument('--positions', required=True)
|
|
p.add_argument('--user-id', type=int, required=True)
|
|
p.add_argument('--start-date', required=True)
|
|
p.add_argument('--end-date', required=True)
|
|
p.add_argument('--output', required=True)
|
|
args = p.parse_args()
|
|
|
|
trades = []
|
|
with open(args.trades) as f:
|
|
reader = csv.DictReader(f)
|
|
for row in reader:
|
|
# filter by date range (date part of exec_timestamp)
|
|
exec_date = parse_date(row['exec_timestamp'])
|
|
if args.start_date <= exec_date <= args.end_date:
|
|
trades.append(row)
|
|
|
|
# total_fills
|
|
total_fills = len(trades)
|
|
|
|
# total_pnl (only completed trades)
|
|
total_pnl = sum(float(r['net_pnl']) for r in trades if r['completed_trade_id'])
|
|
|
|
# symbol_quantities
|
|
symbol_qty = defaultdict(int)
|
|
for r in trades:
|
|
symbol_qty[r['symbol']] += int(r['qty'])
|
|
|
|
# position mismatches
|
|
mismatches = []
|
|
# load positions into dict keyed by symbol
|
|
pos_by_symbol = defaultdict(int)
|
|
with open(args.positions) as f:
|
|
reader = csv.DictReader(f)
|
|
for row in reader:
|
|
if row['user_id'] == str(args.user_id):
|
|
pos_by_symbol[row['symbol']] += int(row['net_qty'])
|
|
|
|
for symbol, trade_total in symbol_qty.items():
|
|
pos_total = pos_by_symbol.get(symbol, 0)
|
|
if abs(trade_total - pos_total) > 1:
|
|
mismatches.append(symbol)
|
|
|
|
report = {
|
|
"user_id": args.user_id,
|
|
"date_range_start": args.start_date,
|
|
"date_range_end": args.end_date,
|
|
"total_fills": total_fills,
|
|
"total_pnl": round(total_pnl, 2),
|
|
"symbol_quantities": dict(symbol_qty),
|
|
"position_mismatches": mismatches
|
|
}
|
|
|
|
with open(args.output, 'w') as f:
|
|
json.dump(report, f, indent=2)
|
|
|
|
if __name__ == '__main__':
|
|
main() |