This Django application serves as a comprehensive financial data analysis platform. It offers functionalities for fetching and analyzing stock data, backtesting trading strategies, and leveraging machine learning for price predictions.
- Historical stock data retrieval from Alpha Vantage API
- PostgreSQL database for data storage
- Backtesting of simple buy/sell strategies
- Integration of a pre-trained machine learning model for stock price prediction
- Comprehensive report generation
- Deployed on Google Cloud Run
- Dockerfile included for easy deployment
- Python 3.x
- pip
- git
- Docker
-
Clone the repository:
git clone https://github.com/1611Dhruv/stocker
-
Set up the environment variables: Create a
.env
file in the project root with the following contents:ALPHA_VANTAGE_API_KEY=[your_api_key] DB_URL=[your_psql_database_url] DB_USER=[your_psql_username] DB_PASSWORD=[your_psql_password]
-
Install dependencies:
python3 -m venv .venv source .venv/bin/activate pip install -r requirements.txt
The database schema consists of two tables:
- Symbol Table: Stores stock symbols.
- Financial Data Table: Stores financial data with
symbol
as a foreign key. There is an index built on the financial data, ensuring that data is always ordered by date. Retrievals are optimized with no sorting costs due to fixed ordering ofsymbol
followed bydate
.
The ML predictions are stored similarly to financial data. If the user specifies format=pdf
, the predicted prices are returned as a Matplotlib-generated PDF and stored in the database.
Start the development server:
python manage.py runserver
-
POST /financial_data/add
- Add a new stock symbol
- Expects
symbol
in the request body
-
POST /financial_data/refresh
- Refresh data for existing symbols
-
GET /financial_data/data
- List available symbols
-
GET /financial_data/data?symbol=<symbol>
- Get data for a specific symbol
GET /backtesting?format=<pdf|json>&symbol=<symbol>&winsell=<sell_threshold>&winbuy=<buy_threshold>&amt=<initial_amt>
- Perform backtesting with specified parameters
GET /ai_prediction?symbol=<symbol>&format=<pdf|json>
- Get predicted prices for a symbol. If
format=pdf
, the predictions are returned as a PDF and stored in the database.
- Get predicted prices for a symbol. If
- Access the reporting interface at
/reporting/
. This provides a user-friendly UI to interact with the various endpoints. Here are some views available:
A Dockerfile
is attached for easy deployment. Before running the Docker container, ensure the following environment variables are set:
ALPHA_VANTAGE_API_KEY
: Key to interact with the APIDB_HOST
: PostgreSQL database hostDB_PORT
: PostgreSQL database portDB_NAME
: PostgreSQL database nameDB_USER
: PostgreSQL usernameDB_PASSWORD
: PostgreSQL passwordALLOWED_HOST
: Django specifications for allowed hosts (for CSRF tokens)DEBUG
: Django debug mode (set toTrue
orFalse
)
To build and run the Docker container:
docker build -t financial-data-analysis-app .
docker run -p 8000:8000 --env-file .env financial-data-analysis-app
The platform is currently deployed on Google Cloud Run. You can access the site at this link.
The open-source repository is available at https://github.com/1611Dhruv/stock_analyzer.