Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Allowing only one instance of a strategy?

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

    #16
    Hello,

    I will leave this open for the community to assist as this is not supported c# programming. You may have some success looking this up online with various c# websites such as this one.



    Let me know if I can be of further assistance.
    BrettNinjaTrader Product Management

    Comment


      #17
      Can you answer me this:

      I am close to working this out, but I need to understand the difference between Disable() and clicking the enable/disable box in the strategy tab of control center. If I click the box to disable a strategy it seems that the OnTermination() code works, but when I disable the strategy within the code the OnTermination() code does not work.

      Does Disable() terminate it or not? When more than one instance is opened I need to terminate the current one that is trying to start. I thought Disable() would do that but it does not exicute the OnTermination() code.

      Comment


        #18
        dpaulw, this is working as expected here, calling Disable() in your code would not result in OnTermination() being called, this is in contrast to the behavior when clicking Disable in the Strategies tab.

        Comment


          #19
          Now, this is a real problem. If you have no way to terminate the strategy from within the strategy, then OnTermination() does me no good, I have no way to reset my checks on how many instances are open. You should seriously consider offering a method to check how many instances of a named strategy is open. I surely cannot be the only one that does not want one paid user of my strategy using it to trade accounts other than his.

          I have also discovered another thing that adds to my problem in accomplishing this. It seems that calling Disable() within the strategy just suspends the program, not terminating it. It also seems that when clicking the enable box in the strategy tab of control center after it has disabled from within the strategy. The OnStartUp() does not execute. So, you must have the check box enable just unsuspending the program at this point instead of a restart of it. This problem prevents me from working around the previous problem.

          So, now I am definately down to no way of accomplishing what I need. The only suggestion that I see left is the mutex method. I wish someone out there would give me more details about how to fully impliment it. In the mean time, I appeal to you to realize the importance of this for those of us who are making a living promoting your software. We need to protect our proprietary software the same as you do.

          Comment


            #20
            Thanks for your feedback provided - I'll check into your findings with development, however I believe from my understanding the outcomes are expected unfortunately.

            Comment


              #21
              Since it seems NinjaTrader cannot help me, could someone please help me with using Mutex to insure that only one instance of my strategy runs?????

              I have tried the code given below and cannot get it to work. I have also looked at other sample code on forums. I am still having trouble. Sample code that will work within a strategy would be greatly appreciated. I am not sure where to put the code inside of the strategy or even if it will work at all with the Ninja strategy code. Any suggestions would be greatly appreciated!!!!!

              Comment


                #22
                Re - Allowing only one instance of a strategy?

                A couple things have to happen to make this work:

                1) in your variable section define:

                // The Mutex
                static volatile Mutex m_Mutex;

                bool allowedtotrade = true;


                2) in the OnStartUp Method:

                protected override void OnStartUp()
                {

                string mstring = @"Global\" + "your unique identifier " ;

                if (allowedtotrade)
                {
                bool createdNew;
                m_Mutex = new Mutex(true, mstring, out createdNew);
                if (!createdNew)
                {
                MessageBox.Show("An instance of this strategy is already running, please contact technical support to enable more strategies", Application.ProductName,
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                allowedtotrade = false;
                DrawTextFixed("disabled", " System is disabled " , TextPosition.Center, textBrush.Color, textFont, Color.Transparent,Color.Transparent,0);

                return;
                }

                GC.KeepAlive(m_Mutex);
                }
                }


                3) the OnTermination Method:

                protected override void OnTermination()
                {
                if (allowedtotrade)
                {
                m_Mutex.ReleaseMutex();
                m_Mutex.Close();
                m_Mutex = null;
                }
                }


                4) In the OnBarUpdate() -- wrap everything within a check of allowedtotrade

                if ( allowedtotrade )
                {
                // do you stuff - plot -- calculate - etc.

                }

                Comment


                  #23
                  Thank you for the reply. I will put this code in for a test this afternoon.

                  Comment


                    #24
                    I must be missing some declarations. I am getting the following errors with the code added:

                    The name 'Application' does not exist in the current context
                    The name 'MessageBox' does not exist in the current context
                    The name "MessageBoxButtons' does not exist in the current context
                    The name 'MessageBoxIcon' does not exist in the current context
                    The name 'textBrush' does not exist in the current context
                    The nam 'textFont' does not exist in the current context

                    Comment


                      #25
                      Also, on Application.ProductName am I to put the namespace or the strategy name I have given the strategy?

                      Comment


                        #26
                        Took out the message box code and used Ninja's Log() to trigger the box. It is working great! Thanks again

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                        0 responses
                        633 views
                        0 likes
                        Last Post Geovanny Suaza  
                        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                        0 responses
                        364 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by Mindset, 02-09-2026, 11:44 AM
                        0 responses
                        105 views
                        0 likes
                        Last Post Mindset
                        by Mindset
                         
                        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                        0 responses
                        567 views
                        1 like
                        Last Post Geovanny Suaza  
                        Started by RFrosty, 01-28-2026, 06:49 PM
                        0 responses
                        568 views
                        1 like
                        Last Post RFrosty
                        by RFrosty
                         
                        Working...
                        X