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

Modifying Color of line segment while drawing using sink.AddLine

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

    Modifying Color of line segment while drawing using sink.AddLine

    Hi,

    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

    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))
    continue;
    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;
    } ...

    #2
    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.

    Code:
    /// <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.AddLine(points[i]);
    
        sink.EndFigure(SharpDX.Direct2D1.FigureEnd.Closed) ;
        sink.Close();
    
        DrawGeometry(renderTarget, geometry, brush, strokeWidth, strokeStyle);
    
        geometry.Dispose();
        geometry = null;
        sink.Dispose();
        sink = null;
    }
    See the help guide documentation below for more information.

    RenderTarget.DrawGeometry: https://ninjatrader.com/support/help...awgeometry.htm
    Geometry: https://ninjatrader.com/support/help...thgeometry.htm
    StrokeStyle: https://ninjatrader.com/support/help...trokestyle.htm
    RenderTarget: https://ninjatrader.com/support/help...ndertarget.htm
    SharpDX.Direct2D1: https://ninjatrader.com/support/help..._direct2d1.htm

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

    Comment


      #3
      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?

      Comment


        #4
        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.

        AddPlot: https://ninjatrader.com/support/help...t8/addplot.htm
        PlotBrushes: https://ninjatrader.com/support/help...lotbrushes.htm

        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

        Comment


          #5
          Brandon,

          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?

          Comment


            #6
            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

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by BartMan, Today, 06:49 PM
            0 responses
            3 views
            0 likes
            Last Post BartMan
            by BartMan
             
            Started by Alley93, Today, 04:20 PM
            0 responses
            1 view
            0 likes
            Last Post Alley93
            by Alley93
             
            Started by joselube001, Today, 02:49 PM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_ChrisL  
            Started by TheFil, Today, 06:20 AM
            1 response
            11 views
            0 likes
            Last Post NinjaTrader_Clayton  
            Started by WalterSkinner, Today, 03:14 PM
            0 responses
            11 views
            0 likes
            Last Post WalterSkinner  
            Working...
            X