Skip to content

Commit 39527ab

Browse files
Initial commit: MidnightAI Elite RL Trading Bot - 10 model ensemble, long-only strategy, 30-40min training
0 parents  commit 39527ab

15 files changed

Lines changed: 1833 additions & 0 deletions

.github/workflows/train.yml

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
name: Elite RL Training Pipeline
2+
3+
on:
4+
schedule:
5+
# 10 runs per day: Every 2.4 hours
6+
- cron: '0 0 * * *' # 12:00 AM UTC
7+
- cron: '24 2 * * *' # 2:24 AM UTC
8+
- cron: '48 4 * * *' # 4:48 AM UTC
9+
- cron: '12 7 * * *' # 7:12 AM UTC
10+
- cron: '36 9 * * *' # 9:36 AM UTC
11+
- cron: '0 12 * * *' # 12:00 PM UTC
12+
- cron: '24 14 * * *' # 2:24 PM UTC
13+
- cron: '48 16 * * *' # 4:48 PM UTC
14+
- cron: '12 19 * * *' # 7:12 PM UTC
15+
- cron: '36 21 * * *' # 9:36 PM UTC
16+
17+
workflow_dispatch:
18+
19+
jobs:
20+
train_and_evaluate:
21+
runs-on: ubuntu-latest
22+
timeout-minutes: 180
23+
24+
steps:
25+
- uses: actions/checkout@v3
26+
with:
27+
fetch-depth: 0
28+
token: ${{ secrets.GITHUB_TOKEN }}
29+
30+
- name: Set up Python 3.10
31+
uses: actions/setup-python@v4
32+
with:
33+
python-version: '3.10'
34+
35+
- name: Install system dependencies
36+
run: |
37+
sudo apt-get update
38+
sudo apt-get install -y build-essential wget
39+
40+
# Install TA-Lib from source
41+
cd /tmp
42+
wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
43+
tar -xzf ta-lib-0.4.0-src.tar.gz
44+
cd ta-lib/
45+
./configure --prefix=/usr
46+
make
47+
sudo make install
48+
cd ~
49+
50+
- name: Install Python dependencies
51+
run: |
52+
pip install --upgrade pip
53+
pip install -r requirements.txt
54+
55+
- name: Configure Git
56+
run: |
57+
git config --global user.email "bot@meridian.algo"
58+
git config --global user.name "MidnightAI Bot"
59+
60+
- name: Run training
61+
env:
62+
COMET_API_KEY: ${{ secrets.COMET_API_KEY }}
63+
COMET_PROJECT: midnight-rl
64+
INITIAL_CAPITAL: 10000
65+
run: |
66+
python src/main.py --mode train --initial-capital 10000
67+
68+
- name: Get run number
69+
id: run_number
70+
run: |
71+
if [ -f models/run_counter.txt ]; then
72+
RUN_NUM=$(cat models/run_counter.txt)
73+
else
74+
RUN_NUM=1
75+
fi
76+
echo "run_num=$RUN_NUM" >> $GITHUB_OUTPUT
77+
echo "Run number: $RUN_NUM"
78+
79+
- name: Create milestone tag every 5 runs
80+
if: ${{ steps.run_number.outputs.run_num % 5 == 0 }}
81+
run: |
82+
RUN_NUM=${{ steps.run_number.outputs.run_num }}
83+
TAG_NAME="milestone-run-${RUN_NUM}"
84+
85+
git tag -a "${TAG_NAME}" -m "Training Milestone: Run ${RUN_NUM} completed"
86+
git push origin "${TAG_NAME}" || echo "Tag push failed, continuing..."
87+
88+
- name: Commit and push results
89+
run: |
90+
git add -A
91+
92+
RUN_NUM=${{ steps.run_number.outputs.run_num }}
93+
TIMESTAMP=$(date -u +'%Y-%m-%d %H:%M UTC')
94+
95+
git commit -m "Training Run ${RUN_NUM} - ${TIMESTAMP}" || echo "No changes to commit"
96+
97+
# Push with retry logic
98+
for i in {1..3}; do
99+
git push origin main && break || sleep 5
100+
done

