Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Shading oversold/overbought in rsi

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

    Shading oversold/overbought in rsi

    I was able to shade regions between two average in the price panel s using this script below, I am now trying to shade the regions in the overbought/oversold areas of the rsi in the indicator panel but I am not sure what is wrong here.

    HTML Code:
    if( CurrentBar < 2 )return;
    
    if (Calculate == Calculate.OnBarClose)
    _intCalculationIndex = 1;
    
    if (RSI2[0] > -30)
    {
    BreakOut = true;
    BreakDn = false;
    }
    else if (RSI2[0] < 30)
    {
    BreakOut = false;
    BreakDn = true;
    }
    
    
    
    if (IsFirstTickOfBar && BreakOut)
    {
    _intStartBar = CurrentBar;
    }
    if (IsFirstTickOfBar && BreakDn)
    {
    _intStartBar = CurrentBar;
    }
    
    if (this.BreakOut)
    {
    Draw.Region(this, "up"+ _intStartBar, CurrentBar - _intStartBar + 1, 0, RSI2, 30, null, Brushes.Red, 20);
    
    }
    else if (this.BreakDn)
    {
    Draw.Region(this, "down" + _intStartBar, CurrentBar - _intStartBar + 1, 0, RSI2, -30, null, Brushes.Green, 20);
    }

    Photo of what I'm trying to accomplish.:

    Click image for larger version  Name:	Screen_Capture_-_Jul_12__7_40_AM.png Views:	0 Size:	73.9 KB ID:	1163195

    #2
    Hello augustfay,

    Thanks for your post.

    When testing the code you shared, I am seeing compile errors related to the parameters being used for the Draw.Region() methods.

    See the attached screenshot of the compile errors. Are these the same errors you see occurring in your script?

    To resolve these compile errors, you would need to ensure that you are using the correct number of parameters and the correct type of parameters required by the Draw.Region() method. An example of the Draw.Region() syntax from the help guide documentation is as follows.

    Draw.Region(NinjaScriptBase owner, string tag, int startBarsAgo, int endBarsAgo, ISeries<double> series, double price, Brush areaBrush, int areaOpacity, int displacement = 0)

    See this help guide page for more information about Draw.Region(): https://ninjatrader.com/support/help...raw_region.htm

    Let us know if we may assist further.
    Attached Files
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hi Brandon, yes those are the same errors I am getting. I'm not sure where mine is wrong, I tried to follow the help guide but no matter what I do it doesn't work. Do you know where my error is? Thanks..

      Comment


        #4
        Hello augustfay,

        Thanks for your note.

        The compile errors state that argument 8 for Draw.Region() is currently set to a Brush and should be an int value. As we see in the Draw.Region() syntax below, argument 8 requires an int value for areaOpacity.

        Draw.Region(NinjaScriptBase owner, string tag, int startBarsAgo, int endBarsAgo, ISeries<double> series, double price, Brush areaBrush, int areaOpacity, int displacement = 0)

        In your code, a null value is being used for argument 7 and a Brush is used for argument 8. To resolve the compile errors, you would need to ensure that your Brush is used for argument 7 of Draw.Region() and an int value is used for argument 8.

        An example of what the code could look like is seen below. In the code below, we remove the 'null' value used for argument 7 so that the Brush is used instead. We also add an int value of 0 for the 'int areaOpacity' value.

        Draw.Region(this, "up"+ _intStartBar, CurrentBar - _intStartBar + 1, 0, RSI2, 30, Brushes.Red, 0, 20);

        Let us know if we may assist further.
        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

        Comment


          #5
          Much appreciated Brandon, it appears my count was off with the arguments. I'm glad to finally see an output.
          I simplified the code to: Draw.Region(this, "up", CurrentBar, 0, RSI2, 30, Brushes.Red,50,0)

          I'm not sure why, but the region does not terminate at the 30 mark but rather it shades the entire rsi, how would I stop it from doing so?

          Click image for larger version

