← Back to work
Work

Liquidation that ran only when the client polled

A margin check every 30 seconds replaced one that ran when the account screen loaded.

↓

Margin checking

Closing a losing position before the loss exceeds the deposit works only if something checks the margin level while prices move.

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, and 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 applied a forex-lot formula, price × volume × 100,000 ÷ 100, to crypto pairs as well.
  • Leverage was configurable in the admin panel and stored twice there. The trading form never sent it and the server never saved it.

The 30-second job

  • Liquidation became a job with a warning threshold above a 50% stop-out. I chose 30 seconds over a shorter interval. A shorter one risked overlapping runs and added server load, and 30 seconds still caught price moves. Evaluating positions on each price change was out of the contract's scope.
  • The order path and the account handler shared 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.

Thirty seconds between runs

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

Forex trading platform · contract · 2025

Up next
Fixing money calculations with no automated tests→