Building a trading engine
In my spreadsheet, I could record a purchase with just a quantity and a price. In the trading engine I built, a broker sits between the decision to buy and the record of a trade. It can accept an order, leave it open, or reject it.
Replaying a decision
I started in Excel, testing rules against historical prices. I could point to a row and explain why it triggered a purchase. Once I wanted to compare many sets of parameters, rerunning the calculations by hand became impractical. I moved the rules into code so I could replay a price series under the same conditions.
The engine now runs on C# and .NET. For a backtest it reads historical prices from PostgreSQL. In live operation it receives quotes over NATS. Keeping the price feed separate from the strategy lets me examine the decision it makes on a particular quote. I can test what happens at the broker separately.
I also run a genetic algorithm on historical data. Each candidate holds the parameters of a strategy. The evaluator runs a backtest for each candidate and uses selection, crossover, and mutation to produce another generation. I can compare two configurations over identical windows, then set aside a separate period to see how much the choice depended on the data I used to tune them.
When the price moves
A spreadsheet row could use the last observed price as the purchase price. Sending an order takes time, and the available quantity at that price can change. A market order may fill at several prices. A limit order caps the purchase price and may stay open or fill only in part.
Consider a simulated purchase of ten units. The strategy sees a reference price of 100 and the order eventually fills at an average of 100.20. The purchase costs two currency units more than the spreadsheet predicted, before fees. That calculation captures the effect of slippage on one order. A backtest using that assumption still needs to be checked against observed fills.
Handling an open order
One broker integration allows only one open order from the system at a time. A new signal can arrive while the previous order is still open. Before choosing another buy or sell, the engine asks the broker for its list of pending orders. That costs a round trip and can mean missing an opportunity during an outage. It also keeps the strategy from making a second commitment against money or holdings the first order may already have tied up.
| Broker response | Next step |
|---|---|
| Empty list | Continue with price, quantity, and budget checks. |
| Open order | Wait before selecting another buy or sell. |
| Request failed | Pause selection until the broker can be queried again. |
The regression test uses a simulated broker. It starts with a purchase, changes the strategy’s target, and makes the broker report a pending order. The history should still contain the original buy and no sell. A second test makes the query fail and checks that cash and positions stay put. This lets me reproduce the decision without placing an order in the market.
Tracing a trade in production
I packaged the services with Docker Compose and used Ansible for provisioning. Prometheus collects engine metrics, which I can review over time in Grafana. When I investigate a particular trade, I start with the price the strategy received, its decision, and the broker’s response. Average latency can point me to a problematic interval. Explaining one order takes the records for that order.
If I were starting again, I would write the pending-order tests earlier. It was easy to focus on which signal a strategy would generate. Working out whether the engine could act on that signal turned out to be just as important.