private void holidayOrSunday()
 {
     yDate = Time[0].DayOfWeek.ToString();
     if (yDate == "Sunday")
     {
         //Print("Sunday");
         sunday = true;
         Draw.Dot(this, "sunday" + CurrentBar, false, 0, Low[0] - TickSize, Brushes.Yellow);
     }
     else
     {
         sunday = false;
     }
     foreach (KeyValuePair <DateTime, string> holiday in TradingHours.Holidays)
     {
         string   dateOnly   = String.Format("{0:MM/dd/yyyy}", holiday.Key);
         DateTime myDate     = Time[0]; // DateTime type
         string   prettyDate = myDate.ToString("MM/d/yyyy");
         if (dateOnly == prettyDate)
         {
             Print("\nToday is " + holiday.Value + "\n");
             sunday = true;
             if (Bars.IsFirstBarOfSession)
             {
                 //RemoveDrawObject();
                 NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Helvetica", 18)
                 {
                     Size = 18, Bold = false
                 };
                 Draw.Text(this, "holiday" + prettyDate, true, holiday.Value, 0, MAX(High, 20)[1], 1, Brushes.LightGray, myFont, TextAlignment.Center, Brushes.Transparent, Brushes.Transparent, 50);
             }
         }
     }
 }
        private int getTextHeight(string text)
        {
            SimpleFont sf = new NinjaTrader.Gui.Tools.SimpleFont("Consolas", textSize);

            float textHeight = 0f;

            if (text.Length > 0)
            {
                TextFormat tf = new TextFormat(new SharpDX.DirectWrite.Factory(), sf.Family.ToString(), SharpDX.DirectWrite.FontWeight.Normal, SharpDX.DirectWrite.FontStyle.Normal, (float)sf.Size);
                TextLayout tl = new TextLayout(Core.Globals.DirectWriteFactory, text, tf, ChartPanel.W, ChartPanel.H);

                textHeight = tl.Metrics.Height;

                tf.Dispose();
                tl.Dispose();
            }

            return((int)textHeight);
        }
