Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Accessing data points from EMA series for previous bars

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

    Accessing data points from EMA series for previous bars

    Hi, I am new to ninja, and am writing an indicator that plots some EMAs and changes their colour based on where they are in relation to each other .. all working fine .. I then want to colour some of the bars with barbrush which works fine for the current bar in realtime, but, when the indicator reloads (if I make a code change and reload the script) I lose the bar colours that have accumulated over the time the indicator has been on the chart .. do you have any examples I can look at where you apply the barbrush when the indicator loads for previous bars ?

    many thanks in advance

    #2
    Hello 12tkram,

    Welcome to the NinjaTrader support forum.

    How are you currently using the BarBrush in your code?

    The BarBrush should be able to work historically meaning if this was called for a condition in historical, reloading the script should allow for your conditions to color bars as it progresses. Depending on how your script is coded that may relate to seeing this only work in realtime.

    You can also look into BarBrushes https://ninjatrader.com/support/help...htsub=barbrush

    I look forward to being of further assistance.

    Comment


      #3
      thanks, you probably need to see the code to see where I am going wrong .. can I dm it to you ? can't paste it as its for a strat that isn't mine .. once we find the issue I can post some useful snippets into the thread for other people with similar issues etc ..

      Comment


        #4
        does that sound ok ?

        Comment


          #5
          Hello 12tkram,

          Yes I would likely need more context on the problem as there are not enough details present. We are not able to work through private message but you can upload attachments to the forum or paste text. If the code is not currently in a state that you can share it, I would suggest making a more specific example of the problem before continuing. Our support is unable to debug scripts for you but we can observe your code to provide insight in contrast to that code.

          Rather than trying to debug the existing code I would likely suggest trying something more simple first to use to compare.

          To make a more simple test I would suggest doing the following:
          Create an indicator.
          Create a basic condition like: if(Close[0] > Open[0])
          Then use BarBrush:

          Code:
          protected override void OnBarUpdate()
                  {
                      if(CurrentBar < 1) return;
                      if(Close[0] > Open[1]) 
                      {
                          BarBrush = Brushes.Red; 
                      }
          
                  }
          This type of logic does work in historical so reloading the chart should produce the same results. Now comparing this simplistic use with your custom use, what is different? Where is your code located in contrast to the working sample? Are parts of your condition requiring realtime or live data which is not present during historical processing? Is this happening from outside OnBarUpdate?

          These type of details could help determine why this is not working. For this to work as you expect it would need to be called from OnBarUpdate during the historical processing for the bars you wanted to color.

          In some cases if you are using realtime data or have to wait to process something, you may need to use BarBrushes and set BarsAgo brushes instead of setting the brush as you work forward.


          I look forward to being of further assistance.

          Comment


            #6
            k, so .. for example .. if(Close[1]<EMA[1]&&Close[2]<EMA[2]) // do some stuff .. I am happy with the bar colour change .. it just does not work for any previous bars on the chart, works fine for bars on the chart in realtime as those bars close ..

            Comment


              #7
              where EMA represents say the ema(20) of the current chart (no shift), so EMA[0] would be current bar[0], EMA[1] would be bar[1] etc ..

              Comment


                #8
                Hello 12tkram,

                My suggestion at this point would be the same, you will need to find what is preventing this from drawing during historical. Based on your description it sounds like you have either used this in a place which only sees realtime data or you are not doing this during historical bars.

                In the last post I provided a simple sample which can draw on historical bars to test with. I modified that sample in contrast to what you said you are doing:
                Code:
                protected override void OnBarUpdate()
                {
                    if(CurrentBar < 2) return;
                    if(Close[0] < EMA(20)[1] && Close[2] < EMA(20)[2]) 
                    {
                        BarBrush = Brushes.Red; 
                    }
                
                }
                Again this type of logic will work in historical so now you would need to review the items that I mentioned to compare and contrast with your existing code. In your code are you using any conditions to check for realtime or are you doing this check for all historical bars? How is your syntax different from the simple test which works?


                I look forward to being of further assistance.

                Comment


                  #9
                  so .. if(State == State.Realtime) // it works .. if not, then I get the following error .. "Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart"

                  I am assuming thats just a generic error as I am not specifically accessed barsAgo 5 or 4 .. 1 or 2 yes, 4 or 5 no ..

                  Comment


                    #10
                    k, will give that a go

                    Comment


                      #11
                      Hello 12tkram,

                      Yes if you have a state check that checks for realtime that is likely the specific reason you cannot see this working historically.

                      The error you are seeing is a generic error however it relates to a specific situation. You are using data before it is available, and actually the sample I provided can produce this error if you remove the CurrentBar check:

                      Code:
                      if(CurrentBar < 2) return;
                      If the case is that you put in the State check to avoid the error, you may just need to replace the State check with a CurrentBar check so it can work historically. If you use a maximum of [2] BarsAgo, the condition would use 2. Otherwise you would replace 2 with the maximum BarsAgo you use in code:

                      Code:
                       if(CurrentBar < 2) return;
                      
                      //remainder of your logic

                      I look forward to being of further assistance.

                      Comment


                        #12
                        thanks for your feedback, seems like that could be the issue, I will check it out .. thanks again ..

                        Comment


                          #13
                          yup, that was it .. great stuff .. many thanks

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          605 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          351 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          105 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          560 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          561 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X