Traders

Base Trader

Base class for traders in a market simulation.

class pymicrostructure.traders.base.Trader(market: Market, name: str = None, include_in_results=True)[source]

Bases: object

Represents a trader in a financial market.

This class serves as a base class for specific trader implementations. It provides the basic structure for managing orders, trades, and interactions with the market.

Attributes:

marketMarket

The market instance in which the trader participates.

orderslist

A list of orders submitted by the trader.

filled_tradeslist

A list of trades that have been executed for the trader.

positionint or float

The current position of the trader in the market.

include_in_resultsbool

Flag indicating whether to include this trader in result calculations.

trader_idint

A unique identifier for the trader within the market.

Methods:

cancel_orders(side)

Cancel active or partially filled orders on a specific side.

cancel_all_orders()

Cancel all active or partially filled orders.

submit_order()

A method to be implemented by subclasses for submitting orders to the market.

cancel_all_orders() None[source]

Cancel all active or partially filled orders for this trader.

cancel_order_by_id(order_id: int) None[source]

Cancel an order by its unique identifier.

Parameters:

order_idint

The unique identifier of the order to cancel.

cancel_orders_by_side(side: str) None[source]

Cancel all active or partially filled orders on a specific side.

Parameters:

sidestr

The side of the market (either ‘buy’ or ‘sell’) on which to cancel orders.

Informed Traders

Informed traders that have an opinion on the future price of a security.

class pymicrostructure.traders.informed.BaseInformedTrader(market: ContinuousDoubleAuction, fair_price_strategy: Callable, volume_strategy: Callable, max_inventory: int = 1000, name: str = None, include_in_results: bool = True)[source]

Bases: Trader

A base class for informed traders who have an opinion on the future price of a security.

This trader uses strategies to determine fair price and trading volume, and places market orders based on these strategies and current market conditions.

Attributes:

fair_price_strategy (Callable): A strategy to determine the fair price. volume_strategy (Callable): A strategy to determine the trading volume. max_inventory (int): The maximum inventory the trader can hold. fair_price (float): The current fair price as determined by the strategy.

update() None[source]

Update the trader’s state and potentially place orders.

This method updates the fair price, calculates the trading volume, and places market orders if the current market price is favorable compared to the fair price.

class pymicrostructure.traders.informed.DummyInformedTrader(market: ContinuousDoubleAuction)[source]

Bases: BaseInformedTrader

A dummy informed trader with constant fair price and maximum allowed volume.

This trader uses a constant fair price of 1050 and always trades the maximum allowed volume.

class pymicrostructure.traders.informed.NewsInformedTrader(market: ContinuousDoubleAuction)[source]

Bases: BaseInformedTrader

A news-informed trader that reacts to market news.

This trader uses a news impact exponential fair price strategy and a time-weighted volume strategy.

class pymicrostructure.traders.informed.TWAPInformedTrader(market: ContinuousDoubleAuction)[source]

Bases: BaseInformedTrader

A Time-Weighted Average Price (TWAP) informed trader.

This trader uses a constant fair price of 1050 and a time-weighted volume strategy.

Market Makers

Module for market maker traders.

This module provides implementations of various market maker strategies for trading in financial markets. It includes a base class for market makers and several specific implementations with different pricing and volume strategies.

class pymicrostructure.traders.market_maker.AdaptiveMarketMaker(market: Market, name: str = None, include_in_results=True)[source]

Bases: BaseMarketMaker

An adaptive market maker that adjusts its strategy based on market conditions.

This market maker uses order flow to adjust its fair price and spread, and sets its volume as a fraction of the market volume.

Attributes:

market (Market): The market in which the trader operates. name (str): Name of the trader. include_in_results (bool): Whether to include this trader in results.

class pymicrostructure.traders.market_maker.BaseMarketMaker(market: Market, fair_price_strategy: Callable, volume_strategy: Callable, spread_strategy: Callable, max_inventory: int, name: str = None, include_in_results: bool = True)[source]

Bases: Trader

Base class for market maker traders.

This class provides a foundation for implementing market maker strategies. It manages the core logic for updating orders based on fair price, spread, and volume strategies.

Attributes:

market (Market): The market in which the trader operates. fair_price_strategy (Callable): Strategy for determining the fair price. spread_strategy (Callable): Strategy for determining the bid-ask spread. volume_strategy (Callable): Strategy for determining the trading volume. max_inventory (int): Maximum inventory the market maker can hold. name (str): Name of the trader. include_in_results (bool): Whether to include this trader in results.

update() None[source]

Update the market maker’s orders based on current market conditions.

This method calculates the fair price, spread, and volumes, cancels existing orders, and submits new orders to the market.

