NexusFi: Find Your Edge


Home Menu

 





preventing non-coded trades


Discussion in NinjaTrader

Updated
    1. trending_up 293 views
    2. thumb_up 2 thanks given
    3. group 1 followers
    1. forum 6 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?
Prop Firm Tracked Payouts Hit $115M in Q1 but Growth Fla …
Funded Trading Evaluation Firms
Expiration Day: Wall Street Rallies on Peace Hopes While …
Prediction Markets & Event Contracts
CME Group Fee Schedule Changes Hit All Four Exchanges -- …
Traders Hideout
White House Drops First Alien Files Today -- Market Says …
Prediction Markets & Event Contracts
TradingView Deploys AI to Monitor SEC Filings in Real Ti …
TradingView
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Sober Journey With S&P
22 thanks
2026 Jlab journal
10 thanks
Trying to learn Volume and price action correlation
8 thanks
Algo automated / semi-automated trading anyone?
6 thanks
Lady Vols Primer: Trading Volatility Journal
5 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




Last Updated on March 25, 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