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

Exposing non dataseries variable

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

    Exposing non dataseries variable

    I am calculating a double variable (not a plotted dataseries) which represents a temp level of S/R. Can I make that value public, so it can referenced by strategies? How?

    #2
    Try creating a property that accesses your internal variable.

    Code:
    private double tempLevel = 0;
    
    public double TempLevel
    {
        get { return tempLevel; }
    }
    RayNinjaTrader Customer Service

    Comment


      #3
      I have a follow-up question on this topic. I have a custom indicator that doesn't return any values (i.e., it's a void function), but similar to the OP I would like to implement my indicator in a strategy and have access to several of the values used internally in the function. As you directed below, I created public variables in the properties section (plotAbove is my private variable in the function).

      public double TrendAbove
      {
      get { return plotAbove; }
      }

      I did this for eight different variables of types int, double and bool. The indicator compiled okay.

      My questions are as follows:

      1) I understand from a previous question that I should create an instance of my indicator using the Add() function (it's named ATRDots and takes 5 inputs). I assume this should go in the initialize block of the strategy code.

      Add(ATRDots(5, 3, 13, 0.75, 0.9));

      If I want to analyze multiple timeframes in my strategy, will the ATRDots indicator automatically be created for each bars object I add to strategy? Let's say, for example, I created the following bars objects.

      Add(PeriodType.Volume, 4000);
      Add(PeriodType.Volume, 1000);
      Add(PeriodType.Tick, 444);

      If I added my indicator as noted above following the creation of these bars objects would it automatically create a separate instance of my indicator for each of the three bars objects, or I do I need to add the indicator three separate times, one for each bars object?


      2) Given that I hopefully have three instances of my ATRDots indicator at this point (one for each bars object), how do I reference the public variables for each one (e.g., for the TrendAbove variable noted above)?

      Thank you.

      Comment


        #4
        See below.

        Originally posted by JangoFolly View Post
        I have a follow-up question on this topic. I have a custom indicator that doesn't return any values (i.e., it's a void function), but similar to the OP I would like to implement my indicator in a strategy and have access to several of the values used internally in the function. As you directed below, I created public variables in the properties section (plotAbove is my private variable in the function).

        public double TrendAbove
        {
        get { return plotAbove; }
        }

        I did this for eight different variables of types int, double and bool. The indicator compiled okay.

        My questions are as follows:

        1) I understand from a previous question that I should create an instance of my indicator using the Add() function (it's named ATRDots and takes 5 inputs). I assume this should go in the initialize block of the strategy code.

        Add(ATRDots(5, 3, 13, 0.75, 0.9));

        >>> This Add() method is only used for visualization, nothing more. By calling this method and passing in the indicator as you have done, it will display on the chart when the strategy runs on a chart.

        If I want to analyze multiple timeframes in my strategy, will the ATRDots indicator automatically be created for each bars object I add to strategy? Let's say, for example, I created the following bars objects.

        Add(PeriodType.Volume, 4000);
        Add(PeriodType.Volume, 1000);
        Add(PeriodType.Tick, 444);

        If I added my indicator as noted above following the creation of these bars objects would it automatically create a separate instance of my indicator for each of the three bars objects, or I do I need to add the indicator three separate times, one for each bars object?

        >>> No, an indicator instance is created when you call the method in the OnBarUpdate() method.

        // Print the primary series value
        Print(ATRDots(5, 3, 13, 0.75, 0.9).TrendAbove);

        // Print the 4000 volume bar
        Print(ATRDots(BarsArray[1], 5, 3, 13, 0.75, 0.9).TrendAbove);

        // Print the 1000 volume bar
        Print(ATRDots(BarsArray[2], 5, 3, 13, 0.75, 0.9).TrendAbove);


        2) Given that I hopefully have three instances of my ATRDots indicator at this point (one for each bars object), how do I reference the public variables for each one (e.g., for the TrendAbove variable noted above)?

        >>> See above.

        Thank you.
        RayNinjaTrader Customer Service

        Comment


          #5
          Ray,

          When I add the following strategy to a chart, the indicator doesn't display on the chart, and the prints to the output window are all zero. I feel like I'm missing part of the big picture about how these things communicate. Thanks.

          protected override void Initialize()
          {
          CalculateOnBarClose = false;

          Add(ATRDots(5, 3, 13, 0.75, 0.9));
          }

          protected override void OnBarUpdate()
          {
          Print(ATRDots(5, 3, 13, 0.75, 0.9).TrendAbove);
          }

          Comment


            #6
            My fault, I forgot one critical piece of information. When you have an indicator that does not have a plot and you create a public property that you wish to access from another indicator or strategy, you have to call the Update() method within your property "Getter" to force an update of the OnBarUpdate().

            Additional information can be found here on this method - http://www.ninjatrader-support.com/H...V6/Update.html

            I have included a Sample.cs (indicator) and SampleStrategy.cs (strategy) that demonstrates everything discussed in this thread.
            Attached Files
            RayNinjaTrader Customer Service

            Comment


              #7
              It's still not working for me. Can I e-mail you the indicator and strategy to see whether I'm missing something? I would rather not post it publicly. The strategy is just a test print of a single variable.

              I really appreciate your patience and help. Thank you.


              Originally posted by NinjaTrader_Ray View Post
              My fault, I forgot one critical piece of information. When you have an indicator that does not have a plot and you create a public property that you wish to access from another indicator or strategy, you have to call the Update() method within your property "Getter" to force an update of the OnBarUpdate().

              Additional information can be found here on this method - http://www.ninjatrader-support.com/H...V6/Update.html

              I have included a Sample.cs (indicator) and SampleStrategy.cs (strategy) that demonstrates everything discussed in this thread.

              Comment


                #8
                I hope you understand when I say that we don't have the bandwidth to trouble shoot user generated code.

                Have you tried the sample code I posted? For sure this works. Its simple and clean. What I would suggest is use my code and build on top of it to achieve the result you are looking for.
                RayNinjaTrader Customer Service

                Comment


                  #9
                  I do understand.

                  Your sample code worked just fine. I tried changing the indicator reference in your sample strategy to use my indicator rather than your sample indicator, but it still put out only zero.

                  My indicator works just fine when I add it to a chart, but I did just notice this message in the log. Could it be causing the problem?

                  Failed to call method 'Initialize' for indicator 'ATRDots': Object reference not set to an instance of an object.


                  Thank you.

                  Originally posted by NinjaTrader_Ray View Post
                  I hope you understand when I say that we don't have the bandwidth to trouble shoot user generated code.

                  Have you tried the sample code I posted? For sure this works. Its simple and clean. What I would suggest is use my code and build on top of it to achieve the result you are looking for.

                  Comment


                    #10
                    For sure that is causing a problem.

                    - Did you add a call to the Update() method within your indicator for each properties' getter ?
                    - If yes, apply your indicator to a chart and see if you get the "object" reference error
                    - If yes, then your indicator is not functioning, somewhere in your indicator code you are accessing a variable that does not hold an object, you will have to add debug print statements to determine where that might be

                    For example:

                    private object myObject = null; // Might be an object variable

                    Where ever you reference this variable, you might add a line of code such as:

                    if (myObject == null) Print ("Null object at line XXXX");

                    You then know where in your code is causing the exception and thus can figure out why its null.
                    RayNinjaTrader Customer Service

                    Comment


                      #11
                      This was causing the problem...


                      private string currSymbolRoot; // In the variable section


                      currSymbolRoot = Instrument.MasterInstrument.Name; // In the initialization block


                      Everything seems to be working now. The strange thing is that the currSymbolRoot variable was populated correctly, so it wouldn't suggest an error.

                      Again, thank you for all of your help and patience.


                      Regards,


                      Originally posted by NinjaTrader_Ray View Post
                      For sure that is causing a problem.

                      - Did you add a call to the Update() method within your indicator for each properties' getter ?
                      - If yes, apply your indicator to a chart and see if you get the "object" reference error
                      - If yes, then your indicator is not functioning, somewhere in your indicator code you are accessing a variable that does not hold an object, you will have to add debug print statements to determine where that might be

                      For example:

                      private object myObject = null; // Might be an object variable

                      Where ever you reference this variable, you might add a line of code such as:

                      if (myObject == null) Print ("Null object at line XXXX");

                      You then know where in your code is causing the exception and thus can figure out why its null.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by andrewtrades, Today, 04:57 PM
                      0 responses
                      0 views
                      0 likes
                      Last Post andrewtrades  
                      Started by chbruno, Today, 04:10 PM
                      0 responses
                      3 views
                      0 likes
                      Last Post chbruno
                      by chbruno
                       
                      Started by josh18955, 03-25-2023, 11:16 AM
                      6 responses
                      436 views
                      0 likes
                      Last Post Delerium  
                      Started by FAQtrader, Today, 03:35 PM
                      0 responses
                      6 views
                      0 likes
                      Last Post FAQtrader  
                      Started by rocketman7, Today, 09:41 AM
                      5 responses
                      19 views
                      0 likes
                      Last Post NinjaTrader_Jesse  
                      Working...
                      X