Perp: Price Impact Module
The Price Impact Module calculates and applies price impact to trades based on their size and market depth. This module ensures that larger trades more significantly affect the execution price, reflec
Key Features
Dynamic Calculation: Computes price impact based on trade size and market depth.
Open Interest Tracking: Records open interest across multiple time windows.
Market Depth Management: Allows configuration of market depth parameters per trading pair.
Time-Weighted Open Interest: Uses a time-windowed approach to calculate cumulative open interest.
Long/Short Handling: Calculates price impact differently for long and short trades.
Components
State
OI_WINDOWS_SETTINGS
: Settings for open interest windows.WINDOWS
: Stores open interest data for specific time windows.PAIR_DEPTHS
: Market depth information for trading pairs.TRADE_LAST_WINDOW_OI
: Tracks the last window's open interest for trades.
Functions
get_trade_price_impact
: Calculates price impact for a trade.add_price_impact_open_interest
: Updates open interest when a trade is opened.remove_price_impact_open_interest
: Updates open interest when a trade is closed.get_price_impact_oi
: Retrieves current price impact open interest.
Price Impact Calculation
The price impact is calculated using this formula:
Start OI: Starting open interest for the relevant direction (long/short).
Trade Size: Size of the trade.
One Percent Depth: Trading volume needed to move the price by 1%.
Calculation Process
Fetch Market Depth: The module retrieves the market depth from
PAIR_DEPTHS
.Calculate Current Open Interest: It sums open interest across multiple time windows, giving more weight to recent windows.
Compute Price Impact Percentage:
let price_impact_p = Decimal::from_ratio( start_open_interest_usd + trade_open_interest_usd.checked_div(2_u64.into())?, one_percent_depth_usd ).checked_div(Decimal::from_atomics(100_u64, 0)?)?;
Apply Price Impact: The calculated impact is applied to the trade's execution price:
let price_after_impact = if long { open_price + price_impact } else { open_price - price_impact };
Example
Let's look at a long trade with these values:
Open Price: $1000
Trade Size: $100,000
Current Open Interest: $500,000
One Percent Depth: $1,000,000
Calculate price impact percentage:
Price Impact % = (500,000 + 100,000 / 2) / 1,000,000 / 100 = 0.55%
Calculate price impact:
Price Impact = $1000 * 0.55% = $5.50
Apply price impact:
Price After Impact = $1000 + $5.50 = $1005.50
Open Interest Management
The module uses a time-windowed approach for open interest.
Window Configuration:
OI_WINDOWS_SETTINGS
defines the duration and count of windows.Adding OI:
add_price_impact_open_interest
updates the current window's open interest when a trade is opened.Removing OI:
remove_price_impact_open_interest
adjusts open interest when a trade is closed.Time-Weighted Calculation: Open interest from multiple recent windows is used for a more stable metric.
Usage
Initialize
PAIR_DEPTHS
with market depth values.Configure
OI_WINDOWS_SETTINGS
.Call
get_trade_price_impact
to calculate price impact for a trade.After execution, call
add_price_impact_open_interest
orremove_price_impact_open_interest
to update open interest.
Configuration
Fine-tune the module by adjusting these parameters:
one_percent_depth_above_usd
andone_percent_depth_below_usd
inPAIR_DEPTHS
.windows_count
andwindows_duration
inOI_WINDOWS_SETTINGS
.
Future Improvements
Implement dynamic adjustment of market depth.
Add support for asymmetric price impact.
Introduce more advanced time-weighting algorithms.
Last updated