Database¶
FinanceLib provides database storage for market data and news articles.
SQLite (Built-in)¶
from financelib.database.sqlite import SQLite
from financelib.database.models import NewsModel, StockDataModel
# Context manager ensures proper cleanup
with SQLite("market_data") as db:
# Insert news
news = NewsModel(
title="BIST Up 3%",
content="Turkish stocks surged...",
source="Bloomberg",
published_at="2026-01-15",
)
db.insert_article(news)
# Insert stock data
stock = StockDataModel(
symbol="THYAO.IS",
price=172.45,
open_price=170.00,
high=173.50,
low=169.80,
volume=24532100,
change=2.45,
change_percent=1.44,
)
db.insert_stock_data(stock)
# Query
results = db.fetchall("SELECT * FROM stock_data WHERE symbol = ?", ("THYAO.IS",))
PostgreSQL¶
For production deployments, use the PostgreSQL backend.
from financelib.database.postgres import PostgreSQL
from financelib.database.models import StockDataModel
# Connect via connection string or DATABASE_URL env var
with PostgreSQL("postgresql://user:pass@localhost:5432/financelib") as db:
stock = StockDataModel(
symbol="THYAO.IS",
price=172.45,
volume=24532100,
)
db.insert_stock_data(stock)
# Store trade history
db.insert_trade(
symbol="BTC/USDT",
side="buy",
price=65000.0,
amount=0.1,
market="spot",
)
# Store indicator snapshots
db.insert_indicator_snapshot(
symbol="THYAO.IS",
rsi=45.2,
macd=0.0023,
bb_upper=175.0,
bb_lower=168.0,
)
results = db.fetchall("SELECT * FROM stock_data")
Tip
Set DATABASE_URL in your environment and you can create a connection with just PostgreSQL().
Additional PostgreSQL Tables¶
The PostgreSQL backend includes extra tables not in SQLite:
trade_history— Records all trades (symbol, side, price, amount, market, leverage, pnl)indicator_snapshots— Stores all 11 indicator values at a point in timecreated_attimestamps on all tables
Data Models¶
NewsModel¶
| Field | Type | Description |
|---|---|---|
title |
str | Article title |
content |
str | Article body |
published_at |
str | Publication date |
author |
str | Author name |
category |
str | Article category |
source |
str | Source name |
article_url |
str | Article URL |
article_thumbnail_url |
str | Thumbnail URL |
StockDataModel¶
| Field | Type | Description |
|---|---|---|
symbol |
str | Stock ticker |
price |
float | Current price |
open_price |
float | Opening price |
high |
float | Day high |
low |
float | Day low |
volume |
int | Trading volume |
change |
float | Price change |
change_percent |
float | Change % |
currency |
str | Currency code |
timestamp |
str | Data timestamp |