Beispiel #3
0
        protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
        {
            // This sample should be used along side the help guide educational resource on this topic:
            // http://www.ninjatrader.com/support/helpGuides/nt8/en-us/?using_sharpdx_for_custom_chart_rendering.htm

            // Default plotting in base class. Uncomment if indicators holds at least one plot
            // in this case we would expect NOT to see the SMA plot we have as well in this sample script
            //base.OnRender(chartControl, chartScale);

            // 1.1 - SharpDX Vectors and Charting RenderTarget Coordinates

            // The SharpDX SDK uses "Vector2" objects to describe a two-dimensional point of a device (X and Y coordinates)
            SharpDX.Vector2 startPoint;
            SharpDX.Vector2 endPoint;

            // For our custom script, we need a way to determine the Chart's RenderTarget coordinates to draw our custom shapes
            // This info can be found within the NinjaTrader.Gui.ChartPanel class.
            // You can also use various chartScale and chartControl members to calculate values relative to time and price
            // However, those concepts will not be discussed or used in this sample
            // Notes:  RenderTarget is always the full ChartPanel, so we need to be mindful which sub-ChartPanel we're dealing with
            // Always use ChartPanel X, Y, W, H - as chartScale and chartControl properties WPF units, so they can be drastically different depending on DPI set
            startPoint = new SharpDX.Vector2(ChartPanel.X, ChartPanel.Y);
            endPoint   = new SharpDX.Vector2(ChartPanel.X + ChartPanel.W, ChartPanel.Y + ChartPanel.H);

            // These Vector2 objects are equivalent with WPF System.Windows.Point and can be used interchangeably depending on your requirements
            // For convenience, NinjaTrader provides a "ToVector2()" extension method to convert from WPF Points to SharpDX.Vector2
            SharpDX.Vector2 startPoint1 = new System.Windows.Point(ChartPanel.X, ChartPanel.Y + ChartPanel.H).ToVector2();
            SharpDX.Vector2 endPoint1   = new System.Windows.Point(ChartPanel.X + ChartPanel.W, ChartPanel.Y).ToVector2();

            // SharpDX.Vector2 objects contain X/Y properties which are helpful to recalculate new properties based on the initial vector
            float width  = endPoint.X - startPoint.X;
            float height = endPoint.Y - startPoint.Y;

            // Or you can recalculate a new vector from existing vector objects
            SharpDX.Vector2 center = (startPoint + endPoint) / 2;

            // Tip: This check is simply added to prevent the Indicator dialog menu from opening as a user clicks on the chart
            // The default behavior is to open the Indicator dialog menu if a user double clicks on the indicator
            // (i.e, the indicator falls within the RenderTarget "hit testing")
            // You can remove this check if you want the default behavior implemented
            if (!IsInHitTest)
            {
                // 1.2 - SharpDX Brush Resources

                // RenderTarget commands must use a special brush resource defined in the SharpDX.Direct2D1 namespace
                // These resources exist just like you will find in the WPF/Windows.System.Media namespace
                // such as SolidColorBrushes, LienarGraidentBrushes, RadialGradientBrushes, etc.
                // To begin, we will start with the most basic "Brush" type
                // Warning:  Brush objects must be disposed of after they have been used
                SharpDX.Direct2D1.Brush areaBrushDx;
                SharpDX.Direct2D1.Brush smallAreaBrushDx;
                SharpDX.Direct2D1.Brush textBrushDx;

                // for convenience, you can simply convert a WPF Brush to a DXBrush using the ToDxBrush() extension method provided by NinjaTrader
                // This is a common approach if you have a Brush property created e.g., on the UI you wish to use in custom rendering routines
                areaBrushDx      = areaBrush.ToDxBrush(RenderTarget);
                smallAreaBrushDx = smallAreaBrush.ToDxBrush(RenderTarget);
                textBrushDx      = textBrush.ToDxBrush(RenderTarget);

                // However - it should be noted that this conversion process can be rather expensive
                // If you have many brushes being created, and are not tied to WPF resources
                // You should rather favor creating the SharpDX Brush directly:
                // Warning:  SolidColorBrush objects must be disposed of after they have been used
                SharpDX.Direct2D1.SolidColorBrush customDXBrush = new SharpDX.Direct2D1.SolidColorBrush(RenderTarget,
                                                                                                        SharpDX.Color.DodgerBlue);

                // 1.3 - Using The RenderTarget
                // before executing chart commands, you have the ability to describe how the RenderTarget should render
                // for example, we can store the existing RenderTarget AntialiasMode mode
                // then update the AntialiasMode to be the quality of non-text primitives are rendered
                SharpDX.Direct2D1.AntialiasMode oldAntialiasMode = RenderTarget.AntialiasMode;
                RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.Aliased;

                // Note: The code above stores the oldAntialiasMode as a best practices
                // i.e., if you plan on changing a property of the RenderTarget, you should plan to set it back
                // This is to make sure your requirements to no interfere with the function of another script
                // Additionally smoothing has some performance impacts

                // Once you have defined all the necessary requirements for you object
                //  You can execute a command on the RenderTarget to draw specific shapes
                // e.g., we can now use the RenderTarget's DrawLine() command to render a line
                // using the start/end points and areaBrushDx objects defined before
                RenderTarget.DrawLine(startPoint, endPoint, areaBrushDx, 4);

                // Since rendering occurs in a sequential fashion, after you have executed a command
                // you can switch a property of the RenderTarget to meet other requirements
                // For example, we can draw a second line now which uses a different AntialiasMode
                // and the changes render on the chart for both lines from the time they received commands
                RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.PerPrimitive;
                RenderTarget.DrawLine(startPoint1, endPoint1, areaBrushDx, 4);

                // 1.4 - Rendering Custom Shapes

                // SharpDX namespace consists of several shapes you can use to draw objects more complicated than lines
                // For example, we can use the RectangleF object to draw a rectangle that covers the entire chart area
                SharpDX.RectangleF rect = new SharpDX.RectangleF(startPoint.X, startPoint.Y, width, height);

                // The RenderTarget consists of two commands related to Rectangles.
                // The FillRectangle() method is used to "Paint" the area of a Rectangle
                RenderTarget.FillRectangle(rect, areaBrushDx);

                // and DrawRectangle() is used to "Paint" the outline of a Rectangle
                RenderTarget.DrawRectangle(rect, customDXBrush, 2);

                // Another example is an ellipse which can be used to draw circles
                // The ellipse center point can be used from the Vectors calculated earlier
                // The width and height an absolute 100 device pixels
                // To ensure that pixel coordinates work across all DPI devices, we use the NinjaTrader ChartingExteions methods
                // Which will convert the "100" value from WPF pixels to Device Pixels both vertically and horizontally
                int ellipseRadiusY = ChartingExtensions.ConvertToVerticalPixels(100, ChartControl.PresentationSource);
                int ellipseRadiusX = ChartingExtensions.ConvertToHorizontalPixels(100, ChartControl.PresentationSource);

                SharpDX.Direct2D1.Ellipse ellipse = new SharpDX.Direct2D1.Ellipse(center, ellipseRadiusX, ellipseRadiusY);

                // 1.5 - Complex Brush Types and Shapes
                // For this ellipse, we can use one of the more complex brush types "RadialGradientBrush"
                // Warning:  RadialGradientBrush objects must be disposed of after they have been used
                SharpDX.Direct2D1.RadialGradientBrush radialGradientBrush;

                // However creating a RadialGradientBrush requires a few more properties than SolidColorBrush
                // First, you need to define the array gradient stops the brush will eventually use
                SharpDX.Direct2D1.GradientStop[] gradientStops = new SharpDX.Direct2D1.GradientStop[2];

                // With the gradientStops array, we can describe the color and position of the individual gradients
                gradientStops[0].Color    = SharpDX.Color.Goldenrod;
                gradientStops[0].Position = 0.0f;
                gradientStops[1].Color    = SharpDX.Color.SeaGreen;
                gradientStops[1].Position = 1.0f;

                // then declare a GradientStopCollection from our render target that uses the gradientStops array defined just before
                // Warning:  GradientStopCollection objects must be disposed of after they have been used
                SharpDX.Direct2D1.GradientStopCollection gradientStopCollection =
                    new SharpDX.Direct2D1.GradientStopCollection(RenderTarget, gradientStops);

                // we also need to tell our RadialGradientBrush to match the size and shape of the ellipse that we will be drawing
                // for convenience, SharpDX provides a RadialGradientBrushProperties structure to help define these properties
                SharpDX.Direct2D1.RadialGradientBrushProperties radialGradientBrushProperties =
                    new SharpDX.Direct2D1.RadialGradientBrushProperties
                {
                    GradientOriginOffset = new SharpDX.Vector2(0, 0),
                    Center  = ellipse.Point,
                    RadiusX = ellipse.RadiusY,
                    RadiusY = ellipse.RadiusY
                };

                // we now have everything we need to create a radial gradient brush
                radialGradientBrush = new SharpDX.Direct2D1.RadialGradientBrush(RenderTarget, radialGradientBrushProperties,
                                                                                gradientStopCollection);

                // Finally, we can use this radialGradientBrush to "Paint" the area of the ellipse
                RenderTarget.FillEllipse(ellipse, radialGradientBrush);

                // 1.6 - Simple Text Rendering

                // For rendering custom text to the Chart, there are a few ways you can approach depending on your requirements
                // The most straight forward way is to "borrow" the existing chartControl font provided as a "SimpleFont" class
                // Using the chartControl LabelFont, your custom object will also change to the user defined properties allowing
                // your object to match different fonts if defined by user.

                // The code below will use the chartControl Properties Label Font if it exists,
                // or fall back to a default property if it cannot obtain that value
                NinjaTrader.Gui.Tools.SimpleFont simpleFont = chartControl.Properties.LabelFont ?? new NinjaTrader.Gui.Tools.SimpleFont("Arial", 12);

                // the advantage of using a SimpleFont is they are not only very easy to describe
                // but there is also a convenience method which can be used to convert the SimpleFont to a SharpDX.DirectWrite.TextFormat used to render to the chart
                // Warning:  TextFormat objects must be disposed of after they have been used
                SharpDX.DirectWrite.TextFormat textFormat1 = simpleFont.ToDirectWriteTextFormat();

                // Once you have the format of the font, you need to describe how the font needs to be laid out
                // Here we will create a new Vector2() which draws the font according to the to top left corner of the chart (offset by a few pixels)
                SharpDX.Vector2 upperTextPoint = new SharpDX.Vector2(ChartPanel.X + 10, ChartPanel.Y + 20);
                // Warning:  TextLayout objects must be disposed of after they have been used
                SharpDX.DirectWrite.TextLayout textLayout1 =
                    new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Globals.DirectWriteFactory,
                                                       NinjaTrader.Custom.Resource.SampleCustomPlotUpperLeftCorner, textFormat1, ChartPanel.X + ChartPanel.W,
                                                       textFormat1.FontSize);

                // With the format and layout of the text completed, we can now render the font to the chart
                RenderTarget.DrawTextLayout(upperTextPoint, textLayout1, textBrushDx,
                                            SharpDX.Direct2D1.DrawTextOptions.NoSnap);

                // 1.7 - Advanced Text Rendering

                // Font formatting and text layouts can get as complex as you need them to be
                // This example shows how to use a complete custom font unrelated to the existing user-defined chart control settings
                // Warning:  TextLayout and TextFormat objects must be disposed of after they have been used
                SharpDX.DirectWrite.TextFormat textFormat2 =
                    new SharpDX.DirectWrite.TextFormat(NinjaTrader.Core.Globals.DirectWriteFactory, "Century Gothic", FontWeight.Bold,
                                                       FontStyle.Italic, 32f);
                SharpDX.DirectWrite.TextLayout textLayout2 =
                    new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Globals.DirectWriteFactory,
                                                       NinjaTrader.Custom.Resource.SampleCustomPlotLowerRightCorner, textFormat2, 400, textFormat1.FontSize);

                // the textLayout object provides a way to measure the described font through a "Metrics" object
                // This allows you to create new vectors on the chart which are entirely dependent on the "text" that is being rendered
                // For example, we can create a rectangle that surrounds our font based off the textLayout which would dynamically change if the text used in the layout changed dynamically
                SharpDX.Vector2 lowerTextPoint = new SharpDX.Vector2(ChartPanel.W - textLayout2.Metrics.Width - 5,
                                                                     ChartPanel.Y + (ChartPanel.H - textLayout2.Metrics.Height));
                SharpDX.RectangleF rect1 = new SharpDX.RectangleF(lowerTextPoint.X, lowerTextPoint.Y, textLayout2.Metrics.Width,
                                                                  textLayout2.Metrics.Height);

                // We can draw the Rectangle based on the TextLayout used above
                RenderTarget.FillRectangle(rect1, smallAreaBrushDx);
                RenderTarget.DrawRectangle(rect1, smallAreaBrushDx, 2);

                // And render the advanced text layout using the DrawTextLayout() method
                // Note:  When drawing the same text repeatedly, using the DrawTextLayout() method is more efficient than using the DrawText()
                // because the text doesn't need to be formatted and the layout processed with each call
                RenderTarget.DrawTextLayout(lowerTextPoint, textLayout2, textBrushDx, SharpDX.Direct2D1.DrawTextOptions.NoSnap);

                // 1.8 - Cleanup
                // This concludes all of the rendering concepts used in the sample
                // However - there are some final clean up processes we should always provided before we are done

                // If changed, do not forget to set the AntialiasMode back to the default value as described above as a best practice
                RenderTarget.AntialiasMode = oldAntialiasMode;

                // We also need to make sure to dispose of every device dependent resource on each render pass
                // Failure to dispose of these resources will eventually result in unnecessary amounts of memory being used on the chart
                // Although the effects might not be obvious as first, if you see issues related to memory increasing over time
                // Objects such as these should be inspected first
                areaBrushDx.Dispose();
                customDXBrush.Dispose();
                gradientStopCollection.Dispose();
                radialGradientBrush.Dispose();
                smallAreaBrushDx.Dispose();
                textBrushDx.Dispose();
                textFormat1.Dispose();
                textFormat2.Dispose();
                textLayout1.Dispose();
                textLayout2.Dispose();
            }
        }
