Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SetTrailingStop() causing 'Unable to change order' error... how is this possible?

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

    SetTrailingStop() causing 'Unable to change order' error... how is this possible?

    :: EDIT :: Apologies, thread subject should read SetStopLoss() instead of SetTrailingStop()


    Hello NT team,

    From Google searching, I know that this error has been posted about numerous times; however, I am very confused as to how this happens given my current code logic.

    For example, on a long, how is it even possible for a trailing stop to be set above market price?

    You can see in my code snippit below that trailing stop is set to execute immediately upon entry. This error is being generated in slow markets, and the trailing stops are set at ~8 ticks on average. There is no way that price has moved so quickly that the trailing stop being dropped in would actually be above the entry price (i.e. current market price) that was entered milliseconds prior.

    Code:
    if (isLongConditionMet && Position.MarketPosition == MarketPosition.Flat)
                    {
                        // Print($"Entering Long Position at {Times[0][0]} at price {Closes[0][0]}");
                        EnterLong(Convert.ToInt32(DefaultQuantity), @"LongEntry");
                        SetStopLoss(CalculationMode.Ticks, MaxTicks);  // Sets initial SL at MaxDollarRisk input
                    }​
    For the record, this happens on both longs and shorts.

    Could you help me understand what's going on here, and how I might be able to eliminate this from happening? I'd rather not turn off RealtimeErrorHandling, if possible. My understanding is doing this would prevent a trailing SL being implemented at all (leaving me in a position with no SL) if this error is thrown, no?

    Click image for larger version  Name:	image.png Views:	4 Size:	49.7 KB ID:	1310698
    Attached Files
    Last edited by gcaver; 07-15-2024, 10:51 AM.

    #2
    Hello gcaver,

    Thank you for your post.

    If you are using Set methods, like SetStopLoss(), please note that these cannot be unset so they will continue to have the previous entry’s price when a new entry is submitted. This can also cause this error.

    From the Help Guide on SetStopLoss():

    ”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.”

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

    Set methods should always be set or reset before the entry is called with CalculationMode.Ticks before calling a new entry. They can then be adjusted to a specific price after the entry fills.

    Please let us know if you have any further questions. ​

    Comment


      #3
      Gaby, thanks for the quick response!

      I apologize. I made a bit of a mess out of the original post (e.g. method in thread subject misnamed, see edit at top of OP)... bit of brain fog this morning, and I don't think it was real clear what I was specifically asking.

      To clarify, the initial SetStopLoss(), as seen in my code snippit above, seems to function flawlessly.

      What seems to be throwing the error is the attempt to actually implement my trailing stop code logic, where I still use SetStopLoss(), as I remember reading that you cannot use both SetStopLoss() and SetTrailingStop() together on the same position. Here's what the code looks like to achieve this:

      Code:
      if (Position.MarketPosition == MarketPosition.Long)
      {
      
      if (TSLticks < Math.Abs((Position.AveragePrice - Highs[1][0]) / TickSize))
      {
      TSLticks = Math.Abs(Position.AveragePrice - Highs[1][0]) / TickSize;
      }
      else TSLlast = TSLticks;
      
      if (Highs[1][0] >= Position.AveragePrice + (MaxPoints * TrailerOn) && TSLlast < TSLticks) // Sets Trailing SL kick-in at % MaxDollarRisk input
      {
      SetStopLoss(CalculationMode.Price, (Position.AveragePrice + (TSLticks * TickSize) - (MaxPoints * TrailerPercent))); // Sets TSL trailer at % MaxDollarRisk input
      // Print($"Moving trailing stop at {Times[0][0]}");
      
      }
      }
      ​Would you speculate that the issue is still being caused by the lack of a reset in my OnBarUpdate() method? Or does this lead your answer in a different direction?

      Extremely grateful for your time and help!

      Comment


        #4
        Hello,

        Thank you for your response.

        Yes, as mentioned set methods should always be reset before the entry is called with CalculationMode.Ticks, or it could lead to this error.

        Have you attempted to try resetting SetStopLoss() before calling a new entry?

        Comment


          #5
          Just added it and testing it now.

          Side question: If I add code to and recompile a script, do I have to completely remove the strategy from the Control Center Strategies tab and re-add it for the changes to take effect? Or can I just Disable and re-Enable it after the code changes? (I'm working with a lot of them here.)

          Comment


            #6
            Hello,

            Yes, you need to remove and re-add it if you are using the Strategies tab of the Control Center.

            If you were applying it to a chart you could just right-click > Reload NinjaScript.

            However, if you change any code in State.SetDefaults, it is always necessary to remove the old strategy instance and add a new instance in order to load the new defaults.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Today, 05:17 AM
            0 responses
            50 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
            69 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