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

DrawRegion for shading indicator Overbought and Oversold

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

    DrawRegion for shading indicator Overbought and Oversold

    I try to use DrawRegion for shading Stochastics' overbought and oversold area. I got the code compiled but the shading is in the chart panel, not in the indicator area as intended (see attached chart image), even though I coded Overlay = false;.

    My code is as follows:

    protected override void Initialize()
    {
    Add(new Plot(Color.Black, "Stochastics")); // [0]

    Plots[0].Pen.Width=2;

    Add(new Line(Color.Red, 80, "Upper"));
    Add(new Line(Color.Green, 20, "Lower"));
    Add(new Line(Color.Gray, 50, "Middle"));

    Lines[0].Pen.Width=2;
    Lines[1].Pen.Width=2;
    Lines[2].Pen.DashStyle = DashStyle.Dash;
    Lines[2].Pen.Width=1;


    CalculateOnBarClose = false;
    AutoScale = false;
    Overlay = false;

    }

    /// <summary>
    /// Calculates the indicator value(s) at the current index.
    /// </summary>
    protected override void OnBarUpdate()
    {

    Value.Set(Stochastics(periodD,periodK,smooth).K[0]);

    if (Value[0] > 80)
    {
    DrawRegion("Region", CurrentBar, 0, Stochastics(periodD, periodK, smooth).K, 80, Color.Empty, Color.Green, 6);
    }

    }

    This is a test code to shade the region of Stochastics > 80 in Green color.

    Any help in identifying the problem is greatly appreciated!

    Richard
    Attached Files

    #2
    Originally posted by Richard168 View Post
    I try to use DrawRegion for shading Stochastics' overbought and oversold area. I got the code compiled but the shading is in the chart panel, not in the indicator area as intended (see attached chart image), even though I coded Overlay = false;.

    My code is as follows:

    protected override void Initialize()
    {
    Add(new Plot(Color.Black, "Stochastics")); // [0]

    Plots[0].Pen.Width=2;

    Add(new Line(Color.Red, 80, "Upper"));
    Add(new Line(Color.Green, 20, "Lower"));
    Add(new Line(Color.Gray, 50, "Middle"));

    Lines[0].Pen.Width=2;
    Lines[1].Pen.Width=2;
    Lines[2].Pen.DashStyle = DashStyle.Dash;
    Lines[2].Pen.Width=1;


    CalculateOnBarClose = false;
    AutoScale = false;
    Overlay = false;

    }

    /// <summary>
    /// Calculates the indicator value(s) at the current index.
    /// </summary>
    protected override void OnBarUpdate()
    {

    Value.Set(Stochastics(periodD,periodK,smooth).K[0]);

    if (Value[0] > 80)
    {
    DrawRegion("Region", CurrentBar, 0, Stochastics(periodD, periodK, smooth).K, 80, Color.Empty, Color.Green, 6);
    }

    }

    This is a test code to shade the region of Stochastics > 80 in Green color.

    Any help in identifying the problem is greatly appreciated!

    Richard
    You need to specify DrawOnPricePanel as false.

    ref: http://ninjatrader.com/support/helpG...pricepanel.htm

    Comment


      #3
      Hello Richard168,

      Thanks for your post.

      As forum member koganam has advised, add the statement DrawOnPricePanel = false; to your initialize section.
      Paul H.NinjaTrader Customer Service

      Comment


        #4
        Hi Paul & koganam,

        Thank you for replying to my question.

        This addresses the problem of not shading in the indicator panel. However, I could not get DrawRegion() working 100%. It did not shade Red when Stochastics is above 80 and Green when it is below 20.

        Also, the plots are inconsistent (see attached images), sometimes both Stochastics > 80 and Stochastics < 20 are shaded Green, sometimes both Stochastics > 80 and Stochastics < 20 are shaded Red, and sometimes the entire Stochastics are shaded Red. and sometimes the entire Stochastics are shaded Red...

        The following is my code, I have to fudge it by shading it White between 80 and 20.

        protected override void Initialize()
        {
        Add(new Plot(Color.Black, "Stochastics")); // [0]
        Add(new Plot(Color.Red, "High")); // [1]
        Add(new Plot(Color.Green, "Low")); // [2]

        Plots[0].Pen.Width=2;

        Add(new Line(Color.Red, 80, "Upper"));
        Add(new Line(Color.Green, 20, "Lower"));
        Add(new Line(Color.Gray, 50, "Middle"));

        Lines[0].Pen.Width=2;
        Lines[1].Pen.Width=2;
        Lines[2].Pen.DashStyle = DashStyle.Dash;
        Lines[2].Pen.Width=1;


        CalculateOnBarClose = false;
        AutoScale = false;
        Overlay = false;
        DrawOnPricePanel = false;

        }

        /// <summary>
        /// Calculates the indicator value(s) at the current index.
        /// </summary>
        protected override void OnBarUpdate()
        {

        if (CurrentBar < 1) // Code added to address: Error on ploting indicator ... Index was outside the bounds of the array.
        {
        return;
        }

        Value.Set(Stochastics(periodD,periodK,smooth).K[0]);

        High.Set(80);
        Low.Set(20);

        if (Value[0] > 80)
        {
        DrawRegion("Region1", CurrentBar, 0, Stochastics(periodD, periodK, smooth).K, High, Color.Empty, Color.Red, 6);
        }


        else if (Value[0] < 80 && Value[0] > 20 )
        {
        DrawRegion("Region2", CurrentBar, 0, High, Low, Color.Empty, Color.White, 10);
        }



        else if (Value[0] < 20)
        {
        DrawRegion("Region3", CurrentBar, 0, Stochastics(periodD, periodK, smooth).K, Low, Color.Empty, Color.Green, 6);
        }
        else
        {
        return;
        }

        }

        I'd appreciate it very much if you can direct me to identify the problem in using the DrawRegion() method, so that I can fix the problem and better understand the DrawRegion() method.

        Richard
        Attached Files

        Comment


          #5
          Hello Richard168,

          Thanks for your reply and posting your code.

          You have 3 draw objects labeled Region1, Region 2 and Region3 that are drawing 3 regions from the very first bar 0 to the CurrentBar. Depending on which one drew last is likely what you are seeing in terms of layers.

          In each Drawregion you are asking to not only draw from the beginning but to color between the K line and either the low or the high so on each bar historically it will do just that. What you want to do is to draw a region only when those conditions are true which regrettably means creating many more draw objects. For example on 6 bars in a row the K line is above 80, that would be one draw object that starts and stops on the first bar of the 6 bar sequence and ends on the 6th bar.

          So that would mean significantly increasing your logic controls using bools and saving bar numbers so that you can draw only what and where you need to.

          For example (using bits of your code)

          if (Value[0] > 80)
          {
          if (newHighregion)
          {
          newHighRegion = false;
          newLowRegion = true; // set bool so we create a new low region when we get there
          startBarHigh = CurrentBar; // save the first bar in this region
          }
          DrawRegion("Region1"+startBarHigh, CurrentBar - startBarHigh, 0, Stochastics(periodD, periodK, smooth).K, High, Color.Empty, Color.Red, 6);
          }
          }

          In the above example the bool newHighRegion would need to be set true (you would set it true when setting some other region), we (re)set the other regions bool to be true, we save the first bar number of this region. Next we draw the region from the CurrentBar to the start bar and name the region Region1+startBarHigh so that we are creating a new named region. If we have subsequent bars above 80, the same start bar is used and the region extends until the last bar that meets Value[0]>80 is true.

          You would have similar logic for the other 2 regions but using separate variables as needed. Actually, I think you don't need the middle region so that would save some effort.

          Drawing from one bar to the next means your region shading may end before a cross of the line or after. You may be able to clean that up by using the date/time DrawRegion() overload but that leads to even more complexity for your coding.

          Edit: 6/2/17 - Corrected my draw region code example, moved CurrentBar- startBar to starting bars ago syntax position and changed CurrentBar to 0 as the bars ago value to draw to.
          Last edited by NinjaTrader_PaulH; 06-02-2017, 06:57 AM.
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Thanks Paul for the quick reply. I finally got the code working 100% by adding the Boolean state and a minor change to the DrawRegion() parameters!

            if (Value[0] > 80)
            {
            if (newHighRegion)
            {
            newHighRegion = false;
            newLowRegion = true; // set bool so we create a new low region when we get there
            DrawRegion("Region1"+CurrentBar, 1, 0, Stochastics(periodD, periodK, smooth).K, High, Color.Empty, Color.Red, 6);

            }
            }

            BTW, I still need to white out the region between 80 and 20.

            Thanks again for your help. Much appreciated!

            Richard

            Comment


              #7
              Hello,
              I'm having a similar issue of the region filling the entire void.
              I tried to define the starting and ending points for the region to draw, but it is still filling in the entire indicator.

              DrawOnPricePanel = false;
              if(CrossAbove(DM(Close,14).DiPlus, DM(Close,14).DiMinus,1))
              {
              upward_starting_bar = CurrentBar;
              }
              if(up_line_fast > down_line_fast)
              {

              DrawRegion("Upward move", CurrentBar, CurrentBar - upward_starting_bar, UpLineFast, DownLineFast, Color.Empty, Color.Blue, 2 );
              }
              if(CrossAbove(DM(Close,14).DiMinus, DM(Close,14).DiPlus,1)))
              {
              downward_starting_bar = CurrentBar;
              }
              if(up_line_fast < down_line_fast)
              {

              DrawRegion("Downward move",CurrentBar, CurrentBar - downward_starting_bar , DownLineFast, UpLineFast, Color.Empty, Color.Red, 2);
              }
              Can someone tell me what I did wrong?
              Thanks.

              Comment


                #8
                Hello CaptainAmericaXX,

                Thanks for your post.

                You will want to create a unique tag name for your draw regions and you will want to replace CurrentBar with 0. For example:

                DrawRegion("Upward move"+upward_starting_bar, CurrentBar - upward_starting_bar, 0, UpLineFast, DownLineFast, Color.Empty, Color.Blue, 2 );

                In the example above, each time you switch sides you will create a new region named "Upward move"+upward_starting_bar it will draw starting from the starting bar via the bars ago of CurrentBar - upward_starting_bar and will draw to the current bar via bars ago of 0. Note that I moved CurrentBar - upward_starting_bar to the starting point location in the syntax of the DrawRegion().

                In looking at your code I realized my example in post#5 was incorrect regarding the use of CurrentBar at the starting point. The starting point is supposed to be a "bars ago" value and using CurrentBar will actually draw the region from the very first bar of data. I will edit/correct my example in post 5.
                Paul H.NinjaTrader Customer Service

                Comment


                  #9
                  Shading for T3 Indicators

                  Good evening guys. My first post on this forum as I'm new to NT. I also use tradestation and I'm a bit more familiar with their basic programming code.
                  Anyways, I found this indicator in another forum and I would like to include, or add, the T3 Mov Average option that comes standard with NT.
                  If anybody can help I will appreciate it.
                  Here is the code:

                  Well, I couldn't attach the code, so here is the zip file. The name of the indicator is the DoubleMA.


                  Thanks in advance
                  Richard
                  Attached Files

                  Comment


                    #10
                    Hello rdelrisc,

                    Thanks for your post and welcome to the forums.

                    For your understanding, in the support department at NinjaTrader we do not create, debug, or modify code for our clients. This is so that we can maintain a high level of service for all of our clients.

                    If you would like to take on learning NinjaScript, we have a fully documented help guide which will help you get started. You will find language references to all of the methods and functions you will be using. You will also see a tutorial section which will help you create your first indicator and get you started with some of these concepts.
                    a link to our help guide can be found below

                    NT7: https://ninjatrader.com/support/help..._resources.htm

                    This video introducing the NinjaScript editor is an excellent resource,

                    NT7: https://www.youtube.com/watch?v=K8v_...56536A44DD7105


                    You will find reference samples online as well as some tips and tricks for both indicators and strategies:




                    These samples can be downloaded, installed and modified from NinjaTrader and hopefully serve as a good base for your custom works.

                    There is also a growing library of user submitted custom indicators (100+) and strategies that can be downloaded from our support form. Please look in the NinjaScript file sharing section of our support forum as you may find what you are looking for there:



                    Finally, the following link is to our help guide with an alphabetical reference list to all supported methods, properties, and objects that are used in NinjaScript.

                    NT7: http://ninjatrader.com/support/helpG..._reference.htm

                    Also, for future reference please use the "new thread" button to create a new thread as this post is off the topic of this thread. The "new thread" button shows at the top left of the catagory (see attached) , thanks for understanding.
                    Attached Files
                    Paul H.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Christopher_R, Today, 12:29 AM
                    0 responses
                    9 views
                    0 likes
                    Last Post Christopher_R  
                    Started by sidlercom80, 10-28-2023, 08:49 AM
                    166 responses
                    2,235 views
                    0 likes
                    Last Post sidlercom80  
                    Started by thread, Yesterday, 11:58 PM
                    0 responses
                    3 views
                    0 likes
                    Last Post thread
                    by thread
                     
                    Started by jclose, Yesterday, 09:37 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post jclose
                    by jclose
                     
                    Started by WeyldFalcon, 08-07-2020, 06:13 AM
                    10 responses
                    1,415 views
                    0 likes
                    Last Post Traderontheroad  
                    Working...
                    X