DEV Community

JX Le0
JX Le0

Posted on

🤖 Let's Build an AI Trading Agent with LangChain: A Step-by-Step Guide

Have you ever wondered if you could use the power of Large Language Models (LLMs) to analyze financial markets or even automate trading strategies? It sounds like something out of a sci-fi movie, but it's becoming a reality.
The open-source project langchain-trading-agents is a fantastic playground for this exact idea. It uses a multi-agent system built on LangChain to perform complex financial analysis.
Enter fullscreen mode Exit fullscreen mode

In this tutorial, I'll walk you through the entire process, from zero to a running AI analysis, step-by-step. Let's dive in!

📋 What You'll Need (Prerequisites)
Before we start, make sure you have the basics installed on your machine:
Python: Version 3.9 or newer is recommended.
Git: To clone the project from GitHub.
A Code Editor: VS Code or PyCharm will make your life much easier.

🚀 Step 1: Get the Project Code
First, let's grab the source code from GitHub.
Create a folder for your project (e.g., AI_Trading) and navigate into it using your terminal.
Clone the repository with the following command:
git clone https://github.com/aitrados/langchain-trading-agents.git
Now, move into the newly created project directory:
cd langchain-trading-agents

📦 Step 2: Install Dependencies
The project comes with a requirements.txt file that lists all the necessary Python packages. You can install them all with a single command.

pip install -r requirements.txt

Pro Tip: If you're having trouble with slow downloads, you can use a domestic mirror by adding -i https://pypi.tuna.tsinghua.edu.cn/simple to the end of the command.

⚙️ Step 3: Configure Your Environment (The Most Important Part!)
This is where we connect all the pieces. We need to set up our API keys and configurations.

3.1 Create the Configuration Files

The project provides templates. Let's copy and rename them to create our active configuration files.

cp env_example .env
cp config_example.toml config.toml

You should now see .env and config.toml in your project's root directory.

3.2 Get Your Aitrados Secret Key

Aitrados is the core service that powers this project. You'll need a free key to use it.
Go to the Aitrados website: https://www.aitrados.com/ and sign up for a free account to get your SECRET_KEY.
Open the .env file and add your key:

AITRADOS_SECRET_KEY=your-secret-key-goes-here

3.3 Connect to a (Simulated) Exchange

To let the agents perform trades (without risking real money!), we'll set up a demo account with OKX.
Head over to https://www.okx.com/, create an account, and generate your API Key.
Open the config.toml file and find the [broker] section. Fill it in with your OKX API details.

auto_run_brokers=["okx_demo"] # This enables the okx_demo broker
[broker]
[broker.okx_demo]
display_name="OKX simulation"
provider = "okx"
api_key = "YOUR_OKX_API_KEY" # Replace with your key
secret_key = "YOUR_OKX_SECRET_KEY" # Replace with your secret
passphrase = "YOUR_OKX_PASSPHRASE" # Replace with your passphrase
server = "demo" # IMPORTANT: Use "demo" for simulation!
spread_trading = "false"

Safety First! Make absolutely sure that server = "demo". This ensures you're using the simulated trading environment, so no real money is at risk.

3.4 Choose a Brain for Your Agent (LLM)

The "intelligence" of the agents comes from a Large Language Model. You need to configure one that supports Function Calling. Here's an example using DeepSeek.
Get your API Key from DeepSeek.
In config.toml, find the [llm_models] section and add your configuration:

[llm_models.deepseek]
provider = "deepseek"
base_url = "https://api.deepseek.com/v1"
model_name = "deepseek-chat" # Ensure this model supports tool calling
api_key = "YOUR_DEEPSEEK_API_KEY" # Replace with your key
temperature = 0.7

🛠️ Step 4: Tweak the Example Scripts
We need to make two small changes to the example scripts so they can find the modules and use the model we just configured.
Go into the examples folder and apply these changes to all .py files except those starting with common or my:
Fix the Import Path:
Change this line:

from common_lib_example import *

to this:

from examples.common_lib_example import *

Select Your Model:

Find this line:

model_config=get_llm_model_config(ModelProvider.OLLAMA)

Change OLLAMA to the provider you configured in config.toml (e.g., DEEPSEEK):

model_config = get_llm_model_config(ModelProvider.DEEPSEEK)

🏁 Step 5: Fire It Up!
Everything is ready. Let's start the system and run our first AI analysis!

5.1 Start the MCP Server

The MCP (Model Context Protocol) server is the central hub that connects everything.
Open a new terminal in the project's root directory.
Run the following command:

finance-trading-ai-agents-mcp --env-file .env

Heads Up! If this fails, double-check that your AITRADOS_SECRET_KEY in the .env file is correct. Keep this terminal window open; the server needs to keep running.

5.2 Run an Example Script

Open another new terminal and cd into the project root.
Run one of the example scripts, for instance:

python examples/price_action_analyst_example.py

Troubleshooting ModuleNotFoundError: If you get this error, it's a common Python path issue. The simplest fix is to copy the example file (e.g., price_action_analyst_example.py) from the examples folder directly into the project root, then run it again from there:

python price_action_analyst_example.py

Ask Your Own Questions!: Open the script you're running and find the query or ask variable. Change the prompt to ask whatever you want about the market!
If all goes well, you'll see the agents spring to life, analyzing data and collaborating on an answer.

🎉 You Did It! What's Next?
Congratulations! You've successfully deployed a multi-agent AI trading analysis system. You've just stepped into the future of fintech.
This is just the beginning. You can now start experimenting, creating your own analysis prompts, or even building more complex trading strategies. Keep an eye on the Aitrados project for updates, or get creative and integrate your own logic.
Happy coding, and enjoy your journey into the world of AI-powered trading!

python #ai #langchain #fintech #tutorial #machinelearning #llm

Top comments (0)