Files
project-work/sources/solve.sh

46 lines
1.9 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
# solution/solve.sh – oracle for the Trading Activity Summary task
set -euo pipefail
# 1. Parse arguments (user_id, start_date, end_date) – assume they are passed as env vars for simplicity
USER_ID="${USER_ID:-1}"
START_DATE="${START_DATE:-2024-01-01}"
END_DATE="${END_DATE:-2024-12-31}"
# 2. Export the relevant data to CSV files (or use psql \copy) – keep it simple:
# We'll use psql to emit CSV files for each table we need.
# The container should have a psql binary and a connection to the bundled DB dump.
# Export trades (only fill events within date range)
psql --quiet --no-psqlrc --set ON_ERROR_STOP=1 <<SQL > /tmp/trades_filtered.csv
\copy (
SELECT trade_id, exec_timestamp, event_type, symbol, qty, net_pnl, completed_trade_id, user_id, account_id
FROM public.trades
WHERE event_type = 'fill'
AND (date_trunc('day', exec_timestamp)::date) BETWEEN '${START_DATE}' AND '${END_DATE}'
) TO '/tmp/trades_filtered.csv' CSV HEADER
SQL
# Export positions for the same user and date range (need account_id)
psql --quiet --no-psqlrc --set ON_ERROR_STOP=1 <<SQL > /tmp/positions_filtered.csv
\copy (
SELECT p.position_id, p.symbol, p.net_qty, p.account_id, p.user_id
FROM public.positions p
JOIN public.trades t ON t.account_id = p.account_id AND t.user_id = p.user_id
WHERE p.user_id = ${USER_ID}
AND (date_trunc('day', t.exec_timestamp)::date) BETWEEN '${START_DATE}' AND '${END_DATE}'
) TO '/tmp/positions_filtered.csv' CSV HEADER
SQL
# 3. Compute aggregates using a small Python script (or awk) that reads the CSVs.
python3 /app/solution/aggregator.py \
--trades /tmp/trades_filtered.csv \
--positions /tmp/positions_filtered.csv \
--user-id ${USER_ID} \
--start-date ${START_DATE} \
--end-date ${END_DATE} \
--output /app/report.json
# 4. Exit with 0 to signal success (reward = 1)
exit 0