Name:	Screen_Capture_-_Jul_13__12_15_PM.png
Views:	641
Size:	8.7 KB
ID:	1163359

          Comment


            #6
            Hello augustfay,

            Thanks for your note.

            To understand why the script is behaving as it is, such as drawing objects when expected, it is necessary to add prints to the script that print the values used for the logic of the script to understand how the script is evaluating.

            In the script add prints (outside of any conditions) that print the values of every variable used in every condition. Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

            Below is a link to a forum post that demonstrates using prints to understand behavior.
            https://ninjatrader.com/support/foru...121#post791121

            Below is a link to an example that uses Draw.Region().
            https://ninjatraderecosystem.com/use...acrossbuilder/

            Let us know if we may assist further.
            <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

            Comment


              #7
              Thanks Brandon, I am a bit unclear how adding these prints would help me?

              Comment


                #8
                Hello augustfay,

                Thanks for your note.

                Adding prints to your script will allow you to compare the values that are being used for calculations in your script. Seeing these values will allow you to understand how the indicator is behaving.

                Please see the example linked in the previous post regarding how to use Draw.Region() to color the region between indicator plots.

                Let us know if we may further assist.
                <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                Comment


                  #9
                  This is a bit confusing for me. So I'm trying to find a workaround. I couldn't get the prints to work right.
                  I have another question to try and get my workaround going. According to the help guide:
                  If you wanted to fill a region between a value (20 period simple moving average) and the upper edge of the chart, pass in an extreme value to the "y" parameter such as 1000000.

                  I did this and things work as expected, one color draws from the indicator to the top of the panel, and the other from the bottom of the panel to the indicator.

                  Draw.Region(this, "ugh", CurrentBar, 0, RSI2, 100, Brushes.Blue, 100,0);
                  Draw.Region(this, "ugh2", CurrentBar, 0, RSI2, -100, Brushes.Yellow, 100,0);

                  How could I make the region terminates at a specific y value? Like say i only want it to fill the region up to 0. Thanks.

                  Comment


                    #10
                    I figured something out and got it to work alright using:


                    if (BreakOut && (savedUBar > savedDBar))
                    {
                    Draw.Region(this, "Up" + savedUBar, CurrentBar - savedUBar + 2, 0, RSI1, 30, Brushes.Red, 80, 0);
                    }

                    if (BreakDn && (savedDBar > savedUBar))
                    {
                    Draw.Region(this, "Dwn" + savedDBar, CurrentBar - savedDBar + 2, 0, RSI1, -30, Brushes.Lime, 80, 0);

                    }

                    But now I'm stuck again with this issue (see in photo). Some of the region is being drawn into the middle area, I'm not sure how I should prevent that? Also still unclear on how to terminate things at a specific y value, if possible.


                    My bad-at-programing workaround was to just cover it with a highlight y region that is the same color as my chart background
                    Draw.RegionHighlightY(this, "block", true, 30, -30, Brushes.Transparent, Brushes.Black, 100);

                    and then use an EMA with the RSI going through as the input series so I can change the z order and still have the regions behind it. It sort of works, but I would appreciate any help on actually removing those inner regions, thank you.


                    Attached Files

                    Comment


                      #11
                      Another thing I find odd, even when blocking out the middle area using a region highlight y, I'm seeing some of the green color bleed out a bit (see photo). I have to make the highlight -31 instead of the preferred -30 to cover it, any idea what could be causing this?
                      Attached Files

                      Comment


                        #12
                        Hello augustfay,

                        Thanks for your note.

                        Please see the attached example script which demonstrates drawing a region between the RSI plot and a upper\lower line on the chart.

                        In the example script, when the RSI is above the upper line we call Draw.Region() to draw the region between the RSI plot and the upper line. When the RSI is below the lower line, we call Draw.Region() to draw the region between the RSI plot and the lower line.

                        Let us know if we may further assist you.
                        Attached Files
                        <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                        Comment


                          #13
                          Thanks a ton Brandon, that's much cleaner and less intensive than what I had come up with. I had trouble calling the plots for the lines in mine. I'm also not actually using the default rsi, but I now see what I need to do for my project. Much appreciated
                          Last edited by augustfay; 07-14-2021, 09:06 PM.

                          Comment


                            #14
                            One other thing though, the same problem persists in your logic as I mentioned in post #10. The regions are not fully shaded but rather have gaps in some areas. For example:



                            If I were to add a +1 into this line:

                            Draw.Region(this, "test30"+region30StartBar, (CurrentBar - region30StartBar+1), 0, RSIplot, 30.0,Brushes.Lime, 80, 0);

                            It fixes that issue and fills the entire region, but it creates a new problem and the region bleeds into the middle ares, as shown in my post #10, which then led to my solution of hiding it with a Draw.RegionHighlightY.

                            Is there another way that could achieve the full shaidng without hiding it with another region?



                            And my final question for you is - can you point me to information on how I could make it so the regions would only shade once the plot has crossed back into the middle area after crossing out? Would I have to somehow combine crossabove and crossbelow?

                            Thank you so much for your help.

                            Comment


                              #15
                              Hello augustfay,

                              Thanks for your note.

                              The reason you see a small gap is because this is where the plot crosses in between bars. Draw.Region() colors the data points that are above/below the line and where you see a gap is where the next bar is below/above that line. For example, if the RSI is above the line, the region will be colored. Then, when it crosses below the line on the next bar, you would see a small gap where the cross happens between bars.

                              You would need to have an intuitive way to see if the next bar crosses, see where the cross is pixel-wise, and then use that pixel coordinate to fill in the "corner" of the region. This could be accomplished using SharpDX methods. Please see the "FindIntersection" code section of the Ichimoku Cloud indicator linked below to see how this could be accomplished.

                              Ichimoku Cloud: https://ninjatraderecosystem.com/use...indicator-nt8/

                              To have the regions only color once the plot has crossed back into the middle area, you could add a condition that checks if the plot is greater than the line on the previous bar and checks if the plot is less than the line on the next (current) bar, followed by calling Draw.Region. This way, we would be checking if the plot would have been above the line on the previous bar and below the line on the next (current) bar before calling Draw.Region.

                              Let us know if we may assist further.

                              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.
                              Last edited by NinjaTrader_BrandonH; 07-15-2021, 09:29 AM.
                              <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              580 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              335 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              102 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              554 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              552 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X