67 lines
2.5 KiB
Bash
67 lines
2.5 KiB
Bash
#!/usr/bin/env bash
|
||
# solution/solve.sh – oracle for the Trading Activity Summary task
|
||
|
||
set -euo pipefail
|
||
|
||
# ------------------------------------------------------------------
|
||
# 1️⃣ Parse inputs (you can also read env vars or command‑line args)
|
||
# ------------------------------------------------------------------
|
||
USER_ID="${USER_ID:-1}" # default for quick testing
|
||
START_DATE="${START_DATE:-2024-01-01}" # inclusive
|
||
END_DATE="${END_DATE:-2024-12-31}" # inclusive
|
||
|
||
# ------------------------------------------------------------------
|
||
# 2️⃣ Export the data we need to CSV files (psql \copy is fast and clean)
|
||
# ------------------------------------------------------------------
|
||
# 2a – trades: only 'fill' events within the date window
|
||
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
|
||
|
||
# 2b – positions: we need the net quantity per symbol for the same user/account
|
||
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
|
||
AND (date_trunc('day', t.exec_timestamp)::date) BETWEEN '${START_DATE}' AND '${END_DATE}'
|
||
WHERE p.user_id = ${USER_ID}
|
||
) TO '/tmp/positions_filtered.csv' CSV HEADER
|
||
SQL
|
||
|
||
# ------------------------------------------------------------------
|
||
# 3️⃣ Run a tiny Python helper that does the heavy lifting
|
||
# ------------------------------------------------------------------
|
||
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 successfully → verifier will give reward = 1
|
||
# ------------------------------------------------------------------
|
||
exit 0 |