• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

PaintBars

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

    PaintBars

    Could you please help me to convert the following NT 7 code to NT 8 ..?..

    private SolidBrush downBrush=new SolidBrush(Color.Red);
    private SolidBrush upBrush= new SolidBrush(Color.Green);

    public override void PaintBars(NinjaTrader.Gui.Chart.ChartControl chartControl, Graphics graphics, NinjaTrader.Data.Bars bars, int panelIdx, int fromIdx, int toIdx, Rectangle bounds, double max, double min)
    {
    if (bars.BarsType.DisplayName.Contains("STR"))
    {
    if (this.downBrush.Color != this.DownColor)
    {
    this.downBrush.Color = this.DownColor;
    }
    if (this.upBrush.Color != this.UpColor)
    {
    this.upBrush.Color = this.UpColor;
    }
    int barWidthUI = bars.BarsData.ChartStyle.BarWidthUI;
    Color color2 = base.Pen.Color;
    Color color3 = base.Pen2.Color;
    for (int i = fromIdx; i <= toIdx; i++)
    {
    Color barColor = chartControl.GetBarColor(bars, i);
    int barPaintWidth = this.GetBarPaintWidth(barWidthUI);
    double high = bars.GetHigh(i);
    double low = bars.GetLow(i);
    double open = bars.GetOpen(i);
    double close = bars.GetClose(i);
    int yByValue = chartControl.GetYByValue(bars, close);
    double num14 = (i > 0) ? bars.GetClose(i - 1) : close;
    chartControl.GetYByValue(bars, num14);
    double num15 = (i > 0) ? bars.GetOpen(i - 1) : close;
    if (i == 0)
    {
    high = bars.GetClose(i);
    low = bars.GetClose(i);
    }
    int MyPeriod = 0;
    int MyPeriod2 =0;
    int MyBasePeriod = 0;
    if(bars.Period.Value.ToString().Length == 6)
    {

    MyPeriod = Convert.ToInt32(bars.Period.Value.ToString().Subst ring(0, 2))-10;
    MyPeriod2 = Convert.ToInt32(bars.Period.Value.ToString().Subst ring(2, 2))-10;
    MyBasePeriod = Convert.ToInt32(bars.Period.Value.ToString().Subst ring(2, 2))-10;
    }
    else
    {
    MyPeriod = bars.Period.Value ;
    MyPeriod2 = bars.Period.Value2 ;
    MyBasePeriod = bars.Period.BasePeriodValue ;

    }

    int num17 = MyPeriod;//bars.Period.Value;
    int num18 = MyPeriod2;//bars.Period.Value2;

    double num19 = (num17 - num18) * bars.Instrument.MasterInstrument.TickSize;
    int num10 = chartControl.GetYByValue(bars, open - num19);
    int num11 = chartControl.GetYByValue(bars, open + num19);
    open = (num14 > num15) ? (num14 - num19) : (num14 + num19);
    int num9 = chartControl.GetYByValue(bars, open);
    int num5 = chartControl.GetYByValue(bars, high);
    int num7 = chartControl.GetYByValue(bars, low);
    int xByBarIdx = chartControl.GetXByBarIdx(bars, i);
    Color candleOutlineColor = chartControl.GetCandleOutlineColor(bars, i);
    bool flag = chartControl.GetCandleOutlineColor(bars, i) != Color.Empty;
    base.Pen.Color = color2;
    if (barColor != Color.Empty)
    {
    base.Pen.Color = (candleOutlineColor != Color.Empty) ? candleOutlineColor : barColor;
    }
    else if (candleOutlineColor != Color.Empty)
    {
    base.Pen.Color = candleOutlineColor;
    }
    SolidBrush brush = (close >= open) ? this.upBrush : this.downBrush;
    Color color = brush.Color;
    if (barColor != Color.Empty)
    {
    brush.Color = barColor;
    }
    if (i != 0)
    {
    graphics.FillRectangle(brush, (int) ((xByBarIdx - (barPaintWidth / 2)) + 1), (int) (Math.Min(yByValue, num9) + 1), (int) (barPaintWidth - 1), (int) ((Math.Max(num9, yByValue) - Math.Min(yByValue, num9)) - 1));
    }
    if (barColor != Color.Empty)
    {
    brush.Color = color;
    }
    if (num14 > num15)
    {
    graphics.DrawRectangle(base.Pen, (xByBarIdx - (barPaintWidth / 2)) + (base.Pen.Width / 2f), (float) Math.Min(yByValue, num10), barPaintWidth - base.Pen.Width, (float) (Math.Max(num10, yByValue) - Math.Min(yByValue, num10)));
    }
    if (num14 < num15)
    {
    graphics.DrawRectangle(base.Pen, (xByBarIdx - (barPaintWidth / 2)) + (base.Pen.Width / 2f), (float) Math.Min(yByValue, num11), barPaintWidth - base.Pen.Width, (float) (Math.Max(num11, yByValue) - Math.Min(yByValue, num11)));
    }
    if (num14 > num15)
    {
    graphics.DrawLine(base.Pen, xByBarIdx - (barPaintWidth / 2), num10, xByBarIdx + (barPaintWidth / 2), num10);
    }
    if (num14 < num15)
    {
    graphics.DrawLine(base.Pen, xByBarIdx - (barPaintWidth / 2), num11, xByBarIdx + (barPaintWidth / 2), num11);
    }
    base.Pen2.Color = flag ? candleOutlineColor : color3;
    if ((num5 < Math.Min(num10, yByValue)) && (num14 > num15))
    {
    graphics.DrawLine(base.Pen2, xByBarIdx, num5, xByBarIdx, Math.Min(num10, yByValue));
    }
    if ((num7 > Math.Max(num11, yByValue)) && (num14 < num15))
    {
    graphics.DrawLine(base.Pen2, xByBarIdx, num7, xByBarIdx, Math.Max(num11, yByValue));
    }
    if ((num5 < Math.Min(num11, yByValue)) && (num14 < num15))
    {
    graphics.DrawLine(base.Pen2, xByBarIdx, num5, xByBarIdx, Math.Min(num11, yByValue));
    }
    if ((num7 > Math.Max(num10, yByValue)) && (num14 > num15))
    {
    graphics.DrawLine(base.Pen2, xByBarIdx, num7, xByBarIdx, Math.Max(num10, yByValue));
    }
    }
    base.Pen.Color = color2;
    base.Pen2.Color = color3;
    }

    }

    #2
    We would not be able to convert all of this, but to help get you started:

    PaintBars is now OnRender

    bars.BarsData.ChartStyle.BarWidthUI is now just BarWidthUI

    Instead of colors, you will need to use SharpDX.Direct2D1.Brush

    To loop through the bars, use chartBars.FromIndex & chartBars.ToIndex

    Instead of chartControl.GetBarColor, use chartControl.GetBarOverrideBrush

    Instead of bars.GetHigh(i);, use chartBars.Bars.GetHigh(i);

    Instead of chartControl.GetYByValue(bars, num14);, use chartScale.GetYByValue(num14);

    Instead of bars.Period.Value, use chartBars.Bars.BarsPeriod.Value

    Instead of base.Pen.Color use UpBrushDX

    Instead of base.Pen.Color2 use DownBrushDX

    For filling rectangles, use the RenderTarget.Fil Rectangle(), and you will need to construct a SharpDX. RectangleF

    Code:
    RenderTarget.FillRectangle(new SharpDX.RectangleF(), brush);
    For drawing lines, use the RenderTarget.DrawLine and you will need to construct a SharpDX.Vector2()
    Code:
    RenderTarget.DrawLine(new SharpDX.Vector2(), new SharpDX.Vector2(), UpBrushDX);
    There may be more I missed... if you could point out specific errors you're having trouble converting, I'll be glad to provide you the NT8 alternative.
    MatthewNinjaTrader Product Management

    Comment


      #3
      thanks for your Reply,

      I converted the code based on your Reply.But Still i got some issues
      i changed the code like below

      SharpDX.Direct2D1.SolidColorBrush downBrush;
      SharpDX.Direct2D1.SolidColorBrush upBrush;

      this.downBrush =new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, SharpDX.Color.Red);
      this.upBrush = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, SharpDX.Color.LightGreen);

      public override void OnRender(ChartControl chartControl, ChartScale chartScale, ChartBars chartBars)
      {


      if(chartBars.Bars.BarsType.DisplayNames.ContainsVa lue("STR"))
      {
      if (this.downBrush.Color != this.DownBrush)
      {
      this.downBrush = this.DownBrush;
      }
      if (this.upBrush != this.UpBrush)
      {
      this.upBrush = this.UpBrush;
      }

      int barWidthUI =BarWidthUI;

      System.Windows.Media.Brush color2 = base.Stroke.Brush;
      System.Windows.Media.Brush color3 = base.Stroke2.Brush;

      for (int i = chartBars.FromIndex; i <= chartBars.ToIndex; i++)
      {
      SharpDX.Direct2D1.Brush barColor = chartControl.GetBarOverrideBrush(chartBars, i);
      int barPaintWidth = this.GetBarPaintWidth(barWidthUI);
      double high = chartBars.Bars.GetHigh(i);
      double low = chartBars.Bars.GetLow(i);
      double open = chartBars.Bars.GetOpen(i);
      double close = chartBars.Bars.GetClose(i);

      int yByValue= chartScale.GetYByValue(close);
      double num15 = (i > 0) ? chartBars.Bars.GetClose(i - 1) : close;

      int num14 = chartScale.GetYByValue(num15);
      double num16 = (i > 0) ? chartBars.Bars.GetOpen(i - 1) : close;

      if (i == 0)
      {
      high = chartBars.Bars.GetClose(i);
      low = chartBars.Bars.GetClose(i);
      }


      int MyPeriod = 0;
      int MyPeriod2 =0;
      int MyBasePeriod = 0;

      if( chartBars.Bars.BarsPeriod.Value.ToString().Length == 6)
      {

      MyPeriod = Convert.ToInt32(chartBars.Bars.BarsPeriod.Value.To String().Substring(0, 2))-10;
      MyPeriod2 = Convert.ToInt32(chartBars.Bars.BarsPeriod.Value.To String().Substring(2, 2))-10;
      MyBasePeriod = Convert.ToInt32(chartBars.Bars.BarsPeriod.Value.To String().Substring(2, 2))-10;

      }
      else
      {
      MyPeriod = chartBars.Bars.BarsPeriod.Value ;
      MyPeriod2 = chartBars.Bars.BarsPeriod.Value2 ;
      MyBasePeriod = chartBars.Bars.BarsPeriod.BaseBarsPeriodValue ;

      }

      int num18 = MyPeriod;//bars.Period.Value;
      int num19 = MyPeriod2;//bars.Period.Value2;

      double num20 = (num18 - num19) * chartBars.Bars.Instrument.MasterInstrument.TickSiz e;
      int num10 = chartScale.GetYByValue(open - num20);
      int num11 = chartScale.GetYByValue(open + num20);
      open = (num15 > num16) ? (num15 - num20) : (num15 + num20);
      int num9 = chartScale.GetYByValue(open);
      int num5 = chartScale.GetYByValue(high);
      int num7 = chartScale.GetYByValue(low);
      int xByBarIdx = chartControl.GetXByBarIndex(chartBars, i);
      SharpDX.Direct2D1.Brush candleOutlineColor = chartControl.GetCandleOutlineOverrideBrush(chartBa rs, i);
      bool flag = chartControl.GetCandleOutlineOverrideBrush(chartBa rs, i) != null;
      base.Stroke.Brush = color2;

      if (barColor != null)
      {
      base.Stroke.Brush = (candleOutlineColor != null) ? candleOutlineColor : barColor;
      }
      else if (candleOutlineColor != null)
      {
      base.Stroke.Brush = candleOutlineColor;
      }

      SharpDX.Direct2D1.SolidColorBrush brush = (close >= open) ? this.upBrush : this.downBrush;
      Brush color = brush.Color;
      if (barColor != null)
      {
      brush.Color = barColor;
      }
      if (i != 0)
      {
      RenderTarget.DrawRectangle(new RectangleF((float) ((xByBarIdx - (barPaintWidth / 2)) + 1), (float) (Math.Min(yByValue, num9) + 1), (float) (barPaintWidth - 1), (float) ((Math.Max(num9, yByValue) - Math.Min(yByValue, num9)) - 1)), brush);

      }
      if (barColor != null)
      {
      brush.Color = color;
      }
      if (num15 > num16)
      {
      RenderTarget.DrawRectangle(new RectangleF( (float) ((xByBarIdx - (barPaintWidth / 2)) + (base.Stroke.Width / 2f)), (float) Math.Min(yByValue, num10),(float) (barPaintWidth - base.Stroke.Width), (float) (Math.Max(num10, yByValue) - Math.Min(yByValue, num10))), base.Stroke);

      }
      if (num15 < num16)
      {
      RenderTarget.DrawRectangle(new RectangleF( (float) (xByBarIdx - (barPaintWidth / 2)) + (base.Stroke.Width / 2f), (float) Math.Min(yByValue, num11),(float) (barPaintWidth - base.Stroke.Width), (float) (Math.Max(num11, yByValue) - Math.Min(yByValue, num11))), base.Stroke);

      }

      base.Stroke2.Brush = flag ? candleOutlineColor : color3;

      RenderTarget.DrawRectangle(new RectangleF( (float) (xByBarIdx - (barPaintWidth / 2)) + (base.Stroke.Width / 2f), (float) Math.Min(yByValue, num14),(float) (barPaintWidth - base.Stroke.Width), (float) (Math.Max(num14, yByValue) - Math.Min(yByValue, num14)) ), base.Stroke2);


      if ((num5 < Math.Min(num10, yByValue)) && (num15 > num16))
      {
      Vector2 point02;
      Vector2 point12;

      point02.X = xByBarIdx;
      point02.Y = num5;
      point12.X = xByBarIdx;
      point12.Y = Math.Min(num10, yByValue);
      RenderTarget.DrawLine(point02, point12, base.Stroke2.Brush);


      }
      if ((num7 > Math.Max(num11, yByValue)) && (num15 < num16))
      {
      Vector2 point03;
      Vector2 point13;

      point03.X = xByBarIdx;
      point03.Y = num7;
      point13.X = xByBarIdx;
      point13.Y = Math.Max(num11, yByValue);
      RenderTarget.DrawLine(point03, point13, base.Stroke2.Brush);

      }
      if ((num5 < Math.Min(num10, yByValue)) && (num15 < num16))
      {
      Vector2 point04;
      Vector2 point14;

      point04.X = xByBarIdx;
      point04.Y = num5;
      point14.X = xByBarIdx;
      point14.Y = num14;
      RenderTarget.DrawLine(point04, point14, base.Stroke2.Brush);

      }
      if ((num7 > Math.Max(num11, yByValue)) && (num15 > num16))
      {
      Vector2 point05;
      Vector2 point15;

      point05.X = xByBarIdx;
      point05.Y = num7;
      point15.X = xByBarIdx;
      point15.Y = num14;
      RenderTarget.DrawLine(point05, point15, base.Stroke2.Brush);

      }

      }
      base.Stroke.Brush = color2;
      base.Stroke2.Brush = color3;
      }
      }
      Error 1: Operator != canot applied to operands of Type SharpDX4.Color and System.Windows.Media.Brush

      Error 2: cannot implicitly convert SharoDX.Direct2D1.Brush to System.Windows.Media.Brush

      So how we match these two types of objects..?

      Comment


        #4
        It appears these errors are coming from the code below:

        Code:
        if(chartBars.Bars.BarsType.DisplayNames.ContainsVa lue("STR"))
        {
        if (this.downBrush.Color != this.DownBrush)
        {
        this.downBrush = this.DownBrush;
        }
        if (this.upBrush != this.UpBrush)
        {
        this.upBrush = this.UpBrush;
        }
        You can use this.UpBrushDX and this.DownBrushDX instead of this.UpBrush and this.DownBrush, to ensure that you are comparing a SharpDX brush to another SharpDX brush.
        Dave I.NinjaTrader Product Management

        Comment


          #5
          Thanks for the reply.

          Here downBrush is SharpDX.Direct2D1.SolidColorBrush object and DownBrushDX is SharpDX Brush object. So how cam we compare this.?

          or suppose i rewrtie the code like this way

          private SharpDX.Direct2D1.Brush downBrush;

          here How can i assign a color (red color) to this object.Is it possible...?

          or
          if i rewrite like this way
          private SharpDX.Color downBrush;
          this.downBrush = this.downBrush;

          then it will shows the error like one is Color object and another is Brush object.So How can i solve this issue.


          MY NT 7 code is below ..Could you please tell its alternative conversion of NT 8

          private SolidBrush downBrush;
          private SolidBrush upBrush;

          this.downBrush = new SolidBrush(Color.Red);
          this.upBrush = new SolidBrush(Color.LightGreen);

          public override void PaintBars(NinjaTrader.Gui.Chart.ChartControl chartControl, Graphics graphics, NinjaTrader.Data.Bars bars, int panelIdx, int fromIdx, int toIdx, Rectangle bounds, double max, double min)
          {


          if (this.downBrush.Color != this.DownColor)
          {
          this.downBrush.Color = this.DownColor;
          }
          if (this.upBrush.Color != this.UpColor)
          {
          this.upBrush.Color = this.UpColor;
          }

          }

          Comment


            #6
            NT 7 Code is below

            for (int i = fromIdx; i <= toIdx; i++)
            {
            Color barColor = chartControl.GetBarColor(bars, i);
            Color candleOutlineColor = chartControl.GetCandleOutlineColor(bars, i);

            if (barColor != Color.Empty)
            {
            base.Pen.Color = (candleOutlineColor != Color.Empty) ? candleOutlineColor : barColor;
            }
            else if (candleOutlineColor != Color.Empty)
            {
            base.Pen.Color = candleOutlineColor;
            }
            }


            In NT 7 we are assign this values into Pen.Instead of this NT8 we are using Stoke ..Correct..?


            for (int i = chartBars.FromIndex; i <= chartBars.ToIndex; i++)
            {

            SharpDX.Direct2D1.Brush barColor = chartControl.GetBarOverrideBrush(chartBars, i);
            SharpDX.Direct2D1.Brush candleOutlineColor = chartControl.GetCandleOutlineOverrideBrush(chartBa rs, i);

            if (barColor != null)
            {
            base.Stroke.BrushDX = (candleOutlineColor != null) ? candleOutlineColor : barColor;
            }
            else if (candleOutlineColor != null)
            {
            base.Stroke.BrushDX = candleOutlineColor;
            }
            }

            if i convert like this then the following error will display.

            Property or indexer NinjaTrader.Gui.Stroke.BrushDX cannot be assigned to -- it is read only..

            So how can i convert the above NT 7 code to NT 8.


            Could you please help me..

            Comment


              #7
              Hello,

              Just to clarify -- am I correct in assuming that you are creating a ChartStyle with this code?

              The error you are seeing is coming from base.Stroke.BrushDX = candleOutlineColor;. Because base.Stroke.BrushDX does not have a setter method, you can not simply assign a color to that brush. If you are creating a chart style, coloring bars will not be the same as it is when you develop an indicator. Indicators have bars that already exist, and there are methods that can be used to change the colors. However, with a ChartStyle, you actually have to draw your bars manually, using things like RenderTarget.DrawRectangle(), which will take your SharpDX brush as an argument.

              There are a few key pieces in the existing Box ChartStyle that should shed some light on this for you (and others, as well, but I'll use Box as an example). In the Box chart style, you see that Brushes are first instantiated like this:

              Code:
              Brush	overriddenBarBrush		= chartControl.GetBarOverrideBrush(chartBars, idx);
              Brush	overriddenOutlineBrush	= chartControl.GetCandleOutlineOverrideBrush(chartBars, idx);
              Then a few lines later, those brushes are being conditionally altered:

              Code:
              TransformBrush(overriddenBarBrush ?? UpBrushDX, rect); 
              TransformBrush(overriddenOutlineBrush ?? Stroke.BrushDX, rect);
              Then, those brushes are used to draw rectangles which are the bars themselves:

              Code:
              RenderTarget.FillRectangle(rect, overriddenBarBrush ?? DownBrushDX);
              RenderTarget.DrawRectangle(rect, overriddenOutlineBrush ?? Stroke2.BrushDX, Stroke2.Width, Stroke2.StrokeStyle);
              I hope this helps.
              Dave I.NinjaTrader Product Management

              Comment


                #8
                Update bar color dynamically

                Hi Dave,

                Does the 'TransformBrush' check for brush overrides at the indicator level? If I'm writing candlestyle code that wishes to 'fix' or change the color of the candles dynamically via a volume parameter, would the following be the right way of going about it?

                Code:
                SharpDX.Color cb;
                cb = new ColorBGRA(0, 255, 0, 255);.
                SharpDX.Direct2D1.Brush cbrush = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, cb);
                Do you have a better suggestion? By which other means can I update a bar's color dynamically based on some other parameter?

                Comment


                  #9
                  The TransformBrush defined in the ChartStyle base class simply changes the provided Brush's Transform to match that of a provided Rectangle.

                  Your idea for changing bar colors conditionally should work (although I don't see your condition in your sample code), and then, like other ChartStyles, you would pass your brush into RenderTarget.DrawRectangle() and/or RenderTarget.FillRectangle() to draw your bars.
                  Dave I.NinjaTrader Product Management

                  Comment


                    #10
                    Thank you for your prompt response!
                    In my custom ChartStyle, I removed all references to TransformBrush() as well as overriddenBarBrush() and overriddenOutlineBrush() without adverse effects, what am I missing here?

                    Btw, are brushes the only objects that need to be explicitly disposed() at the end of the loop?
                    Code:
                    for (int idx = chartBars.FromIndex; idx <= chartBars.ToIndex; idx++){...}
                    cbrush.Dispose();
                    Any tips on graphic memory management (I'm a bit confused by SharpDX as parts of it are managed code while other parts are not - on top of that, I cannot locate any documentation on the subject at all; the official wiki appears to be down)? Which other objects within the OnRender() method need to be manually Disposed()? Can you make a call to an OnBarUpdate() method from within a ChartStyle class?
                    Last edited by bkinvent; 01-19-2016, 05:45 PM.

                    Comment


                      #11
                      As far as I've seen, Brushes are the big thing that needs disposed after use. Just keep in mind that it is best practice, from a performance standpoint, to manually dispose of objects that will be instantiated and reside in memory on each iteration.
                      Dave I.NinjaTrader Product Management

                      Comment


                        #12
                        Originally posted by NinjaTrader_Dave View Post
                        As far as I've seen, Brushes are the big thing that needs disposed after use. Just keep in mind that it is best practice, from a performance standpoint, to manually dispose of objects that will be instantiated and reside in memory on each iteration.
                        Hi Dave,

                        Thanks for replying. Just to clarify, do you mean to say that if the color were to vary dependent on the iteration, the 'Dispose()' statement should be inside the loop, and new brushes instantiated and 'disposed' within each iteration?

                        Comment


                          #13
                          Not necessarily in every iteration. Disposal is generally advised when you are terminating the script, such as in OnStateChange where State == State.Terminated. Think of it as doing a bit of garbage collection manually to ensure that nothing resides in memory when you are finished using the brushes.
                          Dave I.NinjaTrader Product Management

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by mntemel, Today, 09:07 PM
                          1 response
                          8 views
                          0 likes
                          Last Post MojoJojo  
                          Started by martyn73, Today, 08:22 PM
                          0 responses
                          8 views
                          0 likes
                          Last Post martyn73  
                          Started by RobertoPaez, Today, 08:13 PM
                          0 responses
                          3 views
                          0 likes
                          Last Post RobertoPaez  
                          Started by LANwarr1or, Today, 08:07 PM
                          0 responses
                          4 views
                          0 likes
                          Last Post LANwarr1or  
                          Started by djt1102, Today, 07:55 PM
                          0 responses
                          8 views
                          0 likes
                          Last Post djt1102
                          by djt1102
                           
                          Working...
                          X