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

Generic Indicator Overlay on price

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

    Generic Indicator Overlay on price

    I've altered the Plot method originally from user 'Josh' s Stochastic and RSI Overlay code, to be more generic. Just add the following code to your favorite indicator to overlay it on top of price

    //[Gui.Design.DisplayName(".Test")]
    //Add the following method under the 'NinjaTrader.Indicator' namespace
    //to override the 'Plot' method to draw the indicator overlayed on the price chart
    public override void Plot(Graphics graphics, Rectangle bounds, double min, double max)
    {
    // Add the following variables to Initialize() method
    // AutoScale = false;
    // Overlay = true;
    //
    // The following variables can be moved out to '#region Variables'
    // and corresponding '#region Properties' entries added to make these
    // variables user selectable from the indicator properties sheet
    // Example: for scaleHigh:
    //
    // Delete the scaleHigh variable from this routine.
    // In '#region Variables' add the following:
    // private double scaleHigh = 100;
    //
    // In '#region Properties' add the following:
    // [Description("Scale High")]
    // [Category("Parameters")]
    // [Gui.Design.DisplayName("Scale High")]
    // public double Scale_High
    // {
    // get { return scaleHigh; }
    // set { scaleHigh = value; }
    // }
    bool drawscale = true; // Limited scale marks
    double scaleHigh = 200; // Max value of indicator (100 for Stochastic, +400 for CCI, etc.)
    double scaleLow = -200; // Min value of indicator (0 for Stochastic, -400 for CCI, etc.)
    double upperline = 175; // Optional horizontal line 1 (80 for Stochastic)
    double lowerline = -175; // Optional horizontal line 2 (20 for Stochastic)
    Color uppercolor = Color.Green; // Color for line 1
    Color lowercolor = Color.Green; // Color for line 2

    //Draw Scale
    StringFormat stringFormat = new StringFormat();
    SolidBrush textBrush = new SolidBrush(Color.Black);
    System.Drawing.Font textFont = new Font("Arial", 8);
    double scaleRange = (scaleHigh - scaleLow);
    double scaleShift = ( scaleLow < 0 ) ? -scaleLow : 0;
    if(drawscale)
    {
    //Upper line
    double line = ((upperline+scaleShift)/scaleRange)*100;
    graphics.DrawLine(new Pen(uppercolor,2),bounds.X,(float)((bounds.Bottom-bounds.Y)*(1-((double)line/100))),bounds.X+bounds.Width,(float)((bounds.Botto m-bounds.Y)*(1-((double)line/100))));
    graphics.DrawString(upperline.ToString(), textFont, textBrush, bounds.X, (float)((bounds.Bottom-bounds.Y)*(1-((double)line/100))),stringFormat);

    //1/4 up the scale
    line = (scaleRange*.75)+scaleLow;
    graphics.DrawString(line.ToString(), textFont, textBrush, bounds.X, (float)((bounds.Bottom-bounds.Y)*(1-0.75)),stringFormat);

    //Center Line
    line = ((scaleRange/2)+scaleLow)*100;
    graphics.DrawLine(new Pen(Color.DarkGray,2),bounds.X,(float)((bounds.Bot tom-bounds.Y)*0.50),bounds.X+bounds.Width,(float)((bou nds.Bottom-bounds.Y)*0.50));
    graphics.DrawString(((scaleRange/2)+scaleLow).ToString(), textFont, textBrush, bounds.X, (float)((bounds.Bottom-bounds.Y)*(1-0.50)),stringFormat);

    //3/4 up the scale
    line = (scaleRange*.25)+scaleLow;
    graphics.DrawString(line.ToString(), textFont, textBrush, bounds.X, (float)((bounds.Bottom-bounds.Y)*(1-0.25)),stringFormat);

    //Lower line
    line = ((lowerline+scaleShift)/scaleRange)*100;
    graphics.DrawLine(new Pen(lowercolor,2),bounds.X,(float)((bounds.Bottom-bounds.Y)*(1-((double)line/100))),bounds.X+bounds.Width,(float)((bounds.Botto m-bounds.Y)*(1-((double)line/100))));
    graphics.DrawString(lowerline.ToString(), textFont, textBrush, bounds.X, (float)((bounds.Bottom-bounds.Y)*(1-((double)line/100))),stringFormat);
    }

    //Plot Indicator
    int barWidth = ChartControl.ChartStyle.GetBarPaintWidth(ChartCont rol.BarWidth);
    SmoothingMode oldSmoothingMode = graphics.SmoothingMode;
    GraphicsPath dpath = new GraphicsPath();
    int lastX = -1;
    int lastY = -1;
    DataSeries series = (DataSeries) Values[0];
    double seriesvalue = 0;
    Gui.Chart.Plot plot = Plots[0];
    for(int count = 0; count < ChartControl.BarsPainted; count++)
    {
    int idx = ChartControl.LastBarPainted - ChartControl.BarsPainted + 1 + count;
    if (idx < 0 || idx >= Input.Count || (!ChartControl.ShowBarsRequired && idx < BarsRequired))
    continue;

    seriesvalue = ((series.Get(idx))+scaleShift)/scaleRange;
    if (seriesvalue == 0)
    continue;

    int x = (int) (ChartControl.CanvasRight - ChartControl.BarMarginRight - barWidth / 2 - (ChartControl.BarsPainted - 1) * ChartControl.BarSpace + count * ChartControl.BarSpace) + 1;
    int y = (int) ((bounds.Bottom-bounds.Y)*(1-seriesvalue));

    if (lastX >= 0)
    {
    dpath.AddLine(lastX - plot.Pen.Width / 2, lastY, x - plot.Pen.Width / 2, y);
    }

    lastX = x;
    lastY = y;
    }
    dpath.Reverse();

    graphics.SmoothingMode = SmoothingMode.AntiAlias;
    graphics.DrawPath(Plots[0].Pen, dpath);
    graphics.SmoothingMode = oldSmoothingMode;
    }

    #2
    Awesome monpere. Thanks!
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Excellent! Thank you!

      Comment


        #4
        Hmmm...can you give us an example of exactly where to paste your code in? I've been unable to find the right location...keep getting compile errors.

        Comment


          #5
          It would go outside of the OnBarUpdate(), but inside the Indicator namespace.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            Hmmm...still having trouble. Can someone check the posted code to see if it's missing a "}"...or has too many of them perhaps? If not, then can someone post a test indicator that has this new code in there correctly? Thanks!


            Originally posted by Josh View Post
            It would go outside of the OnBarUpdate(), but inside the Indicator namespace.

            Comment


              #7
              What you probably experienced was some weirdness in the formatting when you copy and paste the code. Extra white space was added between variable names so that caused errors.
              Attached Files
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Hi Josh,
                Is this a possible starting area to create a MACD Line overlay on price chart indicator?
                ie, instead of having the "Diff" option set to Histogram , instead it is selected to Line to represent the value above and below the horizontal zero line ?
                Thanks
                Michael b

                Comment


                  #9
                  For sure MichaelB, but this is beyond the level of support we can offer. Feel free to play around with the code though. I believe that is the reason why monpere released it .
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi Josh,
                    Thanks
                    cheers
                    Michael B

                    Comment


                      #11
                      Is there a way to add alerts to those lines?

                      Comment


                        #12
                        Hello cachevery,

                        Thank you for your post.

                        Unfortunately, there is no supported method to compare the price against the graphics.DrawLine() in the indicator as it is based on the Bounds of the chart window and not the price values on the chart.

                        Comment


                          #13
                          Originally posted by NinjaTrader_JoshP View Post
                          What you probably experienced was some weirdness in the formatting when you copy and paste the code. Extra white space was added between variable names so that caused errors.
                          Hi there!

                          Is there a way to use the Plot for alert?

                          Comment


                            #14
                            Hello,

                            Thank you for the follow-up.

                            Whatever meaningful values you calculate in an indicator will be stored in the Values[] array. Plots are graphical objects and have properties:
                            Brush, Name, PlotStyle, and Stroke.

                            We do have a considerable amount of control since we are scripting. You may add alerts based on any value you wish.

                            What would you like to accomplish in your script?

                            I look forward to your reply.
                            Chris L.NinjaTrader Customer Service

                            Comment


                              #15
                              Originally posted by NinjaTrader_ChrisL View Post
                              Hello,

                              Thank you for the follow-up.

                              Whatever meaningful values you calculate in an indicator will be stored in the Values[] array. Plots are graphical objects and have properties:
                              Brush, Name, PlotStyle, and Stroke.

                              We do have a considerable amount of control since we are scripting. You may add alerts based on any value you wish.

                              What would you like to accomplish in your script?

                              I look forward to your reply.
                              Chris, yes i would like to have an alert when the price touches the plot line.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by arvidvanstaey, Today, 02:19 PM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_Zachary  
                              Started by mmckinnm, Today, 01:34 PM
                              3 responses
                              5 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by f.saeidi, Today, 01:32 PM
                              2 responses
                              8 views
                              0 likes
                              Last Post f.saeidi  
                              Started by alifarahani, 04-19-2024, 09:40 AM
                              9 responses
                              55 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by Conceptzx, 10-11-2022, 06:38 AM
                              3 responses
                              60 views
                              0 likes
                              Last Post NinjaTrader_SeanH  
                              Working...
                              X