How to Auto-Correct Mismatched NinjaTrader Positions with your TradingView Strategy

Strategy Sync can now auto-correct position mismatches with resync mode and verify exact contract counts with target_quantity. No more manual intervention.

Auto-correct checkmark and X icons over a candlestick chart — CrossTrade Strategy Sync resync mode for fixing TradingView to NinjaTrader position mismatches

Strategy Sync has prevented a lot of bad trades since we introduced it. When your TradingView strategy thinks you're long but NinjaTrader is flat, the sync engine blocks the next signal instead of letting it create a position that makes no sense in context. That's valuable. But blocking based on mismatched market direction is a blunt instrument. The signal gets rejected, the mismatch persists, and someone has to notice and fix it before the strategy can continue.

Two new capabilities change that. Resync mode (out_of_sync=resync) calculates the exact correction needed and submits it automatically. Target quantity (target_quantity) lets the sync engine verify not just the direction of a position but the exact number of contracts. Together, they turn Strategy Sync from a safety gate into a self-healing system.

Both require XT Add-On v1.12.0 or later. resync requires target_quantity to calculate the correction delta. If you're not familiar with the basics of Strategy Sync, read the primer first on how to synchronize NinjaTrader 8 with TradingView strategies. This post assumes you already understand sync_strategy, market_position, prev_market_position, and the existing wait, flatten, and ignore behaviors.

The Problem Resync Solves

Consider a strategy that enters long 3 contracts and later scales out to 1. If the webhook that sold 2 contracts gets dropped (connection blip, timeout, TradingView hiccup), the strategy now thinks it's long 1 while NinjaTrader is still holding 3. Every subsequent signal is going to be wrong.

With out_of_sync=wait, the next signal is blocked. The position stays at 3 until you manually intervene. With out_of_sync=flatten, the position is wiped entirely, which fixes the mismatch but also nukes 3 contracts when you only needed to sell 2. Neither outcome is what you actually want.

With out_of_sync=resync, the Add-On looks at the target state (long 1), looks at the actual state (long 3), calculates the delta (sell 2), and submits a corrective market order. The original signal then proceeds normally. No manual intervention, no unnecessary flattening, no accumulated drift.

How Resync Works

When a sync mismatch is detected and out_of_sync=resync is set, the behavior depends on the order type.

For market orders, the Add-On treats the incoming signal as a request to reach an absolute position state. It computes the total delta between the current NT8 position and the target, then rewrites the order to close that gap in a single market order. If your strategy says you should be long 5 but NT8 only has 3, it submits Buy 2. If NT8 somehow has 7, it submits Sell 2 to trim back. If the position already matches the target, no order is placed at all.

For limit and stop orders, the logic is more involved. A resting order can't fix an immediate mismatch because it won't execute until price reaches it. So the Add-On performs two actions in sequence: first, a corrective market order to align the position to what we call the "base target" (the target minus the expected impact of the limit/stop once it fills), and then the original limit/stop order is submitted so it's in place for future price action.

The correction order waits up to 2 seconds for a fill confirmation before proceeding with the original order. If it doesn't fill in time, the original order is placed anyway so you maintain protective coverage.

If the correction order conflicts with Opposing Position Protection, the resync is blocked and the original order is withheld to prevent creating a worse mismatch. If OPP blocks the correction repeatedly within a 2-minute window, the system falls back to wait behavior and logs a warning that manual intervention is required.

What Target Quantity Adds

Without target_quantity, Strategy Sync compares state transitions: "was flat, going long" vs "is flat, would be long after this order." That's sufficient for simple strategies that enter and exit in whole positions. But it can't detect quantity mismatches. If your strategy expects long 5 and NT8 has long 3, the transition check says "long → long" on both sides and calls it in sync.

Adding target_quantity switches the sync engine from transition-based comparison to absolute quantity comparison. Instead of asking "does the direction match?", it asks "will the resulting position be exactly N contracts in the right direction?"

TradingView provides the dynamic variable for this:

target_quantity={{strategy.position_size}};

The value can be a positive integer (interpreted as long), a negative integer (interpreted as short), or zero (flat). If you pass an unsigned integer, the direction is inferred from the market_position field.

A full webhook payload with resync and target quantity looks like this:

key=your-secret-key;
command=PLACE;
account=Sim101;
instrument=NQ1!;
action={{strategy.order.action}};
qty={{strategy.order.contracts}};
order_type=MARKET;
tif=DAY;

sync_strategy=true;
market_position={{strategy.market_position}};
prev_market_position={{strategy.prev_market_position}};
target_quantity={{strategy.position_size}};
out_of_sync=resync;

That's it. Every field after sync_strategy=true is populated by TradingView at alert time. You never hard-code position states or quantities. The strategy provides the expected state, NT8 provides the actual state, and the sync engine reconciles the difference.

When to Use Each Mode

The four out_of_sync behaviors serve different risk profiles.

wait is for traders who want full manual control. Nothing happens until you review the mismatch and decide what to do. Use this if you don't trust automation to correct positions, or if your strategy is complex enough that the "right" correction depends on context the sync engine can't see.

flatten is the self-healing option for strategies that always enter from flat. If something goes wrong, wipe the slate and let the next clean entry re-establish the position. It's simple and effective but wasteful if you were in a partial position that only needed a small correction.

resync is the surgical option. It calculates the minimum correction needed and applies it. Use this for strategies that scale in and out, run multiple contracts, or where flattening would destroy a position that's 90% correct. Pair it with target_quantity for precise contract-level targeting.

ignore bypasses sync entirely. The order goes through regardless of mismatch. Only use this if you have a specific reason to force an order and you understand the consequences.

For most automated TradingView strategies, resync with target_quantity is the right answer. It handles missed signals, partial fills, and connection drops without human intervention, and target_quantity catches quantity drift that transition-based sync would miss.

Testing

Test this in simulation first. Run your strategy on a sim account with out_of_sync=resync and target_quantity enabled, then deliberately create mismatches: manually close a position in NT8 while TradingView is still in a trade, or reduce the position size by hand. Watch the Add-On's control panel logs to see the correction orders fire. The logs show the exact calculation: current position, target position, delta, and the corrective action taken.

If a correction is blocked by OPP, you'll see that in the logs too. Make sure your OPP settings are compatible with the correction direction your strategy might need.

📘 Full Strategy Sync Documentation


New to CrossTrade? Start your free 7-day trial — full access, unlimited alerts, no credit card required.

Start your free trial

Try CrossTrade for 7 days.

Sign Up