Perp: Price Impact Module
このコンテンツはまだ日本語訳がありません。
Key Features
Section titled “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
Section titled “Components”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
Section titled “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
Section titled “Price Impact Calculation”The price impact is calculated using this formula:
$$Price\ Impact\ % = \frac{(Start\ OI + \frac{Trade\ Size}{2})}{One\ Percent\ Depth}$$
- 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
Section titled “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
Section titled “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
Section titled “Open Interest Management”The module uses a time-windowed approach for open interest.
- Window Configuration:
OI_WINDOWS_SETTINGSdefines the duration and count of windows. - Adding OI:
add_price_impact_open_interestupdates the current window’s open interest when a trade is opened. - Removing OI:
remove_price_impact_open_interestadjusts open interest when a trade is closed. - Time-Weighted Calculation: Open interest from multiple recent windows is used for a more stable metric.
- Initialize
PAIR_DEPTHSwith market depth values. - Configure
OI_WINDOWS_SETTINGS. - Call
get_trade_price_impactto calculate price impact for a trade. - After execution, call
add_price_impact_open_interestorremove_price_impact_open_interestto update open interest.
Configuration
Section titled “Configuration”Fine-tune the module by adjusting these parameters:
one_percent_depth_above_usdandone_percent_depth_below_usdinPAIR_DEPTHS.windows_countandwindows_durationinOI_WINDOWS_SETTINGS.
Future Improvements
Section titled “Future Improvements”- Implement dynamic adjustment of market depth.
- Add support for asymmetric price impact.
- Introduce more advanced time-weighting algorithms.