Beispiel #4
0
        /* Steps:
         *	1. Project start/end points for rays and extended lines
         *	2. Find collitions with ChartPanel for TextBox coordinates
         *	3. Determine price to be appended
         *	4. Create message
         *	5. Draw TextBox
         */

        public override void OnRender(ChartControl chartControl, ChartScale chartScale)
        {
            base.OnRender(chartControl, chartScale);

            Stroke.RenderTarget        = RenderTarget;
            OutlineStroke.RenderTarget = RenderTarget;

            bool             snap           = true;
            bool             startsOnScreen = true;
            bool             priceOffScreen = false;
            double           priceToUse     = 0;
            string           pricetime      = String.Empty;
            string           TextToDisplay  = DisplayText;
            MasterInstrument masterInst     = GetAttachedToChartBars().Bars.Instrument.MasterInstrument;

            Point startPoint = StartAnchor.GetPoint(chartControl, ChartPanel, chartScale);
            Point endPoint   = EndAnchor.GetPoint(chartControl, ChartPanel, chartScale);

            double strokePixAdj   = ((double)(Stroke.Width % 2)).ApproxCompare(0) == 0 ? 0.5d : 0d;
            Vector pixelAdjustVec = new Vector(strokePixAdj, strokePixAdj);

            Point startAdj = (LineType == ChartLineType.HorizontalLine ? new Point(ChartPanel.X, startPoint.Y) : new Point(startPoint.X, ChartPanel.Y)) + pixelAdjustVec;
            Point endAdj   = (LineType == ChartLineType.HorizontalLine ? new Point(ChartPanel.X + ChartPanel.W, startPoint.Y) : new Point(startPoint.X, ChartPanel.Y + ChartPanel.H)) + pixelAdjustVec;

            Vector distVec  = Vector.Divide(Point.Subtract(endPoint, startPoint), 100);
            Vector scalVec  = (LineType == ChartLineType.ExtendedLine || LineType == ChartLineType.Ray || LineType == ChartLineType.HorizontalLine) ? Vector.Multiply(distVec, 10000) : Vector.Multiply(distVec, 100);
            Point  extPoint = Vector.Add(scalVec, startPoint);

            // Project extended line start point if it is off screen
            if (LineType == ChartLineType.ExtendedLine && TextDisplayMode != TextMode.EndPoint)
            {
                startPoint = Point.Subtract(startPoint, scalVec);
            }

            // Project TextBox coordinate for extended lines and rays to get ChartPanel bounds
            if (LineType == ChartLineType.ExtendedLine || LineType == ChartLineType.Ray)
            {
                extPoint = Vector.Add(scalVec, extPoint);
            }

            // Find collisions with ChartPanel bounds for PriceScale bound TextBox coordinates
            if (LineType == ChartLineType.HorizontalLine || LineType == ChartLineType.VerticalLine)
            {
                extPoint   = endAdj;
                startPoint = startAdj;
            }
            else if (TextDisplayMode == TextMode.EndPoint)
            {
                extPoint = endPoint;
                snap     = false;
            }
            else
            {
                if (extPoint.X <= ChartPanel.X || extPoint.Y < ChartPanel.Y || extPoint.X > ChartPanel.W || extPoint.Y > ChartPanel.H)
                {
                    switch (LineIntersectsRect(startPoint, extPoint, new SharpDX.RectangleF(ChartPanel.X, ChartPanel.Y, ChartPanel.W, ChartPanel.H)))
                    {
                    case RectSide.Top:
                        extPoint = FindIntersection(startPoint, extPoint, new Point(ChartPanel.X, ChartPanel.Y), new Point(ChartPanel.W, ChartPanel.Y));
                        break;

                    case RectSide.Bottom:
                        extPoint = FindIntersection(startPoint, extPoint, new Point(ChartPanel.W, ChartPanel.H), new Point(ChartPanel.X, ChartPanel.H));
                        break;

                    case RectSide.Left:
                        extPoint = FindIntersection(startPoint, extPoint, new Point(ChartPanel.X, ChartPanel.H), new Point(ChartPanel.X, ChartPanel.Y));
                        break;

                    case RectSide.Right:
                        extPoint = FindIntersection(startPoint, extPoint, new Point(ChartPanel.W, ChartPanel.Y), new Point(ChartPanel.W, ChartPanel.H));
                        break;

                    default:
                        return;
                    }
                }

                if (startPoint.X <= ChartPanel.X || startPoint.Y < ChartPanel.Y || startPoint.X > ChartPanel.W || startPoint.Y > ChartPanel.H)
                {
                    switch (LineIntersectsRect(extPoint, startPoint, new SharpDX.RectangleF(ChartPanel.X, ChartPanel.Y, ChartPanel.W, ChartPanel.H)))
                    {
                    case RectSide.Top:
                        startPoint = FindIntersection(extPoint, startPoint, new Point(ChartPanel.X, ChartPanel.Y), new Point(ChartPanel.W, ChartPanel.Y));
                        break;

                    case RectSide.Bottom:
                        startPoint = FindIntersection(extPoint, startPoint, new Point(ChartPanel.W, ChartPanel.H), new Point(ChartPanel.X, ChartPanel.H));
                        break;

                    case RectSide.Left:
                        startPoint = FindIntersection(extPoint, startPoint, new Point(ChartPanel.X, ChartPanel.H), new Point(ChartPanel.X, ChartPanel.Y));
                        break;

                    case RectSide.Right:
                        startPoint = FindIntersection(extPoint, startPoint, new Point(ChartPanel.W, ChartPanel.Y), new Point(ChartPanel.W, ChartPanel.H));
                        break;

                    default:
                        return;
                    }
                }

                if (endPoint.X <= ChartPanel.X || endPoint.Y < ChartPanel.Y || endPoint.X > ChartPanel.W || endPoint.Y > ChartPanel.H)
                {
                    priceOffScreen = true;
                }
            }

            // Scale coordinates by HorizontalOffset/VerticalOffset
            distVec     = Point.Subtract(extPoint, startPoint);
            scalVec     = Vector.Multiply(Vector.Divide(distVec, 100), HorizontalOffset);
            extPoint    = Point.Subtract(extPoint, scalVec);
            extPoint.Y -= VerticalOffset;

            // Get a Price or a Timestamp to append to the label
            switch (LineType)
            {
            case ChartLineType.VerticalLine:
                pricetime = StartAnchor.Time.ToString();
                break;

            case ChartLineType.HorizontalLine:
                priceToUse = StartAnchor.Price;
                break;

            case ChartLineType.ExtendedLine:
            case ChartLineType.Ray:
                priceToUse = TextDisplayMode == TextMode.PriceScale
                                                           ? chartScale.GetValueByY(endPoint.X >= startPoint.X
                                                                                                                ? (float)FindIntersection(startPoint, endPoint, new Point(ChartPanel.W, ChartPanel.Y), new Point(ChartPanel.W, ChartPanel.H)).Y
                                                                                                                : (float)FindIntersection(startPoint, endPoint, new Point(ChartPanel.X, ChartPanel.Y), new Point(ChartPanel.X, ChartPanel.H)).Y)
                                                           : EndAnchor.Price;
                break;

            default:
                priceToUse = priceOffScreen && TextDisplayMode == TextMode.PriceScale
                                                           ? chartScale.GetValueByY(endPoint.X >= startPoint.X
                                                                                                                ? (float)FindIntersection(startPoint, endPoint, new Point(ChartPanel.W, ChartPanel.Y), new Point(ChartPanel.W, ChartPanel.H)).Y
                                                                                                                : (float)FindIntersection(startPoint, endPoint, new Point(ChartPanel.X, ChartPanel.Y), new Point(ChartPanel.X, ChartPanel.H)).Y)
                                                           : EndAnchor.Price;
                break;
            }

            // Round the price
            if (LineType != ChartLineType.VerticalLine)
            {
                pricetime = priceToUse <= masterInst.RoundDownToTickSize(priceToUse) + masterInst.TickSize * 0.5
                                                        ? pricetime = masterInst.RoundDownToTickSize(priceToUse).ToString("0.00")
                                                        : pricetime = masterInst.RoundToTickSize(priceToUse).ToString("0.00");
            }

            // Check if we need to append price or time
            if (AppendPriceTime && DisplayText.Length > 0)
            {
                TextToDisplay = String.Format("{0} {1}", DisplayText, pricetime);
            }
            else if (AppendPriceTime)
            {
                TextToDisplay = pricetime;
            }

            // Use Label Font if one is not specified by template
            if (Font == null)
            {
                Font = new NinjaTrader.Gui.Tools.SimpleFont(chartControl.Properties.LabelFont.Family.ToString(), 16);
            }

            // Update DX Brushes
            if (offScreenDXBrushNeedsUpdate)
            {
                offScreenDXBrush.Dispose();
                offScreenDXBrush            = offScreenMediaBrush.ToDxBrush(RenderTarget);
                offScreenDXBrushNeedsUpdate = false;
            }

            if (backgroundDXBrushNeedsUpdate)
            {
                backgroundDXBrush.Dispose();
                backgroundDXBrush            = backgroundMediaBrush.ToDxBrush(RenderTarget);
                backgroundDXBrush.Opacity    = (float)AreaOpacity / 100f;
                backgroundDXBrushNeedsUpdate = false;
            }

            // Draw TextBoxes
            switch (LineType)
            {
            case ChartLineType.VerticalLine:
                DrawTextBox(snap, TextToDisplay, extPoint.X, extPoint.Y, Stroke.BrushDX, backgroundDXBrush, OutlineStroke, 1.5708f);
                break;

            case ChartLineType.HorizontalLine:
                DrawTextBox(snap, TextToDisplay, extPoint.X, extPoint.Y, Stroke.BrushDX, backgroundDXBrush, OutlineStroke, 0);
                break;

            default:
                DrawTextBox(snap, TextToDisplay, extPoint.X, extPoint.Y, priceOffScreen && TextDisplayMode == TextMode.EndPointAtPriceScale ? offScreenDXBrush : Stroke.BrushDX, backgroundDXBrush, OutlineStroke, 0);
                break;
            }
        }
