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

How do I draw a rectangle at the OPEN of a minute based bar?

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

    How do I draw a rectangle at the OPEN of a minute based bar?

    I am trying to draw a rectangle at the OPEN of a 15 minutes bar highlighting the gap between the close of the previous bar, and the open of the current bar.
    My code seems to work for all previous bars - HOWEVER, I cannot seem to be able to call the draw function WHEN the bard is OPENED. It seems to draw after the bar is closed - which is too late for me.

    Code:
    protected override void Initialize()
    {
            Overlay				= true;
    	CalculateOnBarClose = false;
    }
    
    protected override void OnBarUpdate()
    {
            if (CurrentBar < 1)
                    return;
    			
            if (Open[0] != Close[1])
            {
                    DrawRectangle("Region"+ CurrentBar.ToString(), ......); //This section does the right thing.
            }
    }
    The problem for me is that the rectangles are drawn at the CLOSE of the current bar, instead of the OPEN of the current bar.

    What can I do to fix it?

    Thank you.

    #2
    Hello NBKing,

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

    I am not seeing the same item on my end unless the Open[0] is the same as Close[1]. Can you provide a screenshot of this on your end on the current bar?

    Comment


      #3
      Originally posted by NBKing View Post
      I am trying to draw a rectangle at the OPEN of a 15 minutes bar highlighting the gap between the close of the previous bar, and the open of the current bar.
      My code seems to work for all previous bars - HOWEVER, I cannot seem to be able to call the draw function WHEN the bard is OPENED. It seems to draw after the bar is closed - which is too late for me.

      Code:
      protected override void Initialize()
      {
              Overlay                = true;
          CalculateOnBarClose = false;
      }
      
      protected override void OnBarUpdate()
      {
              if (CurrentBar < 1)
                      return;
                  
              if (Open[0] != Close[1])
              {
                      DrawRectangle("Region"+ CurrentBar.ToString(), ......); //This section does the right thing.
              }
      }
      The problem for me is that the rectangles are drawn at the CLOSE of the current bar, instead of the OPEN of the current bar.

      What can I do to fix it?

      Thank you.
      With the way that you have that coded, it will draw on the Open. If you seem to be seeing something else, it has to be a misinterpretation. The only possibility that I can see at this time would be on how you are using DrawRectangle(). Would you care to show the statement as you are using it?

      Comment


        #4
        Details

        Here is the code in it's full state.
        I have also enclosed a screen shot - of the current bar.
        You will notice how the previous bars shows the rectangle correctly.
        However, the "live" bar, with the open at a different spot than the close, does not show the rectangle.

        Code:
            public class eGap : Indicator
            {
        			private int rOpacity = 2;
        			private double rLow;
        			private double rHigh;
        			private Color rColor;
                protected override void Initialize()
                {
                    Overlay				= true;
        			CalculateOnBarClose = false;
                }
        
                protected override void OnBarUpdate()
                {
        			if (CurrentBar < 1)
        				return;
        //			if( ! FirstTickOfBar)
        //				return;
        			
        			if (Open[0] != Close[1])
        			{
        				if (Open [0] > Close[1])
        				{
        					rLow=Close[1];
        					rHigh=Open[0];
        				}
        				else
        				{
        					rHigh=Close[1];
        					rLow=Open[0];
        				}
        				
        				rColor = Color.White;
        				
        				DrawRectangle("Region1"+ CurrentBar.ToString(), false, 1, rLow, 0, rHigh, Color.Empty, rColor, rOpacity);
        				
        			}
                }
        Thank you for looking into this.
        Attached Files

        Comment


          #5
          Originally posted by NBKing View Post
          Here is the code in it's full state.
          I have also enclosed a screen shot - of the current bar.
          You will notice how the previous bars shows the rectangle correctly.
          However, the "live" bar, with the open at a different spot than the close, does not show the rectangle.

          Code:
              public class eGap : Indicator
              {
                      private int rOpacity = 2;
                      private double rLow;
                      private double rHigh;
                      private Color rColor;
                  protected override void Initialize()
                  {
                      Overlay                = true;
                      CalculateOnBarClose = false;
                  }
          
                  protected override void OnBarUpdate()
                  {
                      if (CurrentBar < 1)
                          return;
          //            if( ! FirstTickOfBar)
          //                return;
                      
                      if (Open[0] != Close[1])
                      {
                          if (Open [0] > Close[1])
                          {
                              rLow=Close[1];
                              rHigh=Open[0];
                          }
                          else
                          {
                              rHigh=Close[1];
                              rLow=Open[0];
                          }
                          
                          rColor = Color.White;
                          
                          DrawRectangle("Region1"+ CurrentBar.ToString(), false, 1, rLow, 0, rHigh, Color.Empty, rColor, rOpacity);
                          
                      }
                  }
          Thank you for looking into this.
          Is your picture from a live chart, or a backtest?

          Comment


            #6
            Live and Market Replay are the same

            The previous picture was from market replay.
            The picture provided is from live data.
            They both exhibit the same problem.
            Attached Files

            Comment


              #7
              Hello NBKing,

              Thank you for your response.

              Using your code I am not seeing the same behavior.

              Can you advise if there are any error messages listed in the Log tab of the NinjaTrader Control Center?

              In addition, even though the code sets CalculateOnBarClose to False, if you right click in the chart and select Indicators > is the indicator set to CalculateOnBarClose = True?

              Comment


                #8
                CalculateOnBarClose setting on the indicator was true

                NinjaTrader_PatrickH - THANK YOU.

                You nailed it - the CalculateOnBarClose setting on the indicator was "true".
                I set it to false - and everything is working now.

                Thank you again!!!

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by xiinteractive, 04-09-2024, 08:08 AM
                5 responses
                13 views
                0 likes
                Last Post NinjaTrader_Erick  
                Started by swestendorf, Today, 11:14 AM
                2 responses
                6 views
                0 likes
                Last Post NinjaTrader_Kimberly  
                Started by Mupulen, Today, 11:26 AM
                0 responses
                2 views
                0 likes
                Last Post Mupulen
                by Mupulen
                 
                Started by Sparkyboy, Today, 10:57 AM
                1 response
                6 views
                0 likes
                Last Post NinjaTrader_Jesse  
                Started by TheMarlin801, 10-13-2020, 01:40 AM
                21 responses
                3,918 views
                0 likes
                Last Post Bidder
                by Bidder
                 
                Working...
                X