Problem · Math

Debug the Risk-Limit Quoter

Learn this problem
MediumMillennium logoMillenniumNEW GRADINTERNOA

Problem statement

A shipped risk-limit quoter contains two defects: its inventory skew can push a position farther from zero, and it can accept a trade exactly at the hard risk cap. Repair the supplied implementation without changing its interface.

The source-backed Java translation of the shipped starter is reproduced below for diagnosis. It intentionally retains the buggy behavior:

public String[] priceQuote(int currentInventory, String side, int quantity, double reference, double halfSpread, int baseLimit, double softFraction, double skewCoefficient) {
    int signedQuantity = side.equals("BUY") ? quantity : -quantity;
    long projected = (long) currentInventory + signedQuantity;
    double hard = baseLimit;
    double soft = softFraction * hard;
    if (Math.abs(projected) > hard) {
        return new String[]{"reject", "null"};
    }
    double skew = skewCoefficient * currentInventory;
    double quote = side.equals("BUY")
        ? reference + halfSpread + skew
        : reference - halfSpread + skew;
    String action = "accept";
    if (Math.abs(projected) > soft) {
        action = "widen";
        quote += side.equals("BUY") ? halfSpread : -halfSpread;
    }
    double rounded = Math.floor(quote * 1000000.0 + 0.5) / 1000000.0;
    return new String[]{action, String.format(java.util.Locale.US, "%.6f", rounded)};
}

For this quoter, apply these corrected rules in order:

  1. Use signedQuantity = +quantity for BUY and -quantity for SELL, then compute projected = currentInventory + signedQuantity.
  2. Set hard = baseLimit and soft = softFraction * hard. If abs(projected) >= hard, return ["reject", "null"].
  3. Otherwise use skew = -skewCoefficient * currentInventory. The base BUY quote is reference + halfSpread + skew; the base SELL quote is reference - halfSpread + skew.
  4. If abs(projected) > soft, return action widen after adding another halfSpread for BUY or subtracting another halfSpread for SELL. Otherwise return action accept.

The two source-backed risk defects to diagnose are the hard-cap comparison and skew sign shown above. For the execution contract, every decimal input is exact and has at most seven digits after the decimal point; do not copy the shipped binary rounding expression into the repaired implementation. Return a two-element string array [action, quote]. A non-rejected quote is rounded half upward to six decimal places and serialized with exactly six digits after the decimal point.

Function

priceQuote(currentInventory: int, side: String, quantity: int, reference: double, halfSpread: double, baseLimit: int, softFraction: double, skewCoefficient: double) → String[]

Examples

Example 1

currentInventory = 900side = "BUY"quantity = 100reference = 100.0halfSpread = 0.05baseLimit = 1000softFraction = 0.6skewCoefficient = 0.001return = ["reject","null"]

The projected inventory is exactly 1000. Equality at the hard cap is rejected.

Example 2

currentInventory = 500side = "SELL"quantity = 100reference = 100.0halfSpread = 0.05baseLimit = 1000softFraction = 0.6skewCoefficient = 0.001return = ["accept","99.450000"]

Projected inventory is 400, below the soft limit. Correct skew is -0.5, so the SELL quote is 100 - 0.05 - 0.5 = 99.45.

Constraints

  • -10^9 <= currentInventory <= 10^9
  • side is either BUY or SELL.
  • 1 <= quantity <= 10^9
  • 0 < reference <= 2 * 10^8 and 0 <= halfSpread <= 10^6.
  • 1 <= baseLimit <= 10^9 and 0 <= softFraction <= 1.
  • 0 <= skewCoefficient <= 10^3.
  • Each decimal input has at most seven digits after the decimal point.
  • Every non-rejected quote is positive and at most 10^12.

Source note: The original assessment screenshot supplies the debugging setup, post-trade inventory direction, risk-cap behavior, and shipped Python implementation context.

More Millennium problems

drafts saved locally
public String[] priceQuote(int currentInventory, String side, int quantity, double reference, double halfSpread, int baseLimit, double softFraction, double skewCoefficient) {
    // write your code here
}
currentInventory900
side"BUY"
quantity100
reference100.0
halfSpread0.05
baseLimit1000
softFraction0.6
skewCoefficient0.001
expected["reject", "null"]
checking account