Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

CurrentBar goes negative

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

    CurrentBar goes negative

    I have some print statements for Debugging, the output is CurrentBar, a barsAgo calculation, and a startBar for a condition. However occasionally the CurrentBar is off causing a brief misdrawing of my indicator. The CurrentBar should be 4350 NOT 869.

    Click image for larger version

Name:	image.png
Views:	93
Size:	36.1 KB
ID:	1342154

    #2
    How are you calculating the Current Bar? I would assume that there is some logic in your code causing this to set wrong, thus throwing everything off.

    Comment


      #3
      I'm not calculating CurrentBar, I am merely referencing CurrentBar which is built into NInjatrader. Although I mispoke Current Bar is not negative but the prior bar is going from 4349 on the previous bar to 869 on the current bar which causes my calculation for barsAgo to be negative.

      Click image for larger version

Name:	image.png
Views:	80
Size:	36.9 KB
ID:	1342160

      Comment


        #4
        How would the current bar jump from 4349 back to 869? How are the other prints calculating the barsAgo? It's obvious the startbar is still being kept track of and is set to the correct thing. Regardless, you will need to provide the code for anyone to figure out what is potentially wrong with it.

        Comment


          #5
          Code:
          if (startBarShort == -1)  // Determines if this is the first bar of the condition
          {
              startBarShort = CurrentBar;
              barsAgo = CurrentBar - startBarShort;
              highPrice = High[0];
              lowPrice = Low[0];
              
              Print(string.Format("Start bar set - barsAgo: {0}", barsAgo));
          }
          else // If not first bar of the condition update the barsAgo 
          {
              // Update high and low prices
              
              barsAgo = CurrentBar - startBarShort;
              highPrice = Math.Max(highPrice, High[0]);
              lowPrice = Math.Min(lowPrice, Low[0]);
              Print(string.Format("Continuation bar set: barsAgo: {0}", barsAgo));
              Print(string.Format("Current Bar: {0}", CurrentBar));
              Print(string.Format("Start Bar: {0}", startBarShort));
          }​

          Comment


            #6
            Ok. I don't see any reason why the CurrentBar would go backwards. Is there any other section in your code that would assign CurrentBar to something?

            Comment


              #7
              I think the issue is with startBarShort not being properly set.

              Comment


                #8
                Actually it had nothing to do with startBarShort. It has to do with using the stochastics. I created a proof of concept indicator to develop code to dynamically update and draw a rectangle based on price > or < vwap. This code generated a smooth uninterrupted dynamic rectangle. when I changed the condition to use the stochastics indicator the rectangle for the most part works with intermittent hiccups to CurrentBar causing temporary disruption to the draw updates. The following fix returned it to running smooth.

                FIX:
                Code:
                private double currentBar;
                
                protected override void OnBarUpdate()
                {
                    currentBar = BarsArray[0].Count;​
                
                    // Inidcator coded below
                }

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                0 responses
                574 views
                0 likes
                Last Post Geovanny Suaza  
                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                0 responses
                332 views
                1 like
                Last Post Geovanny Suaza  
                Started by Mindset, 02-09-2026, 11:44 AM
                0 responses
                101 views
                0 likes
                Last Post Mindset
                by Mindset
                 
                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                0 responses
                553 views
                1 like
                Last Post Geovanny Suaza  
                Started by RFrosty, 01-28-2026, 06:49 PM
                0 responses
                551 views
                1 like
                Last Post RFrosty
                by RFrosty
                 
                Working...
                X