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 Mindset, 04-21-2026, 06:46 AM
      0 responses
      90 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      137 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      68 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      120 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      69 views
      0 likes
      Last Post PaulMohn  
      Working...
      X