private void DrawLine(DXMediaBrush brush, double x1, double y1, double x2, double y2, float width, DashStyleHelper dashStyle) { SharpDX.Direct2D1.StrokeStyleProperties ssProps = new SharpDX.Direct2D1.StrokeStyleProperties(); if (dashStyle == DashStyleHelper.Dash) { ssProps.DashStyle = SharpDX.Direct2D1.DashStyle.Dash; } if (dashStyle == DashStyleHelper.DashDot) { ssProps.DashStyle = SharpDX.Direct2D1.DashStyle.DashDot; } if (dashStyle == DashStyleHelper.DashDotDot) { ssProps.DashStyle = SharpDX.Direct2D1.DashStyle.DashDotDot; } if (dashStyle == DashStyleHelper.Dot) { ssProps.DashStyle = SharpDX.Direct2D1.DashStyle.Dot; } if (dashStyle == DashStyleHelper.Solid) { ssProps.DashStyle = SharpDX.Direct2D1.DashStyle.Solid; } SharpDX.Direct2D1.StrokeStyle strokeStyle = new SharpDX.Direct2D1.StrokeStyle(Core.Globals.D2DFactory, ssProps); SharpDX.Vector2 startPoint = new System.Windows.Point(x1, y1).ToVector2(); SharpDX.Vector2 endPoint = new System.Windows.Point(x2, y2).ToVector2(); RenderTarget.DrawLine(startPoint, endPoint, brush.DxBrush, width, strokeStyle); strokeStyle.Dispose(); strokeStyle = null; }
private void DrawString(string text, SimpleFont font, DXMediaBrush brush, double pointX, double pointY, DXMediaBrush areaBrush) { SharpDX.DirectWrite.TextFormat textFormat = font.ToDirectWriteTextFormat(); SharpDX.DirectWrite.TextLayout textLayout = new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Globals.DirectWriteFactory, text, textFormat, ChartPanel.X + ChartPanel.W, textFormat.FontSize); float newW = textLayout.Metrics.Width; float newH = textLayout.Metrics.Height; SharpDX.Vector2 TextPlotPoint = new System.Windows.Point(pointX - newW, pointY - textLayout.Metrics.Height / 2 - 1).ToVector2(); SharpDX.RectangleF PLBoundRect = new SharpDX.RectangleF((float)pointX - newW - 4, (float)pointY - textLayout.Metrics.Height / 2 - 1, newW + 6, newH + 2); SharpDX.Direct2D1.RoundedRectangle PLRoundedRect = new SharpDX.Direct2D1.RoundedRectangle(); PLRoundedRect.RadiusX = newW / 4; PLRoundedRect.RadiusY = newH / 4; PLRoundedRect.Rect = PLBoundRect; RenderTarget.FillRoundedRectangle(PLRoundedRect, areaBrush.DxBrush); RenderTarget.DrawTextLayout(TextPlotPoint, textLayout, brush.DxBrush, SharpDX.Direct2D1.DrawTextOptions.NoSnap); textLayout.Dispose(); textLayout = null; textFormat.Dispose(); textFormat = null; }
protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Places a marker on the chart displaying where the current price is."; Name = "CurrentPriceMarker"; Calculate = Calculate.OnPriceChange; IsOverlay = true; DisplayInDataBox = true; DrawOnPricePanel = true; DrawHorizontalGridLines = true; DrawVerticalGridLines = true; PaintPriceMarkers = true; ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right; //Disable this property if your indicator requires custom values that cumulate with each new market data event. //See Help Guide for additional information. IsSuspendedWhileInactive = true; Font = new SimpleFont("Arial", 10); PriceLineColor = Brushes.RoyalBlue; PriceTextColor = Brushes.White; PriceAreaColor = Brushes.Black; PriceLineLength = 15; PriceLineWidth = 2; PriceLineStyle = DashStyleHelper.Dash; PriceLineOpacity = 50; } else if (State == State.Configure) { // Create our DXMediaBrushes PriceLineBrush = new DXMediaBrush(); PriceTextBrush = new DXMediaBrush(); PriceAreaBrush = new DXMediaBrush(); // Set our DXMediaBrush properties PriceLineBrush.UpdateBrush(RenderTarget, PriceLineColor, PriceLineOpacity); PriceTextBrush.UpdateBrush(RenderTarget, PriceTextColor, PriceLineOpacity); PriceAreaBrush.UpdateBrush(RenderTarget, PriceAreaColor, PriceLineOpacity); } }
protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Demonstration script using the SharpDXHelper class for managed custom rendering"; Name = "SharpDXHelperExample"; Calculate = Calculate.OnBarClose; IsOverlay = true; DisplayInDataBox = true; DrawOnPricePanel = true; DrawHorizontalGridLines = true; DrawVerticalGridLines = true; PaintPriceMarkers = true; ScaleJustification = NinjaTrader.Gui.Chart.ScaleJustification.Right; //Disable this property if your indicator requires custom values that cumulate with each new market data event. //See Help Guide for additional information. IsSuspendedWhileInactive = true; // Set defaults for our properties, can be assigned to our DX/Media Brush in states from State.DataLoaded to State.Historical LineBrush = Brushes.RoyalBlue; LineBrushWidth = 2; LineBrushOpacity = 50.0; RectangleBrush = Brushes.LightGreen; RectangleBorderBrush = Brushes.Green; RectangleBrushWidth = 5; RectangleBrushOpacity = 10.0; RoundedRectangleBrush = Brushes.DarkGreen; RoundedRectangleBorderBrush = Brushes.LimeGreen; RoundedRectangleBrushWidth = 5; RoundedRectangleBrushOpacity = 10.0; EllipseBrush = Brushes.DarkRed; EllipseBrushWidth = 5; EllipseBrushOpacity = 40.0; string image1Path = System.IO.Path.Combine(NinjaTrader.Core.Globals.UserDataDir, "SharpDXHelper.png"); Image1Path = System.IO.File.Exists(image1Path) ? image1Path : String.Empty; Image1Opacity = 30.0f; string image2Path = System.IO.Path.Combine(NinjaTrader.Core.Globals.UserDataDir, "SharpDXHelper2.png"); Image2Path = System.IO.File.Exists(image2Path) ? image2Path : String.Empty; Image2Opacity = 20.0f; } else if (State == State.DataLoaded) { // Create a Series<bool> to tell if we want to draw something for a particular bar, must have MaximumBarsLookBack.Infinite. // This is more efficient than calculating what should be rendered during OnRender(), where we should only worry about how things should be rendered. DrawSeries = new Series <bool>(this, MaximumBarsLookBack.Infinite); // Create a list of brush names for us to manage with the helper class' "DXMediaBrush" Dictionary string[] brushes = new string[] { "LineBrush", "RectBrush", "RectBorderBrush", "RoundedRectBrush", "RoundedRectBorderBrush", "EllipseBrush" }; // If we use resourses managed by the helper class (bitmaps and internally managed brushes created from Windows Media Brush references) // and want to use a timer to clear brushes after a certain period of time, ChartControl must be used with the constructor for the resource timer and cannot be null if (ChartControl != null) { // Create new instance of the Helper Class // We are using managed brushes in this example, and have a 5 minute refresh period // minutesToRefersh values of 0 or less will not create a timer for brush disposal // This is useful for cleaning up programmatically created brushes that may not always be reused thorught a NinjaScript's life. DXH = new DXHelper(true, ChartControl, 0); // Add the brushes we defined in our brushes list DXH.AddBrushes(brushes); // Set our brushes to the values we would like DXH.UpdateBrush(RenderTarget, "LineBrush", LineBrush, LineBrushOpacity); DXH.UpdateBrush(RenderTarget, "RectBrush", RectangleBrush, RectangleBrushOpacity); DXH.UpdateBrush(RenderTarget, "RectBorderBrush", RectangleBorderBrush, RectangleBrushOpacity); DXH.UpdateBrush(RenderTarget, "RoundedRectBrush", RoundedRectangleBrush, RoundedRectangleBrushOpacity); DXH.UpdateBrush(RenderTarget, "RoundedRectBorderBrush", RoundedRectangleBorderBrush, RoundedRectangleBrushOpacity); DXH.UpdateBrush(RenderTarget, "EllipseBrush", EllipseBrush, EllipseBrushOpacity); } // Create a DXMediaBrush DXMBrush = new DXMediaBrush(); DXMBrush.UpdateBrush(RenderTarget, LineBrush, LineBrushOpacity); // Apply Opacity to LineBrush (used for User Defined Media Brush Demonstration. See overlay line 5.) LineBrush = LineBrush.Clone(); LineBrush.Opacity = LineBrushOpacity / 100.0; LineBrush.Freeze(); } else if (State == State.Terminated) { // Dispose of the resources created by our helper class. if (DXH != null) { DXH.Dispose(); } } }