From cc68bc880407d55837e02052c7ab098079641843 Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Sun, 27 Apr 2025 20:56:41 -0600 Subject: add market option to bids --- server/api/bids.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'server/api/bids.py') diff --git a/server/api/bids.py b/server/api/bids.py index 9da6825..075b82c 100644 --- a/server/api/bids.py +++ b/server/api/bids.py @@ -7,8 +7,15 @@ from datetime import datetime, timezone from zoneinfo import ZoneInfo from typing import List, Optional +# TODO: Can you submit a bid for 2AM the next day after 11AM? + router = APIRouter() -NEW_ENGLAND_TZ = ZoneInfo("America/New_York") + +MARKET_TIMEZONES = { + "ISONE": ZoneInfo("America/New_York"), + "NYISO": ZoneInfo("America/New_York"), + "MISO": ZoneInfo("America/Chicago"), +} def get_db(): db = SessionLocal() @@ -22,6 +29,7 @@ class BidBase(BaseModel): quantity: float price: float user_id: int # In production the user_id should be obtained from the authenticated user + market: str class BidCreate(BidBase): pass @@ -32,7 +40,7 @@ class BidResponse(BidBase): pnl: Optional[float] class Config: - from_atrributes = True + from_attributes = True @router.get("/", response_model=List[BidResponse]) def get_bids(db: Session = Depends(get_db)): @@ -40,13 +48,17 @@ def get_bids(db: Session = Depends(get_db)): @router.post("/", response_model=BidResponse) def submit_bid(bid: BidCreate, db: Session = Depends(get_db)): - # TODO: Can you submit a bid for 2AM the next day after 11AM? - now = datetime.now(NEW_ENGLAND_TZ) - bid_timestamp_local = bid.timestamp.astimezone(NEW_ENGLAND_TZ) + if bid.market not in MARKET_TIMEZONES: + raise HTTPException(status_code=400, detail=f"Invalid market. Supported markets: {list(MARKET_TIMEZONES.keys())}") + + market_tz = MARKET_TIMEZONES[bid.market] + + now = datetime.now(market_tz) + bid_timestamp_local = bid.timestamp.astimezone(market_tz) bid_day = bid_timestamp_local.date() today = now.date() - print(f"Received bid for {bid_day} at {bid.timestamp}") + print(f"Received bid for {bid_day} at {bid.timestamp} ({bid.market})") if bid.timestamp < now: raise HTTPException(status_code=400, detail="Cannot submit bids in the past.") @@ -55,24 +67,26 @@ def submit_bid(bid: BidCreate, db: Session = Depends(get_db)): if bid_day == today: if now > cutoff_time: - raise HTTPException(status_code=400, detail="Cannot submit bids for today after 11am.") + raise HTTPException(status_code=400, detail="Cannot submit bids for today after 11AM local time.") start_of_hour = bid.timestamp.replace(minute=0, second=0, microsecond=0) end_of_hour = start_of_hour.replace(minute=59, second=59, microsecond=999999) bid_count = db.query(BidModel).filter( BidModel.timestamp >= start_of_hour, - BidModel.timestamp <= end_of_hour + BidModel.timestamp <= end_of_hour, + BidModel.market == bid.market ).count() if bid_count >= 10: - raise HTTPException(status_code=400, detail="Cannot submit more than 10 bids for this hour.") + raise HTTPException(status_code=400, detail="Cannot submit more than 10 bids for this hour in this market.") db_bid = BidModel( timestamp=bid.timestamp, quantity=bid.quantity, price=bid.price, user_id=bid.user_id, + market=bid.market, status="Submitted", pnl=None ) -- cgit v1.2.3