File size: 2,882 Bytes
590a501
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
from datetime import datetime
from enum import Enum

from pydantic import BaseModel, Field


class OrderSide(str, Enum):
    BUY = "BUY"
    SELL = "SELL"


class OrderType(str, Enum):
    MARKET = "MARKET"
    LIMIT = "LIMIT"
    STOP = "STOP"


class OrderStatus(str, Enum):
    PENDING = "PENDING"
    FILLED = "FILLED"
    PARTIAL = "PARTIAL"
    CANCELLED = "CANCELLED"
    REJECTED = "REJECTED"


class PositionSide(str, Enum):
    LONG = "LONG"
    SHORT = "SHORT"


class StrategyStatus(str, Enum):
    RUNNING = "RUNNING"
    STOPPED = "STOPPED"
    ERROR = "ERROR"


class MarketData(BaseModel):
    symbol: str
    name: str = ""
    exchange: str = ""
    category: str = ""
    open: float
    high: float
    low: float
    close: float
    volume: int
    timestamp: datetime
    bid: float = 0.0
    ask: float = 0.0
    turnover: float = 0.0
    open_interest: int = 0
    pre_close: float = 0.0
    pre_settlement: float = 0.0
    settlement: float = 0.0
    change_pct: float = 0.0


class KlineData(BaseModel):
    symbol: str
    interval: str = "1m"
    open: float
    high: float
    low: float
    close: float
    volume: int
    timestamp: datetime


class OrderRequest(BaseModel):
    symbol: str
    side: OrderSide
    order_type: OrderType = OrderType.MARKET
    quantity: int = Field(gt=0)
    price: float | None = None
    stop_price: float | None = None
    strategy_id: str | None = None


class OrderResponse(BaseModel):
    order_id: str
    symbol: str
    side: OrderSide
    order_type: OrderType
    quantity: int
    filled_quantity: int = 0
    price: float | None = None
    avg_price: float = 0.0
    status: OrderStatus
    strategy_id: str | None = None
    created_at: datetime
    updated_at: datetime


class Position(BaseModel):
    symbol: str
    side: PositionSide
    quantity: int
    avg_price: float
    current_price: float = 0.0
    unrealized_pnl: float = 0.0
    realized_pnl: float = 0.0
    margin: float = 0.0
    leverage: int = 10


class AccountInfo(BaseModel):
    total_balance: float = 1_000_000.0
    available_balance: float = 1_000_000.0
    used_margin: float = 0.0
    unrealized_pnl: float = 0.0
    realized_pnl: float = 0.0
    positions: list[Position] = []


class StrategyConfig(BaseModel):
    strategy_id: str
    strategy_type: str
    symbol: str
    params: dict = {}
    status: StrategyStatus = StrategyStatus.STOPPED


class StrategyPerformance(BaseModel):
    strategy_id: str
    total_trades: int = 0
    winning_trades: int = 0
    losing_trades: int = 0
    total_pnl: float = 0.0
    max_drawdown: float = 0.0
    sharpe_ratio: float = 0.0
    win_rate: float = 0.0


class TradeRecord(BaseModel):
    trade_id: str
    order_id: str
    symbol: str
    side: OrderSide
    quantity: int
    price: float
    pnl: float = 0.0
    strategy_id: str | None = None
    timestamp: datetime