Beispiel #5
0
 public Indicators.JBMarketProfileLevels JBMarketProfileLevels(ISeries <double> input, int opacity, int brokenOpacity, bool showPOC, SolidColorBrush pOCColor, bool showHILO, SolidColorBrush hILOColor, bool showVA, SolidColorBrush vAColor, bool showONPOC, SolidColorBrush oNPOCColor, bool showONHILO, SolidColorBrush oNHILOColor, bool showONVA, SolidColorBrush oNVAColor, bool showSettlement, SolidColorBrush settlementColor, NinjaTrader.Gui.Tools.SimpleFont labelFont, int lineWidth, DashStyleHelper lineStyle, String rTHTemplate, String oNTemplate)
 {
     return(indicator.JBMarketProfileLevels(input, opacity, brokenOpacity, showPOC, pOCColor, showHILO, hILOColor, showVA, vAColor, showONPOC, oNPOCColor, showONHILO, oNHILOColor, showONVA, oNVAColor, showSettlement, settlementColor, labelFont, lineWidth, lineStyle, rTHTemplate, oNTemplate));
 }
Beispiel #6
0
 public JBMarketProfileLevels JBMarketProfileLevels(ISeries <double> input, int opacity, int brokenOpacity, bool showPOC, SolidColorBrush pOCColor, bool showHILO, SolidColorBrush hILOColor, bool showVA, SolidColorBrush vAColor, bool showONPOC, SolidColorBrush oNPOCColor, bool showONHILO, SolidColorBrush oNHILOColor, bool showONVA, SolidColorBrush oNVAColor, bool showSettlement, SolidColorBrush settlementColor, NinjaTrader.Gui.Tools.SimpleFont labelFont, int lineWidth, DashStyleHelper lineStyle, String rTHTemplate, String oNTemplate)
 {
     if (cacheJBMarketProfileLevels != null)
     {
         for (int idx = 0; idx < cacheJBMarketProfileLevels.Length; idx++)
         {
             if (cacheJBMarketProfileLevels[idx] != null && cacheJBMarketProfileLevels[idx].Opacity == opacity && cacheJBMarketProfileLevels[idx].BrokenOpacity == brokenOpacity && cacheJBMarketProfileLevels[idx].ShowPOC == showPOC && cacheJBMarketProfileLevels[idx].POCColor == pOCColor && cacheJBMarketProfileLevels[idx].ShowHILO == showHILO && cacheJBMarketProfileLevels[idx].HILOColor == hILOColor && cacheJBMarketProfileLevels[idx].ShowVA == showVA && cacheJBMarketProfileLevels[idx].VAColor == vAColor && cacheJBMarketProfileLevels[idx].ShowONPOC == showONPOC && cacheJBMarketProfileLevels[idx].ONPOCColor == oNPOCColor && cacheJBMarketProfileLevels[idx].ShowONHILO == showONHILO && cacheJBMarketProfileLevels[idx].ONHILOColor == oNHILOColor && cacheJBMarketProfileLevels[idx].ShowONVA == showONVA && cacheJBMarketProfileLevels[idx].ONVAColor == oNVAColor && cacheJBMarketProfileLevels[idx].ShowSettlement == showSettlement && cacheJBMarketProfileLevels[idx].SettlementColor == settlementColor && cacheJBMarketProfileLevels[idx].LabelFont == labelFont && cacheJBMarketProfileLevels[idx].LineWidth == lineWidth && cacheJBMarketProfileLevels[idx].LineStyle == lineStyle && cacheJBMarketProfileLevels[idx].RTHTemplate == rTHTemplate && cacheJBMarketProfileLevels[idx].ONTemplate == oNTemplate && cacheJBMarketProfileLevels[idx].EqualsInput(input))
             {
                 return(cacheJBMarketProfileLevels[idx]);
             }
         }
     }
     return(CacheIndicator <JBMarketProfileLevels>(new JBMarketProfileLevels()
     {
         Opacity = opacity, BrokenOpacity = brokenOpacity, ShowPOC = showPOC, POCColor = pOCColor, ShowHILO = showHILO, HILOColor = hILOColor, ShowVA = showVA, VAColor = vAColor, ShowONPOC = showONPOC, ONPOCColor = oNPOCColor, ShowONHILO = showONHILO, ONHILOColor = oNHILOColor, ShowONVA = showONVA, ONVAColor = oNVAColor, ShowSettlement = showSettlement, SettlementColor = settlementColor, LabelFont = labelFont, LineWidth = lineWidth, LineStyle = lineStyle, RTHTemplate = rTHTemplate, ONTemplate = oNTemplate
     }, input, ref cacheJBMarketProfileLevels));
 }
