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

Passing Value to a method

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

    Passing Value to a method

    Hi
    I am trying to develop a custom slope indicator which identifies sequences of n up or down bars.
    The else branch of the OnBarUpdate() method looks like this:
    {
    slowEma.Set((2.0 / (1 + Slow)) * Input[0] +
    (1 - (2.0 / (1 + Slow))) * slowEma[1]);
    double avr = slowEma[0];
    Value.Set(avr);
    Print(Value[0].ToString("0.00");
    slope = Slopes(Value); // Sorry, I left ut the call
    }
    This prints Value to the output window, but if I call Slopes() with many
    different ways to reference Value, either by passing it directly or assuming
    it is global, it fails.

    Slopes(), as defined in Indicator.UserDefinedMethods, looks like this:
    public int Slopes(DataSeries Value)
    {
    int retval = 0;
    if(Value[0] > Value[1] && Value[1] > Value[2]) retval = 1;
    if(Value[0] < Value[1] && Value[1] < Value[2]) retval = -1;
    return retval;
    }
    How do I pass the reference to Value? Thanks
    Last edited by Wholesome; 08-03-2009, 01:01 PM.

    #2
    Wholesome, not sure I follow - how does your call to the Slopes method look then in the code?
    BertrandNinjaTrader Customer Service

    Comment


      #3
      I left out the call

      The call to Slopes();

      int slope = Slopes(Value);

      I just want to access the Value[] array with an index.

      Comment


        #4
        You can't do that. Slope() requires two data points which means you need to call the DataSeries and not a specific value.

        The Slope() method requires 3 parameters.

        Slope(IDataSeries series, int startBarsAgo, int endBarsAgo)
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Slopes() is a custom method

          Hi Josh
          Slopes() is a custom method I am writing (not to be confused with the Slope() method) My custom method appears at the start of this thread.
          How do I access the Value[] DataSeries in a custom method. Is Value[] global, or do I pass it as an argument? If so, how do I reference it?
          Thank you.

          Comment


            #6
            Try printing Value[1] from in there. You need to first ensure that you have enough bars to actually access such an index value though. Please see this tip: http://www.ninjatrader-support2.com/...ead.php?t=3170

            If you can't access it from in there then you will need to pass those values into the method as input parameters when you call Slopes() from OnBarUpdate().
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              Still mystified

              Hi again
              The Value[1] suggestion didn't work so I tried to pass the individual values. When the code looked like this in OnBarUpdate(), it printed the values and drew the average.

              {
              slowEma.Set((2.0 / (1 + Slow)) * Input[0] + (1 - (2.0 / (1 + Slow))) * slowEma[1]);
              double avr = slowEma[0];
              Value.Set(avr);
              Print(Value[0].ToString("0.00");
              }

              When I modified the code to try to pass a single value, it didn't print or draw the average line.

              {
              slowEma.Set((2.0 / (1 + Slow)) * Input[0] + (1 - (2.0 / (1 + Slow))) * slowEma[1]);
              double avr = slowEma[0];
              Value.Set(avr);
              double v0 = Value[0];
              Print(v0.ToString("0.00");
              }

              Suggestions?

              Comment


                #8
                Sorry I don't see where you are passing any value anywhere. All you did was set it to a double variable.
                Josh P.NinjaTrader Customer Service

                Comment


                  #9
                  OK Here it is

                  HI Josh

                  {
                  slowEma.Set((2.0 / (1 + Slow)) * Input[0] + (1 - (2.0 / (1 + Slow))) * slowEma[1]);
                  double avr = slowEma[0];
                  Value.Set(avr);
                  double v0 = Value[0];
                  Print(v0.ToString("0.00"); // This failed, so I saw no point in including
                  int slope = Slopes(v0); // this.
                  }

                  Comment


                    #10
                    Failed as in how? If Value[0] holds a value then v0 will as well. Please print both to see.
                    Josh P.NinjaTrader Customer Service

                    Comment


                      #11
                      Further clarification

                      HI Josh
                      I thought so too. I thought when I set V0 = Value[0] that they would contain the same value. I'm pretty sure Value[0] contained a valid number because I printed it to the output window. Then when I set double v0 = Value[0] and tried to print v0, the indicator, which had previously drawn a 50 bar SMA on the chart and printed Value[0] to the output window, no longer did so. That is how it failed.

                      Comment


                        #12
                        You will need to check your Control Center logs for errors then. If you can access Value[0] you for sure can set it to a variable.
                        Josh P.NinjaTrader Customer Service

                        Comment


                          #13
                          Found the bug

                          Hi Josh
                          Here's the Print() statement:

                          Print(v0.ToString("0.00");

                          The addition of the second right paren makes it right. I've made that mistake before. Sorry.

                          Comment


                            #14
                            Spoke too soon

                            That was just the first bug. I return again to my first question. How do I pass the reference from a DataSeries in an indicator to be indexed in a user defined method?

                            Is the data global? If so it could be referenced directly.

                            Can I pass it by name?

                            from:
                            int value = UserMethod(Value);
                            to:
                            public int UserMethod(DataSeries Value)
                            {
                            ...
                            }

                            Can I pass parameters derived from the data in the indicator?

                            from:
                            double v0, v1, v2, v3;
                            v0 = Value[0]; v1 = Value[1]; v2 = Value[2]; v3 = Value[3];
                            int value = UserMethod(v0,v1,v2,v3);
                            to:
                            public int UserMethod(double v0, double v1, double v2, double v3)
                            {
                            ...
                            }

                            All of these approaches compile, but none of them work.

                            Comment


                              #15
                              Wholesome,

                              What is your end game? Not following what you are trying to do.

                              From OnBarUpdate() you have bar information, indicator information, etc. If you have a custom method you need that information from you can pass that information in.

                              Create a custom method that takes parameters. Call that method with the parameters you want to pass in. Then you will have access to the values you need in that method. Then you can return back from the method to OnBarUpdate() with whatever value you want.
                              Josh P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by inanazsocial, Today, 01:15 AM
                              1 response
                              6 views
                              0 likes
                              Last Post NinjaTrader_Jason  
                              Started by rocketman7, Today, 02:12 AM
                              0 responses
                              10 views
                              0 likes
                              Last Post rocketman7  
                              Started by dustydbayer, Today, 01:59 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post dustydbayer  
                              Started by trilliantrader, 04-18-2024, 08:16 AM
                              5 responses
                              23 views
                              0 likes
                              Last Post trilliantrader  
                              Started by Davidtowleii, Today, 12:15 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post Davidtowleii  
                              Working...
                              X