NexusFi: Find Your Edge


Home Menu

 





preventing non-coded trades


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one Fi with 4 posts (0 thanks)
    2. looks_two senrab with 2 posts (1 thanks)
    3. looks_3 Leonardo Llaneza with 1 posts (0 thanks)
    4. looks_4 selion89 with 1 posts (1 thanks)
    1. trending_up 416 views
    2. thumb_up 2 thanks given
    3. group 2 followers
    1. forum 7 posts
    2. attach_file 0 attachments




 
Search this Thread
  #1 (permalink)
 senrab 
Gold Coast
 
Experience: Intermediate
Platform: NinjaTrader
Broker: VisionFinancialMarkets/Rithmic
Trading: CL
Posts: 5 since Sep 2011
Thanks Given: 47
Thanks Received: 2

All my auto-strats are coded to trade in one direction only. They are either exclusively Long or Short.
Every so often I experience what I call a phantom trade.
For example: A Long enters a trade that goes to Stop Loss. At that point an unscripted Short activates. Sometimes they luck out but mostly they end in a costly loss.
I'm wanting to find coding that I can add to my strats that will deal with this problem.
Can you help out?


Started this thread Reply With Quote
Thanked by:

Can you help answer these questions
from other members on NexusFi?
$500M Riding on World Cup 2026: France/Spain Co-Favored …
Prediction Markets & Event Contracts
April CPI Preview: +3.7% YoY Expected at 8:30 AM ET -- C …
Traders Hideout
CFTC Rewrites the Rulebook -- Kalshi Cracks $1B Non-Spor …
Prediction Markets & Event Contracts
Peace Deal Forward Curve: May 22%, June 51%, December 81 …
Prediction Markets & Event Contracts
Strike Pause Holds, Oil Erases Monday Spike -- May CPI W …
Traders Hideout
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
NexusFi site changelog and issues/problem reporting
8 thanks
Darmok and Jalad at Tanagra
1 thanks
  #2 (permalink)
 
Fi's Avatar
 Fi 
NexusFi
 


senrab View Post
All my auto-strats are coded to trade in one direction only. They are either exclusively Long or Short.
Every so often I experience what I call a phantom trade.
For example: A Long enters a trade that goes to Stop Loss. At that point an unscripted Short activates.

@senrab,

Classic NinjaTrader Managed Approach trap -- and it bites more people than you'd think.

Here's what's happening: when your stop loss fills, NinjaTrader's Managed Approach Entry() methods will automatically reverse your position if entry conditions for the opposite direction happen to evaluate true in that same OnBarUpdate() cycle. Your strategy says "long only," but NinjaTrader doesn't enforce that constraint unless you explicitly code it.

The fix has three layers:

1. Hard Direction Guard (Essential)
At the top of your OnBarUpdate(), block the opposite direction entirely:
 
Code
// For a LONG-ONLY strategy:
if (Position.MarketPosition == MarketPosition.Short)
{
    ExitShort();  // Emergency flatten
    return;
}
2. Stop-Hit Flag via OnExecutionUpdate (Recommended)
Track when your stop fires and prevent any new entries that bar:
 
Code
private bool stopHitThisBar = false;

protected override void OnBarUpdate()
{
    stopHitThisBar = false;  // Reset each bar
    if (stopHitThisBar) return;
    // ... your entry logic here
}

protected override void OnExecutionUpdate(...)
{
    if (execution.Order.Name == "Stop loss"
        && execution.Order.OrderState == OrderState.Filled)
    {
        stopHitThisBar = true;
    }
}
3. Unique Order Names
Give your stop orders unique signal names so you can track and cancel them explicitly when the position goes flat. The default SetStopLoss() orders can't be canceled with CancelOrder() -- they persist and cause exactly the phantom fills you're describing.

One more thing -- EntriesPerDirection alone won't fix this. The NinjaTrader support forum confirms that detecting stop loss fills for reversal prevention requires manual coding via OnExecutionUpdate(). The Strategy Builder can't handle this logic, so you'll need to unlock the script.

There's a good NexusFi discussion on EntriesPerDirection behavior worth reading:



What platform version are you running and are these strategies built in Strategy Builder or hand-coded NinjaScript? That'll determine the cleanest path forward.

TGIF! Have a good weekend!

-- Fi

"The market doesn't care about your code -- it only cares about your orders."


Learn more about Fi AI trading companion
IMPORTANT: I can make mistakes! Always verify data before relying on it.

Please leave feedback here. You can disable my ability to reply to your posts by placing me on your ignore list.

Fi provides educational information on a best-effort basis only. You are responsible for your own trading decisions and for verification of all data. This message is not trading advice.
Reply With Quote
  #3 (permalink)
 
Fi's Avatar
 Fi 
NexusFi
 



senrab View Post
All my auto-strats are coded to trade in one direction only. They are either exclusively Long or Short.
Every so often I experience what I call a phantom trade.
For example: A Long enters a trade that goes to Stop Loss. At that point an unscripted Short activates.

@senrab,

Classic NinjaTrader Managed Approach trap -- and it bites more people than you'd think.

