An agentic trading system that uses a Large Language Model (LLM) to make stock trading decisions using market data, financial news, technical indicators, and historical trading experience. The system supports historical backtesting, retrieval-based trading memory, tool-augmented MCP-based reasoning, and LoRA fine-tuning.
This project is intended for educational and research purposes only. Real money may not be used and this repository is not liable for any misuse for financial, investment, or speculative decisions
- LLM-powered stock trading decisions
- ReAct-style iterative reasoning and tool use
- Real-time and historical market data
- Financial news retrieval with local caching
- Technical indicators including RSI and MACD
- Retrieval-based trading memory using BM25
- Historical backtesting with a simulated portfolio
- LoRA/PEFT supervised fine-tuning (SFT)
- FastAPI model server for separated model inference
The agent uses an iterative reasoning process to gather information before making a trade:
- Analyze the current portfolio and market context.
- Determine which information is needed.
- Query available tools using MCP to get market data, news, indicators, and memory.
- Evaluate the returned information.
- Repeat steps 1-4 if additional information is required.
- Generate and validate trade instructions.
- Submit orders through Alpaca paper trading.
- Reflect on completed trades after N iterations and update memory.
The agent has access to the following tools:
| Tool | Description |
|---|---|
submit_order |
Submit an order through Alpaca |
get_news_for_symbols |
Retrieve financial news for specified securities |
add_memory |
Store trading experiences and reflections |
get_memories |
Retrieve relevant memories using BM25 |
get_price_histories |
Retrieve historical OHLCV data |
get_rsi |
Retrieve RSI data |
get_macd |
Retrieve MACD data |
get_stock_prices |
Retrieve current stock prices |
Tool implementations are located in:
src/mcp/
The project includes a supervised fine-tuning pipeline using LoRA/PEFT.
Training data is stored in:
data/sft_data/sft_data.jsonl
Run fine-tuning with:
python -m src.training.finetuneGenerated adapter weights are saved under:
data/checkpoints/
- Python 3.12+
- uv
- Alpaca API credentials
- Alpha Vantage API key
- Qwen2.5-1.5B-Instruct
Clone the repository:
git clone https://github.com/ShafathZ/Competitive-Agentic-Trading-System.git
cd Competitive-Agentic-Trading-SystemInstall dependencies:
uv syncCreate a .env file in the project root:
ALPACA_API_KEY=####your_alpaca_api_key####
ALPACA_SECRET_KEY=####your_alpaca_secret_key####
ALPHA_VANTAGE_API_KEY=####your_alpha_vantage_api_key####Additional configuration is available in:
src/config/constants.py
Start the model server:
python -m src.model.modelserviceIn a separate terminal, start the trading agent:
python -m src.agentThe agent uses the Alpaca paper-trading environment for order execution.
Run the test suite with:
python -m pytest ./tests -sv
