A real-time algorithmic trading bot that uses the Alpaca API to execute stock trades based on technical indicators like MACD, EMA (200), and PSAR.
The demo video provides a walkthrough of the Alpaca trading bot in action, featuring:
- Real-time stock price monitoring
- Buy and sell operations based on technical indicators
- Detailed logging of trade results
- Profit and loss calculation for each trade
- Node.js for backend logic
- Alpaca API for stock trading integration
- Technical Indicators (MACD, EMA, PSAR)
- WebSocket for real-time market data updates
- Paper trading environment for testing strategies
- Uses Alpaca's WebSocket to subscribe to live stock data and make trades based on indicators:
- MACD: Used for trend-following signals.
- EMA (200): Used as a long-term trend indicator.
- PSAR: Provides buy or sell signals based on the stock’s trend.
- Buy Signal: When the stock price is above both EMA-200 and PSAR, and the MACD line crosses above the signal line.
- Sell Signal: When the price reaches the target profit or stop loss for each trade.
- Limit Orders: When the quantity is greater than 0, the bot submits a limit order at a specified price.
- Market Orders: Executes at the best available market price when conditions are met.
- Logs each transaction and calculates the profit or loss percentage for each trade.
- Node.js
- Alpaca API for stock trading
- Technical Indicators library (MACD, EMA, PSAR)
- dotenv for environment variable management
- Lodash for utility functions
@alpacahq/alpaca-trade-api: Alpaca trading APItechnicalindicators: Technical indicators for trading strategieslodash: Utility librarydotenv: Environment variable management
AlpacaTradingBot/
├── index.js # Main trading bot logic
├── .env # Environment variables (API keys)
├── package.json # Project dependencies and scripts
└── README.md # Project documentation
- Alpaca Account with paper trading API credentials.
- Node.js (v14 or higher) environment.
- Set up an
.envfile with your API_KEY and SECRET_API_KEY.
-
Clone the repository:
git clone https://github.com/your-repository/AlpacaTradingBot.git
-
Install the dependencies:
npm install
-
Create a
.envfile with your Alpaca API credentials:API_KEY=your_api_key_here SECRET_API_KEY=your_secret_api_key_here
-
Start the bot:
node index.js
- The stock symbol (
stock) is set toAAPLby default. To change the stock, modify thestockvariable inindex.js:let stock = "AAPL"; // Change to your desired stock symbol
- The bot trades 10 stocks per order by default. You can adjust the quantity in the script.
- Real-time data is processed with 1-minute bars (candles).
- The bot operates in a paper trading environment to test strategies without real money involved.
- Execution Speed: Instant market data processing
- Order Handling: Real-time market orders and limit orders
- Cost Optimization: Paper trading environment for risk-free testing
- API Key Management: Secure handling of API keys through environment variables
- No real money is used in paper trading
- No real trading costs, as the bot operates in paper trading mode.
- Alpaca's API is free for paper trading accounts.
This project is licensed under the MIT License - see the LICENSE file for details.
Project Link: https://github.com/your-repository/AlpacaTradingBot