A production-ready, cloud-deployed machine learning trading system that uses LSTM neural networks to predict stock price movements and execute automated trading decisions. The system runs on Google Cloud Run with automated scheduling and integrates with Firebase for data persistence.
QuantModelBackend is a fully automated Python-based trading system that:
- Trains LSTM models on historical stock data with hyperparameter optimization
- Tests model stability through Monte Carlo simulations
- Predicts trading signals every 15 minutes during market hours
- Executes automated trading decisions based on ensemble model predictions
- Stores all data, models, and results in Firebase (Firestore + Storage)
- Deploys on Google Cloud Run with automated scheduling via Cloud Scheduler
- Architecture
- Core Components
- Deployment
- Firebase Integration
- Automated Scheduling
- Local Development
- API Endpoints
- Configuration
QuantModelBackend/
βββ models/ # Neural network model definitions
β βββ lstm_strategy_a.py # LSTM model architecture
βββ training/ # Model training and evaluation
β βββ trainer.py # LSTM_Trainer class
β βββ __init__.py
βββ data/ # Data loading and preprocessing
β βββ data_loader.py # DataLoader for yfinance integration
βββ lib/ # Firebase and utility modules
β βββ firebase_init.py # Firebase Admin SDK initialization
β βββ save_model_to_firestore.py # Model upload/download
β βββ save_next_result_to_firebase.py # Position and trade data storage
βββ cloud_run_server.py # Cloud Run HTTP server
βββ main.py # Core trading logic and functions
βββ Dockerfile # Container configuration
βββ requirements.txt # Python dependencies
βββ deploy_cloud_run.sh # Deployment script
βββ setup_cloud_run_schedulers.sh # Scheduler setup script
βββββββββββββββββββ
β Cloud Scheduler β (Every 15 min, Mon-Fri, 8:01 AM - 4:46 PM CST)
ββββββββββ¬βββββββββ
β HTTP POST
βΌ
βββββββββββββββββββ
β Cloud Run β (trading-predictions service)
β HTTP Server β
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β main.py β make_predictions_on_next_data()
β - Fetch data β
β - Run models β
β - Make decisionβ
ββββββββββ¬βββββββββ
β
βΌ
βββββββββββββββββββ
β Firebase β
β - Firestore β (positions, trades, strategy data)
β - Storage β (model files)
βββββββββββββββββββ
Performs comprehensive model selection and training:
-
Hyperparameter Search: Grid search over:
mv_low: Moving average lookback period (5-15)mv_high: Moving average window (6-20)lookback_periods: LSTM sequence length (3-13)
-
Stability Testing: Runs 100 Monte Carlo simulations per configuration
-
Model Selection: Selects top 3 models based on:
- Mean accuracy across runs
- Standard deviation (lower = more stable)
- Consistent performance
-
Output:
- Saves top 3 models to Firebase Storage
- Stores model parameters in Firestore
- Updates strategy configuration
Usage:
from main import LSTM_Strategy_A_Selection
LSTM_Strategy_A_Selection() # Takes ~30-60 minutesMain prediction function called by Cloud Run:
-
Data Fetching: Loads latest 15-minute market data
-
Feature Engineering: Computes:
MV_Change_Percent: Moving average trendVolume_Change: Volume momentumDirection: Price direction indicator
-
Ensemble Prediction:
- Runs all 3 trained models
- Aggregates predictions (majority voting)
- Signal: BUY if β₯2 models predict UP, else HOLD/SELL
-
Firebase Storage:
- Saves prediction results to Firestore
- Updates position data
- Records trade history
Output: Boolean (True = BUY, False = HOLD)
Called by: Cloud Run /weekday endpoint (automated every 15 minutes)
Generates historical predictions for analysis:
- Loads 50 days of 15-minute data
- Generates predictions for each candle
- Returns DataFrame with:
- Date, Close price
- Individual model predictions
- Aggregated signal (Result_Sum)
Calculates P&L from backtest results:
- Simulates trades: Entry on BUY signal, Exit on SELL signal
- Calculates profit per trade
- Returns total cumulative profit
-
Google Cloud SDK: Install and authenticate
gcloud auth login gcloud config set project autotrader-5998a -
Firebase Admin SDK: Service account key
- Place
lib/autotrader.jsonin thelib/directory - Or use default credentials in Cloud Run
- Place
-
Docker: For building container images
Option 1: Using deployment script (Recommended)
cd QuantModelBackend
chmod +x deploy_cloud_run.sh
./deploy_cloud_run.shOption 2: Manual deployment
# Build and deploy
gcloud builds submit --tag gcr.io/autotrader-5998a/trading-predictions
gcloud run deploy trading-predictions \
--image gcr.io/autotrader-5998a/trading-predictions \
--platform managed \
--region us-central1 \
--memory 2Gi \
--cpu 2 \
--timeout 3600 \
--allow-unauthenticatedAfter deploying Cloud Run, set up automated scheduling:
chmod +x setup_cloud_run_schedulers.sh
./setup_cloud_run_schedulers.shThis creates a Cloud Scheduler job that:
- Triggers every 15 minutes during market hours
- Schedule:
1,16,31,46 8-16 * * 1-5(CST, Monday-Friday) - Calls
/weekdayendpoint on Cloud Run service
-
Check Cloud Run service:
gcloud run services describe trading-predictions --region us-central1
-
View logs:
gcloud run services logs read trading-predictions --region us-central1 --limit 50 -
Test manually:
curl -X POST https://trading-predictions-xxx.run.app/weekday
positions/{strategy_name}:
current_position: Current holding (shares)running_pnl: Unrealized P&Lrealized_pnl: Total realized profit/lossdata: 15-minute candlestick datahistory_trades: Completed trade historyparams: Model parameters (MA periods, etc.)
strategies/{strategy_name}:
- Strategy metadata and configuration
- Model parameters
- Performance metrics
models/:
{model_name}_{model_number}.pth: PyTorch model weights- Models are uploaded after training
- Downloaded on-demand for predictions
The system uses lib/firebase_init.py which:
- Automatically detects local vs. cloud environment
- Uses service account key locally (
lib/autotrader.json) - Uses default credentials in Cloud Run
- Handles lazy initialization to prevent startup errors
Weekday Predictions:
- Frequency: Every 15 minutes
- Hours: 8:01 AM - 4:46 PM CST
- Days: Monday - Friday
- Cron:
1,16,31,46 8-16 * * 1-5 - Endpoint:
/weekday - Function:
make_predictions_on_next_data()
Managed via setup_cloud_run_schedulers.sh:
- Creates HTTP-triggered Cloud Scheduler job
- Uses OIDC authentication
- 10-minute timeout
- 3 retry attempts
-
Install dependencies:
cd QuantModelBackend python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate pip install -r requirements.txt
-
Configure Firebase:
- Place
autotrader.jsoninlib/directory - Or set
GOOGLE_APPLICATION_CREDENTIALSenvironment variable
- Place
-
Run locally:
from main import make_predictions_on_next_data result = make_predictions_on_next_data() print(f"Should buy: {result}")
# Install functions-framework
pip install functions-framework
# Run locally
functions-framework --target=run_server --source=cloud_run_server.py --port=8080
# Test endpoint
curl -X POST http://localhost:8080/weekdayPOST /weekday
- Runs
make_predictions_on_next_data() - Returns JSON response with status
- Called by Cloud Scheduler
GET /health
- Health check endpoint
- Returns
{"status": "healthy"}
Response Format:
{
"status": "success",
"message": "Weekday predictions completed and saved to Firebase",
"timestamp": "2025-12-30T14:30:00"
}GOOGLE_CLOUD_PROJECT: Project ID (default:autotrader-5998a)GOOGLE_APPLICATION_CREDENTIALS: Path to service account key (local only)PORT: Server port (default:8080)
Edit constants in main.py:
MODEL_TICKER: Stock symbol (default:"AAPL")INTERVAL: Data interval (default:"15m")MV_LOW_RANGE,MV_HIGH_RANGE: Moving average rangesLOOKBACK_PERIODS_RANGE: LSTM sequence length rangeSIMULATIONS: Number of stability tests (default:100)
Edit deploy_cloud_run.sh:
PROJECT_ID: Google Cloud project IDREGION: Deployment regionSERVICE_NAME: Cloud Run service nameMEMORY: Container memory (default:2Gi)CPU: Container CPU (default:2)TIMEOUT: Request timeout (default:3600s)
LSTM_Strategy_A:
- Input: 3 features (MV_Change_Percent, Volume_Change, Direction)
- Architecture:
- LSTM layer: 64 hidden units
- Dropout: 0.2
- Fully connected output layer
- Output: Binary classification (0 = DOWN, 1 = UP)
- Loss: Binary Cross-Entropy
- Optimizer: Adam
- Provider: yfinance (Yahoo Finance API)
- Ticker: AAPL (configurable)
- Interval: 15-minute candles
- Lookback:
- Training: 50 days
- Prediction: 1 day (latest data)
View logs in Google Cloud Console:
gcloud run services logs read trading-predictions --region us-central1- Firestore: Monitor position updates and trade history
- Storage: Check model uploads/downloads
- Usage: Track API calls and data reads
1. Model not found error
- Ensure models are uploaded to Firebase Storage
- Run
LSTM_Strategy_A_Selection()to train and upload models
2. Firebase initialization error
- Check service account key exists (
lib/autotrader.json) - Verify Firebase Admin SDK credentials
- Check project ID matches in
firebase_init.py
3. Cloud Run deployment fails
- Verify Dockerfile is correct
- Check
requirements.txthas all dependencies - Ensure Cloud Build API is enabled
4. Scheduler not triggering
- Verify Cloud Scheduler job exists
- Check job schedule is correct
- Verify Cloud Run service URL is correct
- Check OIDC service account permissions
5. Timeout errors
- Increase Cloud Run timeout (max: 3600s)
- Check prediction function isn't hanging
- Review logs for slow operations
See requirements.txt for full list:
torch>=2.0.0: PyTorch deep learning frameworkyfinance>=0.2.0: Stock market datapandas>=1.3.0: Data manipulationnumpy>=1.21.0: Numerical computingfirebase-admin>=6.5.0: Firebase integrationfunctions-framework>=3.0.0: Cloud Run serverscikit-learn>=1.0.0: Feature scalingpytz>=2021.3: Timezone handling
- Service account keys stored securely (not in git)
- Cloud Run uses OIDC authentication for scheduler
- Firebase security rules restrict access
- Environment variables for sensitive data
Private project - All rights reserved
Last Updated: December 2025
Version: 1.0.0
Status: Production