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 States - changing

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

    Strategy States - changing

    Hello,

    I have simple strategy which I am trying to properly initialize but when I try. I have so many state changes, and I am not sure, If I am missing something.

    1. When I click right button and strategies I get these changes:
    2024-03-27 14:59:43.039:StrategyName SetDefaults - Default values are set (pushed to UI) IsEnabled:False
    2024-03-27 14:59:43.039:StrategyName Terminated - Begins to shut down, IsEnabled:False
    2024-03-27 14:59:43.064:StrategyName SetDefaults - Default values are set (pushed to UI) IsEnabled:False

    2. when I choose strategy i get
    2024-03-27 14:59:43.039:StrategyName SetDefaults - Default values are set (pushed to UI) IsEnabled:False - which I think is ok.

    3. but when I click enabled and apply i get this:
    2024-03-27 15:02:49.452:StrategyName SetDefaults - Default values are set (pushed to UI) IsEnabled:False
    2024-03-27 15:02:49.453:StrategyName Terminated - Begins to shut down, IsEnabled:False
    2024-03-27 15:02:49.456:StrategyName SetDefaults - Default values are set (pushed to UI) IsEnabled:False
    2024-03-27 15:02:49.457:StrategyName SetDefaults - Default values are set (pushed to UI) IsEnabled:False
    2024-03-27 15:02:49.458:StrategyName Configure - User the presses the OK or Apply button IsEnabled:True
    2024-03-27 15:02:49.458:StrategyName AddDataSeries
    2024-03-27 15:02:49.461:StrategyName Configure - User the presses the OK or Apply button IsEnabled:True
    2024-03-27 15:02:49.461:StrategyName AddDataSeries
    2024-03-27 15:02:49.462:StrategyName SetDefaults - Default values are set (pushed to UI) IsEnabled:Fals
    2024-03-27 15:02:49.462:StrategyName Terminated - Begins to shut down, IsEnabled:False
    2024-03-27 15:02:49.463:StrategyName Configure - User the presses the OK or Apply button IsEnabled:True
    2024-03-27 15:02:49.463:StrategyName AddDataSeries
    2024-03-27 15:02:49.472:StrategyName Terminated - Begins to shut down, IsEnabled:False
    2024-03-27 15:02:49.473:StrategyName DataLoaded - All data series have been loaded IsEnabled:True
    2024-03-27 15:02:49.476:StrategyName Historical - Begins to process historical data IsEnabled:True
    2024-03-27 15:02:50.983:StrategyName Transition - Finished processing historical data IsEnabled:True
    2024-03-27 15:02:50.983:StrategyName Finished processing historical data

    Which I consider confusing. When reaching Configure state i would like to do some configuration stuff - reading from API etc. but i think this would make too many requests.

    Thanks for claryfication

    Pavel
    Attached Files

    #2
    Hello Pavel,

    Thank you for your post.

    We have some information about the different states and the NinjaScript Lifecycle at the following links:Do you happen to have more than one instance of the strategy on the list of strategies that you are changing and clicking OK/Apply on? You could potentially use State.DataLoaded for the configuration and API stuff you would like to do. Rather than using DebugMessage(), what are the results if you add a simple print statement within OnStateChange() that will print every time the method is called?

    Code:
    protected override void OnStateChange()
    {​
    Print("OnStateChange state: " + State);
    // the rest of OnStateChange() logic below
    if (State == State.SetDefaults)
    {
    }
    else if (State == State.Configure)
    {​
    }
    }
    Please let us know if we may be of further assistance.
    Emily C.NinjaTrader Customer Service

    Comment


      #3
      thank you, i guess i get.

      if i want to have strategy property which is list, and values are read from api it should be handled in setdefaults or is there another approach?

      Comment


        #4
        Originally posted by kujista View Post
        thank you, i guess i get.

        if i want to have strategy property which is list, and values are read from api it should be handled in setdefaults or is there another approach?
        Yes, I suggest using State.SetDefaults for that purpose and testing if it suits your needs. You would be able to read the values of properties that are set and also access data related objects like Bars, Instruments, BarsPeriod, TradingHours or instantiating indicators at that point.

        Please let us know if we may be of further assistance.
        Emily C.NinjaTrader Customer Service

        Comment


          #5
          Last thing, please :-)

          Could you please guide me if i get from api in setdefaults JSON
          [{ id: 'strategy_a', label: 'Strategy A'}, { id: 'strategy_b', label: 'Strategy B'}] - how to create such property, which is list with label and if i select item, i get value id inside strategy.

          Thanx a lot

          Comment


            #6
            Originally posted by kujista View Post
            Last thing, please :-)

            Could you please guide me if i get from api in setdefaults JSON
            [{ id: 'strategy_a', label: 'Strategy A'}, { id: 'strategy_b', label: 'Strategy B'}] - how to create such property, which is list with label and if i select item, i get value id inside strategy.

            Thanx a lot
            Support for the API is limited. Please see the following links for more details on using the API:



            Please don't hesitate to reach out with any additional NinjaTrader items we may assist you with.
            Emily C.NinjaTrader Customer Service

            Comment


              #7
              It's not about nt8 api - it's about getting data from my custom CRUD API.... The question was not about API, but about creating property as i mentioned...
              List / Select with 2 items given by JSON... getting and parsing data from my endpoint is not the issue.

              Comment


                #8
                Originally posted by kujista View Post
                It's not about nt8 api - it's about getting data from my custom CRUD API.... The question was not about API, but about creating property as i mentioned...
                List / Select with 2 items given by JSON... getting and parsing data from my endpoint is not the issue.
                Since your question is focused on creating JSON properties, it goes beyond the scope of supported offered by NinjaTrader. We would be able to assist with any questions you have about NinjaScript-specific methods and properties. Unfortunately, in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services or one-on-one educational support in our NinjaScript Support department. This is so that we can maintain a high level of service for all of our clients as well as our associates.

                That said, through email or on the forum we are happy to answer any questions you may have about NinjaScript if you decide to code this yourself. We are also happy to assist with finding resources in our help guide as well as simple examples, and we are happy to assist with guiding you through the debugging process to assist you with understanding unexpected behavior.

                You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our NinjaTrader Ecosystem team to follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request or provide one-on-one educational services.

                Thank you for your patience and understanding.​
                Emily C.NinjaTrader Customer Service

                Comment


                  #9
                  Can you look at attachment. There is nothing about creating JSON properties.I just want to create List of values of variable allAvailableStrategies and be able to access which value is selected id State Configured. But I do not know how to properly set values... it's about lines

                  Code:
                  [XmlIgnore]
                  
                  [Display(Name = "Strategy")]
                  
                  [PropertyEditor("NinjaTrader.Gui.Tools.StringStandardValuesEditorKey")]
                  
                  public Collection<StrategyObject> MyListValues
                  
                  {
                  
                  get { return myListValues; }
                  
                  set { myListValues = new Collection<StrategyObject>(value.ToList()); }
                  
                  }
                  
                  
                  
                  
                  [Browsable(false)]
                  
                  public Collection<StrategyObject> MyListValuesSerialize
                  
                  {
                  
                  get
                  
                  {
                  
                  //Remove actual defaults
                  
                  //foreach (string pw in collectionDefaults.ToList())
                  
                  //{
                  
                  //PercentWrapper temp = MyListValues.FirstOrDefault(p => p.PercentageValue == pw.PercentageValue && p.IsDefault == true);
                  
                  //if (temp != null)
                  
                  //collectionDefaults.Remove(temp);
                  
                  //}
                  
                  
                  
                  
                  //Force user added values to not be defaults
                  
                  //MyListValues.All(p => p.IsDefault = false);
                  
                  
                  
                  
                  return MyListValues;
                  
                  }
                  
                  set
                  
                  {
                  
                  MyListValues = value;
                  
                  }
                  
                  }
                  ​
                  Could you please help me out or point me to some docs? I took this from example, but cannot make it working for my case.
                  Attached Files

                  Comment


                    #10
                    Originally posted by kujista View Post
                    Can you look at attachment. There is nothing about creating JSON properties.I just want to create List of values of variable allAvailableStrategies and be able to access which value is selected id State Configured. But I do not know how to properly set values... it's about lines

                    Code:
                    [XmlIgnore]
                    
                    [Display(Name = "Strategy")]
                    
                    [PropertyEditor("NinjaTrader.Gui.Tools.StringStandardValuesEditorKey")]
                    
                    public Collection<StrategyObject> MyListValues
                    
                    {
                    
                    get { return myListValues; }
                    
                    set { myListValues = new Collection<StrategyObject>(value.ToList()); }
                    
                    }
                    
                    
                    
                    
                    [Browsable(false)]
                    
                    public Collection<StrategyObject> MyListValuesSerialize
                    
                    {
                    
                    get
                    
                    {
                    
                    //Remove actual defaults
                    
                    //foreach (string pw in collectionDefaults.ToList())
                    
                    //{
                    
                    //PercentWrapper temp = MyListValues.FirstOrDefault(p => p.PercentageValue == pw.PercentageValue && p.IsDefault == true);
                    
                    //if (temp != null)
                    
                    //collectionDefaults.Remove(temp);
                    
                    //}
                    
                    
                    
                    
                    //Force user added values to not be defaults
                    
                    //MyListValues.All(p => p.IsDefault = false);
                    
                    
                    
                    
                    return MyListValues;
                    
                    }
                    
                    set
                    
                    {
                    
                    MyListValues = value;
                    
                    }
                    
                    }
                    ​
                    Could you please help me out or point me to some docs? I took this from example, but cannot make it working for my case.
                    Please elaborate what you are looking for when you say "I just want to create List of values of variable allAvailableStrategies and be able to access which value is selected id State Configured."

                    You had previously stated, "The question was not about API, but about creating property as i mentioned...
                    List / Select with 2 items given by JSON​" my impression was that you wanted to create or set a property based on JSON information. I apologize for the confusion. Are you looking to get the values of NinjaScript properties? Please provide a more concrete explanation/example of what you are looking to achieve so I may better understand and assist you more accurately.

                    Where did you locate the example that you are pulling from?

                    I look forward to your reply.
                    Emily C.NinjaTrader Customer Service

                    Comment


                      #11
                      I found example here: https://forum.ninjatrader.com/forum/...operty-editors and tried to modify IT.

                      I try to descripe my needs as accurately as possible.

                      1. I have variable which i need to set in SetDefault.
                      // I need to set these values in State == State.SetDefaults
                      Code:
                      private List<StrategyObject> allAvailableStrategies = new List<StrategyObject>()
                      {
                        new StrategyObject("stratA", "Strategy A", true),
                        new StrategyObject("stratB", "Strategy B"),
                        new StrategyObject("stratC", "Strategy C"),
                      };

                      2. this variable will be used to create combobox - property in strategy properties. see image included Combobox
                      There will be 3 items in combobox in this case ( Strategy A, Strategy B, Strategy C). In this case will be default value Strategy A (see true parametr).

                      3. when I click combobox i can change it on Strategy B or Strategy C
                      after apply I will be able to access selected value.

                      Is it clear what i am trying?
                      Attached Files

                      Comment


                        #12
                        Hello kujista,

                        Thank you for your reply.

                        Based on your description, for the dropdown/combobox that you are planning to use, you could likely use a custom enum that would allow you to have a user-defined dropdown menu that could default to Strategy A. You could access the enum parameter value programmatically in your script similarly to the switch-case in the following example:


                        You would be able to access the selected value for the enum.

                        Please let us know if we may be of further assistance.
                        Emily C.NinjaTrader Customer Service

                        Comment


                          #13
                          This was just example... Values will be dynamic and will be set inside if (State == State.SetDefaults) there will be command which returns list of strategies.. I gues i cannot set dynamically enum so your answer does not help :-/

                          Comment


                            #14
                            Hello kujista,

                            Thank you for your patience.

                            Since you mentioned there would be three items in the combo box, I was under the impression that it was static and not that you would need the values to be dynamic. In that case, then yes you will need to use a type converter. The ideas for use case 2.c "Display a custom collection / list with user defined values at run time" could be used from this example:


                            As mentioned in the comments on that script, the values are similar to using PriceLevels in drawing tools. You can look at the PriceLevels script in the DrawingTools folder of the NinjaScript Editor as another example. A type converter is a general C# concept, so more information may be found via external resources as well such as the following publicly available link:


                            Please refer to those examples and let us know if we may be of further assistance.
                            Emily C.NinjaTrader Customer Service

                            Comment


                              #15
                              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?

                              Code:
                              protected override void OnStateChange()
                              
                              {
                              
                              // DOCS: https://ninjatrader.com/support/helpGuides/nt8/NT%20HelpGuide%20English.html?state.htm
                              
                              if (State == State.SetDefaults)
                              
                              {
                              
                              Name = "Golem 2.0";
                              
                              Calculate = Calculate.OnEachTick;
                              
                              TimeInForce = TimeInForce.Gtc;
                              
                              IncludeCommission = false;
                              
                              BarsRequiredToTrade = 3;
                              
                              EntryHandling = EntryHandling.UniqueEntries;
                              
                              IsAutoScale = false;
                              
                              IsOverlay = true;
                              
                              TraceOrders = true;
                              
                              // DebugMessage("SetDefaults - Default values are set (pushed to UI) IsEnabled:" + IsEnabled);
                              
                              }
                              
                              else if (State == State.Configure)
                              
                              {
                              
                              if (IsEnabled)
                              
                              {
                              
                              DebugMessage("Configure - User the presses the OK or Apply button IsEnabled:" + IsEnabled + " _isConfigured" + _isConfigured);
                              
                              DebugMessage("AddDataSeries");
                              
                              _isConfigured = true;
                              
                              }
                              
                              }
                              
                              else if (State == State.DataLoaded)
                              
                              {
                              
                              DebugMessage("DataLoaded - All data series have been loaded IsEnabled:" + IsEnabled);
                              
                              
                              
                              
                              }

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by DJ888, Yesterday, 10:57 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post DJ888
                              by DJ888
                               
                              Started by MacDad, 02-25-2024, 11:48 PM
                              7 responses
                              158 views
                              0 likes
                              Last Post loganjarosz123  
                              Started by Belfortbucks, Yesterday, 09:29 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post Belfortbucks  
                              Started by zstheorist, Yesterday, 07:52 PM
                              0 responses
                              7 views
                              0 likes
                              Last Post zstheorist  
                              Started by pmachiraju, 11-01-2023, 04:46 AM
                              8 responses
                              151 views
                              0 likes
                              Last Post rehmans
                              by rehmans
                               
                              Working...
                              X