Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 Haiasi, 04-25-2024, 06:53 PM
      2 responses
      16 views
      0 likes
      Last Post Massinisa  
      Started by Creamers, Today, 05:32 AM
      0 responses
      4 views
      0 likes
      Last Post Creamers  
      Started by Segwin, 05-07-2018, 02:15 PM
      12 responses
      1,785 views
      0 likes
      Last Post Leafcutter  
      Started by poplagelu, Today, 05:00 AM
      0 responses
      3 views
      0 likes
      Last Post poplagelu  
      Started by fx.practic, 10-15-2013, 12:53 AM
      5 responses
      5,407 views
      0 likes
      Last Post Bidder
      by Bidder
       
      Working...
      X