Exchange Execution Simulator - stock exchange single bar execution simulator
Project description
XSim
Stock Exchange Order Execution Simulator for developers This package simulates order(s) execution based on provided market OHLCV data. This is not an independent package, but its the core logic required to implement backtesting when it comes on running on candlebar data.
This is the first drop and wasn't tested out in the wild yet, more functionality and capabilities to come
disclaimer
Own risk warning - Execution prices are best estimations base on worst case scenarios and statistics, there will be price differences between simulations and real-world execution, using this package the user acknowledges his consent and takes full responsibility on the implications caused due to any error or misinterpetation of this package and it's results.
Trailing commands warning - Trailing commands are currently roughly estimated and carry high deviation from the real world
The challenge
Core Problem: Reconstructing intra-bar price movement to determine order execution.
The golden standard of market data comes in chunks of Candle-bars providing Open, High, Low, Close and Volume of predefined time range ie. 1-minute, 5-minutes, an hour, a day, a week, a month, etc... Within each such data-unit there is a gap of the inner price motions, unless you work with tick-by-tick data which is expensive, noisy and resource intense. On top of that, fill prices are results of a consiquent rules and formations that are hard to simulate.
The solution
Output: Realistic execution fills within statistical uncertainty. or consiquent order either original or modified.
XSim relies on OHLC Data to simulate the inner motion of prices within single data-unit, and attempt to perform a set of decision to execute orders in the most authentic way.
Supported order types
- MarketOrder
- LimitOrder
- StopOrder
- StopLimitOrder
- TrailingStopMarket
Not supported order types (at the moment)
- Trailing Stop Limit Orders
- Market-on-Close (MOC) / Limit-on-Close (LOC)
- Bracket Orders (OCO - One-Cancels-Other)
- Market-if-Touched (MIT)
- Others...
Current Execution algorithm assumptions
- No slippage
- No partial fills
- Aggressive approach - Order will be filled if there's a possible path between order's formation and the candlebar.
- Trail orders assume the following order of the candles: On Bullish bar: prev_extremePrice [optional] -> open -> low -> high -> close On Bearish bar: prev_extremePrice [optional] -> open -> high -> low -> close
Use cases
- Market order - Market order executes immediately on submission time (close to open)
- Limit order - The algorithm will determine whether the price motion goes through the limit defined in the order, if so it will execute the order with some statistical error depends on the volatility of the candle-bar.
- Additional types of orders: Trailing stop, StopLimitOrder, StopOrder,
- Supporting buy/sell(long/short) directions
- Supporting Time In Force (Tif), goodAfterTime, goodTillDate
- Supporting ocaGroup
- Supporting parent/child relationships
- Multiple orders
- Working with fractional time of submission is Unsupported yet
Installation
pip install -e .
Running Tests
pytest tests/ -v
Usage
Here is a minimal example of simulating a market order execution:
from models import MarketOrder, BarData
from execution import ExecutionEngine
from decimal import Decimal
from datetime import datetime
engine = ExecutionEngine()
bar = BarData(
date=datetime(2025, 1, 1, 9, 30),
open=Decimal('100.00'),
high=Decimal('105.00'),
low=Decimal('95.00'),
close=Decimal('102.00'),
volume=1000000,
)
order = MarketOrder(action='BUY', totalQuantity=100)
result = engine.execute(order, bar)
print(result.fills)
Visualizations
Stop-limit and Trailing test cases can be visualized For stop-limit visualization run:
docs/stop-limit-chart-generator.py test-data/stop-limit/<filename.csv>
For trailing visualization run:
docs/trailing-stop-chart-generator.py test-data/trailing-stop/<filename.csv>
TODO: Trailing cases are not organized systemactically enough
Contributing
Contributions are welcome! Please see the documentation in the docs folder for more details and test specifications.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file xxsim-0.5.0.tar.gz.
File metadata
- Download URL: xxsim-0.5.0.tar.gz
- Upload date:
- Size: 17.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7c536098e9927951ddbaef157e14f93f471c7a73f5e12ca6496b171385f328b7
|
|
| MD5 |
1ec6fd83113180b431547e85d97333f3
|
|
| BLAKE2b-256 |
28c304366570d1c175ebcd4f2b99b9968b4ff5d38632721bb4de89c9c5185451
|
File details
Details for the file xxsim-0.5.0-py3-none-any.whl.
File metadata
- Download URL: xxsim-0.5.0-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d40bcab014ac986d565285200b602cb8624b9119e854bdf1d0cf500e41a44889
|
|
| MD5 |
906210bab481fb5b1fbe94b39ef7b91e
|
|
| BLAKE2b-256 |
92eb796d50601139a3361709d7b7377ce4ad71391ed6eb7c728e00bba14ae7d2
|