Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Competitive Trading Agent

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

Alpaca Earnings

Architecture

Trading Agent Diagram

Features

  • 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

Agentic System Workflow

The agent uses an iterative reasoning process to gather information before making a trade:

  1. Analyze the current portfolio and market context.
  2. Determine which information is needed.
  3. Query available tools using MCP to get market data, news, indicators, and memory.
  4. Evaluate the returned information.
  5. Repeat steps 1-4 if additional information is required.
  6. Generate and validate trade instructions.
  7. Submit orders through Alpaca paper trading.
  8. Reflect on completed trades after N iterations and update memory.

Tools

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/

Fine-Tuning

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.finetune

Generated adapter weights are saved under:

data/checkpoints/

User Manual

Requires

Clone the repository:

git clone https://github.com/ShafathZ/Competitive-Agentic-Trading-System.git
cd Competitive-Agentic-Trading-System

Install dependencies:

uv sync

Configuration

Create 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

Running

Start the model server:

python -m src.model.modelservice

In a separate terminal, start the trading agent:

python -m src.agent

The agent uses the Alpaca paper-trading environment for order execution.

Testing

Run the test suite with:

python -m pytest ./tests -sv

About

Agentic LLM-powered stock trading system with realtime market/news retrieval, trading memory, and MCP tool use.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Contributors

Languages