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

Indicator Selection as Strategy NinjaScript Property

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

    Indicator Selection as Strategy NinjaScript Property

    Hi team, I am trying to allow my strategy to pick indicators to backtest via the UI

    In my strategy, I added a NinjaTrader.Gui.Tools.InputEditor Property

    Code:
    public class AIStrategyGenerator : Strategy
    {
    protected override void OnStateChange()
    {
    
    if (State == State.SetDefaults)
    {
    InputIndicator1 = null;
    }
    }
    
    
    protected override void OnBarUpdate()
    {}
    [NinjaScriptProperty]
    [Display(Name = "Indicator 1", GroupName = "Parameters", Prompt = "Edit indicator 1...", Order = 0)]
    [PropertyEditor("NinjaTrader.Gui.Tools.InputEditor" )]
    [XmlIgnore]
    public Indicator InputIndicator1 { get; set; }
    }
    The indicator dropdown select successfully renders in my properties tab in Strategy analyzer. See below image, but when I select an indicator I get an error message from NinjaTrader.Core Object Reference not set to an Instance of an Object.

    To solve I manually populated an Enum with all the indicators in NinjaTrader but I feel like using InputEditor or CollectionsEditor from NinjaTrader.Gui would be a cleaner solution. Anyways let me know if it's possible to add multiple indicators to a strategy via the NinjaScript Property GUI.


    #2
    Hello JakeOfSpades,

    I can point you to a working example posted by a colleague.
    https://ninjatrader.com/support/foru...75#post1054875

    I don't see specifically what in your suggested code would cause the error, everything looks correct.. This is the complete code?
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_ChelseaB View Post
      Hello JakeOfSpades,

      I can point you to a working example posted by a colleague.
      https://ninjatrader.com/support/foru...75#post1054875

      I don't see specifically what in your suggested code would cause the error, everything looks correct.. This is the complete code?
      Hey Chelsea, I recorded a video with the same code above on what happens. Check it out, I voice recorded it to explain the error in more detail. The error is in NinjaTrader.Core.



      Can you replicate the error on your end with the above same code?

      Comment


        #4
        Hello JakeOfSpades,

        What happened when you tested the SampleIndicatorInput I have given you a link to?
        Did the same behavior occur?

        I'm not seeing any script has been attached to your post.

        To export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
        1. Click Tools -> Export -> NinjaScript...
        2. Click the 'add' link -> check the box(es) for the script(s) and reference(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


        I'm not able to reproduce with this code.

        Below is a link to a video.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Originally posted by NinjaTrader_ChelseaB View Post
          Hello JakeOfSpades,

          What happened when you tested the SampleIndicatorInput I have given you a link to?
          Did the same behavior occur?

          I'm not seeing any script has been attached to your post.

          To export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
          1. Click Tools -> Export -> NinjaScript...
          2. Click the 'add' link -> check the box(es) for the script(s) and reference(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


          I'm not able to reproduce with this code.

          Below is a link to a video.
          https://drive.google.com/file/d/1kZ9...w?usp=drivesdk
          I have attached the sample script, I will try the sample you provided but that's for an indicator class. I want a strategy that has indicator(s) as parameters.
          Attached Files

          Comment


            #6
            Hello JakeOfSpades,

            In the video it appears you are selecting a Price series and not an indicator. As a heads up, this would be an Indicator type variable and not a price series type object. Be sure to select an indicator.

            Between the example script I have linked and your suggested code, I am seeing the use of the [NinjaScriptProperty] attribute in your code while the example does not. Try modeling your code after the working example code you have been provided which does not include this.

            The NinjaScriptProperty attribute cannot be used with these object types as these type of objects cannot be optimized over.
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Originally posted by NinjaTrader_ChelseaB View Post
              Hello JakeOfSpades,

              In the video it appears you are selecting a Price series and not an indicator. As a heads up, this would be an Indicator type variable and not a price series type object. Be sure to select an indicator.

              Between the example script I have linked and your suggested code, I am seeing the use of the [NinjaScriptProperty] attribute in your code while the example does not. Try modeling your code after the working example code you have been provided which does not include this.

              The NinjaScriptProperty attribute cannot be used with these object types as these type of objects cannot be optimized over.
              Hey Chelsea, still get the same error. I removed the [NinjaScriptProperty] attribute and still get a popup error when I select an indicator, NinjaTrader.Gui.Tools.InputEditor does work in an Indicator Class but doesn't seem to work in a Strategies class.

              Code:
              [Display(Name = "Indicator 1", GroupName = "Parameters", Prompt = "Edit indicator 1...", Order = 0)]
              [PropertyEditor("NinjaTrader.Gui.Tools.InputEditor" )]
              [XmlIgnore]
              public Indicator InputIndicator1 { get; set; }
              Attached Files

              Comment


                #8
                Hello JakeOfSpades,

                I'm finding that same code adapted to a strategy works exactly the same. There are exactly 0 differences between running this code in a strategy vs an indicator.

                I started by creating a new strategy and copying over the code from the example, it worked without issue.

                I then decided to expand on this to work with xml (mostly because I thought this would make a good example). Sans fancy code, it still works without any errors.

                My advice is to start with something that works. Then add your code to it one step at a time until it breaks, then focus on the last change.

                As a tip, I tested the script you posted in post #7 and this has no errors on my end. Your script doesn't actually print anything or do anything, but it doesn't error and doesn't disable.

                Below is a link to a video of testing your script.


                Are you certain you have removed the instance of the script and added a new instance to test the new code, or you are you just reloading NinjaScript without adding a new instance?
                Attached Files
                Last edited by NinjaTrader_ChelseaB; 11-02-2021, 02:54 PM.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_ChelseaB View Post
                  Hello JakeOfSpades,

                  I'm finding that same code adapted to a strategy works exactly the same. There are exactly 0 differences between running this code in a strategy vs an indicator.

                  I started by creating a new strategy and copying over the code from the example, it worked without issue.

                  I then decided to expand on this to work with xml (mostly because I thought this would make a good example). Sans fancy code, it still works without any errors.

                  My advice is to start with something that works. Then add your code to it one step at a time until it breaks, then focus on the last change.

                  As a tip, I tested the script you posted in post #7 and this has no errors on my end. Your script doesn't actually print anything or do anything, but it doesn't error and doesn't disable.

                  Below is a link to a video of testing your script.


                  Are you certain you have removed the instance of the script and added a new instance to test the new code, or you are you just reloading NinjaScript without adding a new instance?
                  In the video you didn't click on the indicators button or choose an indicator, that's where it errors out for me. The script compiles fine and does render in the Strategy Analyzer UI but when I select an indicator that's when I get an error. See below image I used the exact same code as you, as soon as I select an Indicator from the Input Series that's when the error pops up. I'm using NT 8.0.25

                  Click image for larger version

Name:	Untitled.png
Views:	258
Size:	61.5 KB
ID:	1177162

                  Comment


                    #10
                    Hello JakeOfSpades,

                    May I have a screenshot of the error from the InputEditorStrategyExample_NT8 I have just done the work to code as an example strategy for you?

                    My guess is the line where the indicator is set to null in State.SetDefaults is causing the issue.
                    I realize this was in the example script provided by Kate, but I personally am not understanding the logic from that. Variables that are nullable types are already null when declared if no object is assigned to the variable. So seems redundant.

                    Below is a link to a video of a test of your script and selecting an indicator.

                    Chelsea B.NinjaTrader Customer Service

                    Comment


                      #11
                      Originally posted by NinjaTrader_ChelseaB View Post
                      Hello JakeOfSpades,

                      May I have a screenshot of the error from the InputEditorStrategyExample_NT8 I have just done the work to code as an example strategy for you?

                      My guess is the line where the indicator is set to null in State.SetDefaults is causing the issue.
                      I realize this was in the example script provided by Kate, but I personally am not understanding the logic from that. Variables that are nullable types are already null when declared if no object is assigned to the variable. So seems redundant.

                      Below is a link to a video of a test of your script and selecting an indicator.
                      Sure, could my NinjaTrader be corrupted? If it's working on your end might be worth a reinstall.
                      Click image for larger version

Name:	Untitled.png
Views:	240
Size:	61.5 KB
ID:	1177169

                      Comment


                        #12
                        Hello JakeOfSpades,

                        Depends, are you still setting the variable to null?

                        An 'object reference not set to instance of an object' error is a null value.

                        Keep in mind that getters and setters are activated by the constructor when a new instance of a class is created. If the variable is null, then the variable (an object reference) doesn't have an object (not set to instance of object).

                        You have not provided a screenshot of the error from the script I have worked to provide you an example of.
                        Please provide the requested screenshot so that I may continue to assist.
                        Chelsea B.NinjaTrader Customer Service

                        Comment


                          #13
                          Originally posted by NinjaTrader_ChelseaB View Post
                          Hello JakeOfSpades,

                          Depends, are you still setting the variable to null?

                          An 'object reference not set to instance of an object' error is a null value.

                          Keep in mind that getters and setters are activated by the constructor when a new instance of a class is created. If the variable is null, then the variable (an object reference) doesn't have an object (not set to instance of object).

                          You have not provided a screenshot of the error from the script I have worked to provide you an example of.
                          Please provide the requested screenshot so that I may continue to assist.
                          I am using the script you sent me, looks like you don't instantiate InputIndicator or InputPriceSeries variables to null. I don't get an error in NinjaScript Editor rather when I select an indicator.
                          InputEditorStrategyExample_NT8.zip I have attached the error below, here is the StackTrace

                          System.NullReferenceException
                          HResult=0x80004003
                          Message=Object reference not set to an instance of an object.
                          Source=NinjaTrader.Core
                          StackTrace:
                          at NinjaTrader.Data.BarsType.CreateInstance(BarsPerio d period)

                          Click image for larger version

Name:	error.png
Views:	240
Size:	159.2 KB
ID:	1177180
                          Attached Files

                          Comment


                            #14
                            Hello JakeOfSpades,

                            I'm not getting any error when selecting an indicator.

                            Please try running the repair from the NinjaTrader 8.0.25.0 installer.
                            1. Shutdown NinjaTrader
                            2. Download the NinjaTrader installation package from the link below
                            3. http://ninjatrader.com/GetStarted
                            4. Run the repair from this (if the repair is unsuccessful, uninstall and then re-install NinjaTrader)
                            Chelsea B.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_ChelseaB View Post
                              Hello JakeOfSpades,

                              I'm not getting any error when selecting an indicator.

                              Please try running the repair from the NinjaTrader 8.0.25.0 installer.
                              1. Shutdown NinjaTrader
                              2. Download the NinjaTrader installation package from the link below
                              3. http://ninjatrader.com/GetStarted
                              4. Run the repair from this (if the repair is unsuccessful, uninstall and then re-install NinjaTrader)
                              Tried repairing and reinstalling, still same error. Will try restarting my computer.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by ghoul, Today, 06:02 PM
                              1 response
                              10 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by jeronymite, 04-12-2024, 04:26 PM
                              3 responses
                              44 views
                              0 likes
                              Last Post jeronymite  
                              Started by Barry Milan, Yesterday, 10:35 PM
                              7 responses
                              20 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by AttiM, 02-14-2024, 05:20 PM
                              10 responses
                              180 views
                              0 likes
                              Last Post jeronymite  
                              Started by DanielSanMartin, Yesterday, 02:37 PM
                              2 responses
                              13 views
                              0 likes
                              Last Post DanielSanMartin  
                              Working...
                              X