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

Comment