Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SetStopLoss() not working as expected

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

    SetStopLoss() not working as expected

    Hello!

    I came across a situation where if you EnterShort() and SetStopLoss() and SetProfitTarget() on a bar that decreases in price. Then on the next bar EnterLong() where the price is increasing and try and also SetStopLoss() you will get an error and your strategy will be disabled. Since the SetStopLoss() was on the bar that EnterShort() the stop loss is set at a price higher than the second bar where you EnterLong() causing the error since you cannot have a stop loss above where you are trying to EnterLong(). The way I have been getting around this by setting the stop loss before EnterLong() on the second bar.

    I feel like this is not how managed orders should work I read in the documentation that the SetStopLoss and SetProfitTargets should be cancelled automatically on strategies that have IsUnmanaged = false;

    Here is a short test strategy to demonstrate this problem. Set Calculate = Calculate.OnEachTick; and IsUnmanaged = false;

    Code:
            
            bool enteredShortPosition = false;
            bool enteredLongPosition = false;
    
            protected override void OnBarUpdate()
            {
                if (State == State.Realtime)
                {
                    if (CurrentBar < 1) return;
    
                    if (Position.MarketPosition == MarketPosition.Flat && Close[0] <= 5255.00 && enteredShortPosition == false)
                    {
                        EnterShort(6, CurrentBar.ToString());
                        SetStopLoss(CalculationMode.Price, 5255.50);
                        SetProfitTarget(CalculationMode.Price, 5251.00);
                        enteredShortPosition = true;
                    }
    
                    if (Position.MarketPosition == MarketPosition.Flat && Close[0] >= 5251.25 && enteredShortPosition == true && enteredLongPosition == false)
                    {
                        //Will prevent errors if set before EnterLong
                        //SetStopLoss(CalculationMode.Price, 5250.75);
    
                        EnterLong(6, CurrentBar.ToString());
    
                        //Will cause errors and diable strategy if SetStopLoss is set after EnterLong
                        SetStopLoss(CalculationMode.Price, 5250.75);
    
                        SetProfitTarget(CalculationMode.Price, 5253.70);
                        enteredLongPosition = true;
                    }
                }
            }​
    To test this on the same bars I am set your playback connection to ES 06-24, one minute time frame, on the day of 05/10/2024 right click on Start on the Playback window and set Go To... Date: 5/10/2024 Time: 06:30:00 AM

    Run this strategy for two bars and you will get an error that disables the strategy. Then change the code so the SetStopLoss is not commented out before the EnterLong and comment out the SetStopLoss(CalculationMode.Price, 5250.75); that comes after EnterLong and you will no longer get an error. This is being caused because the SetStopLoss was first set at a higher price than when it enters long but this should not be since the strategy IsUnmanaged = false; the first SetStopLoss should have been cancelled when the proft target was hit.

    Is this a bug? Is this how SetStopLoss should work, shouldn't the first SetStopLoss() be canceled once the profit target was hit? What is the correct way of handling this without having to SetStopLoss before the EnterLong?

    Thanks
    Last edited by AlgoDreamer; 06-06-2024, 10:07 AM.

    #2
    Hello AlgoDreamer,

    Set methods cannot be unset and will continue to hold their values for new entry orders.

    This means it is important to reset Set methods using a number of Ticks, Percent, or Currency before placing a new entry (or using a unique signalName for each new entry). (Setting to a specific price may be an invalid price when the entry order fills, especially for entry limit and stop orders)

    The help guide notes:

    “Notes:
    As Set method orders are submitted upon receiving an entry execution, the Set method should be called prior to submitting the associated entry order to ensure an initial level is set.“

    “Tips:
    You may call this method from within the strategy OnBarUpdate() method should you wish to dynamically change the stop loss price while in an open position
    Should you call this method to dynamically change the stop loss price in the strategy OnBarUpdate() method, you should always reset the stop loss price / offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your stop loss order on your next open position”

    NinjaScript > Language Reference > Strategy > Order Methods > Managed Approach > SetStopLoss()

    Before calling a new entry call the Set methods first, one line above the entry method:
    Code:
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    // call SetStopLoss() with CalculationMode.Ticks one line above calling EnterLong() to reset
    // this ensures the set method is not holding a price value from a previous order which may be invalid when the new entry fills
    SetStopLoss(CalculationMode.Ticks, 20)
    EnterLong();
    }
    
    // after the entry fills, the stop loss can be moved to any desired valid price with CalculationMode.Price
    if (Position.MarketPosition == MarketPosition.Long && Close[0] > Position.AveragePrice + 10 * TickSize)
    {
    // after 10 ticks of profit, set the stop loss to the maximum valid price
    SetStopLoss(CalculationMode.Price, GetCurrentBid() - TickSize);
    }​



    See the ProfitChaseStopTrailSetMethodsExample example linked below, which demonstrates properly resetting set methods before submitting a new entry.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for clearing that confusion up for me I really appreciate it Chelsea! Love the example code NinjaTrader Customer Service always provides its really helpful!
      Last edited by AlgoDreamer; 06-06-2024, 10:07 AM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      81 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      149 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      79 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      52 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      59 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X