File size: 1,690 Bytes
b1f38ad
 
 
 
 
 
 
7869a12
b1f38ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# database.py
from sqlmodel import SQLModel, create_engine, Session, select
from dotenv import load_dotenv 
import os

load_dotenv() 

database_url = os.environ.get("DATABASE_URL", "sqlite:///./algoquant.db")

engine = create_engine(
    database_url, 
    echo=True,
    pool_pre_ping=True,
    pool_recycle=300,
    pool_size=5,
    max_overflow=10
)

def create_db_and_tables():
    SQLModel.metadata.create_all(engine)


def initialize_portfolio_if_empty(user_email: str = "default_user"):
    """
    Initialize portfolio with 10,000 USDT only on first run.
    Persistent across restarts - won't reset if portfolio already exists.
    """
    from models import PortfolioAsset
    
    with Session(engine) as session:
        # Check if this user has any portfolio assets
        statement = select(PortfolioAsset).where(PortfolioAsset.user_email == user_email)
        existing_assets = session.exec(statement).all()
        
        if not existing_assets:
            # First run - initialize with starting capital
            print(f"[Portfolio] Initializing new portfolio for {user_email} with 10,000 USDT")
            usdt_asset = PortfolioAsset(
                symbol="USDT",
                balance=10000.0,
                user_email=user_email
            )
            session.add(usdt_asset)
            session.commit()
            print("[Portfolio] ✅ Starting capital deposited: 10,000 USDT")
        else:
            total_assets = len(existing_assets)
            print(f"[Portfolio] Loading existing portfolio for {user_email} ({total_assets} assets)")
            for asset in existing_assets:
                print(f"  - {asset.symbol}: {asset.balance:.8f}")