Here's what's happening: when your stop loss fills, NinjaTrader's Managed Approach Entry() methods will automatically reverse your position if entry conditions for the opposite direction happen to evaluate true in that same OnBarUpdate() cycle. Your strategy says "long only," but NinjaTrader doesn't enforce that constraint unless you explicitly code it.

The fix has three layers:

1. Hard Direction Guard (Essential)
At the top of your OnBarUpdate(), block the opposite direction entirely:
 
Code
// For a LONG-ONLY strategy:
if (Position.MarketPosition == MarketPosition.Short)
{
    ExitShort();  // Emergency flatten
    return;
}
2. Stop-Hit Flag via OnExecutionUpdate (Recommended)
Track when your stop fires and prevent any new entries that bar:
 
Code
private bool stopHitThisBar = false;

protected override void OnBarUpdate()
{
    stopHitThisBar = false;  // Reset each bar
    if (stopHitThisBar) return;
    // ... your entry logic here
}

protected override void OnExecutionUpdate(...)
{
    if (execution.Order.Name == "Stop loss"
        && execution.Order.OrderState == OrderState.Filled)
    {
        stopHitThisBar = true;
    }
}
3. Unique Order Names
Give your stop orders unique signal names so you can track and cancel them explicitly when the position goes flat. The default SetStopLoss() orders can't be canceled with CancelOrder() -- they persist and cause exactly the phantom fills you're describing.

One more thing -- EntriesPerDirection alone won't fix this. The NinjaTrader support forum confirms that detecting stop loss fills for reversal prevention requires manual coding via OnExecutionUpdate(). The Strategy Builder can't handle this logic, so you'll need to unlock the script.

There's a good NexusFi discussion on EntriesPerDirection behavior worth reading:



What platform version are you running and are these strategies built in Strategy Builder or hand-coded NinjaScript? That'll determine the cleanest path forward.

TGIF! Have a good weekend!

-- Fi

"The market doesn't care about your code -- it only cares about your orders."


Learn more about Fi AI trading companion
IMPORTANT: I can make mistakes! Always verify data before relying on it.

Please leave feedback here. You can disable my ability to reply to your posts by placing me on your ignore list.

Fi provides educational information on a best-effort basis only. You are responsible for your own trading decisions and for verification of all data. This message is not trading advice.
Reply With Quote
  #4 (permalink)
 
Fi's Avatar
 Fi 
NexusFi
 


senrab View Post
All my auto-strats are coded to trade in one direction only. They are either exclusively Long or Short.
Every so often I experience what I call a phantom trade.
For example: A Long enters a trade that goes to Stop Loss. At that point an unscripted Short activates.

@senrab,

Classic NinjaTrader Managed Approach trap -- and it bites more people than you'd think.

Here's what's happening: when your stop loss fills, NinjaTrader's Managed Approach Entry() methods will automatically reverse your position if entry conditions for the opposite direction happen to evaluate true in that same OnBarUpdate() cycle. Your strategy says "long only," but NinjaTrader doesn't enforce that constraint unless you explicitly code it.

The fix has three layers:

1. Hard Direction Guard (Essential)
At the top of your OnBarUpdate(), block the opposite direction entirely:
 
Code
// For a LONG-ONLY strategy:
if (Position.MarketPosition == MarketPosition.Short)
{
    ExitShort();  // Emergency flatten
    return;
}
2. Stop-Hit Flag via OnExecutionUpdate (Recommended)
Track when your stop fires and prevent any new entries that bar:
 
Code
private bool stopHitThisBar = false;

protected override void OnBarUpdate()
{
    stopHitThisBar = false;  // Reset each bar
    if (stopHitThisBar) return;
    // ... your entry logic here
}

protected override void OnExecutionUpdate(...)
{
    if (execution.Order.Name == "Stop loss"
        && execution.Order.OrderState == OrderState.Filled)
    {
        stopHitThisBar = true;
    }
}
3. Unique Order Names
Give your stop orders unique signal names so you can track and cancel them explicitly when the position goes flat. The default SetStopLoss() orders can't be canceled with CancelOrder() -- they persist and cause exactly the phantom fills you're describing.

One more thing -- EntriesPerDirection alone won't fix this. The NinjaTrader support forum confirms that detecting stop loss fills for reversal prevention requires manual coding via OnExecutionUpdate(). The Strategy Builder can't handle this logic, so you'll need to unlock the script.

There's a good NexusFi discussion on EntriesPerDirection behavior worth reading:



What platform version are you running and are these strategies built in Strategy Builder or hand-coded NinjaScript? That'll determine the cleanest path forward.

TGIF! Have a good weekend!

-- Fi

"The market doesn't care about your code -- it only cares about your orders."


Learn more about Fi AI trading companion
IMPORTANT: I can make mistakes! Always verify data before relying on it.

Please leave feedback here. You can disable my ability to reply to your posts by placing me on your ignore list.

Fi provides educational information on a best-effort basis only. You are responsible for your own trading decisions and for verification of all data. This message is not trading advice.
Reply With Quote
  #5 (permalink)
 
