179 lines
6.5 KiB
Markdown
179 lines
6.5 KiB
Markdown
# invest-bot: Tech Giants Trading Bot
|
|
|
|
## Algorithmic trading system using Alpaca API
|
|
|
|
`invest-bot` is a sophisticated algorithmic trading bot designed to trade a diversified portfolio of 50 "Tech Giants" stocks. It employs a hybrid strategy combining momentum and mean-reversion principles, leveraging various technical indicators to generate precise trade signals. The bot supports both live paper trading and extensive backtesting with historical data, complete with a real-time web dashboard for monitoring performance.
|
|
|
|
## Table of Contents
|
|
|
|
- [Features](#features)
|
|
- [Getting Started](#getting-started)
|
|
- [Prerequisites](#prerequisites)
|
|
- [Installation](#installation)
|
|
- [Configuration](#configuration)
|
|
- [Usage](#usage)
|
|
- [Live Trading](#live-trading)
|
|
- [Backtesting](#backtesting)
|
|
- [Web Dashboard](#web-dashboard)
|
|
- [Strategy Overview](#strategy-overview)
|
|
- [Technology Stack](#technology-stack)
|
|
- [Contributing](#contributing)
|
|
- [License](#license)
|
|
- [Acknowledgements](#acknowledgements)
|
|
|
|
## Features
|
|
|
|
- **Hybrid Trading Strategy**: Combines momentum and mean-reversion logic for robust signal generation.
|
|
- **Multi-Indicator Analysis**: Utilizes a suite of technical indicators including:
|
|
- Relative Strength Index (RSI)
|
|
- Moving Average Convergence Divergence (MACD)
|
|
- Exponential Moving Averages (EMA)
|
|
- Average Directional Index (ADX)
|
|
- Bollinger Bands
|
|
- Custom Momentum Indicators
|
|
- **Live Paper Trading**: Execute trades in a simulated environment using the Alpaca API, without risking real capital.
|
|
- **Comprehensive Backtesting**: Test the strategy against historical data over custom periods (years, months) and initial capital.
|
|
- **Real-time Web Dashboard**: Monitor portfolio performance, account statistics, and open positions through a responsive web interface.
|
|
- **Configurable Timeframes**: Run the bot on `Daily` or `Hourly` data bars.
|
|
- **Robust Logging**: Detailed logging for both console and file, aiding in monitoring and debugging.
|
|
- **Error Handling & Rate Limiting**: Graceful error management and adherence to API rate limits for stable operation.
|
|
|
|
## Getting Started
|
|
|
|
Follow these instructions to set up and run the `invest-bot`.
|
|
|
|
### Prerequisites
|
|
|
|
- [Rust](https://www.rust-lang.org/tools/install) (latest stable version recommended)
|
|
- [Alpaca API Key and Secret Key](https://alpaca.markets/) (for paper trading)
|
|
|
|
### Installation
|
|
|
|
1. **Clone the repository:**
|
|
|
|
```bash
|
|
git clone https://github.com/your-username/invest-bot.git
|
|
cd invest-bot
|
|
```
|
|
|
|
2. **Build the project:**
|
|
|
|
```bash
|
|
cargo build --release
|
|
```
|
|
This will compile the optimized release binary in `target/release/invest-bot`.
|
|
|
|
### Configuration
|
|
|
|
The bot requires API credentials and can be configured via environment variables. Create a `.env` file in the project root directory:
|
|
|
|
```
|
|
ALPACA_API_KEY=YOUR_ALPACA_API_KEY
|
|
ALPACA_SECRET_KEY=YOUR_ALPACA_SECRET_KEY
|
|
DASHBOARD_PORT=5000 # Optional: Customize the dashboard port
|
|
```
|
|
|
|
**Note:** Replace `YOUR_ALPACA_API_KEY` and `YOUR_ALPACA_SECRET_KEY` with your actual Alpaca paper trading API keys.
|
|
|
|
## Usage
|
|
|
|
### Live Trading
|
|
|
|
To run the bot in live paper trading mode:
|
|
|
|
```bash
|
|
cargo run --release
|
|
# Or, if you have built the release binary:
|
|
# ./target/release/invest-bot
|
|
```
|
|
|
|
By default, the bot runs with `Daily` timeframe bars. To specify `Hourly` bars:
|
|
|
|
```bash
|
|
cargo run --release -- --timeframe hourly
|
|
```
|
|
|
|
The bot will continuously analyze the market during trading hours, execute trades, and update the dashboard.
|
|
|
|
### Backtesting
|
|
|
|
To backtest the strategy with historical data, use the `--backtest` flag. You can specify the duration (years/months) and initial capital.
|
|
|
|
- **Backtest for 3 years (default capital):**
|
|
|
|
```bash
|
|
cargo run --release -- --backtest --years 3
|
|
```
|
|
|
|
- **Backtest for 5 years with custom capital:**
|
|
|
|
```bash
|
|
cargo run --release -- --backtest --years 5 --capital 50000
|
|
```
|
|
|
|
- **Combine years and months:**
|
|
|
|
```bash
|
|
cargo run --release -- --backtest --years 1 --months 6
|
|
```
|
|
|
|
- **Hourly timeframe backtest:**
|
|
|
|
```bash
|
|
cargo run --release -- --backtest --years 1 --timeframe hourly
|
|
```
|
|
|
|
Backtest results, including equity curve and trades, will be saved to `backtest_equity_curve.csv` and `backtest_trades.csv` respectively.
|
|
|
|
### Web Dashboard
|
|
|
|
When running in live trading mode, a real-time web dashboard is automatically started. Access it in your browser at:
|
|
|
|
```
|
|
http://localhost:5000
|
|
```
|
|
(or your custom `DASHBOARD_PORT`). The dashboard displays:
|
|
|
|
- Account summary (portfolio value, cash, buying power)
|
|
- Real-time portfolio performance graph
|
|
- Current open positions
|
|
|
|
## Strategy Overview
|
|
|
|
The `invest-bot` utilizes a dynamic hybrid strategy. It identifies potential trades using a combination of:
|
|
|
|
- **RSI (Relative Strength Index):** To gauge overbought/oversold conditions and mean-reversion opportunities.
|
|
- **MACD (Moving Average Convergence Divergence):** For trend identification and reversals.
|
|
- **EMA (Exponential Moving Averages):** To determine short-term and long-term trends.
|
|
- **ADX (Average Directional Index):** To measure trend strength.
|
|
- **Bollinger Bands:** To identify volatility and potential price breakouts/reversals.
|
|
- **Momentum:** Custom indicators to capture price acceleration.
|
|
|
|
Risk management includes position sizing based on portfolio value and buying power, with integrated stop-loss and take-profit mechanisms.
|
|
|
|
## Technology Stack
|
|
|
|
- **Rust**: Primary programming language for performance and safety.
|
|
- **Tokio**: Asynchronous runtime for concurrent operations (e.g., API calls, dashboard).
|
|
- **Alpaca API**: For market data, account information, and trade execution (paper trading).
|
|
- **Axum**: Web framework for building the real-time dashboard.
|
|
- **Chart.js**: JavaScript library for interactive chart visualization on the dashboard.
|
|
- **Serde**: Rust serialization/deserialization framework for JSON and CSV.
|
|
- **Clap**: Command-line argument parser for a user-friendly CLI.
|
|
- **Dotenvy**: For managing environment variables.
|
|
- **Tracing**: Structured logging framework.
|
|
|
|
## Contributing
|
|
|
|
Contributions are welcome! Please feel free to open an issue or submit a pull request.
|
|
|
|
## License
|
|
|
|
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details (if applicable).
|
|
|
|
## Acknowledgements
|
|
|
|
- [Alpaca.markets](https://alpaca.markets/) for their accessible trading API.
|
|
- [Rust Community](https://www.rust-lang.org/) for an amazing language and ecosystem.
|
|
- [Chart.js](https://www.chartjs.org/) for the powerful charting library.
|