Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

error in access to historical indictor value

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

    error in access to historical indictor value

    I am trying to implement some strategy in my code. inState.DataLoaded, AddChartIndicator() is loaded. but when try to acess value of historical indictor, just one bar ago, no way. just nothing.
    for example, in the indictor, I use Values[0][0], Values[1][0],Values[2][0] to descripe the current bar. if I want to acess previous bar Values[0], it should be Values[0][1], the previous of previous bar is Values[0][2].

    I also try EMA20, which is build-in indictor for ninja trader. the code is:
    Print("EMA1: " + EMA(20)[0]);
    Print("EMA2: " + EMA(20)[1]);
    very simple. but it could print EMA1: . for the previous bar value, no printing.​
    Last edited by steinberg123; 12-06-2024, 08:21 AM.

    #2
    Hello steinberg123,

    Are you trying that from OnStateChange? If so you cant access data there, that code needs to go inside OnBarUpdate.

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello steinberg123,

      Are you trying that from OnStateChange? If so you cant access data there, that code needs to go inside OnBarUpdate.
      no. it is On State == State.DataLoaded.

      Comment


        #4
        Hello steinberg123,

        Right that is OnStateChange, you need to move that code into OnBarUpdate where bars get processed so you can access data.

        Comment


          #5
          for this issue, do you have any instruction material? any link?

          Comment


            #6
            Hello steinberg123,

            You can see any of the system indicators, their code that uses prices resides in the OnBarUpdate override. OnBarUpdate is called when bars process, to use bar data that is where your code needs to go. That override looks like this:

            protected override vode OnBarUpdate()
            {
            // your code here
            }
            Last edited by NinjaTrader_Jesse; 12-06-2024, 08:49 AM.

            Comment


              #7
              Originally posted by NinjaTrader_Jesse View Post
              Hello steinberg123,

              You can see any of the system indicators, their code that uses prices resides in the OnBarUpdate override. OnBarUpdate is called when bars process, to use bar data that is where your code needs to go. That override looks like this:

              protected override vode OnBarUpdate()
              {
              // your code here
              }
              ok. but I use calculate.Oneachtick, find that sometimes, the OnBarUpdate() is activated after several seconds of beginning minute.sometimes, it is 5 seconds.

              Comment


                #8
                Hello steinberg123,

                That sounds like a seperate problem which you may want to open another thread for specifically. In regard to your question here, your bar related code needs to go inside OnBarUpdate so you can have bars process. BarsAgo is a result of bars processing, that does not happen in OnStateChange or DataLoaded, that happens in OnBarUpdate.

                To start I would suggest using just OnBarClose so you can get used to how bars process and how your script works in general, for more advanced use cases you can then change to OneachTick or OnPriceChange which each have their own specific design requirements. A simple example of the print you had shown would look like this:


                protected override vode OnBarUpdate()
                {
                if(CurrentBar < 1) return;
                Print("EMA1: " + EMA(20)[0]);
                Print("EMA2: " + EMA(20)[1]);​
                }​

                Comment


                  #9
                  Originally posted by NinjaTrader_Jesse View Post
                  Hello steinberg123,

                  That sounds like a seperate problem which you may want to open another thread for specifically. In regard to your question here, your bar related code needs to go inside OnBarUpdate so you can have bars process. BarsAgo is a result of bars processing, that does not happen in OnStateChange or DataLoaded, that happens in OnBarUpdate.

                  To start I would suggest using just OnBarClose so you can get used to how bars process and how your script works in general, for more advanced use cases you can then change to OneachTick or OnPriceChange which each have their own specific design requirements. A simple example of the print you had shown would look like this:


                  protected override vode OnBarUpdate()
                  {
                  if(CurrentBar < 1) return;
                  Print("EMA1: " + EMA(20)[0]);
                  Print("EMA2: " + EMA(20)[1]);​
                  }​
                  in the beginning, I use OneachTick as well as OnBarUpdate(). but , find there is delay even with OneachTick, sometmes, 5 seconds. So, switching to timer(),which means the exact the end of every minute, extremely accurate. then , the above problem appears, can not access the previous bar data.

                  Comment


                    #10
                    Hello steinberg123,

                    I would really suggest opening a new post for that issue specifically, using a timer is not suggested for any bar processing events. It sounds like there may be some misunderstandings with how scripts process which may need to be resolved before moving forward.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by NullPointStrategies, Yesterday, 05:17 AM
                    0 responses
                    65 views
                    0 likes
                    Last Post NullPointStrategies  
                    Started by argusthome, 03-08-2026, 10:06 AM
                    0 responses
                    139 views
                    0 likes
                    Last Post argusthome  
                    Started by NabilKhattabi, 03-06-2026, 11:18 AM
                    0 responses
                    75 views
                    0 likes
                    Last Post NabilKhattabi  
                    Started by Deep42, 03-06-2026, 12:28 AM
                    0 responses
                    45 views
                    0 likes
                    Last Post Deep42
                    by Deep42
                     
                    Started by TheRealMorford, 03-05-2026, 06:15 PM
                    0 responses
                    50 views
                    0 likes
                    Last Post TheRealMorford  
                    Working...
                    X