← All work
Paper trading · runs locallyRuns locally

MASI Trading Simulator

Moroccan retail investors have nowhere to practise. Brokerage accounts start with real money and no undo, and every paper-trading platform worth using covers US equities. This one runs the local market: real delayed quotes, a simulated order book, exchange hours and commissions that behave like the real thing.

1,000,000
MAD starting balance, per account
09:30–15:30
orders accepted only in market hours
0.10%
commission charged per execution

What it is

A paper-trading platform for the Casablanca exchange. You get an account funded with a million simulated dirhams, live-ish prices pulled from a broker's public feed, and an order book that fills your orders against them. Positions, cash, and profit and loss are tracked the way a broker would track them.

The point is that the simulation is faithful where it matters. Orders outside the trading session are rejected rather than quietly queued at a stale price. Commission is charged at ten basis points on execution, so a strategy that looks profitable before costs stops looking profitable after them — which is the single most common thing a beginner learns too late and too expensively.

Making delayed data behave

The underlying feed is delayed and was never built to be polled by a trading UI. Fetching on demand meant every price tile in the interface became an upstream request, and the upstream is neither fast nor obliged to stay available.

So prices are warmed into Redis by a background process rather than fetched per request. Reads hit the cache with a one-second time-to-live, the refresh loop runs on its own interval with a bounded number of concurrent fetches and a pause between batches, and the API never blocks on the upstream. The trade-off is explicit: quotes are delayed and the platform says so, because pretending otherwise in a tool people use to learn would teach the wrong lesson.

  • Postgres and Redis both gate startup on health checks, so the API never boots against a database that isn't accepting connections.
  • Database access is fully async through SQLAlchemy — a per-request price read must not hold a connection while waiting on the network.
  • Positions are reconciled on an interval rather than trusted from write-time state.

Where it stands

The stack runs end to end with one command, and the roadmap is considerably longer than what is built — which is the honest state of it. Quote ingestion was the last thing being fixed, and the market-data work that came out of this project is what eventually became Casablanca-Bourse Lens, which is live and does have an audience.

The simulator is included here because the interesting engineering is transferable and already done: caching a hostile upstream, modelling executions and costs, and keeping a trading interface responsive while the data underneath it is slow.

Built with

  • FastAPI
  • SQLAlchemy
  • PostgreSQL
  • Redis
  • React
  • TanStack Start
  • Docker Compose

Not hosted: Postgres, Redis, an API, a frontend and a quote-warming worker. Quotes are delayed, and the simulator is for practice — not advice.