Skip to content

Feat: Add Continuous Double Auction (ZI-C) Market Model#471

Merged
tpike3 merged 11 commits into
mesa:mainfrom
shipitdev:zi_doubleauc
Jul 24, 2026
Merged

Feat: Add Continuous Double Auction (ZI-C) Market Model#471
tpike3 merged 11 commits into
mesa:mainfrom
shipitdev:zi_doubleauc

Conversation

@shipitdev

Copy link
Copy Markdown
Contributor

Hi! This PR adds a new Continuous Double Auction (CDA) model to the mesa-examples repo. It simulates a financial exchange using simple, randomized traders (ZI-C traders) and reproduces the classic economic results from Gode & Sunder (1993).

I built this specifically to highlight Mesa's continuous-time and event-driven features, moving away from the old step-by-step tick system.

What's Inside

  • Event-Driven Timing: Uses model.time and the new priority-queue scheduler. Instead of acting all at once, traders arrive at random, staggered times to simulate realistic network delays.
  • Clean Architecture: The core matching engine (the Order Book) is written in pure Python and kept completely separate from the Mesa framework layer.
  • Realistic Market Rules: Uses standard Price-Time priority (first-come, first-served) and clears trades at the midpoint price.
  • Live Dashboard: Hooks up DataCollector to track market metrics (Clearing Price, Spread, Volume) and pipes them into a real-time SolaraViz dashboard without needing a spatial grid.

Testing

  • Added unit tests for the pure Python Order Book (test_order_book.py).
  • Added integration tests to ensure the Mesa model, agents, and time systems play nicely together (test_model.py).
  • Verified that the market naturally converges close to the theoretical equilibrium price (within ~10-12%).

Type of Change

  • 🛠 Feature/enhancement

Checklist

  • My code follows the contributing guidelines of this project.
  • I have added a descriptive README.md for the new example.
  • I have added tests that prove my feature works.
  • I have run the test suite locally and all tests pass.

@falloficaruss falloficaruss left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Make sure you push your changes proplerly and review the PR yourself before requesting for review

Comment thread examples/zi_double_auction/model/__init__.py
…owards the various classes and functions for a easier review
@shipitdev

Copy link
Copy Markdown
Contributor Author

gentle ping @falloficaruss, i have included the order_book.py and its tests, I went ahead and also added some docs on the classes and functions where i felt to need for better readability and an easier review. everything looks good from my end now. However if you want me to tweak anything. I'll be Happy to make any changes.

also the i took at the linting errors caught by the CI bot. i will remove the stray page variable in my app.py but i noticed the bot is also failing due to a pre-exisiting unsused variable in rl/wolf_sheep/agents.py.. If you want i can take a look in and include the fix for wolf_sheep in this PR to get the checks green, so that would cover both of the fixes or if you want me to keep this PR stricly scoped to my Double Auction model. that is fine by me.

@falloficaruss

Copy link
Copy Markdown
Collaborator

Will take a look at this today

@falloficaruss

Copy link
Copy Markdown
Collaborator

I ran tests locally and they are failing

@shipitdev

Copy link
Copy Markdown
Contributor Author

I ran tests locally and they are failing

may i know what tests ? also can you share your python environment.

@falloficaruss falloficaruss left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A few nitpicks before actually reviewing the model

Comment thread examples/zi_double_auction/model/model.py Outdated
Comment thread examples/zi_double_auction/model/model.py
Comment thread examples/zi_double_auction/model/order_book.py Outdated
Comment thread examples/zi_double_auction/notebook/app.py Outdated
@falloficaruss

Copy link
Copy Markdown
Collaborator

I ran tests locally and they are failing

may i know what tests ? also can you share your python environment.

Standard Pytest tests

@shipitdev

Copy link
Copy Markdown
Contributor Author

@falloficaruss thank you for the review, i'm currently a bit preoccupied, i'll have a look soon as i get time.

shipitdev and others added 2 commits July 10, 2026 16:36
Co-authored-by: Abhishek Shinde <139879930+falloficaruss@users.noreply.github.com>
@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown

Caution

Review failed

An error occurred during the review process. Please try again later.

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

@shipitdev

Copy link
Copy Markdown
Contributor Author

Hey @abhishek, just a quick heads-up: I added the self.steps += 1 you suggested, but it actually caused the run_for pytest to fail. i think Mesa auto increments the steps in the background, so adding it manually caused a double-count I reverted that line, and all the pytest checks are passing completely green now.

@shipitdev
shipitdev requested a review from falloficaruss July 10, 2026 11:23
@falloficaruss

Copy link
Copy Markdown
Collaborator
image

@falloficaruss falloficaruss left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The model only attempts a single match per arrival. So a crossed book can stay crossed after one trade.

@falloficaruss

Copy link
Copy Markdown
Collaborator

Also have you tested this locally?

@shipitdev

shipitdev commented Jul 12, 2026

Copy link
Copy Markdown
Contributor Author

hey @falloficaruss, i have a few comments

  1. testing discrepancy : i have tested locally and only after they were all green that i pushed earlier but its failing for you, i also previously pushed a requirements.txt. my local envirnment was testing against 3.x where i think self.steps is initialized by the base Model. you must be testing against the 4.0 dev version, that is why its throwing the AttributeError because that version i read on drops the legacy steps. I've updated the tests to assert against model.time instead now.
  2. on the crossed book yes i see you're right. it was only evealuating a single match per avrrival. I've updated the matching engine to use a while loop so it continously clears the top of the book until the spread is uncrossed.

i'll push up these fixes shortly

@shipitdev shipitdev left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Two fixes:

Crossed Book: Swapped the single match check for a while loop in model.py so it fully clears all crosses per arrival.

Mesa 4.0 Compatibility: Updated the tests to assert model.time instead of steps so it doesn't break on the dev branch.

Tested against both 3.5.1 and 4.0 and everything is green. Ready for another look!

@tpike3

tpike3 commented Jul 20, 2026

Copy link
Copy Markdown
Member

Thanks @shipitdev this is very cool. @falloficaruss already did a good code review. The only thing I would ask is can you add a summary in the README probably under other Other Examples that mirrors the formatting and style, so users can more easily find it.

@shipitdev

shipitdev commented Jul 21, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @shipitdev this is very cool. @falloficaruss already did a good code review. The only thing I would ask is can you add a summary in the README probably under other Other Examples that mirrors the formatting and style, so users can more easily find it.

thank you @tpike3, its good to hear that. I'll be going through the readmes in other project for reference and upload a similar project for this version today itself.

@shipitdev

Copy link
Copy Markdown
Contributor Author

i have updated my readme accordingly if there's anything you need me to tweak, happy to do so :)

@tpike3

tpike3 commented Jul 22, 2026

Copy link
Copy Markdown
Member

i have updated my readme accordingly if there's anything you need me to tweak, happy to do so :)

@shipitdev, apologies I should have been more explicit. I need you to update the repository README so users can find your model. Your model readme is fine.

@shipitdev

shipitdev commented Jul 22, 2026

Copy link
Copy Markdown
Contributor Author

i have updated my readme accordingly if there's anything you need me to tweak, happy to do so :)

@shipitdev, apologies I should have been more explicit. I need you to update the repository README so users can find your model. Your model readme is fine.

ah i see, i just added the entry under the other-examples right after El Farol since they're both economics-flavored models. Let me know if this looks good to you,Thanks.

@tpike3 tpike3 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM! Thanks @shipitdev this is a great addition.

@tpike3
tpike3 merged commit 5ab1c41 into mesa:main Jul 24, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants