A model capable to predicting multiple buy and sell actions simultaneously.
🔥LIve results here https://stocktrader-6dv1.onrender.com
Welcome to the StockTrader project! This repository contains an end-to-end stock trading MLOps pipeline using a Deep Deterministic Policy Gradient (DDPG) algorithm for reinforcement learning. The project involves several key steps, from data ingestion to model training and evaluation, all aimed at creating a robust stock trading system.
- Overview
- Pipeline Details
- Model Architecture
- Prediction Interval
- Usage
- Environment Variables
- Contributing
- License
StockTrader leverages deep reinforcement learning to predict stock market actions such as buying, selling, or holding stocks. The project uses the DDPG algorithm to train an actor-critic model that makes decisions based on historical stock data. The app is developed using Flask and predicts stock actions every 4 hours. The model is capable of predicting multiple buy and sell actions simultaneously.
The data ingestion pipeline uses the Twelve Data API to retrieve historical stock data for the stocks specified in utils/stock_names.txt. The data is then saved to CSV files for further processing.
The data evaluation pipeline cleans the data by removing null values and stores the cleaned data in the artifact/data_evaluation directory.
In the data transformation pipeline, data from 20 different stocks is combined into a single pandas DataFrame. This combined data is then split into training and test datasets, which are saved in the artifacts/data_transformation directory.
The model training pipeline uses the cleaned and transformed data to train the DDPG model. The training process is implemented in model_training.py, where the model is optimized to make accurate stock trading decisions.
The model evaluation pipeline assesses the performance of the trained model. This involves running the model on test data and generating performance metrics, which are saved for analysis.
The actor model in the DDPG algorithm has an output shape of (3, 20) for 20 stocks. The output consists of:
- Action Selection (Row 1): Decides whether to buy, sell, or hold each stock.
- Buy Probability (Row 2): Uses softmax to allocate available money across stocks.
- Sell Probability (Row 3): Uses softmax to determine the probability of selling stocks based on the stock count in the environment.
Here's a simplified explanation of the output layer in the actor model:
out = Lambda(wrapped_tf_fn)(x)
out1 = Dense(3, activation='softmax')(out)
out2 = Lambda(wrapped_tf_fn)(out1)
out2 = Dense(20, activation="softmax")(out2[:,:2])
argmax = Lambda(wrapped_argmax)(out1) # Apply argmax function
argmax = Lambda(wrapped_cast)(argmax) # Cast to float32
expanded = Lambda(wrapped_expand_dims)(argmax) # Expand dimensions
outputs = Concatenate(axis=1)([expanded, out2])out1applies a softmax activation to determine the action (buy, sell, hold).out2further processes the probabilities for buying and selling stocks.argmaxselects the action with the highest probability.expandedadjusts the tensor dimensions for concatenation.outputscombines the action and probability tensors into the final output.
The app predicts stock actions every 4 hours, ensuring timely decision-making in the stock market.
To run the app, follow these steps:
-
Clone the repository:
git clone https://github.com/SirajuddinShaik/StockTrader.git cd StockTrader -
Install the required dependencies:
pip install -r requirements.txt
-
Set up environment variables or create a
.envfile in the working directory with the following content:TWELVE_DATA_API="your_api_key_here" TWELVE_DATA_API2="your_api_key_here" TWELVE_DATA_API3="your_api_key_here" mongo_db="<username>:<password>" security_key="your_security_key_here" Exchange_API="your_exchange_api_key_here"
-
Run the Flask app:
flask run
-
The app will start predicting stock actions every 4 hours.
Ensure you have the following environment variables set up in your system or in a .env file:
TWELVE_DATA_API: Your first Twelve Data API key.TWELVE_DATA_API2: Your second Twelve Data API key.TWELVE_DATA_API3: Your third Twelve Data API key (if unavailable, use the first key).mongo_db: MongoDB connection string (username:password).security_key: Security key for web UI transactions.Exchange_API: Your Exchange API key.
Contributions are welcome! Please open an issue or submit a pull request if you have any improvements or bug fixes.
This project is licensed under the MIT License. See the LICENSE file for details.
For detailed information on each pipeline stage and model implementation, refer to the respective Python scripts in the repository. Happy trading! 📊🚀
GitHub Repository: StockTrader