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

Strategy adjusting stop loss using SetStopLoss is causing chart to freeze

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

    Strategy adjusting stop loss using SetStopLoss is causing chart to freeze

    Hi there,

    I've been having an issue where I am unable to utilize a strategy I've developed because my chart freezes - this specifically happens after my strategy has taken profit and is attempting to adjust the stop loss with each increase in profit (per tick.) The strategy is running OnTickUpdate.

    I originally sent this in as a support request ticket but they're not receiving my replies for some reason, so I'd like to continue the conversation here.
    After my initial support request, I received the following response:

    2024-04-11 11:42:23:716 (TopStep) Rithmic.Adapter.OnPositionUpdate S1Mar193771623 TopstepTrader TopstepTrader CME NQM4 1 18263.25 1712850060 142000
    2024-04-11 11:42:23:716 ERROR: Received position for unkown symbol 'CMENQM4', account 'S1Mar193771623'​

    The error is due to an improper instrument update received from a Rithmic based connection.
    I updated NinjaTrader at that time as requested and updated all of the instruments themselves, but the issue persisted.
    Initially I was receiving an error message whenever the chart would freeze ("Received position for unkown symbol 'CMENQM4', account 'S1Mar193771623"), but after the update the error message no longer appeared. However, the freezing continued in the exact same spot.

    "After the update, the error messages did not appear but the chart did freeze at the same place where the error messages would've occurred - when my trade hits its profit target and the strategy attempts to adjust the stop accordingly. I can see that the chart is still loaded because my PnL statement will change with the price movement, but the chart itself is frozen. If I attempt to manually close the trade when this happens, the entire program freezes and becomes unresponsive. If I force-close NT when this issue occurs and restart the program altogether, my trade will re-appear as still active and at that point I am able to close the trade manually without freezing. Restarting the strategy, then letting it take another entry will result in the same issue occurring again."

    So to confirm, the issue is as follows:
    1) Enable strategy, which enters a trade when specific conditions are met
    2) Strategy hits breakeven and moves the stop loss once (SetStopLoss), no issue
    3) Strategy hits profit target and starts adjusting the stop with the price movement (SetStopLoss)
    4) Chart freezes - strategy stays enabled and the PnL marker for my trade continues to change despite the chart itself being stuck.
    5) Attempting to close the trade manually, or disable the strategy, will result in NinjaTrader freezing altogether.
    6) Restarting NT resolves the frozen chart and allows me to close the trade manually, but the strategy is disabled at this point so I am unable to keep using it.

    This happens on both my Sim101 account and my TopStep live accounts, utilizing the Rithmic R|Trader Pro data connection.


    The script / my strategy is being run OnEachTick as it needs to adjust the stop loss with no latency. Tick Replay is not enabled. This problem only appears in the live environment; running at Max Speed on Playback works perfectly with no lag or latency issues. AddDataSeries() is not called at all. The stop loss updates are being called as part of OnBarUpdate. Here is the relevant code: https://pastebin.com/3vC951TV



    #2
    After sending all of the above info to the support tech, they asked the following: "With this logic that is calling SetStopLoss(), is this being called several times on each tick update? This might be causing the issue. I see you have prints added above the SetStopLoss() calls. Do you see these prints appearing right after the order is submitted multiple times?​"

    *****
    I only ever see the initial printout (SetStopLoss used to adjust stop after hitting profit target) once because I use a boolean to limit it - that printout reads "Profit target achieved, initiating trailing stop loss"
    The method that subsequently updates the trailing stop loss is being called on each OnBarUpdate and triggered whenever my unrealized PnL is above a certain threshold (1 tick of profit - changing this to 4 ticks/1 point didn't seem to help.)

    I've confirmed that I only see this printout appear appear once before the freeze happens. In the log, I see:
    1. a single Stop Loss entry with Change Submitted and Accepted for my initial stop loss (when the order is initiated)
    2. Change Submitted with my breakeven price (no Accepted message for that change in the log)
    3. the next print is Accepted for the profit trail adjustment (no Change Submitted for that change in the log)

    After this final accepted print is where the price movement on the chart freezes on me - I tried reloading historical data while frozen to see if that helped, I saw "Loading" but it was stuck. Hitting Close manually then froze the entire program like it had before, until I force-closed. After relaunching NT, I see that the trade is still open. The first print out in the log after all of the boot up messages is "Cancel submitted" + "Cancelled" with the previous stop loss price that was listed as Accepted.

    Essentially on my end, I am only seeing one print out in both the log and in my strategy output per call of SetStopLoss. However, the previous tech did state that they were seeing many repeat calls of SetStopLoss appearing on their end that is not reflected in the log I am able to see in Control Center.

    Comment


      #3
      Hello lorem,

      What is the Calculate setting set to in the Strategy parameters?

      This is occurring in real-time, is this correct?

      Are you calling SetStopLoss() in OnStateChange() in State.Configure or State.DataLoaded?

      Or are you calling SetStopLoss() in OnBarUpdate()?
      I'm guessing this in OnBarUpdate() as you are also modifying the price of the order by calling the Set method again, correct?
      Do you have logic to ensure that this only gets called once?



      Chelsea B.NinjaTrader Customer Service

      Comment


        #4
        Calculate is set to OnEachTick, and yes it's occurring in real-time. I am calling SetStopLoss() through a separate, longer method that is being called via OnBarUpdate(). I do have the logic in place to only call SetStopLoss() once, but it is once for every tick increase in profit as opposed to once per bar. If the price is moving quickly, it can call SetStopLoss() multiple times in a row to adjust the price accordingly.

        That being said, it works fine in Playback at max speed - the issue only appears in real-time.

        Comment


          #5
          After some more testing today, I noticed that the issue occurs any time the stop is adjusted even once - I just had a trade hit breakeven and immediately after the stop loss was adjusted (a single time, confirmed by the Control Center log and my trade output) the chart froze.

          The BuySTP icon on the chart turned orange and the chart froze. I am able to move my "drawings" like Lines and Rays on the chart, but I cannot do anything else - no orders can be placed and the price action cannot be seen. My PnL for the active order keeps changing, but nothing else does. Closing the order manually freezes NinjaTrader altogether.

          Attempting to close NinjaTrader doesn't do anything when this happens. I have to force-close the program from Task Manager.

          Update: I tried testing with Chart Trader disabled because I found some previous threads with a similar issue that used that as a workaround - unfortunately that does not resolve the issue on my end and the chart still freezes. Enabling the strategy from Control Center instead of right-clicking on the chart doesn't fix it either.
          Last edited by lorem; 04-26-2024, 12:31 PM.

          Comment


            #6
            Hello lorem,

            "I am calling SetStopLoss() through a separate, longer method that is being called via OnBarUpdate()."

            From the log file provided this does not seem to be the case. I am seeing a great many price change attempts for the stop loss. It looks like the script is rapidly attempting to call SetStopLoss() for every tick that updates OnBarUpdate().

            To confirm, you are using a bool that is set to true once the Set method is called and and is required to be false. Then on a new bar (or new entry order) this bool is set back to false to allow this set method to be called again? (This would be preventing the Set method from being called on every tick and only called once)

            What code are you using to ensure this method is only called once?

            May I also confirm you are checking the order price is a valid price for the set method? If you are using CalculationMode.Price, is the price for a sell stop order below the bid and the price for a buy stop order above the ask?


            Try closing all open workspaces and open a new blank chart in a new blank workspace and test this example 'ProfitChaseStopTrailSetMethodsExample' linked below which modifies the price of a SetStopLoss().


            With this example strategy are you seeing the same behavior?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I think I may have found the issue - At some point, I mistakenly changed my profit increase calculation from > to >=, so it was trying to adjust the stop even if the price hadn't changed. Woops! After adjusting that, it seems to be working fine on Sim101. I can't test using my Topstep account since it's after 4:10pm EST, but I'll test again using my TS practice account on Sunday night.

              if (currentProfit > highestProfitSinceEntry)
              {
              highestProfitSinceEntry = currentProfit;
              profitChanged = true;
              }​

              This part had been changed to currentProfit >= highestProfitSinceEntry, which was not my intention. Not quite sure how I managed to do that without realizing it!

              ****

              To answer your questions, I am using this profitChanged bool to determine when the SetStopLoss order is called. It is set to false at the beginning of each OnBarUpdate(), and should only be true when profit has increased by at least one tick (which explains my confusion, since my error above meant it was reading as true on every tick even if the price hadn't moved.)

              The weird thing is that I wasn't seeing the price change attempts on my end - in my output and in the log, I was only seeing a single SetStopLoss() call before it would freeze. I would expect to see the repeated calls to SetStopLoss in my Control Center log at the very least, so I was under the impression this was a bug.

              "May I also confirm you are checking the order price is a valid price for the set method?​"
              Yes, I am checking that the stop loss adjustment is below the current price (for longs) and vice versa before submitting it. In instances where the price moves very quickly (such as a news event,) I do get an error pop up saying the stop loss was rejected - when this happens, I have code in place to quickly close the order without disabling the overall strategy.
              ****

              Anyway, I will be able to confirm for sure that the issue is resolved on Sunday night or possibly Monday morning. I will update this thread at that time!

              Comment


                #8
                Unfortunately, the issue has persisted. I am attaching my updated log and trace folders from today's test. If this were an issue with the strategy updating too many times on my end because of my code, I would expect the issue to persist (and even become worse) when testing via Playback at max speed.

                Below, you can see my log in Control Center. Please note that there is only one Stop Loss accepted entry and one Stop Loss change submitted entry, the freeze happened at that time (chart frozen, PnL still updating but unable to close order or see price action on the chart.) I have to force-close NinjaTrader at this point and then close the order afterwards.

                Click image for larger version

Name:	image.png
Views:	42
Size:	458.7 KB
ID:	1301391

                Opening my strategy in Playback for testing, I can see that it is not calling SetStopLoss multiple times per individual tick:
                Click image for larger version

Name:	image.png
Views:	34
Size:	159.2 KB
ID:	1301392
                As you can see here, even in cases where the stop loss is adjusted multiple times it never manages to call the method twice in the same tick - this is at max playback speed with a print statement inside the method that calls SetStopLoss().

                Comment


                  #9
                  This is what it looks like when it freezes:
                  Click image for larger version  Name:	image.png Views:	0 Size:	18.7 KB ID:	1301394
                  Click image for larger version  Name:	image.png Views:	0 Size:	19.1 KB ID:	1301395

                  As you can see, the price is "moving" but the chart is not updating. The stop will turn orange as well. If I close this order manually, all of NinjaTrader freezes.

                  This is all I'm seeing after it freezes - it updates the stop loss twice on two separate ticks, then no longer adjusts the stop loss or prints output.
                  Click image for larger version  Name:	image.png Views:	0 Size:	110.9 KB ID:	1301396

                  Comment


                    #10
                    Hello lorem,

                    Try closing all open workspaces and open a new blank chart in a new blank workspace and test this example 'ProfitChaseStopTrailSetMethodsExample' linked below which modifies the price of a SetStopLoss().


                    With this example strategy are you seeing the same behavior?​
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      I think I'm using this one incorrectly as it just gets stopped out immediately - I am trying to figure out how to properly set up this strategy to run, I'll let you know once I do

                      Comment


                        #12
                        Okay, it doesn't seem to be having the same issue. I was able to watch it take an entry and adjust the stop multiple times. The stop flashes orange, then adjusts accordingly and goes back to pink each time.

                        When I was using my strategy, I would watch it turn orange the first time and then freeze in that state (chart not moving, stop loss orange instead of pink.)

                        [redacted]

                        Comment


                          #13
                          Tested with my strategy again, same issue. I updated my stop loss logic to only adjust the stop if we have an extra point in profit ($20) but the issue persists. It is freezing after only adjusting the stop a single time. On my end, I still only see a single stop loss accepted and a single stop loss change submitted entry, then nothing else at all. I don't understand why I am not getting an error or seeing the repeated stop loss attempts in the log if that is the cause I realize it seems like my strategy is the cause since my stop was adjusted without issue by the other strategy, but I don't understand why none of this is reflected in my Control Center log or in my strategy output. As far as I can tell, it is freezing after submitting only a single stop loss and then getting caught in a loop on the backend somewhere. It being live seems to have something to do with it as well, given that the issue does not appear in Playback testing at all.

                          5.13 log ColorGameBasic: https://drive.google.com/file/d/1S6S...ew?usp=sharing
                          5.13 trace ColorGameBasic: https://drive.google.com/file/d/183_...ew?usp=sharing

                          Here is the strategy as well, in case I'm missing something on my end or you want to test it on your end:

                          Comment


                            #14
                            Hello lorem,

                            In the log you have provided order 183957821 is modified 29 times in the span of a minute, and 5 times in the last second.

                            However, this order is cancelled by clicking the Chart Trader Close button.

                            What is the order in question?


                            If the example I have provided is not able to reproduce, if you use this as a starting point and add your entries, and adjust this to your code, are you able to avoid the behavior?
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Very strange that this is what you see, I don't see that reflected on my end in any way whatsoever beyond the initial Freeze.

                              The order that was modified 29 times in a minute was a stop loss order that had hit profit. It should only be adjusting once per each point in profit, what you are seeing is happening on the very first stop loss adjustment after hitting profit.

                              As far as I can tell, the stop loss adjustment is being submitted once but it is entering a loop on the backend somehow.

                              *****

                              Yes, using the example strategy you gave me and implementing my current stop loss logic seems to work fine - no freezing. In the log, I can see an entry each time the stop is adjusted like I normally should. Even when I set it to update the price on each tick of profit instead of each point, it does not freeze. I am using the EXACT same stop loss adjustment logic, so it should absolutely still be causing issues if the stop loss logic is the problem.

                              Here is the original stop loss adjustment logic:
                              Code:
                                              bool profitChanged = false;
                                              double currentProfit = 0;
                                              if(currentEntry.IsLong) currentProfit = Close[0] - currentEntry.AverageFillPrice;   
                                                        
                                              if (currentProfit > highestProfitSinceEntry)
                                              {
                                                  highestProfitSinceEntry = currentProfit;
                                                  profitChanged = true;
                                              }​
                              ...
                                              if (cEntryTrailingStopSize > 0 && profitChanged)
                                                  {
                                                      if (currentEntry.IsLong && Close[0] - cEntryTrailingStopSize > currentStopPrice)
                                                      {
                                                          currentStopPrice = Close[0] - cEntryTrailingStopSize;
                                                          if(currentStopPrice < Close[0]) SetStopLoss(currentEntry.Name, CalculationMode.Price, currentStopPrice, false);
                                                          Print("The tick count of the current bar is " + Bars.TickCount.ToString());
                                                      }
                                                  }​
                              *****************
                              Here is the modified stop loss code I used with the template you provided:
                              Code:
                                          if(Position.MarketPosition == MarketPosition.Long) currentProfit = Close[0] - currentEntry.AverageFillPrice;    
                                          
                                          if (currentProfit > highestProfitSinceEntry)
                                              {
                                                  highestProfitSinceEntry = currentProfit;
                                                  profitChanged = true;
                                                  Print("Profit changed. " + currentProfit.ToString("C"));
                                              }​
                              
                              
                                          if (BarsInProgress == 1 && Position.MarketPosition == MarketPosition.Long)
                                          {    
                                        
                                              if (UseStopLoss && TrailStopLoss && profitChanged && Close[0] - StopLossDistance > currentSlPrice)
                                              {
                                                  currentSlPrice = Close[0] - StopLossDistance;
                                                  SetStopLoss(CalculationMode.Price, currentSlPrice);
                                              }
                                          }    ​
                              Note - I'd like to re-mention that when I use my strategy, my printout (""The tick count of the current bar is...") is NOT printing multiple times per tick on my end. It prints every few ticks reflecting when the profit changes, so it should only be submitting stop losses at those times. Clearly on your end, it is reflecting significantly more orders than my strategy is actually attempting to place.. for some reason.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by ETFVoyageur, Today, 02:15 AM
                              5 responses
                              29 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by drnoggin, Today, 12:19 PM
                              1 response
                              14 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by synthhokie, Today, 12:00 PM
                              1 response
                              16 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by moneyexe, Today, 11:22 AM
                              1 response
                              12 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by stevec1824, Today, 11:00 AM
                              2 responses
                              8 views
                              0 likes
                              Last Post stevec1824  
                              Working...
                              X