This project demonstrates three different implementations of a stock backtesting agent that calculates historical returns using AI:
-
Prompt Chaining Implementation (
backtester_agent_prompt_chained.py
)- Uses pure prompt chaining to guide the model through the calculation steps
- Simpler implementation but less structured
- Good for understanding the basic flow of the calculation
-
Function Calling Implementation (
backtester_agent.py
)- Uses OpenAI's function calling feature
- Partially agentic approach with structured tool definitions
- Better error handling and validation
- More robust than prompt chaining
-
LangChain Implementation (
backtester_agent_langchain.py
)- Uses the LangChain framework for full agentic behavior
- Most structured and maintainable implementation
- Built-in tools and agent management
- Best for production use and extensibility
All three implementations provide the same core functionality:
- Calculate historical returns for stocks
- Use Yahoo Finance API for data
- Handle date ranges and price calculations
- Provide detailed step-by-step explanations
- Calculate historical returns for any stock
- Support for custom date ranges
- Detailed step-by-step explanations
- Error handling and validation
- Multiple implementation approaches
- Create a virtual environment:
python -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
- Set up environment variables in
.env
:
YAHOO_FINANCE_SEARCH_API=your_search_api_url
YAHOO_FINANCE_QUERY_API=your_query_api_url
Choose the implementation that best suits your needs:
- For simple usage with prompt chaining:
python backtester_agent_prompt_chained.py
- For function calling implementation:
python backtester_agent.py
- For LangChain implementation:
python backtester_agent_langchain.py
Example query:
What would have been the return of Apple from 2020-01-01 to 2020-12-31?
- Uses sequential prompts to guide the model
- Each step is handled by a separate prompt
- Simpler to understand but less robust
- Good for learning and prototyping
- Uses OpenAI's function calling feature
- Structured tool definitions
- Better error handling
- More robust than prompt chaining
- Good for production use
NOTE: I couldn't get this working with the DeepSeek model in my local but tested it only with OpenAI model(gpt-4o-mini)
- Uses LangChain framework
- Full agentic behavior
- Built-in tools and agent management
- Most maintainable and extensible
- Best for complex applications
here's the working demo of this agent with this user input:
What would have been the return of Apple from 2020-01-01 to 2020-12-31?
Backtester_AI_Agent_demo_.mov
The script includes comprehensive error handling for:
- Invalid company names
- Date format errors
- API connection issues
- Data validation
- Calculation errors
- Requires valid API endpoints for Yahoo Finance data
- Date range must be in YYYY-MM-DD format
- Company names must be recognizable by the Yahoo Finance API
- Historical data availability depends on the stock's listing date
Feel free to submit issues and enhancement requests!