Ejemplo n.º 1
0
        public double GetAvgPrice()
        {
            MasterInstrument maIns = Bars.Instrument.MasterInstrument;

            if (IsLiveTrading())
            {
                return(maIns.RoundToTickSize(PositionAccount.AveragePrice));
            }
            else
            {
                return(maIns.RoundToTickSize(Position.AveragePrice));
            }
        }
Ejemplo n.º 2
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;
            }
        }
Ejemplo n.º 3
0
        public double GetTypicalPrice(int barsAgo)
        {
            MasterInstrument maIns = Bars.Instrument.MasterInstrument;

            return(maIns.RoundToTickSize(Typical[barsAgo]));
        }