File size: 3,977 Bytes
45a77a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

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
}