Problem · Math
Calculate the Half Spread
Learn this problemProblem statement
A market-making model starts with a base half spread, then adjusts it for volatility and liquidity. Higher volatility must make the quote wider, while higher liquidity must make it narrower.
Implement calculateHalfSpread and compute:
modeledHalfSpread = baseHalfSpread * (1 + volatility) / liquidityScore
Each input is a decimal value with at most seven digits after the decimal point; interpret those decimal values exactly. Return the larger of modeledHalfSpread and minimumHalfSpread. Round the result to six decimal places; an exact halfway case rounds upward.
Function
calculateHalfSpread(baseHalfSpread: double, volatility: double, liquidityScore: double, minimumHalfSpread: double) → doubleExamples
Example 1
baseHalfSpread = 0.02volatility = 0.5liquidityScore = 0.75minimumHalfSpread = 0.03return = 0.04The modeled value is 0.02 * 1.5 / 0.75 = 0.04. It is larger than the minimum 0.03.
Example 2
baseHalfSpread = 0.01volatility = 0.1liquidityScore = 1.0minimumHalfSpread = 0.02return = 0.02The modeled value is 0.011, so the minimum half spread 0.02 becomes the result.
Constraints
0 < baseHalfSpread <= 1000 <= volatility <= 1010^-6 <= liquidityScore <= 10 <= minimumHalfSpread <= 100- Every input has at most seven digits after the decimal point.
- The unrounded modeled value is at most
10^9.