How Strategy Sync Keeps TradingView and NinjaTrader in Lockstep
Strategy Sync prevents state drift between your TradingView strategy and NinjaTrader 8 by comparing expected vs. actual positions before every order executes.
Your TradingView strategy says you're long. NinjaTrader says you're flat. The next signal is a sell, expecting to close a long position. But there's nothing to close, so NT8 opens a new short instead. Now you're short when your strategy thinks you're flat, and every subsequent signal compounds the problem.
This is state drift, and it's the single most dangerous failure mode in automated trading. It doesn't happen because your strategy is broken. It happens because two platforms are making independent assumptions about the same account, and something in between went wrong: a missed webhook, a connection drop, a manual intervention, a partial fill, or an NT8 restart at the wrong moment.
Strategy Sync exists to prevent this. It compares what your TradingView strategy thinks the position is against what NinjaTrader actually has before allowing any order through. If they don't match, it acts on rules you define instead of blindly executing into the wrong state.
Strategy Sync is available with every CrossTrade plan. Try CrossTrade free for 7 days — full access, unlimited alerts, no credit card required.
How It Works
When you add sync_strategy=true to a PLACE command, the XT Add-On performs a state comparison before submitting the order. Three pieces of information make this possible:
What does the strategy expect the position to be after this order? That's the market_position field. If your strategy is entering a new long, market_position=long. If it's closing to flat, market_position=flat.
What did the strategy think the position was before this signal? That's prev_market_position. If your strategy was flat and is now going long, prev_market_position=flat. If it was long and is now reversing to short, prev_market_position=long.
What is the actual position in NinjaTrader right now? The Add-On queries this locally in real time. No external API call, no delay. It knows the account's actual state because it's running inside NT8.
The Add-On compares the strategy's expected state transition (e.g., flat → long) against the actual local state transition (what NT8 currently holds → what NT8 would hold after this order). If they match, the order executes immediately. If they don't, the out_of_sync behavior kicks in.
You never have to manually calculate or hard-code these values. TradingView provides dynamic variables that resolve at alert time:
market_position={{strategy.market_position}};
prev_market_position={{strategy.prev_market_position}};The Full Payload
Here's what a sync-enabled webhook alert looks like:
key=your-secret-key;
command=PLACE;
account=Sim101;
instrument=NQ1!;
action={{strategy.order.action}};
qty={{strategy.order.contracts}};
order_type=MARKET;
tif=DAY;
flatten_first=true;
// --- The New Sync Engine ---
sync_strategy=true;
market_position={{strategy.market_position}};
prev_market_position={{strategy.prev_market_position}};
out_of_sync=flatten;This is a single alert that handles entries, exits, and reversals for any strategy. The TradingView variables populate the action, quantity, and position state dynamically. The sync engine validates everything before it reaches NT8.
What Happens When Positions Don't Match
The out_of_sync parameter controls the response when a mismatch is detected. There are currently three available behaviors:
wait (Default)
The order is rejected. A warning is logged with details about the mismatch. No position changes are made. This is the safest option — nothing happens until the accounts naturally re-align (typically when both reach a flat state) or you intervene manually.
When to use it: When you want maximum safety and you're comfortable reviewing mismatches manually. Good for strategies where you're actively monitoring during the session.
flatten
The order is rejected and the NinjaTrader position is immediately flattened for that instrument. This resets NT8 to flat so the next clean entry signal from your strategy can proceed without interference.
When to use it: This is the right choice for most automated strategies. If something goes wrong, flatten wipes the slate clean. Your strategy will generate a new entry on its next signal, and because both platforms are now flat, the sync check will pass. It's self-healing.
ignore
The sync check is bypassed entirely. The order is placed regardless of the mismatch, with a warning logged. The mismatch still exists after the order executes.
When to use it: Only if you understand exactly why the mismatch exists and need to force a trade through anyway. This is the override switch for advanced users. Use it sparingly.
The "New Entry" Safety Rule
There's one overriding constraint that applies regardless of which out_of_sync mode you choose: you cannot open a new position in NinjaTrader if your strategy believes it was already in a position.
If the Add-On sees that NT8 is flat but your prev_market_position says you were long or short, it treats this as out-of-sync no matter what. The logic is simple: if your strategy thinks it had a position but NT8 doesn't, something went wrong. Opening a new position based on a signal that assumes the old one still exists would compound the error.
This rule prevents the most dangerous drift scenario — cascading entries built on a false premise.
When Do You Need Strategy Sync?
Not every automation setup requires it. Here's how to think about it:
You need sync if your strategy generates both entry and exit signals and you expect TradingView and NT8 to mirror each other throughout the session. Stop-and-reverse strategies, scaling strategies, and any strategy that generates more than one signal per session are prime candidates. The more signals fired per day, the higher the probability that one eventually gets lost.
You probably don't need sync if your strategy fires one entry signal and you manage exits entirely through NinjaTrader (ATM strategies, manual exits, or Account Manager thresholds). In that case, there's no ongoing state to compare — TradingView fires once and NT8 takes over.
You also don't need sync if you're already using flatten_first=true on every signal and your strategy only enters from a flat state. Flatten first guarantees a clean slate before every entry, which sidesteps the drift problem entirely. But it also means you can't scale into positions or run strategies that add to existing positions.
Exit Blocking for ATM Users
If you're using an NinjaTrader ATM Strategy template to manage exits and want TradingView's exit signals blocked (so the ATM handles trade management instead of the strategy), add strategy_exit_block=true to your payload.
This tells the sync engine to block any order where prev_market_position is not flat. In other words, only opening orders from a flat state are allowed through. TradingView strategies require offsetting orders for backtest calculations, so TradingView will still generate exit signals — strategy_exit_block just prevents those from reaching NT8, letting your ATM handle the exit logic instead.
This is an aggressive filter. It doesn't support scaling in or out. Anything other than flat → long or flat → short will be blocked.
Coming Soon: Resync Mode and Target Quantity
We're actively developing two additions to Strategy Sync that will take mismatch handling even further.
out_of_sync=resync will calculate the exact delta between your strategy's target position and NT8's actual position, then submit a corrective market order automatically. If your strategy says you should be long 3 but NT8 only has 1 contract, resync will buy 2 more to align. If the position already matches, nothing happens. This is fully hands-off position correction.
target_quantity will let you specify the absolute position size your strategy expects after an order executes. Instead of only comparing directional transitions (flat → long, long → short), the sync engine will verify the exact number of contracts. This is critical for strategies that scale in and out, and for Signal Share subscribers using position multipliers.
Both features are in development and will be available in an upcoming Add-On release. Follow the Updates page for release announcements.
Common Scenarios
"I had a connection drop and missed a signal. Now my strategy is long on TradingView but flat on NT8." With out_of_sync=flatten, the next signal from your strategy will be rejected (because prev_market_position=long but NT8 is flat), and NT8 stays flat. The strategy will eventually close its TradingView position and go flat. Once both are flat, the next entry signal passes the sync check and you're back in lockstep.
"I manually closed a position in NT8 but TradingView doesn't know." Same scenario. The strategy still thinks it's in a position. With flatten mode, the sync engine blocks subsequent signals until both platforms agree on state. With wait mode, the order is rejected and you can intervene. Either way, you don't get a cascading error.
"My strategy reversed from long to short, but NT8 was already flat." The sync engine sees that NT8 is flat and the strategy's prev_market_position was long. The New Entry Safety Rule triggers: the strategy believes it had a position that NT8 doesn't have. The order is blocked regardless of out_of_sync mode. This prevents a short entry based on a reversal signal that lost its context.
📘 Full Strategy Sync Documentation
For a complete walkthrough of automating TradingView strategies including where Strategy Sync fits into your webhook payload, see How to Automate TradingView Strategies with CrossTrade. For the full command reference, see The Complete Guide to NinjaTrader Commands.
New to CrossTrade? Start your free 7-day trial and connect TradingView to NinjaTrader in minutes.
