Stock market simulation
Project description
marketsim
This utility helps simulate a stock market.
- Market orders and limit orders
- Limit order books
- Continuous tradnig and auction
Example: Continuous Trading
from marketsim import Market
market = Market()
market.execute_order('buy', 'symbol1', quantity=10, price=100)
# places a limit order and attempts to execute it immediately
# returns an empty array (no executions at this point)
market.execute_order('sell', 'symbol1', quantity=10, price=None)
# places a market order and attempts to execute it immediately
# returns a list of Execution object(s)
Example: Auction
from marketsim import Market
market = Market()
market.place_order('buy', 'symbol1', quantity=10, price=100, time=0)
# places a limit order without execution
market.place_order('sell', 'symbol1', quantity=10, price=None, time=0)
# places a market order without execution
market.execute()
# executes all trades at once
# returns a list of Execution object(s)
# Note:
# A constant "time" value is specified to effectively disable time priority,
# which ensures all limit orders at the same price will be evenly filled.
class Market
A Market object manages order books of multiple symbols.
from marketsim import Market, Product, Order, Side
market = Market()
# Place and execute orders (returns a list of Execution objects)
market.execute(Order(Side.BUY, 'symbol1', quantity=10, price=100))
market.execute_order(Side.BUY, 'symbol1', quantity=10, price=100)
market.execute_order('buy', 'symbol1', quantity=10, price=100)
# Place orders without execution
market.place(Order(Side.SELL, 'symbol1', quantity=10, price=100))
market.place_order(Side.SELL, 'symbol1', quantity=10, price=100)
market.place_order('sell', 'symbol1', quantity=10, price=100)
# Execute orders that have been placed (returns a list of Execution objects)
market.execute()
# Cancel order by object
order = Order(Side.BUY, 'symbol1', quantity=10, price=100)
market.place(order)
market.cancel(order)
# Note: order ID is internally assigned as the Python object ID by default
# Cancel order by specific ID
market.place_order(Side.SELL, 'symbol1', quantity=10, price=100, id='order #1')
market.cancel_order(id='order #1')
# Retrieve information per symbol
market['symbol1'].bid_price
market['symbol1'].ask_price
market['symbol1'].last_price
# Retrieve Product objects from a market object
market = Market()
product = market.get_product('symbol1') # returns None
product = market.ensure_product('symbol1') # creates a new product if not existing
product = market.get_product('symbol1') # returns the object
product = market['symbol1'] # synonym for get_product()
market.set_product('symbol2', Product('symbol2'))
market.['symbol2'] = Product('symbol2')
# Get all products
market.get_products() # returns a list of Product objects
# Iterate all products
for product in market:
pass
class Product
A Product object represents an order book of a single symbol.
from marketsim import Market, Product, Order, Side
# Create a standalone product
product = Product('symbol1')
# Information per symbol
product.bid_price # initially None
product.ask_price # initially None
product.last_price # initially None
# Order book
product.format_order_book()
# Example output:
"""
| BID | PRICE | ASK |
|========|=======|========|
| | 140 | 20 (2) |
| | 130 | 20 (2) |
| 20 (2) | 110 | |
| 20 (2) | 100 | |
"""
# Order queues
product.order_queues[Side.BUY]
product.order_queues[Side.SELL]
product[Side.BUY] # synonym for product.order_queues[Side.BUY]
product[Side.SELL] # synonym for product.order_queues[Side.SELL]
class Order, Side
An Order object can be used to place an order into a Market instance.
from marketsim import Order, Side
# Limit order
order = Order(Side.BUY, 'symbol1', quantity=10, price=100)
# Market order
order = Order(Side.BUY, 'symbol1', quantity=10)
# Order with ID (useful when cancelling an order)
order = Order(Side.BUY, 'symbol1', quantity=10, id='order #1')
# Order with time (useful to control price-time priority)
order = Order(Side.BUY, 'symbol1', quantity=10, time=12345)
# Default: Unix timestamp in seconds with a fractional value in microseconds
# Specify a constant value across all placed orders to simulate pro-rata.
# Side can be specified in alternative ways
order = Order(Side.BUY, 'symbol1', quantity=10)
order = Order('BUY', 'symbol1', quantity=10)
order = Order('Buy', 'symbol1', quantity=10)
order = Order('buy', 'symbol1', quantity=10)
order = Order(1, 'symbol1', quantity=10) # 1 means BUY, as in FIX tag 54
order = Order(Side.SELL, 'symbol1', quantity=10)
order = Order('SELL', 'symbol1', quantity=10)
order = Order('Sell', 'symbol1', quantity=10)
order = Order('sell', 'symbol1', quantity=10)
order = Order(2, 'symbol1', quantity=10) # 2 means SELL, as in FIX tag 54
# Special case for a cancel
order = Order(id='order #1')
# Retrieve individual attributes
order.side # Side object
order.symbol
order.quantity
order.price (None if market order)
order.id
order.time
class Execution, Fill
An Execution object represents an execution result where bid and ask orders are matched.
In general, when execution is invoked for a market, zero or more execution objects are returned. For example, one bid order can be matched with multiple ask orders of smaller quantity, or it could be queued without being matched with any orders at all.
Each Execution object contains bid- and ask-sides of Fill objects.
from marketsim import Market
market = Market()
market.place_order('buy', 'symbol1', quantity=10, price=100)
market.place_order('sell', 'symbol1', quantity=10, price=100)
executions = market.execute()
for execution in executions:
execution.quantity # executed number of shares
execution.price # executed price
execution.bid_fill # bid-side Fill object
execution.ask_fill # ask-side Fill object
execution.bid_fill.order # original Order object
execution.bid_fill.quantity # same as execution.quantity
execution.bid_fill.price # same as execution.price
execution.bid_fill.order_quantity # original order quantity
execution.bid_fill.order_price # original order price (None if market order)
execution.bid_fill.cumulative_quantity # cumulative quantity filled for the order entry
class OrderQueue, OrderStat
An OrderQueue object maintains a queue of order entries on one side (either bid or ask).
from marketsim import Market, Side
market = Market()
market.place_order(...)
market.place_order(...)
product = market['symbol1']
order_queue = product[Side.BUY]
# Aggregated volume/count
order_queue.market_order_volume
order_queue.market_order_count
order_queue.limit_order_volume
order_queue.limit_order_count
order_stats = order_queue.get_order_book() # returns a list of OrderStat objects
# Individual limit order volume/count per price
for order_stat in order_stats:
order_stat.price
order_stat.volume
order_stat.count
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
File details
Details for the file marketsim-0.0.1.tar.gz
.
File metadata
- Download URL: marketsim-0.0.1.tar.gz
- Upload date:
- Size: 14.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 446a7d7ddc87397ebead96b6f56bba950554ed0e49a3d31c95b9cd108894b3d7 |
|
MD5 | efa0c9da4d565f107ac9fa76c77a215d |
|
BLAKE2b-256 | 0f753cbc5fc31f89b0d8ad738eae4b7f55142b104ce81ecb98a3ddf46cb88d4d |
File details
Details for the file marketsim-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: marketsim-0.0.1-py3-none-any.whl
- Upload date:
- Size: 15.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b3e0def9a40b0e4c80641a26e78471e3931e3672d642cfe1227efc002ee435c |
|
MD5 | d29b3ae2a0c892f7a8fef6e108e798cd |
|
BLAKE2b-256 | 0f5e628d26779474d7548685ae1d7f85edce96b6deac6c1506341399f8eadaf5 |