When a signal stays true, the engine enters once: how entry and exit signals are cleaned, and why scaling in is several strategies
A condition such as “go long when the close is above the moving average” stays true for dozens of bars in a trend. If every true bar counted as an entry, one strategy would quietly hold dozens of positions, and both its profit and its drawdown would be inflated by how long the signal lingered, not by the strategy itself. TradePose’s backtest engine cleans the entry and exit conditions it receives into a tidy sequence: one entry and one exit per position, and an entry condition that becomes true again while holding does not open another one. This post explains how the engine does that, why, and how to write a strategy that scales in or out.
A strategy author hands the engine two boolean columns, entry and exit, one value per bar. The engine reads them at each bar’s open and cleans them in three stages. The first stage turns the entry opportunity into a strictly alternating sequence: when both are true on the same bar the exit wins, an entry while holding is ignored, and an exit while flat is ignored, so only the first of consecutive entries counts and only the first exit after an entry counts. The second stage handles the managed policies applied to the opportunity: delayed entries, stops and targets can fill only once inside one opportunity, and when several triggers fire on the same bar a fixed order picks one. The third stage renumbers positions after fill validation, so every row in the trade table corresponds to one entry and one exit. The design exists so that every trade is an independent unit that can be aligned and measured. To scale in or out, do not emit a second entry signal inside the same strategy; write the add-on as another strategy whose entry has one more condition than the original, with the same or a different exit, and let portfolio weights set the size of each leg. Every leg can then be measured on its own and aligned trade by trade with the original.
That is the conclusion. Each section below is the evidence for one claim; follow the sections or the evidence links into the strategy report for the data on every parameter set.
What the author hands to the engine
A TradePose strategy ends in two boolean columns, one value per bar of the primary timeframe:
builder.base(
direction=tp.TradeDirection.LONG,
entry=close > sma_20, # one true / false per bar
exit=close < sma_20,
)
These columns say whether the condition holds on this bar, not “place an order”. The gap between a condition holding and an order being placed is what this post is about. With a 20-bar moving average, entry is true on every bar the price spends above the average; if each of those bars placed an order, the number of positions would grow without bound with the length of the trend.
close here is the previous, completed bar’s close; the current bar’s high, low and close are not known yet. The timing details are in How a backtest avoids look-ahead bias.
Stage one: a strictly alternating sequence of opportunities
Once the engine has entry and exit, it applies four rules in a fixed order:
| Rule | Effect |
|---|---|
| Null counts as false | The first bars, before an indicator has a value, cannot enter by accident |
| When both hold on the same bar, the exit wins | The entry is ignored; the bar closes the position and does not “close and reopen” |
| An entry while holding is ignored | Only the first of consecutive entries counts |
| An exit while flat is ignored | An exit that holds with no position produces no trade |
After cleaning, entries and exits strictly alternate: in, out, in, out. The engine uses that property to number positions, and each number has exactly one entry bar and one exit bar. A simplified example:
| Bar | Author’s entry | Author’s exit | After cleaning | Position |
|---|---|---|---|---|
| 1 | false | false | ||
| 2 | true | false | entry | 1 |
| 3 | true | false | ignored: holding | 1 |
| 4 | true | false | ignored: holding | 1 |
| 5 | false | true | exit | 1 |
| 6 | false | true | ignored: flat | |
| 7 | true | true | ignored: exit wins, but flat, so neither counts | |
| 8 | true | false | entry | 2 |
Two details come up often:
- The exit bar counts as flat. The exit fills at the open, so the position is no longer held on that bar. An entry that holds on the same bar is still dropped by the exit-wins rule; the earliest re-entry is the next bar.
- Re-entering requires an exit in between. If the entry condition holds for ten bars, turns false, then holds again, the second stretch opens nothing as long as
exitnever held in between, because the first position is still open. A strategy that means to re-enter must end the previous position explicitly throughexit.
This stage deals with the entry opportunity itself, the baseline policy described in One entry opportunity, many exit policies: enter at the open when the opportunity starts, exit at the open when it ends.
Stage two: one fill per policy inside each opportunity
Managed policies trade the opportunity differently: a limit order waiting for a pullback, a stop order waiting for a breakout, a stop loss, a take profit, a breakeven stop, a trailing stop. These triggers are evaluated bar by bar inside the opportunity to decide whether this bar fills, so they also fire repeatedly: once a bar has touched the stop price, the next bar’s low is usually still below it.
At this level the engine groups rows by position number and keeps only the first fill in each group, clearing everything after it:
| Trigger | Grouped by | Kept |
|---|---|---|
| Entry triggers (immediate, pullback, breakout) | The stage-one opportunity number | The first entry fill inside the opportunity |
| Exit triggers (stop loss, take profit, breakeven, trailing, time) | The policy’s own entry number | The first exit fill after the entry |
Entry candidates outside the opportunity do not count. Before the opportunity has started, a trigger whose price condition is already met does not fill; on the bar where the opportunity ends, the baseline policy closes at the open, so an entry candidate produced from that same open is too late and is rejected as well.
When several triggers are satisfied on the same bar, the engine neither picks at random nor picks what favours the strategy:
- Triggers of the same kind, such as two stop losses at different distances, resolve to the one the price path reaches first.
- Triggers of different kinds resolve in a fixed order: for entries, immediate, then breakout, then pullback; for exits, the opportunity’s open exit, then an immediate exit, then stop-type, then target-type. A stop and a target touched on the same bar resolve to the stop, for the reasons given in How a backtest avoids look-ahead bias.
One difference from stage one: a policy may enter and exit on the same bar. If the bar that fills a pullback limit order also touches the stop loss, the trade is recorded as a one-bar loss rather than discarded.
Stage three: renumbering after validation
A filled entry still has to pass a consistency check: on the same bar, a long’s entry price cannot be above its take profit or below its stop loss, and the reverse for a short; a bar with an emergency exit at the open but no gap cannot enter either. Entries that fail are removed, and the engine then renumbers positions from the validated entries and exits to produce the final numbers used by the trade table. Every row in that table is therefore guaranteed one entry bar and one exit bar, and appears at most once per opportunity.
Why the engine works this way
One entry and one exit per position is not a shortcut in the code. Everything downstream assumes that a trade is an independent unit:
- Results do not depend on how long a signal lingers. Whether the condition holds for thirty bars or three, there is one trade. Otherwise profit would scale with trend length, and a parameter comparison would really be comparing signal stickiness.
- Per-trade statistics mean something. Maximum adverse excursion, maximum favourable excursion, holding time and average per trade are all computed for one position. Ten overlapping positions have no “MAE of this trade”.
- Children align with their parent. Layered research compares every child strategy with its parent on the same opportunity; that alignment only works when the opportunity has one entry point.
- Size belongs to the portfolio layer. A signal answers “should I be in”, not “how much”. How much is decided by position sizing and portfolio weights from a risk budget, not hidden in how many times a signal repeats.
So how do you scale in or out?
The usual approach emits a second entry inside the same strategy: one unit when the trend confirms, another when it breaks the prior high; or close half at the first target and run the rest to the stop. In TradePose both are written as several independent strategies in one portfolio.
The reason is that scaling signals are, by nature, “the original condition plus one more condition”:
| Desired behaviour | Written as |
|---|---|
| One unit on trend confirmation, add one on a break of the prior high | Strategy A: enter on confirmation. Strategy B: entry = confirmation and break of the prior high; same exit as A |
| Close half at the first target, run the rest to the stop | Two policies on the same opportunity: one exits at the target, the other has only a stop; each gets half the size |
| Reduce on a pullback to the average, add back on reclaim | Strategy A: core position. Strategy B: entry = A’s condition and price above the average; exit = close below the average |
Each strategy is still a clean in, out sequence on its own, and the engine applies the same three stages to each. Together, on the account, they produce the scaling behaviour. Sizes come from portfolio weights, for example one unit for the core strategy and half a unit for the add-on.
Splitting this way gives three things that multiple signals inside one strategy cannot:
- Each leg is measured on its own. Whether the add-on leg has any edge shows up directly in its own win rate, average per trade and drawdown, compared against the core strategy. Hidden inside one strategy, only the total is visible.
- Each leg can be tuned or switched off alone. If the add-on leg stops working in the validation period, remove it from the portfolio and the core strategy is untouched.
- Alignment still holds. The add-on’s entry contains the core strategy’s condition, so each of its trades maps back to the same opportunity of the core strategy; a partial exit written as two policies on one opportunity is aligned trade by trade by construction.
If “close half” differs only in how it exits, two policies on the same opportunity are the cheapest form: the engine computes the opportunity once. If the reduction has its own entry condition, for example it only starts once the core position has an open profit, it is a separate strategy. Either way, both go into one portfolio and each gets its size from a weight.
What to check when writing a strategy
- A long run of true
entryvalues is normal. There is no need to emit the signal only on the first bar with a “false before, true now” pattern; the engine does that. But if the strategy means “enter every time the price reclaims the average”, thenexitmust hold when the price drops below it, or there will be no second entry. exitonly matters while holding. Exits that depend on holding state, such as “N bars after entry”, belong in a daily cutoff or a policy’s time exit, not in a hand-rolled counter inside theexitcolumn.- Both true on one bar means exit only. A reversal from long to short is two strategies in opposite directions, each with its own entries and exits, not overlapping entries and exits inside one strategy.
- More units means a portfolio. Any thought of “this signal deserves more size” becomes another strategy plus a weight, not an expectation that repeated signals stack positions.
This post describes how the engine treats signals, not any strategy’s results. The same-bar ordering rules apply only when the bar cannot prove the order of events; finer data changes the fills. After splitting into several strategies, the correlation between legs and their combined exposure still have to be checked at the portfolio stage; the engine does not merge their risk because they share an opportunity.
Questions this study answers
How many positions does TradePose open when an entry condition holds for several bars in a row?
One. The engine first cleans entries and exits into a strictly alternating sequence: an entry while holding is ignored, an exit while flat is ignored, and when both hold on the same bar the exit wins. Managed policies also fill only once per opportunity. Re-entering requires an exit in between.
How should a strategy scale in or take partial profits?
As several independent strategies in one portfolio: scaling in is another strategy whose entry has one more condition, and a partial exit is two policies with different exits on the same opportunity. Portfolio weights set the size. Each leg can then be measured, switched off and aligned trade by trade with the core strategy.
Questions raised along the way
How do I check whether the cleaning removed more than I expected?
Compare the Run’s trade table with your own signal columns: the number of trades should equal the number of bars where the entry holds and the previous position has already ended. Far fewer trades than expected usually means exit never held while the first position was open, so it never ended.
After the split, the add-on leg is highly correlated with the core strategy. How should the portfolio read that?
High correlation is expected, since the add-on’s entry contains the core strategy’s condition. The portfolio stage asks whether adding the leg improves the whole’s drawdown and return per unit of risk, not whether it is an independent bet.
Method and data
Research setup, data and calculations; the identifiers needed to reproduce the study are under data sources.
Changelog
TradePose Research (2026). "When a signal stays true, the engine enters once: how entry and exit signals are cleaned, and why scaling in is several strategies." TradePose Research Notes.