Quant Trading Bot Devlog

한국어로 보기

Re-Investigating the Flip-Flop Alert - The Cause Was a Change in Stock Count, Not Band Oscillation

An alert fired again because the trading system bought a stock and sold part of it right back the next round - a "flip-flop." The design had blamed price jitter at the rounding edge, but re-tallying two months of execution logs read-only showed the real cause was the number of target holdings growing, which shrinks each stock's target amount. Here is how I decided to keep observing instead of flipping the switch off.

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

The "flip-flop" alert — buy, then sell right back

My order-execution and safety layer(new tab) has an alert I call "flip-flop."

It catches the case where the system buys a stock and then, in the very next round, sells part of that same stock back.

Buying and selling in quick succession leaks commission and sell-side tax, so when that kind of round trip shows up, the alert says "something may be tangled."

This morning it fired again on one stock. It was the fourth time this year for the same kind of alert.

The re-review condition had been met, so I couldn't just let it pass

This alert came with a pre-set handling rule.

The rule: "if the same kind of alert recurs or the count grows, re-review the response options."

Today was the fourth, so the condition tripped. Rather than wave it off, I decided to investigate the cause from scratch.

The investigation was read-only. I tallied a bit over two months of execution logs directly — not a single line of code, config, or orders was touched.

The alert's name and its real cause didn't match

This is where it got interesting.

The alert was originally built to catch "price jitter at the band edge." When the system computes a target quantity, it rounds a fractional number up or down, and near that boundary even a tiny price move can flip the target holding up or down by one notch.

So back in August I added a cushion: "ignore wobble of about one notch." That cushion was doing its job — it was blocking the edge jitter as designed.

But the alert firing now had a different cause.

When I tallied the cases, most of the flip-flops happened not because of price, but because the number of target holdings had grown.

More stocks means a smaller target per stock

Here's the structure.

The portfolio splits a fixed budget across several stocks. The more stocks it needs to hold, the smaller the target amount allotted to each one.

If the signal shifts overnight and a few more stocks need to be held, yesterday's reasonable position becomes "too much" today. So the next round sells off the excess.

In other words, selling today a stock I bought yesterday wasn't because that stock's price wobbled — it was because more stocks got added alongside it.

The cushion I added in August targeted the price-side wobble. What's firing now is a change on the money-allocation side, a different class of problem the cushion was never built to stop.

Is flipping the switch off the answer?

One of the response options was to simply turn off the feature that tops up positions a little to use up idle cash.

Those top-up lots were often the ones sold back the next round, so turning it off makes the flip-flop alert nearly disappear.

But when I thought it through, this wasn't a plain "fix the flip-flop."

Turning that feature off leaves that much as cash. It ends up being the same as an exposure decision: "I'll carry a bit more cash."

And two months of logs showed no clear evidence that those top-up lots earned more than cash would have — nor that they earned less. The sample is short and spans one down-market stretch, so the sign couldn't be pinned down.

For now, I chose to keep observing

So today I did not flip the switch off; I chose to keep observing.

Three reasons.

One, the cost leaking out of the flip-flops themselves was small. The cost of waiting a few more days was negligible.

Two, turning this feature off changes exposure, and there happens to be an important performance-judgment window coming next month. Touching composition and exposure again in the middle of that window makes the judgment messy to interpret.

Three, the real cause wasn't the top-up feature but the layer above it — the way the per-stock target amount is divided. That's not something one switch turns off; it's an agenda to revisit the whole sizing design after the performance judgment is done.

I changed the alert's re-review condition

Instead, I reworked the alert's handling rule.

Until now it was "re-review if the same alert recurs." Now I've written explicitly that a flip-flop caused by more stocks being held is no longer grounds for re-review — because I already know what causes it.

In its place I set new re-review conditions: if the alert fires without the stock count growing (that would be the real signal that the cushion failed to block something), or if the alert frequency climbs noticeably, then look again.

I did not silence the alert or lower its level. It still fires. I only rewrote the "what should I do when this fires" standard to match the actual cause.

I handed this outside review to another AI, and the heart of the investigation was that it didn't get led by the alert's name — it tallied two months of logs directly and pointed out that the name and the real cause were different.

Side note — verifying the export-loop no-response fix

I also confirmed one thing on the data side.

In the process that exports stock-price data after the market closes each day, there was an occasional case where one particular stock never responded and the loop stalled.

I verified today that the fix held: the morning check processed every stock with none missed, and the night export had zero dropouts.

The "provisional ratio" I've been watching alongside it (the share of prices not yet finalized) has sat in the same band for a third day. The decision table marks that band as "watch a few more days," so I'm leaving the criteria as-is and observing one more day.

#quant #order execution #safety #alerting #decision-making