I am new to OnRender and need some guidance.
My first question is on the code attached.
Did I instantiate the right stuff in the most resource efficient manner?
Do I do it all under OnRender, or some in OnRenderTargetChanged? Or somewhere else?
Did I dispose of everything that needed to be disposed?
My goal is to make the code as efficient as possible when executing cause I have a lot to render
protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
{
// call the base.OnRender() to ensure standard Plots work as designed
base.OnRender(chartControl, chartScale);
// CREATE TEXT FORMAT
SharpDX.DirectWrite.TextFormat textFormat = new SharpDX.DirectWrite.TextFormat(NinjaTrader.Core.Globals.DirectWriteFactory,
"Arial", PriceTimerFontSize);
// CREATE BRUSHES
SharpDX.Direct2D1.Brush brushRed = Brushes.Red.ToDxBrush(RenderTarget);
SharpDX.Direct2D1.Brush brushGreen = Brushes.Green.ToDxBrush(RenderTarget);
SharpDX.Direct2D1.Brush brushWhite = Brushes.White.ToDxBrush(RenderTarget);
SharpDX.Direct2D1.Brush brushGray = Brushes.DarkGray.ToDxBrush(RenderTarget);
SharpDX.Direct2D1.Brush brushLimeGreen = Brushes.LimeGreen.ToDxBrush(RenderTarget);
SharpDX.Direct2D1.Brush brushAqua = Brushes.Aqua.ToDxBrush(RenderTarget);
// SET X COORDINATE AT RIGHT EDGE OF CHART
int xPriceCoordinate = ChartPanel.X + ChartPanel.W - 50;
// SET Y COORDINATE AT CURRENT CLOSE
int yPriceCoordinate = chartScale.GetYByValue(Bars.GetClose(Bars.Count - 1));
// Y OFFSET FOR TARGET LINES
float offset = chartScale.GetPixelsForDistance(50 * TickSize);
SharpDX.Direct2D1.AntialiasMode oldAntialiasMode = RenderTarget.AntialiasMode;
RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.Aliased;
// CREATE RECTANGLE FOR TIME DISPLAY
SharpDX.Rectangle timeBox = new SharpDX.Rectangle(xPriceCoordinate, yPriceCoordinate - 10, 50, 20);
// CHANGE RECTANGLE COLOR TO RED WHEN 10 SECONDS LEFT BEFORE BAR CLOSE
RenderTarget.FillRectangle(timeBox, (barTimeLeft.Hours == 0 && barTimeLeft.Minutes == 0 && barTimeLeft.Seconds <= 10) ? brushRed : brushGreen);
// DRAW TEXT WITH TIME LEFT UNTIL BAR CLOSES
RenderTarget.DrawText(" " + timeLeft, textFormat, timeBox, brushWhite);
// SET START COORDINATE FOR LINE
int x1PriceCoordinate = xPriceCoordinate - 300;
// CREATE START AND ENDPOINTS
SharpDX.Vector2 lineStart;
SharpDX.Vector2 lineEnd;
// DRAW LINE AT CURRENT PRICE
lineStart = new SharpDX.Vector2(x1PriceCoordinate, yPriceCoordinate);
lineEnd = new SharpDX.Vector2(xPriceCoordinate, yPriceCoordinate);
RenderTarget.DrawLine(lineStart, lineEnd, brushGray, 1, lineDash);
// DRAW LINE 50 TICKS ABOVE CURRENT PRICE
float y = yPriceCoordinate + offset;
lineStart = new SharpDX.Vector2(x1PriceCoordinate, y);
lineEnd = new SharpDX.Vector2(xPriceCoordinate, y);
RenderTarget.DrawLine(lineStart, lineEnd, brushAqua, 1);
// DRAW LINE 50 TICKS ABOVE BELOW PRICE
y = yPriceCoordinate - offset;
lineStart = new SharpDX.Vector2(x1PriceCoordinate, y);
lineEnd = new SharpDX.Vector2(xPriceCoordinate, y);
RenderTarget.DrawLine(lineStart, lineEnd, brushAqua, 1);
// DRAW LINE 100 TICKS ABOVE CURRENT PRICE
y = yPriceCoordinate + 2 * offset;
lineStart = new SharpDX.Vector2(x1PriceCoordinate, y);
lineEnd = new SharpDX.Vector2(xPriceCoordinate, y);
RenderTarget.DrawLine(lineStart, lineEnd, brushLimeGreen, 1);
// DRAW LINE 100 TICKS BELOW CURRENT PRICE
y = yPriceCoordinate - 2 * offset;
lineStart = new SharpDX.Vector2(x1PriceCoordinate, y);
lineEnd = new SharpDX.Vector2(xPriceCoordinate, y);
RenderTarget.DrawLine(lineStart, lineEnd, brushLimeGreen, 1);
RenderTarget.AntialiasMode = oldAntialiasMode;
brushRed.Dispose();
brushGreen.Dispose();
brushWhite.Dispose();
brushGray.Dispose();
brushLimeGreen.Dispose();
brushAqua.Dispose();
}
My other OnRender question is for a lot of line drawing, rectangle drawing, etc... is it better to use DrawLine, DrawRectange, DrawText, etc
Or is it a better use of system resource to use OnRender to create the objects?
thanks

Comment