A Django-based application for financial data analysis, backtesting, and machine learning predictions. You can run this locally or deploy it to AWS. A demo version is currently hosted and accessible via example API endpoints.
- Fetch and store financial data from Alpha Vantage API
- Perform backtesting with customizable strategies
- Generate predictions using pre-trained machine learning models
- Create performance reports in PDF and JSON formats
- Dockerized setup for easy deployment
- Python 3.1+
- pip (Python package manager)
- PostgreSQL
- Docker
- Alpha Vantage API key (Alpha Vantage)
- Clone and install:
git clone https://github.com/yourusername/financial-analysis-project.git
cd financial-analysis-project
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
- Create
.env
file:
DATABASE_URL=postgresql://localhost:5432/dbname
ALPHA_VANTAGE_API_KEY=your_alpha_vantage_api_key
DEBUG=True
ALLOWED_HOSTS=localhost,127.0.0.1
- Setup database:
python manage.py migrate
python manage.py createsuperuser # Optional: for admin access
Before running any analysis, you need to fetch stock data:
# Fetch last 30 days of data
python manage.py fetch_stock_data AAPL --days 30
# Fetch 2 years of historical data (recommended for backtesting)
python manage.py fetch_stock_data AAPL --years 2
Important Notes:
- Required before running analysis
- Alpha Vantage API rate limit: 5 requests/minute (free tier)
- Initial fetch may take several minutes
For predictions, train a model for each stock symbol:
python manage.py train_ml_model AAPL
A demo version is hosted at 3.130.162.114:8000
. You can test the functionality using these curl commands:
curl -X POST http://3.130.162.114:8000/financial_data/backtest/ \
-H "Content-Type: application/json" \
-d '{
"symbol": "AAPL",
"initial_investment": 10000,
"buy_ma_window": 5,
"sell_ma_window": 20
}'
Make sure to use the appropriate moving average numbers according to the data fetched
curl -X POST "http://3.130.162.114:8000/financial_data/predict/" \
-H "Content-Type: application/json" \
-d '{"symbol": "IBM"}'
curl -X POST -H "Content-Type: application/json" \
-d '{
"symbol": "AAPL",
"start_date": "2024-11-01",
"end_date": "2024-12-01",
"initial_investment": 10000,
"buy_ma_window": 5,
"sell_ma_window": 20,
"format": "pdf"
}' \
http://3.130.162.114:8000/financial_data/report/ --output AAPL_report.pdf
-
Create PostgreSQL RDS instance:
- Go to AWS RDS Dashboard
- Click "Create database"
- Choose PostgreSQL
- Select Free tier or preferred tier
- Configure instance details and connectivity
-
Configure Security Group:
- Add inbound rule for PostgreSQL (port 5432)
- Source: Your EC2 security group ID
-
Launch EC2 instance:
- Choose Amazon Linux 2 AMI
- Select instance type
- Configure security groups (ports 22, 80, 443, 8000)
- Create/select key pair
-
Install requirements:
sudo yum update -y
sudo yum install -y docker git postgresql15
sudo service docker start
sudo usermod -a -G docker ec2-user
- Deploy application:
# Clone your repository
git clone your-repository-url
cd your-project-name
# Create production .env
DATABASE_URL=postgresql://username:password@your-rds-endpoint:5432/dbname
ALPHA_VANTAGE_API_KEY=your_alpha_vantage_api_key
DEBUG=False
ALLOWED_HOSTS=your-ec2-public-dns,your-domain.com
# Deploy with Docker
docker build -t financial-analysis .
docker run -d --env-file .env -p 8000:8000 financial-analysis
- SSH into your EC2 instance:
ssh -i your-key.pem ec2-user@your-ec2-ip
- Access your Docker container:
docker exec -it $(docker ps -q) bash
- Fetch historical data for each stock you want to analyze:
python manage.py fetch_stock_data AAPL --years 2 # Fetch 2 years of AAPL data
python manage.py fetch_stock_data MSFT --years 2 # Fetch 2 years of MSFT data
# Repeat for other stocks
- Train the ML model for each stock:
python manage.py train_ml_model AAPL # Train model for AAPL
python manage.py train_ml_model MSFT # Train model for MSFT
# Repeat for other stocks
After completing these steps, you can use the same API endpoints shown in the examples above, replacing 3.130.162.114
with your EC2 instance's public IP address.
Required GitHub secrets for automatic deployments:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
EC2_HOST
EC2_USERNAME
EC2_SSH_KEY
See .github/workflows/main.yml
in the repository for the complete CI/CD configuration.