Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Prevent strategy from starting...

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

    Prevent strategy from starting...

    Hello,

    I have some strategy properties that are not compatible with other properties and want to prevent the strategy from starting when the "OK" or "Apply" button is selected and show a message window stating the conflict.

    I found "NTMessageBoxSimple.Show" and CloseStrategy.

    The problem is when to perform the property check and call the two functions.

    Every OnStateChange event I tried had problems.
    Calling it on the OnFirstBarUpdate did not show the message box.

    Can you advise?

    Another question, is there a solution to have all the strategy property fields collapsed when the window is opened? I assume a template would work but, was looking for a configuration option.

    Finally, why is State == State.Configure called in some instances, multiple times?
    Help has "This state is called only once for the life of the object.​"
    I put a print in the event and the event can be called 1 - 4 times when the "OK" or "Apply" button is selected

    Cheers,

    Mark

    Last edited by Mark--; 03-13-2023, 06:55 AM.

    #2
    Hello Mark,

    One choice would be to use Draw.TextFixed() in State.DataLoaded to display a message on the chart and Log() if you want to send a message to the Log tab of the Control Center, and return in OnBarUpdate().

    Or you could call CloseStrategy() in State.DataLoaded if you wanted.

    There are multiple instances of script classes that are created for populating menus. When enabling / disabling a script is cloned.

    See the post below and help guide about the lifecyle of a script.



    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thanks for the response.

      OK, I tried that.
      Selecting the "Apply" button.
      The texted is displayed = good.
      CloseStrategy does not close the strategy.

      From the help file for CloseStrategy Warning: This method can only be call before the State has reached State.Terminated and after the State reaches State.Realtime

      So I moved the check to Realtime, the check is made, CloseStrategy is called and the strategy is closed/disabled but of course the text does not show and NTMessageBoxSimple.Show does not display a message window.

      Perhaps another suggestion?

      And "Another question, is there a solution to have all the strategy property fields collapsed when the window is opened? I assume a template would work but, was looking for a configuration option."

      Comment


        #4
        Hello Mark--,

        You are correct CloseStrategy() does have to be called after the script reaches realtime.

        If you disable the strategy no text is going to display and any message box would be closed. This is why I have suggested you return in OnBarUpdate() instead of calling CloseStrategy(). However, this is up to you.

        The CategoryDefaultExpanded(false) is not documented but may provide the functionality of collapsing a categeory.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks for the response.

          >This is why I have suggested you return in OnBarUpdate() instead of calling CloseStrategy().

          Do you mean put a check in OnBarUpdate to return/exit for a bad configuration? A call that is made every bar update while the strategy is running (good configuration).

          Comment


            #6
            Hello Mark--,

            Right, if you want some information to persist on the chart, like a drawing object, or a message box, the script has to keep running. If you return in OnBarUpdate(), no logic is evaluated and the resource use is minimal.

            Calling Log() would persist in the log after the script is disabled.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Thanks for the response.

              Do you mean a check each time OnBarUpdate is called for a bad configuration or a flag that the configuration is bad, and return/exit from OnBarUpdate?

              Essentially a needless check each time the configuration is valid. Not as efficient as I would hope.

              If so, is there a method to point the caller at a different OnBarUpdate? One for good configuration and one for bad configuration, a pointer that can be set to either method,

              I read a bunch of post about "Gui.CategoryExpanded", interesting it has been talked about since 2010 and is not supported.

              Last edited by Mark--; 03-13-2023, 10:40 AM.

              Comment


                #8
                Hello Mark--,

                Yes, the condition would check the configuration and return in OnBarUpdate().

                "If so, is there a method to point the caller at a different OnBarUpdate?"

                I am not understanding what you are asking here. C# does not use pointers to memory addresses in that manner.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_ChelseaB View Post
                  C# does not use pointers to memory addresses in that manner.
                  Thanks for the response.

                  C# is not my primary language. But, it does provide for an assignment of a class method to a variable/property.

                  Timers are an example

                  cancelTimer.Tick += CancelTimerEventProcessor;

                  Does the parent of class "Strategy" have a property that can be assigned, like "timer.tick", for OnBarUpdate?

                  Comment


                    #10
                    Hello Mark--,

                    No, you do not have access to the event handler that runs OnBarUpdate(). This is controlled by NinjaTrader behind the scenes and updates according to processing data.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Thanks for the response.

                      Comment


                        #12
                        Mark-- If I understand you correctly, you want to evaluate a set of conditions once only at the start of a Strategy to determine if the Strategy should be allowed to continue or should be terminated. Is that correct? And if so, you want that to be as resource efficient as possible.

                        Assuming that is the situation, you could use booleans to "gate" these actions. That would be very efficient, even when called on every bar update.

                        For example, something like this pseudo-code might be appropriate:
                        Code:
                        ... bool MyValidationMethod()
                        { // Validation }
                        
                        bool IsValidated = false;
                        bool IsValid = false;
                        
                        ... OnBarUpdate()
                        {
                          if (IsValid)
                          {
                            // All the Strategy actions when the conditions are valid
                          }
                          else
                          if (!IsValidated)
                          {
                            IsValid = MyValidationMethod();
                            IsValidated = true;
                        
                        ​    if (!IsValid)
                              CloseStrategy();
                          }
                        }
                        This would do the validation on the first update and close the Strategy if not valid. If valid, from that point onwards, it's a single boolean test as a trivial overhead.

                        If you want to validate more than once only on the first update, some adjustments would be required.

                        Hope that is useful.

                        Thanks.
                        Multi-Dimensional Managed Trading
                        jeronymite
                        NinjaTrader Ecosystem Vendor - Mizpah Software

                        Comment


                          #13
                          Thanks for the response jeronymite.​

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by NullPointStrategies, Yesterday, 05:17 AM
                          0 responses
                          72 views
                          0 likes
                          Last Post NullPointStrategies  
                          Started by argusthome, 03-08-2026, 10:06 AM
                          0 responses
                          143 views
                          0 likes
                          Last Post argusthome  
                          Started by NabilKhattabi, 03-06-2026, 11:18 AM
                          0 responses
                          76 views
                          0 likes
                          Last Post NabilKhattabi  
                          Started by Deep42, 03-06-2026, 12:28 AM
                          0 responses
                          47 views
                          0 likes
                          Last Post Deep42
                          by Deep42
                           
                          Started by TheRealMorford, 03-05-2026, 06:15 PM
                          0 responses
                          51 views
                          0 likes
                          Last Post TheRealMorford  
                          Working...
                          X