AlgoTrader is a Python-based algorithmic trading bot that leverages the Alpaca API to automate stock trading. The bot supports real-time market data fetching, executing buy and sell orders, and employing trading strategies based on simple market conditions.
- Real-time Market Data: Fetches historical stock data with customizable timeframes.
- Trading Strategies: Implements basic strategies, such as buying when the price drops below a threshold and selling when it rises above a set value.
- Automated Trading: Executes buy and sell orders on the Alpaca platform based on strategy decisions.
- Scheduler: Allows periodic execution of trading actions, ensuring continuous trading activity.
This is a work in progress.
This project is under active development, and features may change or be incomplete. Please use with caution and contribute if you'd like to help improve it!
- Python 3.8+
- An Alpaca account and API credentials. Sign up here.
- Install dependencies using
pip
.
git clone https://github.com/yourusername/AlgoTrader.git
cd AlgoTrader
python3 -m venv venv
source venv/bin/activate # For Windows, use `venv\Scripts\activate`
pip install -r requirements.txt
Add your Alpaca API key and secret to a configuration file (config.py) or as environment variables. (Recommended)
# config.py
API_KEY = "your-api-key"
API_SECRET = "your-api-secret"
BASE_URL = "https://paper-api.alpaca.markets" # For paper trading
Alternatively, you can set your API keys as environment variables:
export API_KEY="your-api-key"
export API_SECRET="your-api-secret"
export BASE_URL="https://paper-api.alpaca.markets"
To run the bot, simply execute the main script:
python bot/main.py
You can define your custom trading strategies by creating new strategy classes or modifying the existing ones in the strategies/ folder. The bot includes a simple strategy where it buys stocks if the price drops below a certain value and sells if the price rises above another threshold.
Example of a basic strategy (Refer to simple_strategy.py)
You can use the built-in scheduler to automate periodic trading actions. (Refer to scheduler.py) The will execute the trade_function every 5 minutes.
Unit tests are available for the various components of the bot.
To run the tests, use:
pytest
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Commit your changes.
- Push to your forked repository.
- Create a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.
- Alpaca API for providing commission-free stock trading APIs.
- Pandas for data handling.
- APScheduler for scheduling tasks.