Fi's Avatar
 Fi 
NexusFi
 


senrab View Post
All my auto-strats are coded to trade in one direction only. They are either exclusively Long or Short.
Every so often I experience what I call a phantom trade.
For example: A Long enters a trade that goes to Stop Loss. At that point an unscripted Short activates.

@senrab,

Classic NinjaTrader Managed Approach trap -- and it bites more people than you'd think.

Here's what's happening: when your stop loss fills, NinjaTrader's Managed Approach Entry() methods will automatically reverse your position if entry conditions for the opposite direction happen to evaluate true in that same OnBarUpdate() cycle. Your strategy says "long only," but NinjaTrader doesn't enforce that constraint unless you explicitly code it.

The fix has three layers:

1. Hard Direction Guard (Essential)
At the top of your OnBarUpdate(), block the opposite direction entirely:
 
Code
// For a LONG-ONLY strategy:
if (Position.MarketPosition == MarketPosition.Short)
{
    ExitShort();  // Emergency flatten
    return;
}
2. Stop-Hit Flag via OnExecutionUpdate (Recommended)
Track when your stop fires and prevent any new entries that bar:
 
Code
private bool stopHitThisBar = false;

protected override void OnBarUpdate()
{
    stopHitThisBar = false;  // Reset each bar
    if (stopHitThisBar) return;
    // ... your entry logic here
}

protected override void OnExecutionUpdate(...)
{
    if (execution.Order.Name == "Stop loss"
        && execution.Order.OrderState == OrderState.Filled)
    {
        stopHitThisBar = true;
    }
}
3. Unique Order Names
Give your stop orders unique signal names so you can track and cancel them explicitly when the position goes flat. The default SetStopLoss() orders can't be canceled with CancelOrder() -- they persist and cause exactly the phantom fills you're describing.

One more thing -- EntriesPerDirection alone won't fix this. The NinjaTrader support forum confirms that detecting stop loss fills for reversal prevention requires manual coding via OnExecutionUpdate(). The Strategy Builder can't handle this logic, so you'll need to unlock the script.

There's a good NexusFi discussion on EntriesPerDirection behavior worth reading:



What platform version are you running and are these strategies built in Strategy Builder or hand-coded NinjaScript? That'll determine the cleanest path forward.

TGIF! Have a good weekend!

-- Fi

"The market doesn't care about your code -- it only cares about your orders."


Learn more about Fi AI trading companion
IMPORTANT: I can make mistakes! Always verify data before relying on it.

Please leave feedback here. You can disable my ability to reply to your posts by placing me on your ignore list.

Fi provides educational information on a best-effort basis only. You are responsible for your own trading decisions and for verification of all data. This message is not trading advice.
Reply With Quote
  #6 (permalink)
selion89
New York City
 
Posts: 18 since Apr 2025
Thanks Given: 0
Thanks Received: 7

You need to add a state check to your code to ensure no new positions open until the current one is fully closed. Otherwise, your logic is just triggering entry conditions regardless of what's already happening.


Reply With Quote
Thanked by:
  #7 (permalink)
 senrab 
Gold Coast
 
Experience: Intermediate
Platform: NinjaTrader
Broker: VisionFinancialMarkets/Rithmic
Trading: CL
Posts: 5 since Sep 2011
Thanks Given: 47
Thanks Received: 2


selion89 View Post
You need to add a state check to your code to ensure no new positions open until the current one is fully closed. Otherwise, your logic is just triggering entry conditions regardless of what's already happening.

Thanks for your reply. Greatly appreciated.
I'll incorporate your code and test it in the live market alongside my current strategies.
The problem occurs infrequently but when I get a recurrence I'll let you know how it went.

Thanks again for your help.

Additional: To answer your questions
I am not a coder. I started with Strategy Builder and over the years I've become more comfortable working directly with the code.
Currently using NinjaTrader Version 8.1.6.3. 64bit


Started this thread Reply With Quote
  #8 (permalink)
Leonardo Llaneza
Miami Florida
 
Posts: 5 since Jul 2026
Thanks Given: 0
Thanks Received: 1


senrab View Post
All my auto-strats are coded to trade in one direction only. They are either exclusively Long or Short.
Every so often I experience what I call a phantom trade.
For example: A Long enters a trade that goes to Stop Loss. At that point an unscripted Short activates. Sometimes they luck out but mostly they end in a costly loss.
I'm wanting to find coding that I can add to my strats that will deal with this problem.
Can you help out?

One common cause of this: if you're using a boolean flag (like stopHitThisBar) to block a re-entry after a stop fills, double check where in OnBarUpdate() you're resetting that flag to false. If it's getting reset at the top of the method before your entry-blocking check runs, the guard never actually does anything — it's always false by the time you check it. Can you post your OnBarUpdate() logic (or wherever you're setting/checking that flag)? Happy to take a look and pinpoint exactly what's happening.


Reply With Quote




Last Updated on July 16, 2026


© 2026 NexusFi®, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
All information is for educational use only and is not investment advice. There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Downloads - Top
no new posts