City API supports a modified version of live SPAN (standard portfolio analysis of risk) margin calculations for some exchanges. The exchanges currently supported are all Globex exchanges (CME, NYMEX, COMEX, CBOT, KCBT) and the ICE. If a position is held on instruments from other exchanges, they will not factor in to margin calculations.
The official documentation for SPAN can be found here.
At a high level, the margin requirement is the scan risk (also called portfolio risk), plus an intra-commodity spread charge (also called basis risk), minus an inter-commodity spread credit (not currently included), minus the net option value of the position. The greater of this value and the short option minimum is the margin requirement.
The inputs for scanning risk, basis risk and short option minimums is set by the exchange.
There are two differences between the margin calculated by City API and the official SPAN value. One, City API does not currently include the inter-commodity spread credit. Two, in some cases the exchange does not supply the scanning risk for a given strike. If this is the case, the scanning risk for the nearest available strike is used.
Accounting for Open Orders
City API needs to consider the highest possible margin requirement that could result at any instant to perform this check. That is, it assumes that any combination of open orders could conceivably trade at any instant. This calculation can become costly to compute, however if an account has many open orders (the number of calculations is exponential with the number of orders).
Therefore, City API does not calculate the exact worst-case, but instead calculates an upper bound that is much cheaper to calculate (we always err on the side of requiring more, not less margin). Specifically, we make a modification to the calculation to address open orders: we do not apply any credits from risk arrays. That is, we assume the parts that add margin could be applicable, but not the parts that reduce margin.