Quant Trading Bot Devlog

한국어로 보기

The First Live Order Cancel - A Broker Rejection and an Order-Number Type Mismatch

The path that cancels an unfilled sell order mid-session and re-plans fired on a real account for the first time, and the broker's cancel API rejected it over an order-number type mismatch — with the rejection reason missing from the log. This is how I tracked that down, normalized the order number to an integer, and made the rejection reason print — plus how an external AI advisory checked the safety brake that blocked every buy on a rebound morning.

This is the English version of a post originally written in Korean for my algorithmic trading system devlog(new tab).

A safety path that had never fired, finally fired

Since I put the order-execution and safety layer(new tab) onto a real account last week, trading ran suspiciously clean for days.

No rejections, no unfilled orders. So the path that cancels an unfilled order in the next round and re-plans existed only in code — I had never once sent a real cancel request to the broker on a live market.

A few days ago I even wrote in the log that the more "good days" stack up, the more paradoxically the safety machinery never gets its live test.

Today that path fired for the first time. And the moment it fired, it got rejected.

The sell limit sat above the market, so it didn't fill

It started with one stock's sell order.

The sell limit I set in the morning round landed just above the market price at that moment. So it didn't fill and sat there unfilled.

My system cancels an order left like this right before the lunch round and re-plans. It's the brake that keeps a quote stranded above the market from being left dangling forever.

Today became the day that cancel path ran on a live account for the first time.

The cancel API rejected it — and didn't log why

The problem came in two layers.

First, the broker's cancel API rejected the request. When I passed the original order number, I sent it as a string, but the API wanted that field as an integer. The type didn't match, so all I got back was a rejection code.

Second, and this one stung more: the rejection reason wasn't recorded in the log. Because it was the first firing, the handling that properly captures the rejection response was missing. I could see the rejection code but the "why" was blank, so at first I couldn't pin the cause down.

It's a textbook trap for safety code. The less often a path fires, the more likely it is that when it finally does, the log that would explain the situation is thin.

What I fixed — number normalization and reason logging

Once I confirmed the cause, I fixed two things.

One: before sending a cancel request, always normalize the original order number to an integer. However it arrives upstream — string or not — it leaves for the API as the right type.

Two: when a cancel is rejected, write the reason straight to the log. So that next time it gets rejected, I'm not chasing the "why" after the fact the way I was this time.

The stock still sitting unfilled — with the automated path rejected — I cleaned up by hand today and noted that fact separately in the book. A human filled the spot where automation failed.

Worth underlining: the safety design held. When the cancel was rejected, the path didn't go off buying or selling more on its own — it stopped right there. Per the design of "if it fails, do nothing and call a human," the rejection didn't cascade into secondary damage.

Same morning — a deep drawdown blocked every rebound-day buy

The other axis of today was in the morning.

My system has a brake that blocks new buys while the portfolio sits a certain amount below its recent peak. It's the device that keeps me from buying more on a day the whole book has dropped hard.

But since the book had come down a fair bit from its recent high, every new buy candidate that came up in the morning got all caught by this brake and blocked.

The catch is that the book was actually up versus the prior day that morning. So on a day that was rebounding, buys were all blocked under the reason "still in a drawdown versus the peak." That means missing the rebound-day buys entirely.

External AI advisory — the brake was actually preventing losses

To judge whether this needed changing, I handed the question to an external AI, separate from the one doing the work: is it right to loosen this brake or carve out an exception?

The conclusion was "keep it as is."

The alternative schemes I sketched didn't clear a single one of the pass bars I'd set in advance, and the difference between removing the brake and keeping it was statistically indistinguishable.

The decisive part was looking at the days the brake actually fired. The buy candidates it blocked mostly fell further by that day's close. Blocking them had, if anything, dodged losses.

So setting aside the morning's frustration, I decided to leave both this brake and the unfilled-order re-quote exactly as they are.

One misread I corrected

The advisory also caught something my own reporting had gotten wrong.

The tally that counts unfilled orders was only looking at an old path, so it couldn't see the unfilled orders on the broker path I actually use now. So my earlier report of "zero unfilled" was simply wrong.

The very order whose cancel got rejected today was an order that tally couldn't see. I was reminded again that when a tally only sees half the picture, "no problem" is indistinguishable from "not looking."

Why this matters

Today boils down to "what happens when a safety path that has never fired fires for the first time."

The cancel path was complete in code, but the moment it first fired it tripped on an order-number type, and the rejection-reason log was blank on top of that. A path that never fires always stumbles in small places like this.

Still, the core design held. The rejection didn't cascade into secondary trades — it stopped in place and left a gap for a human to fill. Having taken a real rejection once, I was reminded that it's better to trust a safety brake for "falling toward the safe side when it fails" than for "working flawlessly."

The blocked rebound-day buys are the same grain. It looks like a missed opportunity in the moment, but measure the days the brake actually fired and the blocked side was dodging losses. Release a safety brake just because it's inconvenient, and you lose something bigger where you can't see it.

#quant #order execution #safety #incident