Esempio n. 1
0
        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();
                }
            }
        }