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 NullPointStrategies, Today, 05:17 AM
      0 responses
      46 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      126 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      66 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      42 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      46 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X