.gitignore

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Python
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
*.so
6+
.Python
7+
env/
8+
build/
9+
develop-eggs/
10+
dist/
11+
downloads/
12+
eggs/
13+
.eggs/
14+
lib/
15+
lib64/
16+
parts/
17+
sdist/
18+
var/
19+
wheels/
20+
*.egg-info/
21+
.installed.cfg
22+
*.egg
23+
24+
# Virtual Environment
25+
venv/
26+
ENV/
27+
28+
# IDEs
29+
.vscode/
30+
.idea/
31+
32+
# Data & Models
33+
data/*.db
34+
data/raw/
35+
data/features/
36+
models/checkpoints/
37+
models/*.zip
38+
reports/*.html
39+
logs/
40+
41+
# Environment
42+
.env

README.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Elite Self-Adaptive RL Bitcoin Trading Bot
2+
3+
Institutional-grade, self-healing Reinforcement Learning trading system for Bitcoin with adaptive intelligence, automated overfitting detection, and premium visualizations.
4+
5+
## Core Features
6+
- Adaptive Intelligence: Real-time monitor for divergence, variance explosion, and regime shifts
7+
- Multi-Model Ensemble: 10 diverse models with adaptive weighting
8+
- Long-Only Strategy: No shorting, only buy and hold positions
9+
- Advanced Features: 50+ indicators across 5 tiers (Technical, Microstructure, Meta, Time, Cross-Asset)
10+
- Institutional Reporting: Comprehensive Comet ML dashboards with 14+ graphs
11+
- Multi-Exchange Support: Fully integrated with Binance (via CCXT) and Alpaca
12+
- Automated Versioning: Sequential run numbering with automatic git tagging every 5 runs
13+
14+
## Installation
15+
16+
1. Clone the repository
17+
```bash
18+
git clone https://github.com/MeridianAlgo/MidnightAI.git
19+
cd MidnightAI
20+
```
21+
22+
2. Install dependencies
23+
```bash
24+
pip install -r requirements.txt
25+
```
26+
27+
3. Setup Environment - Create a `.env` file:
28+
```env
29+
COMET_API_KEY=your_actual_comet_key
30+
COMET_PROJECT=midnight-rl
31+
BINANCE_API_KEY=your_key
32+
BINANCE_API_SECRET=your_secret
33+
```
34+
35+
## Usage
36+
37+
### Training
38+
```bash
39+
python src/main.py --mode train --initial-capital 10000
40+
```
41+
42+
Training takes 30-40 minutes per run with 10 ensemble models.
43+
44+
### Automated Schedule
45+
The system runs 10 times per day via GitHub Actions (every 2.4 hours). Results are committed and logged to Comet ML.
46+
47+
## Project Structure
48+
- `src/`: Core logic (Agent, Env, Features, Protection)
49+
- `data/`: SQLite database and OHLCV storage
50+
- `reports/`: Interactive HTML dashboards
51+
- `models/`: Trained ensemble checkpoints (10 models)
52+
- `models/run_counter.txt`: Sequential run tracking
53+
54+
## Ensemble Models
55+
56+
The system trains 10 diverse models:
57+
1. PPO Aggressive (high exploration)
58+
2. PPO Conservative (low exploration)
59+
3. PPO Balanced
60+
4. A2C Fast
61+
5. A2C Stable
62+
6. DQN Discrete
63+
7. DQN Double
64+
8. Recurrent PPO
65+
9. Recurrent A2C
66+
10. TD3 Continuous (adapted for discrete)
67+
68+
## Trading Strategy
69+
70+
- **Long-Only**: Buy BTC with cash, sell BTC for cash, or hold
71+
- **No Shorting**: Cannot sell what you don't own
72+
- **Profit-Driven**: Heavy penalties for losses, bonuses for profits
73+
- **Action Distribution**: Balanced buy/sell/hold based on market conditions
74+
75+
## Anti-Overfitting System
76+
77+
The AdaptiveOverfitProtection monitors:
78+
- Train/Val Divergence
79+
- Confidence Collapse
80+
- Regime Drift
81+
- Variance Explosion
82+
83+
## Comet ML Dashboards
84+
85+
Each run generates 14 comprehensive graphs:
86+
1. Equity Curve (line with profit/loss zones)
87+
2. Drawdown Analysis (area chart)
88+
3. Cumulative Returns (line chart)
89+
4. Rolling Sharpe Ratio (line chart)
90+
5. Profit & Loss Over Time (area chart)
91+
6. Returns Distribution (histogram)
92+
7. Volatility Over Time (line chart)
93+
8. Underwater Plot (area chart)
94+
9. Daily Returns (bar chart)
95+
10. Model Performance Comparison (line chart)
96+
11. Ensemble Weights (horizontal bar)
97+
12. Actions Over Time (line chart)
98+
13. Confidence Bands (line with zones)
99+
14. Equity vs Benchmark (line comparison)
100+
101+
## GitHub Actions
102+
103+
Workflow runs 10x daily:
104+
- Every 2.4 hours (0:00, 2:24, 4:48, 7:12, 9:36, 12:00, 14:24, 16:48, 19:12, 21:36 UTC)
105+
- Creates milestone tags every 5 runs
106+
- Commits results automatically
107+
- Tracks run numbers across executions
108+
109+
---
110+
Built by Elite RL Trading Intelligence

init_repo.bat

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
@echo off
2+
echo ============================================================
3+
echo Initializing MidnightAI Git Repository
4+
echo ============================================================
5+
echo.
6+
7+
git --version >nul 2>&1
8+
if errorlevel 1 (
9+
echo ERROR: Git is not installed
10+
pause
11+
exit /b 1
12+
)
13+
14+
if exist .git (
15+
echo Git repository already exists.
16+
git remote set-url origin https://github.com/MeridianAlgo/MidnightAI.git
17+
) else (
18+
echo Initializing Git repository...
19+
git init
20+
git remote add origin https://github.com/MeridianAlgo/MidnightAI.git
21+
git branch -M main
22+
)
23+
24+
echo.
25+
echo Adding files to git...
26+
git add .
27+
28+
echo.
29+
echo Creating initial commit...
30+
git commit -m "Initial commit: MidnightAI Elite RL Trading Bot"
31+
32+
echo.
33+
echo Pushing to GitHub...
34+
git push -u origin main
35+
36+
if errorlevel 1 (
37+
echo.
38+
echo Push failed. Please:
39+
echo 1. Create repository on GitHub: https://github.com/new
40+
echo 2. Repository name: MidnightAI
41+
echo 3. Then run: git push -u origin main
42+
pause
43+
exit /b 1
44+
)
45+
46+
echo.
47+
echo ============================================================
48+
echo SUCCESS: Repository pushed to GitHub
49+
echo ============================================================
50+
echo.
51+
echo Next steps:
52+
echo 1. Add GitHub secret: COMET_API_KEY
53+
echo 2. Enable GitHub Actions
54+
echo.
55+
pause

models/run_counter.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
9

requirements.txt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Core RL
2+
gymnasium==0.29.1
3+
stable-baselines3==2.3.2
4+
sb3-contrib==2.3.0
5+
shimmy>=1.3.0
6+
7+
# Data & Markets
8+
pandas>=2.1.0
9+
numpy>=1.26.0,<2.0.0
10+
yfinance>=0.2.35
11+
ccxt>=4.2.25
12+
python-binance>=1.0.19
13+
alpaca-trade-api>=3.1.1
14+
15+
# Technical Analysis
16+
pandas-ta>=0.3.14b
17+
18+
# Database
19+
sqlalchemy>=2.0.25
20+
21+
# Experiment Tracking
22+
comet-ml>=3.38.0
23+
tensorboard>=2.15.2
24+
25+
# Hyperparameter Optimization
26+
optuna>=3.5.0
27+
28+
# Visualization
29+
matplotlib>=3.8.2
30+
seaborn>=0.13.1
31+
plotly>=5.18.0
32+
kaleido==0.2.1
33+
34+
# ML Utilities
35+
scikit-learn>=1.4.0
36+
scipy>=1.12.0
37+
shap>=0.44.0
38+
imbalanced-learn>=0.12.0
39+
40+
# Notifications
41+
requests>=2.31.0
42+
slack-sdk>=3.26.2
43+
discord-webhook>=1.3.1
44+
45+
# Configuration
46+
pyyaml>=6.0.1
47+
python-dotenv>=1.0.1
48+
49+
# Testing
50+
pytest>=8.0.0
51+
52+
# Utilities
53+
joblib>=1.3.2
54+
tqdm>=4.66.1
55+
python-dateutil>=2.8.2
56+
pytz>=2024.1
57+
58+
# Performance
59+
numba>=0.59.0

0 commit comments

Comments
 (0)