Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Check if Stop Loss was triggered

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Check if Stop Loss was triggered

    Hello,

    I am attempting to create a bool that tells my strategy whether or not a stop loss order was filled.

    in OnExecution update I ahve something like:

    Code:
    if (OrderFilled(execution.Order))
    {
    
        if (IsEntryOrder(execution.Order))
        {
            _entryPrice = execution.Order.AverageFillPrice;
            SetCurrentPositionType(execution.Order);
    
            if (IsLongPosition())
            {
                // do this
            }
            else if (IsShortPosition())
            {
                // do this
            }
        }
        else if (IsExitOrder(execution.Order))
        {
            if (execution.Order.Name == StopLoss)
            {
                _stopLossTriggered = true;
            }
        }
        else
        {
            SetCurrentPositionType(execution.Order);
        }
    }​
    Then, in OnBarUpdate, something like this:

    Code:
    else if (BarsInProgress == 1)
    {
        if (IsFirstTickOfBar)
        {
            _stopLossTriggered = false;
        }
    The second data series is a longer series and I just use it to reset the _stopLossTriggered variable. The idea being that if (within the bar windows of the second data series) a stop loss order would stop the trading during that bars time period.

    However, I am unable to get this to work. Just looking for a point in the right direction. This his stumped me.

    #2
    Hello atariaf,

    Below is a link to a forum post with sample code on detecting if a stop loss has filled.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      NinjaTrader_ChelseaB,

      Thanks for the response. Turned out I was coding that portion correctly. However, I accidentally had the variable I needed changed being update within OnBarUpdate instead of at the class level. That caused it to change my variable at each bar update (clearly) instead of once every bar on the secondary series. So, yay me for having the same code logic as the link you sent. Also, problem solved. Wins all around.

      Thanks,
      atariaf

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by kinfxhk, 07-13-2026, 10:18 AM
      0 responses
      59 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Started by kinfxhk, 07-13-2026, 09:50 AM
      0 responses
      41 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Started by kinfxhk, 07-13-2026, 07:21 AM
      0 responses
      46 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Started by kinfxhk, 07-11-2026, 02:11 AM
      0 responses
      37 views
      0 likes
      Last Post kinfxhk
      by kinfxhk
       
      Started by SalmaTrader, 07-07-2026, 10:26 PM
      0 responses
      157 views
      0 likes
      Last Post SalmaTrader  
      Working...
      X