← Back to work
Work

Liquidation that ran only when the client polled

Positions were closed for insufficient margin only when the account screen was loaded; the check became a 30-second job with one margin formula.

↓

The constraint

A margin account closes a losing position before the loss exceeds the deposit. The closing protects anyone only if something checks the margin level continuously.

Where the check ran

  • The check ran inside the request that loaded the account screen. The handler totalled the open positions and closed all of them if the margin level had reached zero.
  • With the screen closed, nothing ran. A client losing money with the app shut was never stopped out; a client who opened it was stopped out at zero, after the deposit was gone.

Two margins

  • Placing an order reserved a flat $100 per standard lot against the wallet.
  • The account handler computed price × volume × 100,000 ÷ 100, a forex-lot formula, and applied it to crypto pairs as well.
  • Leverage was configurable in the admin panel, stored twice there, never sent by the trading form and never saved by the server.

The 30-second job

  • Liquidation was reimplemented as a job running every 30 seconds, with a warning threshold above a 50% stop-out.
  • The order path and the account handler were given one margin formula, with leverage applied.
  • A price that crossed the stop-out between two runs left the account below zero. The platform set the balance to zero and logged the shortfall.

Left open

Thirty seconds remained between checks. No safety fund or margin-call loan covered a balance that crossed zero; the shortfall was absorbed.

Up next
Fixing money calculations with no automated tests→