Database API Reference¶
SQLite¶
financelib.database.sqlite.SQLite(db_name: str)
¶
SQLite database wrapper for FinanceLib data storage.
Creates a date-stamped database file and provides methods for inserting and querying news articles and stock data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db_name
|
str
|
Logical name for the database (used in filename). |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
filename |
Full database filename with date stamp. |
|
db_name |
Logical database name. |
|
conn |
SQLite connection. |
|
cursor |
SQLite cursor. |
Example
db = SQLite("market_data") db.insert_article(news_model) db.close()
Source code in financelib/database/sqlite.py
Functions¶
check_table(table_name: str) -> bool
¶
Check if a table exists in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Name of the table to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the table exists, False otherwise. |
Source code in financelib/database/sqlite.py
close() -> None
¶
execute(query: str, params: Optional[tuple] = None) -> None
¶
Execute an arbitrary SQL query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
SQL query string. |
required |
params
|
Optional[tuple]
|
Optional query parameters. |
None
|
Source code in financelib/database/sqlite.py
fetchall(query: str, params: Optional[tuple] = None) -> List[Tuple]
¶
Execute a query and return all results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
SQL query string. |
required |
params
|
Optional[tuple]
|
Optional query parameters. |
None
|
Returns:
| Type | Description |
|---|---|
List[Tuple]
|
List of result tuples. |
Source code in financelib/database/sqlite.py
fetchone(query: str, params: Optional[tuple] = None) -> Optional[Tuple]
¶
Execute a query and return the first result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
SQL query string. |
required |
params
|
Optional[tuple]
|
Optional query parameters. |
None
|
Returns:
| Type | Description |
|---|---|
Optional[Tuple]
|
First result tuple or None. |
Source code in financelib/database/sqlite.py
insert_article(news: NewsModel) -> None
¶
Insert a single news article into the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
news
|
NewsModel
|
NewsModel instance to insert. |
required |
Source code in financelib/database/sqlite.py
insert_articles(news_list: List[NewsModel]) -> None
¶
Insert multiple news articles in a single transaction (batch).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
news_list
|
List[NewsModel]
|
List of NewsModel instances to insert. |
required |
Source code in financelib/database/sqlite.py
insert_stock_data(stock_data: StockDataModel) -> None
¶
Insert stock data into the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stock_data
|
StockDataModel
|
StockDataModel instance to insert. |
required |
Source code in financelib/database/sqlite.py
insert_stock_data_batch(stock_list: List[StockDataModel]) -> None
¶
Insert multiple stock data records in a single transaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stock_list
|
List[StockDataModel]
|
List of StockDataModel instances. |
required |
Source code in financelib/database/sqlite.py
transaction() -> _Transaction
¶
Create a transaction context manager.
Inside a transaction, operations are atomic — they all commit together on success, or all roll back on error.
Example
with db.transaction(): ... db.execute("INSERT INTO news (title) VALUES (?)", ("A",)) ... db.execute("INSERT INTO news (title) VALUES (?)", ("B",)) ... # auto-commits on success, rollback on error
Source code in financelib/database/sqlite.py
PostgreSQL¶
financelib.database.postgres.PostgreSQL(connection_string: Optional[str] = None)
¶
PostgreSQL database wrapper for FinanceLib.
Provides the same API as the SQLite wrapper for easy migration. Stores news articles, stock data, trade history, and indicator snapshots.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection_string
|
Optional[str]
|
PostgreSQL connection string.
Format: |
None
|
Example
db = PostgreSQL("postgresql://user:pass@localhost:5432/financelib") db.insert_stock_data(stock_model) db.close()
Or use environment variable¶
import os os.environ["DATABASE_URL"] = "postgresql://user:pass@localhost/financelib" with PostgreSQL() as db: ... db.insert_article(news_model)
Source code in financelib/database/postgres.py
Functions¶
check_table(table_name: str) -> bool
¶
Check if a table exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name
|
str
|
Table name to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the table exists. |
Source code in financelib/database/postgres.py
close() -> None
¶
execute(query: str, params: Optional[tuple] = None) -> None
¶
Execute an arbitrary SQL query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
SQL query (use %s placeholders). |
required |
params
|
Optional[tuple]
|
Query parameters. |
None
|
Source code in financelib/database/postgres.py
fetchall(query: str, params: Optional[tuple] = None) -> List[Tuple]
¶
Execute a query and return all rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
SQL query. |
required |
params
|
Optional[tuple]
|
Query parameters. |
None
|
Returns:
| Type | Description |
|---|---|
List[Tuple]
|
List of result tuples. |
Source code in financelib/database/postgres.py
fetchone(query: str, params: Optional[tuple] = None) -> Optional[Tuple]
¶
Execute a query and return first row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
SQL query. |
required |
params
|
Optional[tuple]
|
Query parameters. |
None
|
Returns:
| Type | Description |
|---|---|
Optional[Tuple]
|
First result tuple or None. |
Source code in financelib/database/postgres.py
insert_article(news: NewsModel) -> None
¶
Insert a news article.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
news
|
NewsModel
|
NewsModel instance. |
required |
Source code in financelib/database/postgres.py
insert_articles(news_list: List[NewsModel]) -> None
¶
Insert multiple news articles in a batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
news_list
|
List[NewsModel]
|
List of NewsModel instances. |
required |
Source code in financelib/database/postgres.py
insert_indicator_snapshot(symbol: str, rsi: float = 0, macd: float = 0, macd_signal: float = 0, sma_20: float = 0, sma_50: float = 0, ema_20: float = 0, bb_upper: float = 0, bb_middle: float = 0, bb_lower: float = 0, atr: float = 0, adx: float = 0, stochastic_k: float = 0, stochastic_d: float = 0, williams_r: float = 0) -> None
¶
Insert a snapshot of all indicator values for a symbol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Stock/crypto symbol. |
required |
rsi
|
float
|
RSI value. |
0
|
macd
|
float
|
MACD line value. |
0
|
macd_signal
|
float
|
MACD signal line value. |
0
|
sma_20
|
float
|
20-period SMA. |
0
|
sma_50
|
float
|
50-period SMA. |
0
|
ema_20
|
float
|
20-period EMA. |
0
|
bb_upper
|
float
|
Bollinger upper band. |
0
|
bb_middle
|
float
|
Bollinger middle band. |
0
|
bb_lower
|
float
|
Bollinger lower band. |
0
|
atr
|
float
|
Average True Range. |
0
|
adx
|
float
|
Average Directional Index. |
0
|
stochastic_k
|
float
|
Stochastic %K. |
0
|
stochastic_d
|
float
|
Stochastic %D. |
0
|
williams_r
|
float
|
Williams %R. |
0
|
Source code in financelib/database/postgres.py
insert_stock_data(stock_data: StockDataModel) -> None
¶
Insert stock data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stock_data
|
StockDataModel
|
StockDataModel instance. |
required |
Source code in financelib/database/postgres.py
insert_trade(symbol: str, side: str, price: float, amount: float, market: str = 'spot', leverage: int = 1, pnl: float = 0.0, paper: bool = True) -> None
¶
Insert a trade record.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
symbol
|
str
|
Trading pair symbol. |
required |
side
|
str
|
Trade side ('buy', 'sell'). |
required |
price
|
float
|
Execution price. |
required |
amount
|
float
|
Trade amount. |
required |
market
|
str
|
Market type. |
'spot'
|
leverage
|
int
|
Leverage used. |
1
|
pnl
|
float
|
Profit/loss. |
0.0
|
paper
|
bool
|
Whether this was a paper trade. |
True
|
Source code in financelib/database/postgres.py
See also: Data Models for NewsModel, StockDataModel, TradeRecord.