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

Help With DrawRegion

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

    Help With DrawRegion

    Hi, I'm new to indicator creation and I've been experimenting with the SampleDrawRegion addon from the Help/Support section. So far, I've been able to shade the region between the two Bollinger Bands and color the regions when the price of a current bar closes above or below the smaller Bollinger Band. What changes do I make to also shade instances where the high or low of a bar exceeds the upper or lower bound of a Bollinger, respectively? Any suggestions would be appreciated. Here is the code so far:



    protected override void OnBarUpdate()
    {
    // With the DrawRegion() method you can fill the space inbetween two DataSeries objects very easily.

    // This region fills in the space between the Upper Bollinger Band and the Middle Bollinger Band. The region extends from the first bar of the chart
    // till the last bar of the chart. It has a border color of black and is filled with blue on an opacity setting of 2. Opacity setting ranges from 0-10.
    // 0 being transparent and 10 being completely colored.


    Draw.Region(this, "Bollinger Upper Region", CurrentBar, 0, Bollinger(2.5, 25).Upper, Bollinger(2, 25).Upper, null, Brushes.DarkSlateGray, 50);

    // This region fills the space between the Lower Bollinger Band and the Middle Bollinger Band. It has the same attributes as the previous region, except
    // the blue fill color is darker in this one. If you wish to create a region without a border you can use this color: Color.Transparent


    Draw.Region(this,"Bollinger Lower Region", CurrentBar, 0, Bollinger(2, 25).Lower, Bollinger(2.5, 25).Lower, null, Brushes.DarkSlateGray, 50);

    // Besides filling inbetween two DataSeries objects we can also fill between a double value and a DataSeries.
    // This is demonstrated in the following code segment.

    // If the price closes above the upper bollinger band, color the price region above the bollinger band red.


    if (Bollinger(2, 25).Upper[0] < Close[0])
    {


    // In our string tag we use "+ CurrentBar" to ensure unique tag names for all our regions. If we did not have unique names each call
    // upon the tag would modify the existing DrawRegion() instead of coloring a new one.


    Draw.Region(this,"Upper Bollinger Broken" + CurrentBar, 1, 0, Bollinger(2.5, 25).Upper, Bollinger(2, 25).Upper, null, Brushes.Red, 100);
    }


    // If the price closes below the lower bollinger band, color the price region below the bollinger band red.

    else if (Bollinger(2, 25).Lower[0] > Close[0])
    {
    Draw.Region(this,"Lower Bollinger Broken" + CurrentBar, 1, 0, Bollinger(2.0, 25).Lower, Bollinger(2.5, 25).Lower, null, Brushes.Red, 100);
    }
    }


    [Range(1, int.MaxValue), NinjaScriptProperty]
    [Display(Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)]
    public int Period
    { get; set; }

    [Range(0, double.MaxValue), NinjaScriptProperty]
    [Display(Name = "NumStdDev", GroupName = "NinjaScriptParameters", Order = 1)]
    public double NumStdDev
    { get; set; }
    }
    }

    #2
    Hello Cicero,

    Thank you for your post.

    It looks like you're pretty on track to that with your logic that colors the bars if they close above or below the bands. To color it if the high or low crosses the bands, you'd want to do pretty much what you've done with the close, but check for whether High[0] is greater than the upper band or Low[0] is less than the lower. So something like this:

    else if (Bollinger(2, 25).Lower[0] > Low[0])
    {
    Draw.Region(this,"Lower Bollinger Broken by Low" + CurrentBar, 1, 0, Bollinger(2.0, 25).Lower, Bollinger(2.5, 25).Lower, null, Brushes.Red, 100);
    }
    }


    Keep in mind that you may want to check that the close isn't also lower than the bollinger and then instead color the region accordingly if it is.

    Please let us know if we may be of further assistance to you.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hi Kate,

      The logic for the the Low[0] works fine, and I've decided to replace it for the original Close[0] for the lower band. However, when I adapt the same for High[0] the entire code/indicator ceases to work. It doesn't seem to want to recognize High[0] for some reason. I am using Renko bars, so I don't know if that has anything to do with it.

      Comment


        #4
        Hello Cicero,

        Thank you for your reply.

        Does it not compile, or do you receive an error in the Log tab of the Control Center?

        Could you provide an example of the code you've tried that doesn't seem to be working?

        Thanks in advance; I look forward to assisting you further.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          if (Bollinger(2, 25).Upper[0] < High[0])
          {

          Draw.Region(this,"Upper Bollinger Broken by High" + CurrentBar, 1, 0, Bollinger(2.5, 25).Upper, Bollinger(2, 25).Upper, null, Brushes.Red, 100);
          }

          Comment


            #6
            Hello Cicero,

            Thank you for your reply.

            I don't think it's that code causing it, as I've added that to the test version of your code you provided above and that compiles and the indicator correctly paints the bars if the high breaches the Bollinger band. I've tested using several bar types including Renko and that seems to work fine for me.

            You didn't mention whether you receive an error or if you're just not seeing the bars being painted as you'd expect, can you clarify with a screenshot of what you're seeing that you wouldn't expect?

            Thanks in advance; I look forward to assisting you further.
            Kate W.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by bmartz, Today, 09:30 AM
            2 responses
            11 views
            0 likes
            Last Post bltdavid  
            Started by f.saeidi, Today, 11:02 AM
            1 response
            3 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by geotrades1, Today, 10:02 AM
            4 responses
            12 views
            0 likes
            Last Post geotrades1  
            Started by rajendrasubedi2023, Today, 09:50 AM
            3 responses
            16 views
            0 likes
            Last Post NinjaTrader_BrandonH  
            Started by lorem, Today, 09:18 AM
            2 responses
            11 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Working...
            X