Beispiel #7
0
 public Indicators.JBWeisWave JBWeisWave(ISeries <double> input, int length, SwingStyle style, bool showLines, bool showVolumes, bool showDeltas, bool showTail, int volumeLabelBarSpacing, int deltaLabelBarSpacing, int waveLineWidth, DashStyleHelper waveLineStyle, SolidColorBrush waveColor, NinjaTrader.Gui.Tools.SimpleFont volumeTextFont, SolidColorBrush upVolColor, SolidColorBrush downVolColor, NinjaTrader.Gui.Tools.SimpleFont deltaTextFont, SolidColorBrush upDeltaColor, SolidColorBrush downDeltaColor)
 {
     return(indicator.JBWeisWave(input, length, style, showLines, showVolumes, showDeltas, showTail, volumeLabelBarSpacing, deltaLabelBarSpacing, waveLineWidth, waveLineStyle, waveColor, volumeTextFont, upVolColor, downVolColor, deltaTextFont, upDeltaColor, downDeltaColor));
 }
Beispiel #8
0
 public JBWeisWave JBWeisWave(ISeries <double> input, int length, SwingStyle style, bool showLines, bool showVolumes, bool showDeltas, bool showTail, int volumeLabelBarSpacing, int deltaLabelBarSpacing, int waveLineWidth, DashStyleHelper waveLineStyle, SolidColorBrush waveColor, NinjaTrader.Gui.Tools.SimpleFont volumeTextFont, SolidColorBrush upVolColor, SolidColorBrush downVolColor, NinjaTrader.Gui.Tools.SimpleFont deltaTextFont, SolidColorBrush upDeltaColor, SolidColorBrush downDeltaColor)
 {
     if (cacheJBWeisWave != null)
     {
         for (int idx = 0; idx < cacheJBWeisWave.Length; idx++)
         {
             if (cacheJBWeisWave[idx] != null && cacheJBWeisWave[idx].Length == length && cacheJBWeisWave[idx].Style == style && cacheJBWeisWave[idx].ShowLines == showLines && cacheJBWeisWave[idx].ShowVolumes == showVolumes && cacheJBWeisWave[idx].ShowDeltas == showDeltas && cacheJBWeisWave[idx].ShowTail == showTail && cacheJBWeisWave[idx].VolumeLabelBarSpacing == volumeLabelBarSpacing && cacheJBWeisWave[idx].DeltaLabelBarSpacing == deltaLabelBarSpacing && cacheJBWeisWave[idx].WaveLineWidth == waveLineWidth && cacheJBWeisWave[idx].WaveLineStyle == waveLineStyle && cacheJBWeisWave[idx].WaveColor == waveColor && cacheJBWeisWave[idx].VolumeTextFont == volumeTextFont && cacheJBWeisWave[idx].UpVolColor == upVolColor && cacheJBWeisWave[idx].DownVolColor == downVolColor && cacheJBWeisWave[idx].DeltaTextFont == deltaTextFont && cacheJBWeisWave[idx].UpDeltaColor == upDeltaColor && cacheJBWeisWave[idx].DownDeltaColor == downDeltaColor && cacheJBWeisWave[idx].EqualsInput(input))
             {
                 return(cacheJBWeisWave[idx]);
             }
         }
     }
     return(CacheIndicator <JBWeisWave>(new JBWeisWave()
     {
         Length = length, Style = style, ShowLines = showLines, ShowVolumes = showVolumes, ShowDeltas = showDeltas, ShowTail = showTail, VolumeLabelBarSpacing = volumeLabelBarSpacing, DeltaLabelBarSpacing = deltaLabelBarSpacing, WaveLineWidth = waveLineWidth, WaveLineStyle = waveLineStyle, WaveColor = waveColor, VolumeTextFont = volumeTextFont, UpVolColor = upVolColor, DownVolColor = downVolColor, DeltaTextFont = deltaTextFont, UpDeltaColor = upDeltaColor, DownDeltaColor = downDeltaColor
     }, input, ref cacheJBWeisWave));
 }
        protected override void OnBarUpdate()
        {
            switch (pOrT)
            {
            case PriceOrTime.Price:
            {
                MAX_HIGH = Math.Max(MAX_HIGH, High[0]);
                MIN_LOW  = Math.Min(MIN_LOW, Low[0]);

                if (CurrentBar == Bars.Count - 2)
                {
                    MAX_HIGH = Math.Round(MAX_HIGH * 1.05, PriceDigits);
                    MIN_LOW  = Math.Round(MIN_LOW * 0.95, PriceDigits);

                    if (InitialPrice < MIN_LOW || InitialPrice > MAX_HIGH)
                    {
                        Log("InitialPrice parameter MUST be between the high price and low price on this chart", NinjaTrader.Cbi.LogLevel.Information);
                        if (InitialPrice > MAX_HIGH)
                        {
                            InitialPrice = MAX_HIGH;
                        }
                        if (InitialPrice < MIN_LOW)
                        {
                            InitialPrice = MIN_LOW;
                        }
                    }


                    double LevelUp = -1.0, LevelDown = -1.0;
                    i = 1;

                    do
                    {
                        if (Angle000Flag)
                        {
                            LevelUp = InitialPrice + Angle0[i] * TickSize * MultiplierForPriceScale;    LevelDown = InitialPrice - Angle0[i] * TickSize * MultiplierForPriceScale;   MakePriceLine(LevelUp, LevelDown, Color0);
                        }
                        if (Angle090Flag)
                        {
                            LevelUp = InitialPrice + Angle90[i] * TickSize * MultiplierForPriceScale;   LevelDown = InitialPrice - Angle90[i] * TickSize * MultiplierForPriceScale;  MakePriceLine(LevelUp, LevelDown, Color90);
                        }
                        if (Angle180Flag)
                        {
                            LevelUp = InitialPrice + Angle180[i] * TickSize * MultiplierForPriceScale;  LevelDown = InitialPrice - Angle180[i] * TickSize * MultiplierForPriceScale; MakePriceLine(LevelUp, LevelDown, Color180);
                        }
                        if (Angle270Flag)
                        {
                            LevelUp = InitialPrice + Angle270[i] * TickSize * MultiplierForPriceScale;  LevelDown = InitialPrice - Angle270[i] * TickSize * MultiplierForPriceScale; MakePriceLine(LevelUp, LevelDown, Color270);
                        }
                        i++;
                        if (LevelUp < 0.0 || LevelDown < 0.0)
                        {
                            break;
                        }
                        if (i >= Angle0.Length)
                        {
                            break;
                        }
                        if (i >= Angle90.Length)
                        {
                            break;
                        }
                        if (i >= Angle180.Length)
                        {
                            break;
                        }
                        if (i >= Angle270.Length)
                        {
                            break;
                        }

                        if (LevelUp > MaxLevelUp)
                        {
                            MaxLevelUp = LevelUp;
                        }
                        if (LevelDown < MinLevelDown)
                        {
                            MinLevelDown = LevelDown;
                        }
                    }while (1 == 1);
                }
                break;
            }

            case PriceOrTime.Time:
            {
                if (ZuluTime.CompareTo(Time[0]) < 0 && ZuluBar < 0)
                {
                    ZuluBar = CurrentBar;                                                                   //set ZuluBar to time the user selected
                }
                Print("Zulubar" + ZuluBar.ToString());

                if (ZuluBar > 0)
                {
                    OutString = null;
                    if (Angle000Flag)
                    {
                        i = Angle0[NextBar0] + ZuluBar;
                        if (i == CurrentBar)
                        {
                            MakeVerticalLine(0, Color0); NextBar0++;
                        }
                        OutString = "Zero line coming in " + (i - CurrentBar).ToString() + " bars" + Environment.NewLine;
                    }
                    if (Angle090Flag)
                    {
                        i = Angle90[NextBar90] + ZuluBar;
                        if (i == CurrentBar)
                        {
                            MakeVerticalLine(0, Color90); NextBar90++;
                        }
                        OutString = OutString + "90 line coming in " + (i - CurrentBar).ToString() + " bars" + Environment.NewLine;
                    }
                    if (Angle180Flag)
                    {
                        i = Angle180[NextBar180] + ZuluBar;
                        if (i == CurrentBar)
                        {
                            MakeVerticalLine(0, Color180); NextBar180++;
                        }
                        OutString = OutString + "180 line coming in " + (i - CurrentBar).ToString() + " bars" + Environment.NewLine;
                    }
                    if (Angle270Flag)

                    {
                        i = Angle270[NextBar270] + ZuluBar;
                        if (i == CurrentBar)
                        {
                            MakeVerticalLine(0, Color270); NextBar270++;
                        }
                        OutString = OutString + "270 line coming in " + (i - CurrentBar).ToString() + " bars";
                    }

                    if (OutString.Length > 0 && DisplayCountDown)
                    {
                        lastBar  = Math.Min(ChartControl.LastSlotPainted, Bars.Count - 1);
                        firstBar = (lastBar - ChartControl.SlotsPainted) + 1;
                        int i;
                        // Find highest and lowest price points
                        HighestPaintedPrice = double.MinValue;
                        LowestPaintedPrice  = double.MaxValue;
                        for (i = firstBar; i <= lastBar && i >= 0; i++)
                        {
                            HighestPaintedPrice = Math.Max(HighestPaintedPrice, Bars.GetHigh(i));
                            LowestPaintedPrice  = Math.Min(LowestPaintedPrice, Bars.GetLow(i));
                        }

                        double Outprice = (HighestPaintedPrice + LowestPaintedPrice) / 2.0;
                        if (Bars.GetClose(lastBar - 1) < Outprice)
                        {
                            Outprice = HighestPaintedPrice;
                        }
                        if (Bars.GetClose(lastBar - 1) >= Outprice)
                        {
                            Outprice = (Outprice + LowestPaintedPrice) / 2.0;
                        }

                        Print(OutString);

                        NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Arial", 10)
                        {
                            Size = 20, Bold = false
                        };

                        if (CurrentBar > 10)
                        {
                            Draw.Text(this, "Info", true, OutString, 10, Outprice, 0, Brushes.Gray, myFont, TextAlignment.Justify, Brushes.White, Brushes.Transparent, 50);
                        }
                    }
                }

                break;
            }
            }
        }
