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

My DrawRectangle is drawing multiple times on the same area

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

    My DrawRectangle is drawing multiple times on the same area

    I have some code that is only drawing a rectangle during a certain daily hour range, such that it covers a five hour area from 6am ET to 11am ET on a 5min chart. Here's how it's working. It tests for the 11th hour, and draws back 59 bars (assumes a 5min chart as that's all I use).

    The problem is it's drawing hundreds of boxes on every date, as if it's drawing that box for every instance in the series that matches the time criteria, but on every instance it draws all the instances before it on top of each other. If I remove the unique barTag for the draw call, it will draw only once, but only for the most recent time period, and none before it.

    What am I doing wrong?
    Code:
       protected override void OnBarUpdate()
            {
    			if (Time[-1].Hour == 10)
    			{
    				string barTag = CurrentBar.ToString();
    			
    				DrawRectangle(barTag, true, 59, Open[59] - TickSize, -1, Open[-1] + TickSize, Color.PaleGreen, Color.PaleGreen, 2);
    
    }
    }
    Last edited by littleredwoolven; 09-27-2015, 09:39 AM.

    #2
    Originally posted by littleredwoolven View Post
    I have some code that is only drawing a rectangle during a certain daily hour range, such that it covers a five hour area from 6am ET to 11am ET on a 5min chart. Here's how it's working. It tests for the 11th hour, and draws back 59 bars (assumes a 5min chart as that's all I use).

    The problem is it's drawing hundreds of boxes on every date, as if it's drawing that box for every instance in the series that matches the time criteria, but on every instance it draws all the instances before it on top of each other. If I remove the unique barTag for the draw call, it will draw only once, but only for the most recent time period, and none before it.

    What am I doing wrong?
    Code:
       protected override void OnBarUpdate()
            {
    			if (Time[-1].Hour == 10)
    			{
    				string barTag = CurrentBar.ToString();
    			
    				DrawRectangle(barTag, true, 59, Open[59] - TickSize, -1, Open[-1] + TickSize, Color.PaleGreen, Color.PaleGreen, 2);
    
    }
    }
    What are the tags on those many object. Just check if a few are different and let us know.

    Comment


      #3
      Originally posted by koganam View Post
      What are the tags on those many object. Just check if a few are different and let us know.
      How do I check? All I knownis that when I create a new tag for each update I've tried using currentbar.tostring and date time.now and it creates all the drawing repeatedly on each matching interval. When I use the same tag it only creates it once (expected I believe).

      Its as if its evaluating the entire time series on every bar and painting every matching time period. I thought NJ scripts operate from the context of the current bar.

      Comment


        #4
        Hello littleredwoolven,

        Thank you for writing in.
        I am not sure exactly what you are trying to accomplish. If you are using a 5 minute data series with CalculateOnBarClose = true, OnBarUpdate will be called 12 times per hour. I have attached the result that I get when I run your code where 12 rectangles are drawn. If you are using a 5 minute data series (or any data series at all) with CalculateOnBarClose = false, OnBarUpdate can be called an infinite amount of times per hour.
        If you just want one rectangle to be drawn, you could try something like the following:
        Code:
        #region Variables
        private bool canDraw = true;
        #endregion
        protected override void OnBarUpdate()
        {
        if (Time[-1].Hour == 10 && canDraw)
        {
        	string barTag = CurrentBar.ToString();
        
        	DrawRectangle(barTag, true, 59, Open[59] - TickSize, -1, Open[-1] + TickSize, Color.PaleGreen, Color.PaleGreen, 2);
        	canDraw = false;
        
        } else if(Time[-1].Hour == 11)
        {
        	canDraw = true;
        }
        }
        The above code would give you the first rectangle that was drawn only.

        If you can elaborate further on exactly what you are trying to accomplish, I will be happy to assist further.

        Thank you in advance.
        Attached Files
        Last edited by NinjaTrader_MichaelM; 09-27-2015, 12:55 PM.
        Michael M.NinjaTrader Quality Assurance

        Comment


          #5
          I thought I replied but don't see it here. All I'm looking to do is paint that rectangle one time for every 6-11am et period. I dont know why it wants to paint all these other ones but they seem to be from prior time periods, stacked on top of each other (when using a unique tag), or only painting one time period and ignoring all those before it (when reusing the tag, and I know this is expected).

          I haven't had a chance to test your code as I'm on the road but does it make sense now?

          Comment


            #6
            Hello littleredwoolven,

            Thank you for clarifying. Here is an updated version of the code which draws a rectangle from 6:00 to 11:00:
            Code:
            if (Time[-1].Hour == 11 && canDraw)
            {
            	string barTag = CurrentBar.ToString();
            
            	DrawRectangle(barTag, true, 59, Open[59] - TickSize, -1, Open[-1] + TickSize, Color.PaleGreen, Color.PaleGreen, 2);
            	canDraw = false;
            
            } else if(Time[-1].Hour == 12)
            {
            	canDraw = true;
            }
            Please let me know if I may be of further assistance.
            Michael M.NinjaTrader Quality Assurance

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by usazencort, Today, 01:16 AM
            0 responses
            1 view
            0 likes
            Last Post usazencort  
            Started by kaywai, 09-01-2023, 08:44 PM
            5 responses
            603 views
            0 likes
            Last Post NinjaTrader_Jason  
            Started by xiinteractive, 04-09-2024, 08:08 AM
            6 responses
            23 views
            0 likes
            Last Post xiinteractive  
            Started by Pattontje, Yesterday, 02:10 PM
            2 responses
            22 views
            0 likes
            Last Post Pattontje  
            Started by flybuzz, 04-21-2024, 04:07 PM
            17 responses
            230 views
            0 likes
            Last Post TradingLoss  
            Working...
            X