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

Initialization error

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

    Initialization error

    When I first add an indicator it returns the attached error. But once I save a template with a value it is fine. This also happens with Strategy Analyzer but there is no way to same a template with a value for optimization variables there. How do I initialize in my code to prevent this?
    Attached Files
    Last edited by EthanHunt; 08-17-2017, 07:03 AM.

    #2
    Hello EthanHunt,

    What is the range allowed for this public property?

    The error indicates you are setting the range between 1 and int.MaxValue, is this correct?

    What is the initial value you are setting it to in State.SetDefaults?

    (This would be something that affects c# and is not specific to NinjaScript)
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Yes the range is 1 and int.MaxValue. I have the value set to 50 in State.SetDefaults. That doesn't seem to be initializing? Thanks.


      Originally posted by NinjaTrader_ChelseaB View Post
      Hello EthanHunt,

      What is the range allowed for this public property?

      The error indicates you are setting the range between 1 and int.MaxValue, is this correct?

      What is the initial value you are setting it to in State.SetDefaults?

      (This would be something that affects c# and is not specific to NinjaScript)

      Comment


        #4
        Hello EthanHunt,

        Can you supply the code where the property is declared as well as the State.SetDefaults action block so that we may take a look?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Property declaration....

          #region Properties
          [Range(1, int.MaxValue), NinjaScriptProperty]
          [Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)]
          public int Period
          {get; set;}
          #endregion


          Here is the State.SetDefaults...




          protected override void OnStateChange()
          {
          if (State == State.SetDefaults)
          {
          Description = @"Enter the description for your new custom Strategy here.";
          Name = "Trading_Strategy";
          Calculate = Calculate.OnBarClose;
          EntriesPerDirection = 1;
          EntryHandling = EntryHandling.AllEntries;
          IsExitOnSessionCloseStrategy = true;
          ExitOnSessionCloseSeconds = 30;
          IsFillLimitOnTouch = false;
          MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
          OrderFillResolution = OrderFillResolution.Standard;
          Slippage = 0;
          StartBehavior = StartBehavior.WaitUntilFlat;
          TimeInForce = TimeInForce.Gtc;
          TraceOrders = false;
          RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
          StopTargetHandling = StopTargetHandling.PerEntryExecution;
          BarsRequiredToTrade = 20;
          // Disable this property for performance gains in Strategy Analyzer optimizations
          // See the Help Guide for additional information
          IsInstantiatedOnEachOptimizationIteration = true;
          int Period = 50;
          }
          else if (State == State.Configure)
          {
          }
          }


          Originally posted by NinjaTrader_ChelseaB View Post
          Hello EthanHunt,

          Can you supply the code where the property is declared as well as the State.SetDefaults action block so that we may take a look?
          Last edited by EthanHunt; 08-17-2017, 10:50 AM.

          Comment


            #6
            Hi EthanHunt,

            This all looks correct.

            I'm not able to reproduce the behavior using the code supplied.

            Attached is a test script. Are you able to reproduce the behavior with the test script?

            Do you have template created for this strategy?
            If the template is deleted does the error does continue?
            Attached Files
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I do not get an error with the code you supplied. I deleted the template and still get the error with my code.


              Originally posted by NinjaTrader_ChelseaB View Post
              Hi EthanHunt,

              This all looks correct.

              I'm not able to reproduce the behavior using the code supplied.

              Attached is a test script. Are you able to reproduce the behavior with the test script?

              Do you have template created for this strategy?
              If the template is deleted does the error does continue?

              Comment


                #8
                Hello EthanHunt,

                Please (copy and then) reduce the script to remove all logic but still produce the error.
                Then, export the reduced script and post the export with your next post.

                To export a NinjaTrader 8 NinjaScript do the following:
                1. Click Tools -> Export -> NinjaScript...
                2. Click the 'add' link -> check the box(es) for the script(s) you want to include
                3. Click the 'Export' button
                4. Enter a unique name for the file in the value for 'File name:'
                5. Choose a save location -> click Save
                6. Click OK to clear the export location message

                By default your exported file will be in the following location:
                • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>


                Below is a link to the help guide on Exporting NinjaScripts.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  File attached. When I run this script in Strategy Analyzer it gives me the error message.


                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hello EthanHunt,

                  Please (copy and then) reduce the script to remove all logic but still produce the error.
                  Then, export the reduced script and post the export with your next post.

                  To export a NinjaTrader 8 NinjaScript do the following:
                  1. Click Tools -> Export -> NinjaScript...
                  2. Click the 'add' link -> check the box(es) for the script(s) you want to include
                  3. Click the 'Export' button
                  4. Enter a unique name for the file in the value for 'File name:'
                  5. Choose a save location -> click Save
                  6. Click OK to clear the export location message

                  By default your exported file will be in the following location:
                  • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>


                  Below is a link to the help guide on Exporting NinjaScripts.
                  http://ninjatrader.com/support/helpG...-us/export.htm
                  Attached Files

                  Comment


                    #10
                    Hello EthanHunt,

                    I overlooked that you are declaring Period twice.

                    Its declared as an int property in the scope of the class and then declared again locally in OnStateChange().

                    When you use int or any other stuct type before a variable name you are declaring a variable with the type of int.



                    (You may notice the other variables being set in OnStateChange do not have type declarations in front of them)

                    This means the local Period that can only be used in State.SetDefaults is being set to 1, while the Period declared in the class of the indicator is never being set and has a default value of 0.
                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Yes that is it, declaring the type within OnStateChange(). I took out the int declaration and it runs fine. Thank you!



                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hello EthanHunt,

                      I overlooked that you are declaring Period twice.

                      Its declared as an int property in the scope of the class and then declared again locally in OnStateChange().

                      When you use int or any other stuct type before a variable name you are declaring a variable with the type of int.



                      (You may notice the other variables being set in OnStateChange do not have type declarations in front of them)

                      This means the local Period that can only be used in State.SetDefaults is being set to 1, while the Period declared in the class of the indicator is never being set and has a default value of 0.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by cls71, Today, 04:45 AM
                      0 responses
                      1 view
                      0 likes
                      Last Post cls71
                      by cls71
                       
                      Started by mjairg, 07-20-2023, 11:57 PM
                      3 responses
                      213 views
                      1 like
                      Last Post PaulMohn  
                      Started by TheWhiteDragon, 01-21-2019, 12:44 PM
                      4 responses
                      544 views
                      0 likes
                      Last Post PaulMohn  
                      Started by GLFX005, Today, 03:23 AM
                      0 responses
                      3 views
                      0 likes
                      Last Post GLFX005
                      by GLFX005
                       
                      Started by XXtrader, Yesterday, 11:30 PM
                      2 responses
                      12 views
                      0 likes
                      Last Post XXtrader  
                      Working...
                      X