Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Draw.Region function question.

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

    Draw.Region function question.

    Hi,

    I am trying to create a very simple indicator. It is compose of two SMAs (one short and on long). I need to color the region between these two SMAs depending on the trend of the SMAs.

    If the short SMA is > than the long SMA I want to color the region using a specific color. And if the short SMA is < than the long SMA I want to change the color of the region.

    I have tried the next code without success:

    protected override void OnBarUpdate()
    {
    if(CurrentBar < 50) return;

    SMAShort[0] = SMA(10)[0];
    SMALong[0] = SMA(50)[0];

    if (SMA(10)[0] > SMA(50)[0])
    Draw.Region(this, "tag1", CurrentBar, 0, ExampleDrawRegionSMAs().SMAShort, ExampleDrawRegionSMAs().SMALong, null, Brushes.Blue, 50);
    else
    Draw.Region(this, "tag2", CurrentBar, 0, ExampleDrawRegionSMAs().SMAShort, ExampleDrawRegionSMAs().SMALong, null, Brushes.Red, 50);

    }

    The region has always the same color.

    Could you tell me please, how to do this?

    Thank you in advance.

    #2
    Hi Plaket, thanks for your note.

    The parameters take a StarBarsAgo and an EndBarsAgo parameter, right now your code is set up to color all of the bars on the chart. You would need to find where the downtrend starts and draw the region from that starting point to the current bar. There are examples that use the OnRender method to do this as well, see this example from our colleague Jim:



    The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.

    Comment


      #3
      Thanks ChrisL,

      Unfortunately the code of that indicator is not simple. Please, do you have an easier example to accomplish this simple thing? Something for a new person at NinjaSciprt? Any help would be very welcome.

      Thanks

      Comment


        #4
        Hi Plaket, thanks for your reply.

        OnRender is used in this case because making multiple Draw.Region calls will cause performance problems in large workspaces. If Draw.Region is to be used the easiest way of highlighting a region a certain color would be to color only the current and second from current bars.

        e.g.

        Code:
        protected override void OnBarUpdate()
                {
                    if(CurrentBar < 1)
                        return; 
        
                    if (SMA(10)[0] > SMA(50)[0])
                    {
                        Draw.Region(this, "tag1" + CurrentBar, 1, 0, Bollinger(2, 14).Upper, Bollinger(2, 14).Lower, null, Brushes.Red, 50);
                    }
                    else
                    {
                        Draw.Region(this, "tag1" + CurrentBar, 1, 0, Bollinger(2, 14).Upper, Bollinger(2, 14).Lower, null, Brushes.Green, 50);
                    }
                }

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        646 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        367 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        107 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        569 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        573 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X