# Statistics

## Capacity

### Introduction

Capacity is a measure of how much capital a strategy can trade before the performance of the strategy degrades from market impact. The capacity calculation is done on a rolling basis with one snapshot taken at the end of each week. This page outlines how LEAN performs the entire calculation.

### Security Capacity

The first step to determine the capacity of the strategy is to compute the capacity of each security the strategy trades.

#### Market Capacity Dollar Volume

Following each order fill, LEAN monitors and records the dollar-volume for a series of bars. To get an estimate of the available capacity, we combine many second and minute trade bars together. For hourly or daily data resolutions, we only use one bar.

_marketCapacityDollarVolume += bar.Close * _fastTradingVolumeDiscountFactor * bar.Volume * conversionRate * Security.SymbolProperties.ContractMultiplier;

##### Crypto Volume

Crypto trade volume is light, but there is significant capacity even at the very top of the order book. The estimated volume of Crypto is based on the average size on the bid and ask.

##### Forex and CFD Volume

Forex and CFD assets do not have a trade volume or quote size information so they were approximated as deeply liquid assets with approximately $25,000,000 depth per minute.

##### Volume Accumulation Period

The number of bars we use to calculate the market volume estimate depends on the asset liquidity. The following table shows the formulas LEAN uses to determine how long of a period the market capacity dollar volume is accumulated for after each order fill, as a function of the security resolution. The $AvgDollarVolume$ in the table represents the average dollar volume per minute for the security you're trading. Notice that for the edge case where the average dollar volume is zero, the calculations use 10 minutes of data.

Resolution | Timeout Period |
---|---|

Second | \[ k = \left\{ \begin{array}{ c l } \frac{100,000}{AvgDollarVolume},& \text{if } AvgDollarVolume \neq 0\\ 10, & \text{otherwise} \end{array} \right. \] \[ \min(120, \max(5, k)) \in [5, 120] \text{ minutes} \] |

Minute | \[ k = \left\{ \begin{array}{ c l } \frac{6,000,000}{AvgDollarVolume},& \text{if } AvgDollarVolume \neq 0\\ 10, & \text{otherwise} \end{array} \right. \] \[ \min(120, \max(1, k)) \in [1, 120] \text{ minutes} \] |

Hour | 1 hour |

Daily | 1 day |

Only a fraction of the market capacity dollar volume is available to be taken by a strategy’s orders because there are other market participants. The data resolution of the security determines how much of the market capacity dollar volume is available for the strategy to consume. The following table shows what percentage of the market capacity dollar volume is available for each of the data resolutions:

Resolution | Available Portion of Market Capacity Dollar Volume (%) |
---|---|

Daily | 2 |

Hour | 5 |

Minute | 20 |

Second | 50 |

Tick | 50 |

#### Fast Trading Volume Discount Factor

To accommodate high-frequency trading strategies, the `_fastTradingVolumeDiscountFactor`

variable scales down the market capacity dollar volume of the security proportional to the number of trades that it places per day for the security. The more frequently the strategy trades a security, the lower the capacity of the security goes since it becomes harder to get into a larger position without incurring significant market impact. The formula that LEAN uses to discount the capacity of the securities that the algorithm trades intraday is

where \( d_i\in{[0.2, 1]} \) is the fast trading volume discount factor after order \(i\) and \(m\) is the number of minutes since order \( i-1 \) was filled. We divide \( m \) by 390 because there are \( 390 = 6.5 * 60 \) minutes of trading in a regular Equity trading day.

#### Sale Volume

In addition to the market capacity dollar volume, for each security the strategy trades, LEAN also accumulates the weekly sale volume of the order fills. The sale volume scales down the weekly snapshot capacity.

SaleVolume += orderEvent.FillPrice * orderEvent.AbsoluteFillQuantity * Security.SymbolProperties.ContractMultiplier;

### Portfolio Capacity

Now that we have the values to calculate the capacity of each security, we can compute the capacity of the portfolio.

#### Snapshot Capacity

To calculate the strategy capactiy, weekly snapshots are taken. When it’s time to take a snapshot, the capacity of the strategy for the current snapshot is calculated by first selecting the security with the least market capacity dollar volume available. The fraction of trading volume that was available for this security is scaled down by the number of orders that were filled for the security during the week. The result is scaled down further by the largest value between the weight of the security’s sale volume in the portfolio sale volume and the weight of the security’s holding value in the total portfolio value. The result of this final scaling is the strategy’s capacity in the current snapshot.

\[ Snapshot \ Capacity = \frac{\frac{Market \ Capacity \ Dollar \ Volume}{Number \ Of \ Trades}}{\max(\frac{Sale \ Volume}{Portfolio \ Sale \ Volume}, \frac{Buying \ Power \ Used}{Total \ Portfolio \ Value})} \]When any of the denominators are 0 in the preceding formula, the quotient that the denominator is part of defaults to a value of 0. After the snapshot is taken, the sale volume and market capacity dollar volume of each security is reset to 0.

#### Strategy Capacity

Instead of using the strategy’s capacity at the current snapshot as the final strategy capacity value, the strategy capacity is smoothed across the weekly snapshots. First, the capacity estimate of the current snapshot is calculated, then the final strategy capacity value is set using the following exponentially-weighted model:

\[ Strategy \ Capacity = \left\{ \begin{array}{ c l } S_{i},& \text{if } i = 1\\ 0.66 * S_{i-1} + 0.33 * S_{i}, & \text{if } i > 1 \end{array} \right. \]where \( S_i \) is the snapshot capacity of week \(i\).

### Summary

Strategies that have a larger capacity are able to trade more capital without suffering from significant market impact. In general, a strategy that trades a large weight of the portfolio in liquid securities with high volume will have a large capacity. To avoid reducing the strategy capacity too much, only trade a small portion of your portfolio in illiquid assets with low volume.