Skip to content

Add stock market example with LLM trader and analyst agents - #281

Open
hussanshabbir wants to merge 2 commits into
mesa:mainfrom
hussanshabbir:add-stock-market-example
Open

Add stock market example with LLM trader and analyst agents#281
hussanshabbir wants to merge 2 commits into
mesa:mainfrom
hussanshabbir:add-stock-market-example

Conversation

@hussanshabbir

Copy link
Copy Markdown

This PR adds a stock market simulation example to mesa-llm, demonstrating
LLM-powered agents reasoning over quantitative financial data.

TraderAgent — uses ReAct reasoning to decide BUY/SELL/HOLD based on
live price, RSI, and trend data. Two archetypes: conservative and aggressive.
AnalystAgent— reads market indicators and broadcasts signals to nearby
traders via speak_to.
execute_trade tool — handles buy/sell/hold logic with budget and share tracking.
StockMarketModel — simulates price movement with random walk + mean
reversion, computes RSI and volatility, collects data via DataCollector.
Solara visualization — color-coded agents (green = profitable, red = loss).

Testing

Follows the same structure as the negotiation example. Compatible with any
LiteLLM provider (OpenAI, Anthropic, Ollama).

@colinfrisch @jackiekazil happy to adjust anything based on your feedback!

@coderabbitai

coderabbitai Bot commented Mar 31, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 255de2a2-a5c3-4d4e-a883-58b177d6a053

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@wang-boyu wang-boyu added the example Release notes label label Mar 31, 2026

@sadrasabouri sadrasabouri left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @hussanshabbir, thanks for contributing to mesa-llm. I tried your example, and I think it fits well into mesa-llm examples. I reviewed your PR as an external reviewer to make it ready for maintainers' review.
In addition to the few comments I left, I highly encourage you to add a README.md in which you explain more about the examples (similar to other projects).

Comment on lines +70 to +81
observation = self.generate_obs()
history = get_trading_history(self)
price = self.model.current_price
prompt = (
f"MARKET DATA:\n"
f"- Price: ${price:.2f}\n"
f"- Trend: {self.model.price_trend()}\n"
f"- RSI: {self.model.rsi():.1f} (>70 overbought, <30 oversold)\n"
f"- Budget: ${self.budget:.2f} | Shares: {self.shares}\n\n"
f"RECENT ACTIVITY:\n{history}\n\n"
"Use execute_trade to BUY, SELL, or HOLD. Justify briefly."
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: put the common part of step and astep into a seprate function.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for all agents.

n=n_conservative,
reasoning=reasoning,
llm_model=llm_model,
system_prompt="You are a conservative trader. Only buy when RSI is below 35 and trend is rising. Sell quickly if you sense risk. Protect capital above all else.",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say don't fixate on an LLM on a number and let it decide by itself what a critical value is as a "conservative trader."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

example Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants