Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,18 @@ def submit_strategy(req: StrategySubmit, db: Session = Depends(get_db)):
).scalar_one_or_none()

if existing:
# Remove the duplicate file we just wrote
os.remove(strategy_path)
# Keep the canonical file referenced by the existing StrategyVersion.
# Only remove the just-written duplicate when it points elsewhere.
if os.path.abspath(strategy_path) != os.path.abspath(existing.strategy_path):
if os.path.exists(strategy_path):
os.remove(strategy_path)

# Repair stale records if the canonical file was removed previously.
if not os.path.exists(existing.strategy_path):
ensure_strategies_dir()
with open(existing.strategy_path, "w", encoding="utf-8") as f:
f.write(req.code)

return StrategyCreateResponse(
strategy_id=existing.strategy_id,
strategy_version_id=existing.id,
Expand Down
26 changes: 24 additions & 2 deletions scripts/demo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,35 @@ STRATEGY_RESPONSE=$(curl -s -X POST http://127.0.0.1:8001/strategies \
-H 'Content-Type: application/json' \
-d "{\"strategy_path\":\"$STRATEGY_PATH\"}")

echo "Strategy response: $STRATEGY_RESPONSE"
echo "Strategy response (path): $STRATEGY_RESPONSE"

RUN_RESPONSE=$(curl -s -X POST http://127.0.0.1:8001/runs \
-H 'Content-Type: application/json' \
-d "{\"strategy_path\":\"$STRATEGY_PATH\"}")

echo "Run response: $RUN_RESPONSE"
echo "Run response (path): $RUN_RESPONSE"

SUBMIT_PAYLOAD=$("$ROOT_DIR/.venv312/bin/python" - <<'PY'
import json
from pathlib import Path
code = Path("examples/strategies/buy_and_hold.py").read_text(encoding="utf-8")
print(json.dumps({"name": "demo_submitted_strategy", "code": code}))
PY
)

SUBMIT_RESPONSE=$(curl -s -X POST http://127.0.0.1:8001/strategies/submit \
-H 'Content-Type: application/json' \
-d "$SUBMIT_PAYLOAD")

echo "Strategy response (submit): $SUBMIT_RESPONSE"

SUBMIT_VERSION_ID=$("$ROOT_DIR/.venv312/bin/python" -c 'import json,sys; print(json.loads(sys.argv[1])["strategy_version_id"])' "$SUBMIT_RESPONSE")

RUN_SUBMIT_RESPONSE=$(curl -s -X POST http://127.0.0.1:8001/runs \
-H 'Content-Type: application/json' \
-d "{\"strategy_version_id\":$SUBMIT_VERSION_ID}")

echo "Run response (submitted code): $RUN_SUBMIT_RESPONSE"

LEADERBOARD=$(curl -s "http://127.0.0.1:8001/leaderboard?dataset_version=$DATASET_VERSION")
echo "Leaderboard: $LEADERBOARD"