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

Frustrated with a compiler error message

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

    Frustrated with a compiler error message

    I am trying to create the simplest indicator that will just return the value of standard Momentum. The help files show examples that I copied but the code won't compile due to a CS0120 error message that directs me to a Microsoft site that I can't understand. If anyone can help me get past the compiler error, I would appreciate it. This is my first attempt at creating an indicator and this should be a no-brainer.

    About the only line of code I inserted was:

    double value = Momentum (3) [1]

    I just want the Momentum (period = 3) for the previous bar. The error message points to the work Momentum and states:

    "An object reference is required for the non-static field, method, or property 'NinjaTrader.Indicator.Indicator.Momentum ()".

    I also tried typing in the long text preceding the word Momentum but still get the same error message. I followed the example in the tutorials and this should have been a simple task. Thanks in advance for any assistance.

    #2
    Hello Geneo,

    Thank you for your post and welcome to the NinjaTrader Support Forum!

    Is the code the following?
    Code:
    double value = Momentum(3)[1];

    Comment


      #3
      Thanks for the reply. However, that is the exact command that gets the error message. When I click on the error message, it highlights the word "Momentum". The error message reads: An object reference is required for the non-static field, method, or property NinjaTrader.Indicator.Indicator.Momemtun (int).

      Is there something else I needed to define earlier in the indicator settings?

      Comment


        #4
        Originally posted by Geneo View Post
        Thanks for the reply. However, that is the exact command that gets the error message. When I click on the error message, it highlights the word "Momentum". The error message reads: An object reference is required for the non-static field, method, or property NinjaTrader.Indicator.Indicator.Momemtun (int).

        Is there something else I needed to define earlier in the indicator settings?
        Please try:

        double value = 0;
        if (CurrentBar != 0) value = Momentum(3)[1];
        ninZa
        NinjaTrader Ecosystem Vendor - ninZa.co

        Comment


          #5
          Hello Geneo,

          Where are you placing this line of code? It should be placed inside the OnBarUpdate() method.

          Comment


            #6
            I have attached a screen capture of the indicator code along with the error message. When I click on the error message it highlights the word 'Momentum" on line 43. I think the code is in the correct location. I still get the same error message. I also tried the previous suggestion which gave me more error messages. This seemed like an easy exercise for a beginner - where did I go wrong?
            Attached Files

            Comment


              #7
              Originally posted by Geneo View Post
              I have attached a screen capture of the indicator code along with the error message. When I click on the error message it highlights the word 'Momentum" on line 43. I think the code is in the correct location. I still get the same error message. I also tried the previous suggestion which gave me more error messages. This seemed like an easy exercise for a beginner - where did I go wrong?
              Please put the statement INSIDE OnBarUpdate. Move it to the line #41, Geneo.
              Also, please use the suggestion I gave previously.
              ninZa
              NinjaTrader Ecosystem Vendor - ninZa.co

              Comment


                #8
                Good News! I inserted your suggestion into the "OnBarUpdate()" section and was able to get a clean compile. However, the resulting answer is not correct. The value returned is the current day's CLOSE price, not the value for Momentum. I have attached a copy of the code.
                Attached Files

                Comment


                  #9
                  Originally posted by Geneo View Post
                  Good News! I inserted your suggestion into the "OnBarUpdate()" section and was able to get a clean compile. However, the resulting answer is not correct. The value returned is the current day's CLOSE price, not the value for Momentum. I have attached a copy of the code.
                  We made a progress

                  How do you check the values?
                  You have neither plotted them nor printed them out, as I see from your code.

                  Thanks.
                  Pi
                  ninZa
                  NinjaTrader Ecosystem Vendor - ninZa.co

                  Comment


                    #10
                    I have placed the new indicator into a Market Analyzer. I can then have the values displayed for numerous stocks that are in the Market Analyzer. I am only interested in retrieving the VALUE of the Momentum for the previous day. That is why I am working on this specific indicator. I also display the standard Momentum for the current day in the Market Analyzer which works flawlessly. Can I assume that the code in this new custom indicator retrieves the VALUE of Momentum(3)[1] ?

                    Comment


                      #11
                      Originally posted by Geneo View Post
                      I have placed the new indicator into a Market Analyzer. I can then have the values displayed for numerous stocks that are in the Market Analyzer. I am only interested in retrieving the VALUE of the Momentum for the previous day. That is why I am working on this specific indicator. I also display the standard Momentum for the current day in the Market Analyzer which works flawlessly. Can I assume that the code in this new custom indicator retrieves the VALUE of Momentum(3)[1] ?
                      Nope. You have not done that.

                      Please add those codes:
                      DataSeries prevMomentum;

                      [Browsable(false)]
                      [XmlIgnore()]
                      public DataSeries PrevMomentum {
                      get { return prevMomentum; }
                      }
                      In Initialize:
                      prevMomentum = new DataSeries(this);
                      In OnBarUpdate:
                      if (CurrentBar == 0) prevMomentum.Set(0);
                      else prevMomentum.Set(Momentum(3)[1]);
                      Then in Market Analyzer, please choose the PrevMomentum in the Plot field. Remember to select the timeframe to be 1 Day as well.

                      Cheers,
                      Pi
                      ninZa
                      NinjaTrader Ecosystem Vendor - ninZa.co

                      Comment


                        #12
                        Pi,

                        I received a compiler error pointing to the item: [Xmllgnore. Was there a typo? Is this an external reference? The Xmllgnore attribute could not be found.

                        Thanks for your continued assistance.

                        Comment


                          #13
                          Please provide me a screenshot of your code where you put my suggestions.

                          Thanks.
                          ninZa
                          NinjaTrader Ecosystem Vendor - ninZa.co

                          Comment


                            #14
                            Here is a screenshot of my updated code. If I placed the code in the wrong location, please indicate what lines to enter the code. Thanks again.
                            Attached Files

                            Comment


                              #15
                              You put it correctly.

                              Please copy ALL of your code lines and document it as a word or notepad file. (yes, Ctrl + A to select all)

                              I need to test your code on my end

                              Thanks.
                              ninZa
                              NinjaTrader Ecosystem Vendor - ninZa.co

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Brevo, Today, 01:45 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post Brevo
                              by Brevo
                               
                              Started by aussugardefender, Today, 01:07 AM
                              0 responses
                              3 views
                              0 likes
                              Last Post aussugardefender  
                              Started by pvincent, 06-23-2022, 12:53 PM
                              14 responses
                              238 views
                              0 likes
                              Last Post Nyman
                              by Nyman
                               
                              Started by TraderG23, 12-08-2023, 07:56 AM
                              9 responses
                              384 views
                              1 like
                              Last Post Gavini
                              by Gavini
                               
                              Started by oviejo, Today, 12:28 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post oviejo
                              by oviejo
                               
                              Working...
                              X