Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy States - changing

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

    #16
    Originally posted by kujista View Post
    Hello,

    I need to detect if strategy is ran "3rd times" - i am refereing to lifecycle - where strategy is cloned...

    here:

    In part State.configure I need to calll external API and i need to run it only once when enabling strategy .is there a way I am running cloned strategy?
    You ask "is there a way I am running cloned strategy?" - what are the results when you test it out? The UI instances of the strategy do not reach State.Configure; that state is only reached once the user clicks "Apply" or "OK" on the strategy window to configure the strategy. More details about states and state management are described in the best practices here, which is a link I did not share previously:


    Please let me know if I may be of further assistance.

    Comment


      #17
      Can you please take a look at short video to explain, what I mean:


      thanks Pavel

      Comment


        #18
        Originally posted by kujista View Post
        Hello Pavel,

        Thank you for the video.

        Does State.DataLoaded work for your needs? This should only be called once and only when the strategy is enabled; State.Configure could be called when the user clicks Apply or OK without the strategy being enabled. Please test out State.DataLoaded and feel free to reach out with any additional questions or concerns.

        Thank you for your time and patience.

        Comment


          #19
          The Problem is, that I also need to get from strategy data series which will be used.. and i have to do it in cofigure state which is run 3 times...

          Comment


            #20
            Originally posted by kujista View Post
            The Problem is, that I also need to get from strategy data series which will be used.. and i have to do it in cofigure state which is run 3 times...
            Hello Pavel,

            Thank you for your reply.

            You can get the data series which will be used from State.DataLoaded as well. It is not exclusive to State.Configure. Are there any other reasons you require State.Configure in particular? If not, please test it in State.DataLoaded and monitor the results to see if they behave as expected or not.

            Please let me know if I may be of further assistance.

            Comment


              #21
              I am affraid, you don't understand what I mean.

              I will add dataseries dynamically after connectin to database server using this code:
              Click image for larger version

Name:	image.png
Views:	39
Size:	143.4 KB
ID:	1298078​I cannot use AddDataSeries when strategy in DataLoaded state...Or can I add Data Series?
              See:
              Click image for larger version

Name:	image.png
Views:	36
Size:	369.4 KB
ID:	1298079

              Comment


                #22
                Originally posted by kujista View Post
                I am affraid, you don't understand what I mean.

                I will add dataseries dynamically after connectin to database server using this code:
                Click image for larger version  Name:	image.png Views:	0 Size:	143.4 KB ID:	1298078​I cannot use AddDataSeries when strategy in DataLoaded state...Or can I add Data Series?
                See:
                Click image for larger version  Name:	image.png Views:	0 Size:	369.4 KB ID:	1298079
                Adding a data series dynamically is not supported. Our support team will not be able to assist you with what you are trying to achieve. You can test it yourself, though you must understand that since it is unsupported results may very and you should only do so at your own risk. More information is noted in the help guide here:
                • https://ninjatrader.com/support/help...dataseries.htm
                  • "Arguments supplied to AddDataSeries() should be hardcoded and NOT dependent on run-time variables which cannot be reliably obtained during State.Configure (e.g., Instrument, Bars, or user input). Attempting to add a data series dynamically is NOT guaranteed and therefore should be avoided. Trying to load bars dynamically may result in an error similar to: Unable to load bars series. Your NinjaScript may be trying to use an additional data series dynamically in an unsupported manner.​"
                This thread is open to the forum community if anyone would like to chime in with their input or experience with what you are looking to achieve.

                Thank you for using NinjaTrader.

                Comment


                  #23
                  OMG... This was not about adding dataseries... this all is about being able not to run 3 times code with green comment?? can you forget the part about adding dataseries? and could you tell me if I am able to detect if part with green comment is run for first time or second time?

                  I can do it by geting data from db, and see if there are 2 previous attempts but that is pretty stupid. I am looking for cleaner solution.

                  Or is this something you also are not allowed to help with?

                  Comment


                    #24
                    Originally posted by kujista View Post
                    OMG... This was not about adding dataseries... this all is about being able not to run 3 times code with green comment?? can you forget the part about adding dataseries? and could you tell me if I am able to detect if part with green comment is run for first time or second time?

                    I can do it by geting data from db, and see if there are 2 previous attempts but that is pretty stupid. I am looking for cleaner solution.

                    Or is this something you also are not allowed to help with?
                    I am not aware of a way to detect if the if (IsEnabled) condition in State.Configure has been hit one, two, or three times. Even if you add an int variable and increment the int when it is State.Configure, it gets reset:
                    Code:
                        public class SampeMAPrints : Strategy
                        {
                            private SMA smaFast;
                            private SMA smaSlow;
                            private int configureCount = 0;
                    
                            protected override void OnStateChange()
                            {
                                if (State == State.SetDefaults)
                                {
                                    Description    = NinjaTrader.Custom.Resource.NinjaScriptStrategyDescriptionSampleMACrossOver;
                                    Name        = "SampeMAPrints";
                                    Fast        = 10;
                                    Slow        = 25;
                                    // This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations
                                    // See the Help Guide for additional information
                                    IsInstantiatedOnEachOptimizationIteration = false;
                                }
                                else if (State == State.Configure)
                                {
                                    Print("SampleMAPrints State.Configure");
                                    configureCount++;
                                    if (IsEnabled)
                                    {
                                        Print("Strategy is enabled. configureCount: " + configureCount);
                                    }    
                                }​
                    If you are able to find a workaround with your database, then that is likely the route you will need to take. I am not aware of any supported/documented solutions that would help you in this scenario.

                    I sincerely appreciate your time and patience.

                    Comment


                      #25
                      It's clear that configureCount cannot work, because there are created 3 encapsuled instances... I thought there might be some property IsCloned - which tells me it is not "the first" strategy but the third one.

                      There is no one in developers team, you could ask or consult with? Someone who has more experiences with this?

                      I really don't the way using database to this..

                      Comment


                        #26
                        Originally posted by kujista View Post
                        It's clear that configureCount cannot work, because there are created 3 encapsuled instances... I thought there might be some property IsCloned - which tells me it is not "the first" strategy but the third one.

                        There is no one in developers team, you could ask or consult with? Someone who has more experiences with this?

                        I really don't the way using database to this..
                        As I mentioned, there is no supported or documented NinjaScript method or property that does this. If you'd like, I could submit a feature request to the development team on your behalf and provide you with a tracking number to see if/when the feature is implemented in a future version of NinjaTrader. Otherwise, you will need to use a different approach to achieve what you are looking for. Please let me know if a feature request interests you and I will gladly submit it.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by NullPointStrategies, Yesterday, 05:17 AM
                        0 responses
                        59 views
                        0 likes
                        Last Post NullPointStrategies  
                        Started by argusthome, 03-08-2026, 10:06 AM
                        0 responses
                        134 views
                        0 likes
                        Last Post argusthome  
                        Started by NabilKhattabi, 03-06-2026, 11:18 AM
                        0 responses
                        74 views
                        0 likes
                        Last Post NabilKhattabi  
                        Started by Deep42, 03-06-2026, 12:28 AM
                        0 responses
                        45 views
                        0 likes
                        Last Post Deep42
                        by Deep42
                         
                        Started by TheRealMorford, 03-05-2026, 06:15 PM
                        0 responses
                        50 views
                        0 likes
                        Last Post TheRealMorford  
                        Working...
                        X