from __future__ import annotations from pydantic import BaseModel, Field from typing import Literal, Optional from datetime import datetime class Summary(BaseModel): major_concern_nifty50: str = Field(..., description="Single most important risk or watch-out for Nifty50") trade_reasoning_nifty50: str trade_strategy_nifty50: str major_concern_banknifty: str trade_reasoning_banknifty: str trade_strategy_banknifty: str class SummaryBankNifty(BaseModel): major_concern: str sentiment: Literal["bullish", "bearish"] reasoning: str trade_strategy: str news_summary: str class TradePlan(BaseModel): status: Literal["No trade", "Trade"] brief_reason: str type: Literal["long", "short", "none"] entry_at: float target: float stoploss: float class DecisionOutput(BaseModel): summary_banknifty: SummaryBankNifty trade: TradePlan class PositionState(BaseModel): entered: bool = False entry_time: Optional[datetime] = None entry_price: Optional[float] = None side: Optional[Literal["long", "short","none"]] = None exited: bool = False exit_time: Optional[datetime] = None exit_price: Optional[float] = None exit_reason: Optional[Literal["target", "stoploss", "Exited at market price","market close"]] = None pnl_pct: Optional[float] = None open_position: bool = False unrealized_pct: Optional[float] = None note: Optional[str] = None def to_compact(self, now: datetime) -> dict: keys = [ "entered","entry_price","side","exited","exit_price","exit_reason", "pnl_pct","open_position","unrealized_pct","note" ] out = {k: getattr(self, k) for k in keys if getattr(self, k) is not None} if self.open_position and self.entry_time: out["trade_duration"] = now - self.entry_time return out TRADE_SCHEMA = { "type": "object", "properties": { "trade": { "type": "object", "properties": { "status": {"type": "string", "enum": ["No trade", "Trade"]}, "brief_reason": {"type": "string"}, "type": {"type": "string", "enum": ["long", "short", "none"]}, "entry_at": {"type": "number"}, "target": {"type": "number"}, "stoploss": {"type": "number"} }, # IMPORTANT: OpenAI requires 'required' and it must list every key "required": ["status","brief_reason", "type", "entry_at", "target", "stoploss"], "additionalProperties": False } }, "required": ["trade"], "additionalProperties": False } RESPONSE_SCHEMA = { "type": "object", "properties": { "summary_banknifty": { "type": "object", "properties": { "major_concern": {"type": "string"}, "sentiment": {"type": "string", "enum": ["bullish", "bearish"]}, "reasoning": {"type": "string"}, "trade_strategy": {"type": "string"}, "news_summary": {"type": "string"} }, "required": ["major_concern", "sentiment", "reasoning", "trade_strategy","news_summary"], "additionalProperties": False }, "trade": { "type": "object", "properties": { "status": {"type": "string", "enum": ["No trade", "Trade"]}, "brief_reason": {"type": "string"}, "type": {"type": "string", "enum": ["long", "short", "none"]}, "entry_at": {"type": "number"}, "target": {"type": "number"}, "stoploss": {"type": "number"} }, "required": ["status", "brief_reason", "type", "entry_at", "target", "stoploss"], "additionalProperties": False } }, "required": ["summary_banknifty", "trade"], "additionalProperties": False }