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


Modifying Color of line segment while drawing using sink.AddLine

  • Filter
  • Time
  • Show
Clear All
new posts

    Modifying Color of line segment while drawing using sink.AddLine


    I am drawing a kind of high low zigzag line, which is not displayed by the standard plot (all values except the highs and lows are reset). I am using the OnRender() PathGeometry and sink.AddLine, but need to dynamically change the line color based on the angle of the line (previousValue>currentValue or previousValue<currentValue).

    How can I change the brush color setting while the path is open, and I add lines?

    Here is the relevant part of the code

    bool previousPoint = previousInx > -1;
    double previousValue;
    double  currentValue;
    // find current points
    for (int idx = ChartBars.FromIndex; idx <= ChartBars.ToIndex; idx++) 
    if (idx < startIndex
    || idx > Bars.Count - (Calculate == NinjaTrader.NinjaScript.Calculate.OnBarClose ? 1 : 0) || idx < Math.Max(BarsRequiredToPlot, 0))
    continue; if (!zigZagSeries.IsValidDataPointAt(idx))
    currentValue = zigZagSeries.GetValueAt(idx); x2 = (chartControl.BarSpacingType == BarSpacingType.TimeBased
    || chartControl.BarSpacingType == BarSpacingType.EquidistantMulti && idx >= ChartBars.Count
    ? chartControl.GetXByTime(ChartBars.GetTimeByBarIdx( chartControl, idx)) : chartControl.GetXByBarIndex(ChartBars, idx));
    y2 = chartScale.GetYByValue(currentValue); if (sink == null) {
    if (!previousPoint) // first is in the chart, run again {
    x1 = x2; y1 = y2; previousValue = currentValue; previousPoint = true; continue;
    g = new SharpDX.Direct2D1.PathGeometry(Core.Globals.D2DFac tory); sink = g.Open(); sink.BeginFigure(new SharpDX.Vector2(x1, y1), SharpDX.Direct2D1.FigureBegin.Hollow);
    } if (previousValue.ApproxCompare(currentValue) < 0) {
    //Set Trend Up color
    } else {
    //Set Trend Down color
    } sink.AddLine(new SharpDX.Vector2(x2, y2)); previousValue = currentValue;
    } ...

    Hello Shai Samuel,

    Thanks for your post.

    The brush would be applied to the line when calling RenderTarget.DrawGeometry().

    The geometry sink would be made first and then a brush would be applied when drawing the object with RenderTarget.DrawGeometry().

    To have a different color for different segments of the object, you would need to create separate pieces of geometry and draw each piece with a different DrawGeometry call using a different brush color.

    See the code snippet below for an example of using DrawGeometry.

    /// <summary>
    /// Draws geometry using SharpDX Brushes.
    /// </summary>
    /// <param name="renderTarget">The hosting NinjaScript's RenderTarget</param>
    /// <param name="points">SharpDX Vector2 array of points for our Geometry</param>
    /// <param name="brush">SharpDX brush used for the Geometry Color</param>
    /// <param name="strokeWidth">Width of Rectangle line</param>
    /// <param name="strokeStyle">SharpDX StrokeStyle used for the Geometry line</param>
    /// <returns></returns>
    public void DrawGeometry(SharpDX.Direct2D1.RenderTarget renderTarget, SharpDX.Vector2[] points, SharpDX.Direct2D1.Brush brush, float strokeWidth, SharpDX.Direct2D1.StrokeStyle strokeStyle)
        SharpDX.Direct2D1.PathGeometry geometry = new SharpDX.Direct2D1.PathGeometry(Core.Globals.D2DFactory);
        SharpDX.Direct2D1.GeometrySink sink = geometry.Open();
        sink.BeginFigure(points[0], new SharpDX.Direct2D1.FigureBegin());
        for (int i = 1; i < points.GetLength(0); i++)
        sink.EndFigure(SharpDX.Direct2D1.FigureEnd.Closed) ;
        DrawGeometry(renderTarget, geometry, brush, strokeWidth, strokeStyle);
        geometry = null;
        sink = null;
    See the help guide documentation below for more information.


    Let us know if we may further assist.
    Brandon H.NinjaTrader Customer Service


      Thank you Brandon, thank you for your reply.
      1. So what you are actually telling me if I understand correctly, is that if I need to have a ZigZag line with the segments going down on red, and the ones up in blue, I can have one Geometry (which is typically how ZigZag line is handled), but I need to have each segment as a separate Geometry?
      2. If this is the case, maybe Geometry is not the way to go, but using something simpler that will add a line? Any example you can add?


        Hello Shai Samuel,

        Thanks for your note.

        That is correct. To have the down line color as red and the up line color as blue, you would need to have a separate Geometry for each segment. I am not aware of any DirectX way to color individual paths in a Geometry.

        Instead, you could consider using a plot for the zigzag line and use PlotBrushes to change the color of the plot based on certain conditions. Note that there would be a maximum of 65,000 brushes when using this approach.


        For an example of using SharpDX RenderTarget.DrawGeometry() to create a zigzag line, see the code in the Miscellaneous section of the ZigZag indicator script that comes default with NinjaTrader. You could view the script by opening a New > NinjaScript Editor window, open the Indicators folder, select the ZigZag file, and expand the 'Miscellaneous' code section.

        Let us know if we may assist further.
        Brandon H.NinjaTrader Customer Service



          The issue with drawing the lines with AddPlot is that the points between each zigzag point are reset (zero), so the plot doesn't show up on the screen. Only when the ZigZag is 1 bar length it is shown. I believe this is the reason why the ZigZag indicator is using OnRender to display.

          I did use the zigzag indicator as a base (I changed all the logic, since the zigzag has 1 bar delay, and only recognizes the HH/LL once a new bar is closed below). But they are using one color only.

          Am I correct?


            Hello Shai Samuel,

            Thanks for your note.

            Yes, that is correct, the ZigZag indicator that comes default with NinjaTrader only uses a single color.

            As seen in post # 4, you would need to have a separate Geometry for each segment if you want the line to use more than a single color. I am not aware of any DirectX way to color individual paths in a Geometry.

            Let us know if we may assist further.
            Brandon H.NinjaTrader Customer Service


            Latest Posts


            Topics Statistics Last Post
            Started by BartMan, Today, 06:49 PM
            0 responses
            Last Post BartMan
            by BartMan
            Started by Alley93, Today, 04:20 PM
            0 responses
            1 view
            Last Post Alley93
            by Alley93
            Started by joselube001, Today, 02:49 PM
            1 response
            Last Post NinjaTrader_ChrisL  
            Started by TheFil, Today, 06:20 AM
            1 response
            Last Post NinjaTrader_Clayton  
            Started by WalterSkinner, Today, 03:14 PM
            0 responses
            Last Post WalterSkinner