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

Plotting background colors between bands

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

    Plotting background colors between bands

    I want toaddcode to a Ninja Script so that a different background color can be drawn between say an Upper and Lower Bollinger band. Apparently this can be done by over riding the Plot method with a custom drawing using native .Net graphics classes.

    If anyone knows how to do this and is happy to share could you please post a few lines of sample code which shows the underlying instruction syntax necessary to achieve the desired result.

    Many thanks





    #2
    imported post

    I'm a beginner with C#, and I wanted the same thing. I could not figure how to do it with .Net graphics so I settled for using NinjaScript to draw rectangles.

    if (Middle[0]>Middle[1])
    BandColor=Color.Blue;
    else
    BandColor=Color.Red;
    DrawRectangle("R"+CurrentBar.ToString(),
    1,Math.Max(Lower[0],Lower[1]),
    0,Math.Min(Upper[0],Upper[1]),
    Color.Transparent,BandColor,1);

    Comment


      #3
      imported post

      Thankyou for the post tquinn - will give what you suggest a try......


      Comment


        #4
        imported post

        Hi

        Try this

        Code:
        SolidBrush brush = new SolidBrush(Color.Gray);
        int barWidth = ChartControl.ChartStyle.GetBarPaintWidth(ChartControl.BarWidth);
        SmoothingMode oldSmoothingMode = graphics.SmoothingMode;
        GraphicsPath path = new GraphicsPath();
        
        for (int seriesIndex = 0; seriesIndex < 2; seriesIndex++)
        {
         int lastX = -1;
         int lastY = -1;
         DataSeries series = (DataSeries) Values[seriesIndex];
         double val = 0;
         Gui.Chart.Plot plot = Plots[seriesIndex];
        
         for (int barIndex = 0; barIndex < ChartControl.BarsPainted; barIndex++)
         {
          int idx = ChartControl.LastBarPainted - ChartControl.BarsPainted + 1 + barIndex;
          if (idx < 0 || idx >= Input.Count || (!ChartControl.ShowBarsRequired && idx < BarsRequired))
           continue;
        
          val = series.Get(idx);
        
          int   x = (int) (ChartControl.CanvasRight - ChartControl.BarMarginRight - barWidth / 2
           - (ChartControl.BarsPainted - 1) * ChartControl.BarSpace + barIndex * ChartControl.BarSpace) + 1;
          int   y = (int) ((bounds.Y + bounds.Height) - ((val - min ) / (max - min)) * bounds.Height);
        
          if (lastX >= 0) 
          {
           path.AddLine(lastX - plot.Pen.Width / 2, lastY, x - plot.Pen.Width / 2, y);
          }
        
          lastX = x;
          lastY = y;
         }
         path.Reverse();
         
        }
        graphics.SmoothingMode = SmoothingMode.AntiAlias;
        graphics.FillPath(brush, path);
        graphics.SmoothingMode = oldSmoothingMode;
        Muly
        Final
        http://fin-alg.com/

        Comment


          #5
          imported post

          Muly,
          I'm not good enough with C# to use this. I placed it in OnBarUpdate() and it produced a bunch of compile errors. I'm sure it is my lack of C# skills.

          Comment


            #6
            imported post

            Wow,

            Well beyond my skills too! Thanks heaps for posting this Muly.Iattempt to get it working...

            Cheers


            Comment


              #7
              imported post

              Hi

              the post bellow is the code for the Plot function

              Code:
              public override void Plot(Graphics graphics, Rectangle bounds, double min, double max)
              Muly
              Final
              http://fin-alg.com/

              Comment


                #8
                imported post

                Muly,
                It is good to get a lesson on how much there is to learn about a new programmimg language. If I understand your code, it is plotting vertical lines between Values[0]
                and Values[1]. So I've created only those 2 Values.

                This is what I have so far, but it still will not compile. Errors about "using directives" and "assemble references", things I know nothing about.

                protected override void Initialize()
                {
                Add(new Plot(Color.Orange, "Upper band"));
                Add(new Plot(Color.Orange, "Lower band"));
                }

                protected override void OnBarUpdate()
                {
                Upper.Set(SMA(14)[0] + 2 * StdDev(14)[0]);
                Lower.Set(SMA(14)[0] - 2 * StdDev(14)[0]);
                }

                public override void Plot(Graphics graphics, Rectangle bounds, double min, double max)
                {

                // Your code from below
                }

                Comment


                  #9
                  imported post

                  Please paste in error messages. They likely are not related to Muly's code. Likely your NT installation is screwed up.

                  Comment


                    #10
                    imported post

                    Probebly the using directive is missing some here is my

                    Code:
                    using System;
                    using System.Diagnostics;
                    using System.Drawing;
                    using System.Drawing.Drawing2D;
                    using System.ComponentModel;
                    using System.Xml.Serialization;
                    using NinjaTrader.Data;
                    using NinjaTrader.Gui.Chart;
                    make sure you include all

                    Muly
                    Final
                    http://fin-alg.com/

                    Comment


                      #11
                      imported post

                      Thanks all,
                      I was missing " using System.Drawing.Drawing2D;", Muly's code runs fine now.

                      Now I need to learn how to put the Colored area behind the price bars, and maybe change the opacity. I'll save this lesson for a later day.

                      I need to get back to Mastering NinjaScript before I wander too far off track, into the world of C# and .NET.

                      Thanks again

                      Comment


                        #12
                        imported post

                        Tom,

                        Just curious:

                        "using System.Drawing.Drawing2D;"

                        should be part of the code generated by the indicator wizard. Do you know why it was not there?

                        Thanks


                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Christopher_R, Today, 12:29 AM
                        0 responses
                        7 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
                        7 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