File size: 1,708 Bytes
b1f38ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4a7fb9
 
 
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
# models.py
from typing import Optional
from datetime import datetime
from sqlmodel import Field, SQLModel

class User(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    email: str = Field(index=True, unique=True) 
    name: Optional[str] = None
    hashed_password: str


class TradingSession(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    session_id: str = Field(index=True, unique=True)
    user_email: str = Field(index=True)
    strategy: str
    symbol: str
    trade_amount: float
    duration_minutes: int
    duration_unit: str = "minutes"  # "minutes" or "days"
    start_time: datetime = Field(default_factory=datetime.now)
    end_time: Optional[datetime] = None
    is_running: bool = True
    total_pnl: float = 0.0
    trades_count: int = 0


class Trade(SQLModel, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    session_id: str = Field(index=True)
    user_email: str = Field(index=True)
    symbol: str
    side: str  # BUY or SELL
    price: float
    quantity: float
    total: float
    pnl: Optional[float] = None
    order_id: Optional[str] = None
    executed_at: datetime = Field(default_factory=datetime.now)


class PortfolioAsset(SQLModel, table=True):
    """Internal simulated wallet for paper trading"""
    symbol: str = Field(primary_key=True)  # e.g., 'USDT', 'BTC', 'ETH'
    user_email: str = Field(primary_key=True, index=True)  # Support multi-user portfolios
    balance: float = Field(default=0.0)
    avg_cost_basis: float = Field(default=0.0)  # Average buy price per unit
    total_invested: float = Field(default=0.0)  # Total USDT invested in this asset