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

Variable reference problem

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

    Variable reference problem

    Hi: I'm creating an Indicator. I declare a variable JU. In the Initialize section I assign JU a calculated value intended to be used as a constant later in the program. I can now print that value and it is correct.

    Now I use that variable in the OnBarUpdate part of my indicator. JU shows equal to 0 rather that the assigned calculated value from the initialize section. How do I pass the value of JU from the initialize section so I can use it in the ONBarUpdate section??

    Any help or suggestions truly appreciated.

    #2
    It sounds like you declared and assigned that variable in Initialize(). If you declare in the variables section of your class, then it will work as expected (modifications done in Initialize() are visible in OnBarUpdate()).

    Regards
    Ralph

    Welcome to the forum, rhbishop5.

    Comment


      #3
      Reply to Reply

      Thanks, Ralph. The variable was declared in the Variables section by private double JU; It was then assigned a value in the initialize section by double JU=(-100/(U-100))-1; . When printed in this section, JU shows the proper value. When used in the OnBar Update area it comes up as 0.

      Comment


        #4
        Attached you find an example that works as I would expect to. An integer is defined in the variables section then modified in Initialize() and finally displayed on a chart as depicted in the little picture. Now you could use this code example to demonstrate where your problem is.

        Regards
        Ralph
        Attached Files

        Comment


          #5
          YES!!!!! Thanks, Ralph. That did the trick. I had a "double" in front of the variable when I assigned it in the initialize area and evidently the compiler considers that a separate variable not related too the one declared in the variable area. Thanks much!!!! Bob

          Comment


            #6
            Glad to see, that variable assignments work too for your Ninja installation, Bob

            The lesson in your case is to take care when and where to declare a variable. C# is very flexible, you could declare a variable nearly everywhere, Although a declaration at the beginning of a method is a viable spot. More strict compilers with the possibility to write out log messages (not only compiler errors) sometimes issue warnings about name-clashes.

            Regards
            Ralph

            Comment


              #7
              Bob, great to hear you've found a working solution. As you've found out, this is a pretty helpful forum. If you have any other questions, just ask.

              Ralph, thank you for your continued support and contributions to the forum.
              AustinNinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by Aviram Y, Today, 05:29 AM
              0 responses
              1 view
              0 likes
              Last Post Aviram Y  
              Started by quantismo, 04-17-2024, 05:13 PM
              3 responses
              25 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by ScottWalsh, 04-16-2024, 04:29 PM
              7 responses
              34 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by cls71, Today, 04:45 AM
              0 responses
              6 views
              0 likes
              Last Post cls71
              by cls71
               
              Started by mjairg, 07-20-2023, 11:57 PM
              3 responses
              216 views
              1 like
              Last Post PaulMohn  
              Working...
              X