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

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.
    Chris L.NinjaTrader Customer Service

    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);
                    }
                }
        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by benmarkal, Yesterday, 12:52 PM
        3 responses
        23 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by helpwanted, Today, 03:06 AM
        1 response
        19 views
        0 likes
        Last Post sarafuenonly123  
        Started by Brevo, Today, 01:45 AM
        0 responses
        11 views
        0 likes
        Last Post Brevo
        by Brevo
         
        Started by pvincent, 06-23-2022, 12:53 PM
        14 responses
        244 views
        0 likes
        Last Post Nyman
        by Nyman
         
        Started by TraderG23, 12-08-2023, 07:56 AM
        9 responses
        388 views
        1 like
        Last Post Gavini
        by Gavini
         
        Working...
        X