Alpaca Live Execution
Alpaca Live Execution
The Signal.Engine live execution module bridges the gap between the AI "Brain" and the financial markets. It leverages the Alpaca Trade API to perform real-time data ingestion and autonomous order execution.
The primary entry point for live trading is the trader_alpaca.py script.
1. Prerequisites & Authentication
To interact with the market, you must provide your Alpaca API credentials via an environment file. Signal.Engine is configured by default to use the Paper Trading API to ensure safety during the experimental phase.
Create a .env file in the project root:
APCA_API_KEY_ID="your_alpaca_key_id"
APCA_API_SECRET_KEY="your_alpaca_secret_key"
APCA_API_BASE_URL="https://paper-api.alpaca.markets"
2. Execution Modes
The execution script supports two primary modes: Dry Run and Live (Paper).
Dry Run (Default)
In Dry Run mode, the agent fetches live market data and runs the full inference pipeline (LSTM + PPO) to generate a decision. However, it does not transmit orders to Alpaca. This is recommended for verifying signal stability and model "sanity" before committing capital.
python -m src.trader_alpaca --symbol RELIANCE.NS --qty 1
Live Paper Trading
By appending the --live flag, the agent becomes autonomous. It will monitor the ticker, process the last 50 candles, and execute Buy, Sell, or Hold orders directly on your Alpaca paper account.
python -m src.trader_alpaca --symbol RELIANCE.NS --qty 1 --live
3. Command Line Arguments
The execution script accepts several parameters to customize the trading session:
| Argument | Description | Default |
| :--- | :--- | :--- |
| --symbol | The ticker symbol to trade (e.g., AAPL, RELIANCE.NS). | Required |
| --qty | Number of shares to trade per order. | 1 |
| --live | Enables real order execution. If omitted, the script runs in "Dry Run" mode. | Flag |
| --interval | The data timeframe for candles (e.g., 1Min, 5Min, 1Day). | 1Min |
4. Operational Workflow
When executed, the trader_alpaca module follows a strict sequential loop:
- Market Sync: Connects to Alpaca to verify account status and market hours.
- Data Ingestion: Fetches the most recent 50 candles to satisfy the LSTM sequence requirements.
- Feature Engineering: Calculates technical indicators (RSI, MACD, Log-Returns) in real-time.
- Brain Inference: Passes the processed sequence through the
best_ppo.ckptmodel. - Risk Validation: Checks the agent's decision against quantitative risk parameters (e.g., current position sizing).
- Order Transmission: If
--liveis active, the agent places Market orders for immediate execution.
5. Monitoring via Dashboard
While the execution script runs in the terminal, you can monitor the agent's performance and "rational" via the Signal.Engine Frontend:
- Ensure the API is running:
python -m src.api.main - Open the dashboard to view:
- Confidence Scores: The probability the model assigns to its current action.
- Decision Rational: A breakdown of why the agent chose to buy or sell (e.g., "High Volatility detected").
- Live Logs: Real-time feedback from the simulation and execution engines.
6. Safety Notes
- Paper Trading Only: The
APCA_API_BASE_URLshould always point to the paper trading endpoint unless you have manually verified the model's performance over significant timeframes. - Symbol Suffixes: For Indian markets (Nifty 500), ensure symbols include the
.NSsuffix for Yahoo Finance/Alpaca compatibility where applicable. - Connection Resilience: The script includes basic retry logic for API timeouts, but it is recommended to run the execution script in a stable environment (e.g., a VPS or dedicated trading terminal).