This Django project fetches stock data from a Google Sheet, validates it, and uses a Simple Moving Average (SMA) crossover strategy for buy/sell signals.
-
Stock Data Import:
- Imports stock data from Google Sheets and stores it in the database.
- Validates the data to ensure correct types (decimals, integers, strings, datetime).
-
SMA Crossover Strategy:
- Implements a strategy to identify buy/sell signals based on short and long SMA windows.
- Python
- PostgreSQL
-
Clone the repository:
git clone https://github.com/yourusername/stock-data-sma.git cd stock-data-sma
-
Create a virtual environment and activate it:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install the dependencies:
pip install -r requirements.txt
-
Set up the
.env
file. A.env.example
file is provided. Rename it to.env
:cp .env.example .env
-
Edit the
.env
file and add your PostgreSQL database credentials and the URL for the Google Sheet you want to import. Here's the format:DATABASE_URL=postgresql://USERNAME:PASSWORD@localhost:PORT/DATABASE_NAME GOOGLE_SHEET_URL=https://docs.google.com/spreadsheets/d/__SPREADSHEET_KEY__/export?format=csv
For example:
DATABASE_URL=postgresql://postgres:password@localhost:5432/stock_db GOOGLE_SHEET_URL=https://docs.google.com/spreadsheets/d/1oCyKT2SFZlw3Ac3GXj0Y1IYcWG183IRgNCNytFojkWA/export?format=csv
-
Set up the Django application:
python manage.py migrate
-
Migrate Data from Google Sheets to Database:
After migrating the database schema, use the custom management command to fetch stock data from the Google Sheet and populate the database:
python manage.py import_stock_data
-
Start the Django server:
python manage.py runserver
-
Visit the following URL to view the SMA crossover strategy:
http://127.0.0.1:8000/stock/sma-crossover/?short_window=30&long_window=365
-
SMA Crossover Strategy Screenshot:
Below is a screenshot showing the SMA crossover strategy output:
Unit tests are provided to validate stock data fetched from Google Sheets, ensuring that the data types are correct (decimals, integers, strings, and datetime).
You can run the tests using the Django test
command:
python manage.py test stock_data
Below is a screenshot showing the SMA crossover strategy output: