first commit of restructured project
This commit is contained in:
129
trahn-trade-backend/db/README.md
Normal file
129
trahn-trade-backend/db/README.md
Normal file
@@ -0,0 +1,129 @@
|
||||
# Database Setup
|
||||
|
||||
## Quick Start
|
||||
|
||||
### 1. Install And Run PostgreSQL
|
||||
|
||||
**macOS:**
|
||||
```bash
|
||||
brew install postgresql@15
|
||||
brew services start postgresql@15
|
||||
```
|
||||
|
||||
**Ubuntu/Debian:**
|
||||
```bash
|
||||
sudo apt update
|
||||
sudo apt install postgresql postgresql-contrib
|
||||
sudo systemctl start postgresql
|
||||
```
|
||||
|
||||
**Docker:**
|
||||
```bash
|
||||
docker run --name trahn-postgres \
|
||||
-e POSTGRES_PASSWORD=your_password \
|
||||
-p 5432:5432 \
|
||||
-d postgres:15
|
||||
```
|
||||
|
||||
### 2. Create Database and Schema
|
||||
|
||||
```bash
|
||||
# Connect as postgres user
|
||||
psql -U postgres
|
||||
|
||||
# Create database
|
||||
CREATE DATABASE trahn_grid_trader;
|
||||
|
||||
# Exit psql
|
||||
\q
|
||||
|
||||
# Run schema
|
||||
psql -U postgres -d trahn_grid_trader -f db/schema.sql
|
||||
```
|
||||
|
||||
### 3. Configure .env
|
||||
|
||||
Add to your `.env` file:
|
||||
```bash
|
||||
DB_HOST=localhost
|
||||
DB_PORT=5432
|
||||
DB_NAME=trahn_grid_trader
|
||||
DB_USER=postgres
|
||||
DB_PASSWORD=your_secure_password_here
|
||||
```
|
||||
|
||||
### 4. Test Connection
|
||||
|
||||
```bash
|
||||
make dev
|
||||
# Verify "[DB] Connection successful" appears in the output
|
||||
```
|
||||
|
||||
## Schema Overview
|
||||
|
||||
### Tables
|
||||
|
||||
1. **price_history** - ETH price data points
|
||||
- Timestamp-indexed for fast queries
|
||||
- Organized by trading day (12:00 EST boundary)
|
||||
|
||||
2. **trade_history** - Executed trades
|
||||
- Buy/sell records with full details
|
||||
- Paper trade flag for simulation tracking
|
||||
|
||||
3. **support_resistance_history** - S/R levels over time
|
||||
- Historical record of Dune API fetches
|
||||
- Tracks when grid was recalculated
|
||||
|
||||
4. **grid_state** - Current grid configuration
|
||||
- Grid levels stored as JSONB
|
||||
- State tracking for bot restarts
|
||||
|
||||
### Indexes
|
||||
|
||||
- All tables indexed on `timestamp` for time-series queries
|
||||
- `trading_day` indexed for day-based queries
|
||||
- Optimized for append-heavy workloads
|
||||
|
||||
## Maintenance
|
||||
|
||||
### View Data
|
||||
|
||||
```sql
|
||||
-- Recent prices
|
||||
SELECT * FROM price_history ORDER BY timestamp DESC LIMIT 10;
|
||||
|
||||
-- Recent trades
|
||||
SELECT * FROM trade_history ORDER BY timestamp DESC LIMIT 10;
|
||||
|
||||
-- S/R history
|
||||
SELECT * FROM support_resistance_history ORDER BY timestamp DESC LIMIT 10;
|
||||
|
||||
-- Latest S/R (using view)
|
||||
SELECT * FROM latest_support_resistance;
|
||||
```
|
||||
|
||||
### Cleanup Old Data (optional)
|
||||
|
||||
```sql
|
||||
-- Delete price data older than 90 days
|
||||
DELETE FROM price_history WHERE timestamp < NOW() - INTERVAL '90 days';
|
||||
|
||||
-- Keep all trade history (don't delete)
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Connection Failed
|
||||
- Check PostgreSQL is running: `pg_isready`
|
||||
- Verify credentials in `.env`
|
||||
- Check firewall/network settings
|
||||
|
||||
### Schema Errors
|
||||
- Drop and recreate: `DROP DATABASE trahn_grid_trader; CREATE DATABASE trahn_grid_trader;`
|
||||
- Re-run schema.sql
|
||||
|
||||
### Performance
|
||||
- If queries slow, add more indexes
|
||||
- Consider partitioning price_history by month (for large datasets)
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
-- Migration: Add Paper Wallet Columns to grid_state
|
||||
-- This allows grid_state to store paper trading virtual wallet data
|
||||
|
||||
ALTER TABLE grid_state
|
||||
ADD COLUMN IF NOT EXISTS paper_eth_balance DECIMAL(18, 8),
|
||||
ADD COLUMN IF NOT EXISTS paper_usdc_balance DECIMAL(12, 2),
|
||||
ADD COLUMN IF NOT EXISTS paper_total_gas_spent DECIMAL(18, 8),
|
||||
ADD COLUMN IF NOT EXISTS paper_trades_json JSONB,
|
||||
ADD COLUMN IF NOT EXISTS paper_start_time TIMESTAMPTZ,
|
||||
ADD COLUMN IF NOT EXISTS paper_initial_eth DECIMAL(18, 8),
|
||||
ADD COLUMN IF NOT EXISTS paper_initial_usdc DECIMAL(12, 2);
|
||||
|
||||
-- For live trading mode, these columns will be NULL
|
||||
-- For paper mode, these columns store the virtual wallet state
|
||||
|
||||
80
trahn-trade-backend/db/schema.sql
Normal file
80
trahn-trade-backend/db/schema.sql
Normal file
@@ -0,0 +1,80 @@
|
||||
-- Trahn Grid Trader Database Schema
|
||||
-- PostgreSQL
|
||||
|
||||
-- Table 1: Price History
|
||||
-- Stores ETH price data points from CoinGecko
|
||||
CREATE TABLE IF NOT EXISTS price_history (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
timestamp TIMESTAMPTZ NOT NULL,
|
||||
price DECIMAL(12, 2) NOT NULL,
|
||||
trading_day DATE NOT NULL,
|
||||
source VARCHAR(50) DEFAULT 'coingecko',
|
||||
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_price_timestamp ON price_history(timestamp);
|
||||
CREATE INDEX IF NOT EXISTS idx_price_trading_day ON price_history(trading_day);
|
||||
|
||||
-- Table 2: Trade History
|
||||
-- Stores executed trades (buys and sells)
|
||||
CREATE TABLE IF NOT EXISTS trade_history (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
timestamp TIMESTAMPTZ NOT NULL,
|
||||
trading_day DATE NOT NULL,
|
||||
side VARCHAR(10) NOT NULL CHECK (side IN ('buy', 'sell')),
|
||||
price DECIMAL(12, 2) NOT NULL,
|
||||
quantity DECIMAL(18, 8) NOT NULL,
|
||||
usd_value DECIMAL(12, 2) NOT NULL,
|
||||
grid_level INTEGER,
|
||||
tx_hash VARCHAR(66),
|
||||
is_paper_trade BOOLEAN DEFAULT FALSE,
|
||||
slippage_percent DECIMAL(5, 3),
|
||||
gas_cost_eth DECIMAL(18, 8),
|
||||
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_trade_timestamp ON trade_history(timestamp);
|
||||
CREATE INDEX IF NOT EXISTS idx_trade_trading_day ON trade_history(trading_day);
|
||||
CREATE INDEX IF NOT EXISTS idx_trade_side ON trade_history(side);
|
||||
|
||||
-- Table 3: Support/Resistance History
|
||||
-- Stores S/R levels fetched from Dune Analytics over time
|
||||
CREATE TABLE IF NOT EXISTS support_resistance_history (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
timestamp TIMESTAMPTZ NOT NULL,
|
||||
method VARCHAR(20) NOT NULL,
|
||||
lookback_days INTEGER NOT NULL,
|
||||
support DECIMAL(12, 2) NOT NULL,
|
||||
resistance DECIMAL(12, 2) NOT NULL,
|
||||
midpoint DECIMAL(12, 2) NOT NULL,
|
||||
avg_price DECIMAL(12, 2),
|
||||
grid_recalculated BOOLEAN DEFAULT FALSE,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_sr_timestamp ON support_resistance_history(timestamp);
|
||||
|
||||
-- Table 4: Grid State
|
||||
-- Stores current grid configuration and state
|
||||
-- Only one active row at a time (or keyed by bot instance)
|
||||
CREATE TABLE IF NOT EXISTS grid_state (
|
||||
id SERIAL PRIMARY KEY,
|
||||
base_price DECIMAL(12, 2),
|
||||
grid_levels_json JSONB,
|
||||
trades_executed INTEGER DEFAULT 0,
|
||||
total_profit DECIMAL(12, 2) DEFAULT 0,
|
||||
last_sr_refresh TIMESTAMPTZ,
|
||||
is_active BOOLEAN DEFAULT TRUE,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_grid_active ON grid_state(is_active);
|
||||
|
||||
-- View: Latest S/R
|
||||
-- Convenience view for getting the most recent S/R data
|
||||
CREATE OR REPLACE VIEW latest_support_resistance AS
|
||||
SELECT * FROM support_resistance_history
|
||||
ORDER BY timestamp DESC
|
||||
LIMIT 1;
|
||||
|
||||
18
trahn-trade-backend/db/setup.sql
Normal file
18
trahn-trade-backend/db/setup.sql
Normal file
@@ -0,0 +1,18 @@
|
||||
-- Setup script for Trahn Grid Trader database
|
||||
-- Run this as PostgreSQL superuser or database owner
|
||||
|
||||
-- Create database (if it doesn't exist)
|
||||
-- Run manually: CREATE DATABASE trahn_grid_trader;
|
||||
|
||||
-- Connect to the database and run schema.sql
|
||||
-- psql -U postgres -d trahn_grid_trader -f schema.sql
|
||||
|
||||
-- Or run this combined script:
|
||||
-- psql -U postgres -f setup.sql
|
||||
|
||||
CREATE DATABASE IF NOT EXISTS trahn_grid_trader;
|
||||
\c trahn_grid_trader;
|
||||
|
||||
-- Now load the schema
|
||||
\i schema.sql
|
||||
|
||||
Reference in New Issue
Block a user