class pymicrostructure.traders.market_maker.DummyMarketMaker(market: Market, name: str = None, include_in_results=True)[source]

Bases: BaseMarketMaker

A simple market maker with constant fair price, volume, and spread.

This market maker uses fixed values for its pricing and volume strategies, making it useful for testing and basic market simulations.

Attributes:

market (Market): The market in which the trader operates. name (str): Name of the trader. include_in_results (bool): Whether to include this trader in results.

class pymicrostructure.traders.market_maker.KyleMarketMaker(market: Market, name: str = None, include_in_results=True)[source]

Bases: BaseMarketMaker

A market maker strategy based on Kyle’s model.

This market maker adjusts its fair price based on recent order flow, while maintaining constant volume and spread.

Attributes:

market (Market): The market in which the trader operates. name (str): Name of the trader. include_in_results (bool): Whether to include this trader in results.

Noise Traders

Base classes for noise traders.

class pymicrostructure.traders.noise.NoiseTrader(market: Market, submission_rate: float = 1.0, volume_size: int | Callable[[], int] = 1)[source]

Bases: Trader

Noise trader that submits random market orders at a fixed rate.

Noise traders are traders that submit random orders to the market, providing liquidity and adding noise to the price process.

Attributes:

marketMarket

The market instance in which the trader participates.

submission_ratefloat

The rate at which the trader submits orders.

volume_sizeint or Callable[[], int]

The size of the orders submitted by the trader.

update() None[source]

Update the trader’s orders.

Strategies

Strategy module for market making algorithms.

This module provides abstract base classes and concrete implementations for various strategies used in market making, including fair price calculation, volume determination, and spread setting.

class pymicrostructure.traders.strategy.ConstantFairPrice(fair_price: int)[source]

Bases: FairPriceStrategy

A strategy that returns a constant fair price.

class pymicrostructure.traders.strategy.ConstantSpread(halfspread: int)[source]

Bases: SpreadStrategy

A strategy that sets a constant spread around the fair price.

class pymicrostructure.traders.strategy.ConstantVolume(volume: int)[source]

Bases: VolumeStrategy

A strategy that sets a constant volume, limited by inventory constraints.

class pymicrostructure.traders.strategy.FairPriceStrategy[source]

Bases: Strategy

Abstract base class for fair price calculation strategies.

class pymicrostructure.traders.strategy.MaxAllowedVolume[source]

Bases: VolumeStrategy

A strategy that sets the maximum allowed volume based on inventory limits.

class pymicrostructure.traders.strategy.MaxFractionVolume(fraction: float)[source]

Bases: VolumeStrategy

A strategy that sets the volume as a fraction of the maximum allowed volume.

class pymicrostructure.traders.strategy.NewsImpactExponentialFairPrice(window: int, agressiveness: int)[source]

Bases: FairPriceStrategy

A strategy that adjusts the fair price based on an exponential function of recent news.

class pymicrostructure.traders.strategy.NewsImpactFairPrice(agressiveness: int)[source]

Bases: FairPriceStrategy

A strategy that adjusts the fair price based on the latest news impact.

class pymicrostructure.traders.strategy.OrderFlowImbalanceSpread(window: int, aggressiveness: int, min_halfspread: int)[source]

Bases: SpreadStrategy

A strategy that adjusts the spread based on order flow imbalance.

class pymicrostructure.traders.strategy.OrderFlowMagnitudeFairPrice(window: int, aggressiveness: int)[source]

Bases: FairPriceStrategy

A strategy that adjusts the fair price based on the magnitude of recent order flow.

class pymicrostructure.traders.strategy.OrderFlowSignFairPrice(window: int, aggressiveness: int)[source]

Bases: FairPriceStrategy

A strategy that adjusts the fair price based on the sign of recent order flow.

class pymicrostructure.traders.strategy.SpreadStrategy[source]

Bases: Strategy

Abstract base class for spread setting strategies.

class pymicrostructure.traders.strategy.Strategy[source]

Bases: ABC

Abstract base class for all strategies.

class pymicrostructure.traders.strategy.TimeWeightedVolume[source]

Bases: VolumeStrategy

A strategy that adjusts volume based on remaining time and current market conditions.

class pymicrostructure.traders.strategy.VolumeStrategy[source]

Bases: Strategy

Abstract base class for volume determination strategies.

Trader Ensembles

Tools for creating ensembles of traders.

pymicrostructure.traders.ensemble.ensemble_traders(trader, params_dict)[source]

Create an ensemble of traders with different parameter sets.

Parameters:

traderclass

The trader class to instantiate.

params_dictdict

A dictionary of parameter names and lists of values.

Returns:

instanceslist

A list of trader instances.