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:
objectRepresents 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.
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:
TraderA 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.
- class pymicrostructure.traders.informed.DummyInformedTrader(market: ContinuousDoubleAuction)[source]
Bases:
BaseInformedTraderA 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:
BaseInformedTraderA 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:
BaseInformedTraderA 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:
BaseMarketMakerAn 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:
TraderBase 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.
- class pymicrostructure.traders.market_maker.DummyMarketMaker(market: Market, name: str = None, include_in_results=True)[source]
Bases:
BaseMarketMakerA 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:
BaseMarketMakerA 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:
TraderNoise 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.
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:
FairPriceStrategyA strategy that returns a constant fair price.
- class pymicrostructure.traders.strategy.ConstantSpread(halfspread: int)[source]
Bases:
SpreadStrategyA strategy that sets a constant spread around the fair price.
- class pymicrostructure.traders.strategy.ConstantVolume(volume: int)[source]
Bases:
VolumeStrategyA strategy that sets a constant volume, limited by inventory constraints.
- class pymicrostructure.traders.strategy.FairPriceStrategy[source]
Bases:
StrategyAbstract base class for fair price calculation strategies.
- class pymicrostructure.traders.strategy.MaxAllowedVolume[source]
Bases:
VolumeStrategyA strategy that sets the maximum allowed volume based on inventory limits.
- class pymicrostructure.traders.strategy.MaxFractionVolume(fraction: float)[source]
Bases:
VolumeStrategyA strategy that sets the volume as a fraction of the maximum allowed volume.
- class pymicrostructure.traders.strategy.NewsImpactExponentialFairPrice(window: int, agressiveness: int)[source]
Bases:
FairPriceStrategyA strategy that adjusts the fair price based on an exponential function of recent news.
- class pymicrostructure.traders.strategy.NewsImpactFairPrice(agressiveness: int)[source]
Bases:
FairPriceStrategyA 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:
SpreadStrategyA strategy that adjusts the spread based on order flow imbalance.
- class pymicrostructure.traders.strategy.OrderFlowMagnitudeFairPrice(window: int, aggressiveness: int)[source]
Bases:
FairPriceStrategyA 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:
FairPriceStrategyA strategy that adjusts the fair price based on the sign of recent order flow.
- class pymicrostructure.traders.strategy.SpreadStrategy[source]
Bases:
StrategyAbstract base class for spread setting strategies.
- class pymicrostructure.traders.strategy.Strategy[source]
Bases:
ABCAbstract base class for all strategies.
- class pymicrostructure.traders.strategy.TimeWeightedVolume[source]
Bases:
VolumeStrategyA strategy that adjusts volume based on remaining time and current market conditions.
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.