Skip to main content Skip to footer

OnRender - gives a user and access to chart canvas and allows to draw anything user wants. It is an advanced tools which requires understanding such objects like ChartControl, ChartViewPort and ChartPanel. The following snippet of code shows how to draw price channel

protected override void OnRender(IChartControlCanvas chartControlCanvas)
{
      double GetY(double price) { return ChartViewport.GetValueOrThrow().GetYByValue(price); }

      //Actually it is possible only some inheritor will call the method explicitly
      if (ChartControl == null || ChartViewport == null || ChartPanel == null || _atr == null)
          return;

      var firstIndexToDraw = Input.Count - 10;

      if (ChartViewport.ToIndex < firstIndexToDraw)
          return;

      var textColor = Colors.LightGray;
      var levelsColor = Colors.DarkGoldenrod;
      var priceColor = Colors.Aqua;

      var x1 = ChartPanel.Width;
      var x2 = ChartControl.GetValueOrThrow().GetXByBarIndex(Input, firstIndexToDraw);

      var priceMiddle = this.Input.GetLastValue().Close;
      var yMiddle = GetY(priceMiddle);

      // Middle
      chartControlCanvas.DrawLine(new Point(x1, yMiddle), new Point(x2, yMiddle), new Stroke(priceColor, 2));
            chartControlCanvas.DrawText($"Current price: " +
                                        $"{Instrument?.Definition.FormatPrice(priceMiddle, ApplicationSettings.FormatCulture)}", 
                new Point(x1, yMiddle), textColor, HorizontalOrigin.Right,VerticalOrigin.Bottom);


      var distance = _atr.GetLastValue() * Multiplier;

       // Top
      var priceTop = priceMiddle + distance;
      var yTop = GetY(priceTop);
      chartControlCanvas.DrawLine(new Point(x1, yTop), new Point(x2, yTop), new Stroke(levelsColor, 2));
            chartControlCanvas.DrawText($"Channel's top: " +
                                        $"{Instrument?.Definition.FormatPrice(priceTop, ApplicationSettings.FormatCulture)}", 
                new Point(x1, yTop), textColor, HorizontalOrigin.Right,
                VerticalOrigin.Bottom);

      // Bottom
      var priceBottom = priceMiddle - distance;
      var yBottom = GetY(priceBottom);
      chartControlCanvas.DrawLine(new Point(x1, yBottom), new Point(x2, yBottom), new Stroke(levelsColor, 2));
            chartControlCanvas.DrawText($"Channel's bottom: " +
                                        $"{Instrument?.Definition.FormatPrice(priceBottom, ApplicationSettings.FormatCulture)}", 
                new Point(x1, yBottom), textColor, HorizontalOrigin.Right,
                VerticalOrigin.Bottom);
}

Cookies Notice

We use cookies to improve your experience, personalize content, and analyze our traffic. By clicking "Accept All Cookies," you agree to the storing of cookies on your device. You can manage your cookie preferences at any time by visiting our Cookie Settings.