Retrying Margin-Rejected Buys and Cancel-Replanning Stale Orders on the Live Account
Some first-round buys on the live account were rejected for insufficient buying power, so I added a guard that retries the order once in the same round if the cash shows up. Orders that sat unfilled all day because of a stale planned price now get canceled and re-planned just before the next round.
This is the English version of a post originally written in Korean for my algorithmic trading system devlog(new tab).
Couldn't buy even though the money was there
Since moving to the live account, a small problem showed up on the order execution(new tab) side.
A few of the morning's first-round buys were being rejected for "insufficient buying power."
At first I assumed the cash plan was wrong, but when I looked closer the cause was different.
A stock I'd planned to sell hadn't filled yet, so it hadn't turned into cash — and the buy that was counting on that cash got rejected for "no money."
In other words, the money was about to arrive, but a timing gap of a few seconds to a few minutes meant a buy I should have made never happened before the round ended.
Trying once more within the same round
So I added a guard that retries a margin-rejected buy exactly once, within the same round.
Instead of giving up immediately, it polls the available buying power a few times at short intervals, and once the cash is there, it resubmits the order.
The thing I was most careful about here was "the same order going out twice."
The retry order carries a tag that distinguishes it from the original, so even if the original order comes back to life late, it won't turn into a duplicate buy.
I kept the scope narrow too. The retry applies only to the morning's first-round buys, and only once.
Orders wearing a stale price tag
Separately, I noticed orders that just sat there all day and never filled.
The cause was a "stale planned price."
When an order goes out, its limit price is set from the price at the moment the round was planned. But if the market moves after that, the original price tag won't fill for the rest of the day.
So I changed it to cancel any of the day's still-unfilled orders right before moving to the next round, and re-plan them based on the price at that moment.
If the price tag is stale, peel it off and put a fresh one on.
Cancellation gets the stricter treatment
Cancel-and-replan is the kind of action where "a wrong cancellation is more dangerous," so I handed the design diff off to an external AI for a separate review.
There were no critical defects, but a few important points came back.
One was that when confirming "what was canceled," I should cross-check the order date, symbol, and side all together, so I never mistake some other order for the one I canceled.
Another was to not cancel at all once the time is close to the market close. If you cancel and the re-order can't get in, that slot ends the day empty.
I also applied its advice to not put orders whose cancellation isn't confirmed into the "confirmed rejected" list. Treating an ambiguous state as confirmed throws off every judgment downstream.
With those points folded in, I shipped both guards to the live account together.
The close price again, this time in the opposite direction
Yesterday I found that the "prior-day close" the intraday filter uses was contaminated — the data source(new tab) had a value that included after-hours trades — and I recut it to use the broker API's regular-session close.
Today that same close-price question came up from another direction.
This time it was about which close to treat as the canonical one for the internal data the model looks at.
The conclusion was the opposite of yesterday's. Here we decided to use the close that's finalized after the market has fully closed.
So I also pushed back the time of the daily data re-export and re-scoring — not right after the close, but after the post-session close is finalized. The monitoring window that checks the reconciliation moved to that evening time to match.
The same word, "close," but used in a different place means a different standard to match — something I ran into on both sides over two days.
Why it matters
What I fixed today isn't about making the judgment "smarter." The judgment stays the same; I worked on the path by which that judgment turns into an actual order.
No matter how well you pick a stock, if the cash arrives a few seconds late and you can't buy, or the price tag is stale and it never fills, that judgment stays only on paper.
So these days I spend as much time on "does it actually fill" as on "what to buy."
And the harder an action is to undo — like a cancellation — the more I'm building the habit of not trusting my own call, but holding it up to an outside eye once more before shipping.