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

Handling Edge-case exceptions

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

    Handling Edge-case exceptions

    Hello,
    I have a strategy that I have written that is working well on a simulated account. However, last night, the strategy apparently lost connection, then got two exceptions when the connection came back, but somehow left a position open with no target or stop. I do not know how that would happen since every entry always has a target and a stop. It did disable the strategy, which is ok, but I it lost $7*4*TickSize*NumberContracts, which is very dangerous as you might assume, in the process before I closed it by hand. Below is a bit of my log as to what happened.

    Strategy 'MachTrade/153775916' lost price connection for more than 10 seconds and will be restarted.
    Disabling NinjaScript strategy 'MachTrade/153775916'
    Enabling NinjaScript strategy 'MachTrade/153775916' : On starting a real-time strategy - StartBehavior=WaitUntilFlat EntryHandling=All entries EntriesPerDirection=1 StopTargetHandling=Per entry execution ErrorHandling=Stop strategy, cancel orders, close positions ExitOnSessionClose=True / triggering 30 seconds before close SetOrderQuantityBy=Strategy ConnectionLossHandling=Recalculate DisconnectDelaySeconds=10 CancelEntriesOnStrategyDisable=False CancelExitsOnStrategyDisable=False Calculate=On bar close IsUnmanaged=False MaxRestarts=4 in 5 minutes
    Indicator 'HMAColourChange': Error on calling 'OnBarUpdate' method on bar 158: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
    Strategy 'MachTrade': Error on calling 'OnBarUpdate' method on bar 3937: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
    Disabling NinjaScript strategy 'MachTrade/153775916'

    My question is: How do I ensure that when the strategy is stopped, even in an exception, that it closes all orders? And also, how to handle connection loss like above?

    Thanks for the help,

    Steve

    #2
    Hello stevepmay,

    Thanks for your post.

    The strategy currently has "ErrorHandling=Stop strategy, cancel orders, close positions" which would have closed the open Stop and profit target orders as well as closing the position but likely due to the strategy errors the strategy was terminated before it could transmit the order to close the position.

    To resolve the errors of the indicator and strategy, you would need to debug both using print statements to determine where the code is failing. Here is a link to our debugging tips: https://ninjatrader.com/support/help...script_cod.htm

    Instead of setting the strategy to recalculate, you may want to set your strategy connection loss handling to "Keep running", please see: https://ninjatrader.com/support/help...sshandling.htm



    Paul H.NinjaTrader Customer Service

    Comment


      #3
      OK. I understand that the exceptions caused by not handling the exceptions properly could have caused an order to close, but it seems to me that there should NEVER be a possibility to leave an order open without a stop or target. If one is closed, all three should be closed. I only enter in one place in my code and it looks like this:

      // Set initial targets and stops
      SetProfitTarget(entryType, CalculationMode.Ticks, TargetTicks);
      SetStopLoss(entryType, CalculationMode.Ticks, StopTicks, false);

      // Log and enter
      MyPrint("In MyEnterLong: TargetTicks is: " + TargetTicks.ToString() + " and StopTicks is: " + StopTicks.ToString() + " Type: " + entryDesc + " " + entryType, 0);
      EnterLong(ContractsToTrade, entryType);

      I want to be able to leave a strategy unattended for several hours and not have to check on it every couple of minutes. If there is a chance to leave a position open, then I need a way to check that the strategy thinks there is a position open when I do not think it is open so I can close all. Of course this would have to be a separate process. Am I doing something wrong in the order entry that would prevent all being closed at the same time?

      Thanks,

      Steve

      Comment


        #4
        Also note that I had two different charts running with the same strategy at the same time. If that makes a difference. Steve

        Comment


          #5
          Hello Steve,

          Thanks for your replies.

          Regarding, "Also note that I had two different charts running with the same strategy at the same time. If that makes a difference." Are these two charts of the same instrument? (meaning the two strategies are running on the same instrument? (if so are they using the same account?).
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            No, the one chart is on ES mini and the other on NQ mini but they are on the same account. Does that make a difference?

            Comment


              #7
              Hello Steve,

              Thanks for your reply.

              No, I just wanted to make sure you did not have multiple strategies on the same instrument and the same account as that would be another consideration.

              You will still need to debug your strategy to resolve the error produce here: Strategy 'MachTrade': Error on calling 'OnBarUpdate' method on bar 3937: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.
              Disabling NinjaScript strategy 'MachTrade/153775916'



              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Thank you for your patience as well as the answers so far. I did have an issue in the OnOrderUpdate, which I have found, and I also changed the connection type and have already tested that as well. However, I did ask a question above that was not answered around the way I am entering into trades. It still seems that there should never be a case that a position is left open where the stop and/or the target was cancelled AND the strategy was stopped. This just doesn't seem like it should be possible. Am I doing something wrong? Is there an issue with NT? Can you explain how that can happen and if it does how I can detect and close everything? Thank you before hand.

                Comment


                  #9
                  Hello Steve,

                  Thanks for your reply.

                  "However, I did ask a question above that was not answered around the way I am entering into trades. It still seems that there should never be a case that a position is left open where the stop and/or the target was cancelled AND the strategy was stopped. This just doesn't seem like it should be possible." Regrettably, you found a case where it is possible where the combination of connections loss and an error in the strategy produced that very situation. There are other such possibilities, that it is impossible to account for them all which is why we recommend monitoring your strategy/NinjaTrader and have your broker's trade desk phone number handy in the event of an unexpected operation.

                  ​​​​​​​There are the risks involved with electronic trading: https://ninjatrader.com/support/help...trading_wi.htm
                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    Thanks again for the response. i understand the risk and believe that it was definitely a unique situation because of the fact that the exception was in the OnOrderUpdate which caused the strategy to stop. As a backup watchdog application:
                    1. Can I run two strategies on a chart, one that is running the main strategy and the other just monitoring if there is an open position without the strategy running so that I can close it?
                    2. Do you have an example of a strategy running monitoring open positions and closing them?

                    Thanks again for the support.

                    Steve

                    Comment


                      #11
                      Hello Steve,

                      Thanks for your reply.

                      Strategies by default do not communicate with each other and are unaware of each other's positions.

                      It would be possible to have them communicate with each other using static variables in an add-on. We do not have any examples of this.

                      Another possibility would be to have the strategy monitor Account.OnOrderUpdate events and AccountPositionUpdate events using an Add-on code. Again we do not have examples of this.
                      References:



                      If you would like the strategy coded for you, we can provide references to 3rd party programmers who could provide that service.
                      Paul H.NinjaTrader Customer Service

                      Comment


                        #12
                        Thank you for the options and the answers. I will look into it.

                        Steve

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by terofs, Yesterday, 04:18 PM
                        1 response
                        21 views
                        0 likes
                        Last Post terofs
                        by terofs
                         
                        Started by CommonWhale, Today, 09:55 AM
                        1 response
                        3 views
                        0 likes
                        Last Post NinjaTrader_Erick  
                        Started by Gerik, Today, 09:40 AM
                        2 responses
                        7 views
                        0 likes
                        Last Post Gerik
                        by Gerik
                         
                        Started by RookieTrader, Today, 09:37 AM
                        2 responses
                        13 views
                        0 likes
                        Last Post RookieTrader  
                        Started by alifarahani, Today, 09:40 AM
                        1 response
                        7 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Working...
                        X