A Flask-based REST API for backtesting trading strategies with PostgreSQL database and Docker support.
- User authentication with JWT
- Multiple trading strategies support (MACD, Moving Average)
- PostgreSQL database for data persistence
- Swagger UI documentation
- Docker and Docker Compose setup
- Automated backtesting with customizable parameters
- Performance metrics calculation
trading-strategy-backtester/
├── app.py # Main application entry point
├── auth.py # Authentication routes and logic
├── backtest.py # Backtesting routes and logic
├── config.py # Configuration settings
├── database_utils.py # Database utilities
├── docker-compose.yml # Docker Compose configuration
├── Dockerfile # Docker container configuration
├── extensions.py # Flask extensions initialization
├── models.py # Database models
├── models_docs.py # Swagger documentation models
├── namespace_auth.py # Auth namespace definitions
├── namespace_backspace.py # Backtest namespace definitions
├── requirements.txt # Python dependencies
├── routes.py # API route definitions
├── strategies.py # Trading strategy implementations
└── .env # Environment variables (create from .env.example)
- Docker and Docker Compose
- Python 3.11+ (for local development)
- PostgreSQL 15+ (handled by Docker)
- Clone the repository:
git clone https://github.com/richdon/trading-strategy-backtester
cd trading-strategy-backtester
- Create a
.env
file from the template:
cp .env.example .env
- Update the
.env
file with your configuration:
# Flask Configuration
FLASK_APP=app.py
FLASK_DEBUG=True
# PostgreSQL Database Configuration
POSTGRES_USER=<user>
POSTGRES_PASSWORD=<password>
POSTGRES_DB=<db_name>
POSTGRES_HOST=db
POSTGRES_PORT=5432
# JWT Configuration
JWT_SECRET_KEY=your_jwt_secret_key_here
# API Configuration
API_HOST=0.0.0.0
API_PORT=5001
- Build and start the containers:
docker-compose up --build
The application will be available at:
- API: http://localhost:5001/api
- Swagger Documentation: http://localhost:5001/api/docs
The API documentation is available through Swagger UI. After starting the application, visit http://localhost:5001/api/docs
to:
- View all available endpoints
- Test API endpoints directly
- See request/response models
- Download OpenAPI specification
POST /api/auth/register
- Register a new userPOST /api/auth/login
- Login and receive JWT tokenGET /api/auth/profile
- Get current user profilePUT /api/auth/profile
- Update user profile
POST /api/backtest
- Execute a new backtestGET /api/backtest/list
- List all backtestsGET /api/backtest/greatest-return
- Get best performing backtest
For local development:
- Create a virtual environment:
python -m venv .venv
source .venv/bin/activate # On Windows: `.venv\Scripts\activate`
- Install dependencies:
pip install -r requirements.txt
- Start only the database:
docker-compose up db -d
- Run the Flask application:
flask run
Using curl:
- Register a new user:
curl -X POST http://localhost:5001/api/auth/register \
-H "Content-Type: application/json" \
-d '{
"username": "testuser",
"email": "test@example.com",
"password": "securepassword123"
}'
- Login:
curl -X POST http://localhost:5001/api/auth/login \
-H "Content-Type: application/json" \
-d '{
"username": "testuser",
"password": "securepassword123"
}'
- Run a backtest (requires JWT token):
curl -X POST http://localhost:5001/api/backtest \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d '{
"strategy": "MACD Crossover",
"asset": "BTC/USDT",
"start_date": "2023-01-01",
"end_date": "2023-12-31",
"interval": "1d",
"initial_capital": 10000
}'
The application includes several database management commands:
# Recreate database (WARNING: Deletes all data)
docker-compose exec web flask recreate-db
# Create database extensions
docker-compose exec web flask create-extensions
# Create additional indexes
docker-compose exec web flask create-indexes
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request