dbt_bitcoin is a dbt project designed to analyze Bitcoin trading data. It transforms raw data into meaningful insights, providing key metrics related to trading performance.
- Hourly and daily trading analysis
- Profit and Loss (PnL) calculations
- Flexible configuration for entry days
- Comprehensive data validation tests
- Homebrew: Ensure you have Homebrew installed on macOS for package management.
- Anaconda: This project uses Anaconda for managing Python environments.
- PostgreSQL: This project is configured to work with PostgreSQL.
Homebrew is a package manager for macOS, which simplifies installing PostgreSQL and other utilities.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Using Homebrew, install PostgreSQL:
brew install postgresql
brew services start postgresqlCheck that PostgreSQL is running:
psql --versionDownload and install Anaconda from their official website.
conda create -n my_dbt_env python=3.9
conda activate my_dbt_envpip install dbt-core dbt-postgres psycopg2Verify the installation:
dbt --versionconda install jupyterCreate a new dbt project named dbt_bitcoin:
dbt init dbt_bitcoinSet up your profiles.yml file:
Navigate to your dbt profiles directory (usually located in ~/.dbt/). Create or edit the profiles.yml file to include your database connection details:
dbt_bitcoin:
outputs:
dev:
type: postgres
host: localhost
user: postgres
password: # Your PostgreSQL password
port: 5432
dbname: bitcoin_data
schema: analytics
target: devConnect to PostgreSQL Start the PostgreSQL service:
brew services start postgresqlConnect to PostgreSQL:
psql -U postgres -h localhostCreate the Database and Schema
Run the following commands in the PostgreSQL shell:
CREATE DATABASE bitcoin_data;
\qConnect to the bitcoin_data database:
psql -U postgres -h localhost -d bitcoin_dataCreate the analytics schema:
CREATE SCHEMA IF NOT EXISTS analytics;Create the Main Table Create the btc_usdt_1s table:
CREATE TABLE IF NOT EXISTS analytics.btc_usdt_1s (
"Open Time" TIMESTAMPTZ PRIMARY KEY,
"Open" NUMERIC,
"High" NUMERIC,
"Low" NUMERIC,
"Close" NUMERIC,
"Volume" NUMERIC,
"Close Time" TIMESTAMPTZ,
"Quote Asset Volume" NUMERIC,
"Number of Trades" INTEGER,
"Taker Buy Base Asset Volume" NUMERIC,
"Taker Buy Quote Asset Volume" NUMERIC,
"Ignore" TEXT
);Handle Duplicates on "Open Time" Column To handle duplicates, use a temporary table:
CREATE TEMP TABLE temp_btc_usdt_1s (
"Open Time" TIMESTAMPTZ,
"Open" NUMERIC,
"High" NUMERIC,
"Low" NUMERIC,
"Close" NUMERIC,
"Volume" NUMERIC,
"Close Time" TIMESTAMPTZ,
"Quote Asset Volume" NUMERIC,
"Number of Trades" INTEGER,
"Taker Buy Base Asset Volume" NUMERIC,
"Taker Buy Quote Asset Volume" NUMERIC,
"Ignore" TEXT
);Load the data into the temporary table:
\copy temp_btc_usdt_1s FROM 'half2_BTCUSDT_1s.csv' DELIMITER ',' CSV HEADER;Insert data into the main table, handling duplicates:
INSERT INTO analytics.btc_usdt_1s (
"Open Time", "Open", "High", "Low", "Close", "Volume",
"Close Time", "Quote Asset Volume", "Number of Trades",
"Taker Buy Base Asset Volume", "Taker Buy Quote Asset Volume", "Ignore"
)
SELECT * FROM temp_btc_usdt_1s
ON CONFLICT ("Open Time") DO NOTHING;This section describes the lineage and dependencies between the models in the dbt_bitcoin project.
- Path:
dbt_bitcoin/models/trading/stg_btc.sql - Description: Loads and prepares the raw Bitcoin OHLC data from the
btc_usdt_1stable in theanalyticsschema. - Downstream Dependencies:
daily_trading
- Path:
dbt_bitcoin/models/trading/daily_trading.sql - Description: Aggregates trades executed every hour. Tracks hourly buy and sell prices and calculates the total trades per hour.
- Upstream Dependencies:
stg_btc
- Downstream Dependencies:
returns_losses
- Path:
dbt_bitcoin/models/trading/returns_losses.sql - Description: Calculates total returns and identifies maximum losses for each hour.
- Upstream Dependencies:
daily_trading
- Downstream Dependencies:
max_returnmin_maxLoss
- Path:
dbt_bitcoin/models/trading/max_return.sql - Description: Identifies the hour of the day with the highest returns.
- Upstream Dependencies:
returns_losses
- Path:
dbt_bitcoin/models/trading/min_maxLoss.sql - Description: Identifies the hour with the lowest maximum loss.
- Upstream Dependencies:
returns_losses
Finally, run dbt with your specified variables:
dbt build --vars '{"entry_days": ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],"start_date": '2024-01-01', "end_date": '2024-08-01'}'
--target dev