Beispiel #10
0
        protected override void OnRender(ChartControl chartControl, ChartScale chartScale)
        {
            // Tip: This check is simply added to prevent the Indicator dialog menu from opening as a user clicks on the chart
            // The default behavior is to open the Indicator dialog menu if a user double clicks on the indicator
            // (i.e, the indicator falls within the RenderTarget "hit testing")
            // You can remove this check if you want the default behavior implemented
            if (!IsInHitTest)
            {
                // 1.2 - SharpDX Brush Resources
                // RenderTarget commands must use a special brush resource defined in the SharpDX.Direct2D1 namespace
                // These resources exist just like you will find in the WPF/Windows.System.Media namespace
                // such as SolidColorBrushes, LienarGraidentBrushes, RadialGradientBrushes, etc.
                // To begin, we will start with the most basic "Brush" type
                // Warning:  Brush objects must be disposed of after they have been used
                SharpDX.Direct2D1.Brush areaBrushDx;
                SharpDX.Direct2D1.Brush smallAreaBrushDx;
                SharpDX.Direct2D1.Brush textBrushDx;
                // for convenience, you can simply convert a WPF Brush to a DXBrush using the ToDxBrush() extension method provided by NinjaTrader
                // This is a common approach if you have a Brush property created e.g., on the UI you wish to use in custom rendering routines
                areaBrushDx      = areaBrush.ToDxBrush(RenderTarget);
                smallAreaBrushDx = smallAreaBrush.ToDxBrush(RenderTarget);
                textBrushDx      = textBrush.ToDxBrush(RenderTarget);
                // 1.3 - Using The RenderTarget
                // before executing chart commands, you have the ability to describe how the RenderTarget should render
                // for example, we can store the existing RenderTarget AntialiasMode mode
                // then update the AntialiasMode to be the quality of non-text primitives are rendered
                SharpDX.Direct2D1.AntialiasMode oldAntialiasMode = RenderTarget.AntialiasMode;
                RenderTarget.AntialiasMode = SharpDX.Direct2D1.AntialiasMode.Aliased;
                // 1.6 - Simple Text Rendering

                // For rendering custom text to the Chart, there are a few ways you can approach depending on your requirements
                // The most straight forward way is to "borrow" the existing chartControl font provided as a "SimpleFont" class
                // Using the chartControl LabelFont, your custom object will also change to the user defined properties allowing
                // your object to match different fonts if defined by user.

                // The code below will use the chartControl Properties Label Font if it exists,
                // or fall back to a default property if it cannot obtain that value
                NinjaTrader.Gui.Tools.SimpleFont simpleFont = chartControl.Properties.LabelFont ?? new NinjaTrader.Gui.Tools.SimpleFont("Arial", 12);

                // the advantage of using a SimpleFont is they are not only very easy to describe
                // but there is also a convenience method which can be used to convert the SimpleFont to a SharpDX.DirectWrite.TextFormat used to render to the chart
                // Warning:  TextFormat objects must be disposed of after they have been used
                SharpDX.DirectWrite.TextFormat textFormat1 = simpleFont.ToDirectWriteTextFormat();

                // Once you have the format of the font, you need to describe how the font needs to be laid out
                // Here we will create a new Vector2() which draws the font according to the to top left corner of the chart (offset by a few pixels)
                SharpDX.Vector2 upperTextPoint = new SharpDX.Vector2(ChartPanel.X + 10, ChartPanel.Y + 20);
                // Warning:  TextLayout objects must be disposed of after they have been used
                SharpDX.DirectWrite.TextLayout textLayout1 =
                    new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Globals.DirectWriteFactory,
                                                       NinjaTrader.Custom.Resource.SampleCustomPlotUpperLeftCorner, textFormat1, ChartPanel.X + ChartPanel.W,
                                                       textFormat1.FontSize);

//			// With the format and layout of the text completed, we can now render the font to the chart
//			RenderTarget.DrawTextLayout(upperTextPoint, textLayout1, textBrushDx,
//				SharpDX.Direct2D1.DrawTextOptions.NoSnap);

//			// 1.7 - Advanced Text Rendering

//			// Font formatting and text layouts can get as complex as you need them to be
//			// This example shows how to use a complete custom font unrelated to the existing user-defined chart control settings
//			// Warning:  TextLayout and TextFormat objects must be disposed of after they have been used
                SharpDX.DirectWrite.TextFormat textFormat2 =
                    new SharpDX.DirectWrite.TextFormat(NinjaTrader.Core.Globals.DirectWriteFactory, "Century Gothic", FontWeight.Bold,
                                                       FontStyle.Italic, 32f);
                SharpDX.DirectWrite.TextLayout textLayout2 =
                    new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Globals.DirectWriteFactory,
                                                       NinjaTrader.Custom.Resource.SampleCustomPlotLowerRightCorner, textFormat2, 400, textFormat1.FontSize);

