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

VWAP Drawing Tool, Path Geometry

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

    VWAP Drawing Tool, Path Geometry

    Hello,

    Been working on a drawing tool that finds the vwap of a user defined range. The user clicks down onto a bar, drags across however many bars they need, then a vwap line is drawn on the range (using Volumetric Bars).

    I have logic that correctly finds the bars indexes, and vwap prices for each bar, the issue is rendering it onto the chart. I found on another thread how using pathGeometry can paint a line with arcs, etc.. that can be used to render the vwap line. I used ChatGPT 4 to help me get started with the rendering logic and below is what I have thus far. When I add the tool to a chart nothing appears, my chart glitches by other drawings disappearing and when I hover over that chart my cursor disappears. Hopefully my code snippet isn't too long but If i can get another pair of eyes to maybe point out why the line is not rendering onto the chart I would appreciate it.





    Code:
    List<SharpDX.Vector2> vwapCoordinates = new List<SharpDX.Vector2>();
    
    
    OnRender()
    {
      ///Find bar Index and VWAP price for every bar in highlighted range
      for (int i = startIdx; i <= endIdx; i++)
      {
        // VWAP Calculations
        {
          .....calculations
        }
    
        //Turn index and vwap prices to appropriate pixel value coordinates
        vwapBarIndex = chartControl.GetXByBarIndex(myBars, i);
        vwapPrice = chartScale.GetYByValue(VWAP);
    
        //Create a chart point from x and y coordinate
        SharpDX.Vector2 vwapPoint = new SharpDX.Vector2((float)vwapBarIndex, (float)vwapPrice);
    
        //add points to a list
        vwapCoordinates.Add(vwapPoint);
      }
    
      ///Line Creation logic
      SharpDX.Direct2D1.Factory d2dFactory = new SharpDX.Direct2D1.Factory();
    
      using (SharpDX.Direct2D1.PathGeometry pathGeometry = new SharpDX.Direct2D1.PathGeometry(d2dFactory))
      {
        using (SharpDX.Direct2D1.GeometrySink geometrySink = pathGeometry.Open())
        {
          if (vwapCoordinates.Count > 0)
          {
            // Get starting point from vwapCoordinates List
            SharpDX.Vector2 startingPoint = vwapCoordinates[0];
    
            // Define the starting point of your VWAP line
            geometrySink.BeginFigure(startingPoint, SharpDX.Direct2D1.FigureBegin.Filled);
    
            //Add each point to the line drawing
            foreach(var vwapPoint in vwapCoordinates)
            {
              geometrySink.AddLine(vwapPoint);
            }
    
            //End Figure
            geometrySink.EndFigure(SharpDX.Direct2D1.FigureEnd .Open);
            geometrySink.Close();
          }
        }
      
        ///Rendering Logic
        // Set the stroke style
        SharpDX.Direct2D1.StrokeStyleProperties strokeStyleProperties = new SharpDX.Direct2D1.StrokeStyleProperties()
        {
          DashStyle = SharpDX.Direct2D1.DashStyle.Solid
        };
    
        using (SharpDX.Direct2D1.StrokeStyle strokeStyle = new SharpDX.Direct2D1.StrokeStyle(d2dFactory, strokeStyleProperties))
        {
          using (SharpDX.Direct2D1.SolidColorBrush strokeBrush = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget, SharpDX.Color.White))
          {
            //Render Target
            RenderTarget.DrawGeometry(pathGeometry, strokeBrush, vwapStrokeWidth, strokeStyle);
          }
        }
      }
      d2dFactory.Dispose();
    }
    Last edited by Don22Trader1; 02-04-2024, 06:35 PM.

    #2
    Hello Don22Trader1,

    Thanks for your post.

    Something standing out in the code you shared is that you likely do not need to call 'using' before defining pathGeometry, geometrySink, strokeStyle, and strokeBrush.

    Further, d2dFactory.Dispose() likely does not need to be called in the script.

    Always dispose of a PathGeometry when finished and always dispose of a brush when finished.

    Correct, PathGeometry and RenderTarget.DrawGeometry would need to be used to accomplish custom rendering of a line like that on the chart.

    You could view the Ichimoku Cloud indicator from the NinjaTrader Ecosystem User App Share for an example of using PathGeometry and RenderTarget.DrawGeometry.

    Ichimoku Cloud: https://ninjatraderecosystem.com/use...indicator-nt8/

    Also, for examples of using PathGeometry, you could see the Region Drawing object, this uses Path Geometry and is a good example of what may be required.

    See the help guide documentation below for more information.

    RenderTarget.DrawGeometry: https://ninjatrader.com/support/help...awgeometry.htm
    PathGeometry: https://ninjatrader.com/support/help...thgeometry.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
    ​Using SharpDX for Custom Chart Rendering - SharpDX Lines and Shapes: https://ninjatrader.com/support/help..._rendering.htm



    The NinjaTrader Ecosystem website is for educational and informational purposes only and should not be considered a solicitation to buy or sell a futures contract or make any other type of investment decision. The add-ons listed on this website are not to be considered a recommendation and it is the reader's responsibility to evaluate any product, service, or company. NinjaTrader Ecosystem LLC is not responsible for the accuracy or content of any product, service or company linked to on this website.
    Brandon H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by AaronKoRn, Today, 09:49 PM
    0 responses
    6 views
    0 likes
    Last Post AaronKoRn  
    Started by carnitron, Today, 08:42 PM
    0 responses
    8 views
    0 likes
    Last Post carnitron  
    Started by strategist007, Today, 07:51 PM
    0 responses
    9 views
    0 likes
    Last Post strategist007  
    Started by StockTrader88, 03-06-2021, 08:58 AM
    44 responses
    3,975 views
    3 likes
    Last Post jhudas88  
    Started by rbeckmann05, Today, 06:48 PM
    0 responses
    9 views
    0 likes
    Last Post rbeckmann05  
    Working...
    X