//			// the textLayout object provides a way to measure the described font through a "Metrics" object
//			// This allows you to create new vectors on the chart which are entirely dependent on the "text" that is being rendered
//			// For example, we can create a rectangle that surrounds our font based off the textLayout which would dynamically change if the text used in the layout changed dynamically
                SharpDX.Vector2 lowerTextPoint = new SharpDX.Vector2(ChartPanel.W - textLayout2.Metrics.Width - 5,
                                                                     ChartPanel.Y + (ChartPanel.H - textLayout2.Metrics.Height));
                SharpDX.RectangleF rect1 = new SharpDX.RectangleF(lowerTextPoint.X, lowerTextPoint.Y, textLayout2.Metrics.Width,
                                                                  textLayout2.Metrics.Height);

//			// We can draw the Rectangle based on the TextLayout used above
                RenderTarget.FillRectangle(rect1, smallAreaBrushDx);
//			RenderTarget.DrawRectangle(rect1, smallAreaBrushDx, 2);

                // And render the advanced text layout using the DrawTextLayout() method
                // Note:  When drawing the same text repeatedly, using the DrawTextLayout() method is more efficient than using the DrawText()
                // because the text doesn't need to be formatted and the layout processed with each call
                RenderTarget.DrawTextLayout(lowerTextPoint, textLayout2, textBrushDx, SharpDX.Direct2D1.DrawTextOptions.NoSnap);

                // 1.8 - Cleanup
                // This concludes all of the rendering concepts used in the sample
                // However - there are some final clean up processes we should always provided before we are done

                // If changed, do not forget to set the AntialiasMode back to the default value as described above as a best practice
                RenderTarget.AntialiasMode = oldAntialiasMode;

                // We also need to make sure to dispose of every device dependent resource on each render pass
                // Failure to dispose of these resources will eventually result in unnecessary amounts of memory being used on the chart
                // Although the effects might not be obvious as first, if you see issues related to memory increasing over time
                // Objects such as these should be inspected first
                areaBrushDx.Dispose();
                //		customDXBrush.Dispose();
                //		gradientStopCollection.Dispose();
                //radialGradientBrush.Dispose();
                smallAreaBrushDx.Dispose();
                textBrushDx.Dispose();
                textFormat1.Dispose();
                textFormat2.Dispose();
                textLayout1.Dispose();
                textLayout2.Dispose();
            }
        }