Esempio n. 1
0
        public override bool IsAlertConditionTrue(AlertConditionItem conditionItem, Condition condition, ChartAlertValue[] values,
                                                  ChartControl chartControl, ChartScale chartScale)
        {
            ChartPanel chartPanel = chartControl.ChartPanels[PanelIndex];
            double     barX       = chartControl.GetXByTime(values[0].Time);
            double     barY       = chartScale.GetYByValue(values[0].Value);
            Point      startPoint = StartAnchor.GetPoint(chartControl, chartPanel, chartScale);
            Point      endPoint   = EndAnchor.GetPoint(chartControl, chartPanel, chartScale);
            Point      barPoint   = new Point(barX, barY);

            if (arcGeometry == null || arcGeometry.IsDisposed)
            {
                UpdateArcGeometry(chartControl, chartPanel, chartScale);
            }

            // Bars have not yet reached edge of drawing tool
            if (barX < Math.Min(startPoint.X, endPoint.X))
            {
                return(false);
            }

            // Do two things, make sure the point is on the right side of the line (the side arc is sweeped into),
            // and if it is, then check if it is in arc geo
            MathHelper.PointLineLocation ptLineLocation = MathHelper.GetPointLineLocation(startPoint, endPoint, barPoint);
            // If its not right/above , its past the line the arc sweeps on, so ignore
            if (ptLineLocation != MathHelper.PointLineLocation.RightOrBelow)
            {
                return(false);
            }

            // Our only conditions are cross inside/outside
            Predicate <ChartAlertValue> arcPredicate = v =>
            {
                if (v.Time == Core.Globals.MinDate || v.Time == Core.Globals.MaxDate)
                {
                    return(false);
                }
                if (v.Value == double.MinValue)
                {
                    return(false);
                }

                double bX      = chartControl.GetXByTime(v.Time);
                double bY      = chartScale.GetYByValue(v.Value);
                Point  bp      = new Point(bX, bY);
                bool   isInGeo = arcGeometry.FillContainsPoint(bp.ToVector2());
                return(condition == Condition.CrossInside ? isInGeo : !isInGeo);
            };

            return(MathHelper.DidPredicateCross(values, arcPredicate));
        }
Esempio n. 2
0
        public override bool IsAlertConditionTrue(AlertConditionItem conditionItem, Condition condition, ChartAlertValue[] values, ChartControl chartControl, ChartScale chartScale)
        {
            PathToolSegment segAnchors = conditionItem.Tag as PathToolSegment;

            if (segAnchors == null)
            {
                return(false);
            }

            ChartPanel chartPanel     = chartControl.ChartPanels[PanelIndex];
            Point      lineStartPoint = segAnchors.StartAnchor.GetPoint(chartControl, chartPanel, chartScale);
            Point      lineEndPoint   = segAnchors.EndAnchor.GetPoint(chartControl, chartPanel, chartScale);
            double     minLineX       = double.MaxValue;
            double     maxLineX       = double.MinValue;

            foreach (Point point in new[] { lineStartPoint, lineEndPoint })
            {
                minLineX = Math.Min(minLineX, point.X);
                maxLineX = Math.Max(maxLineX, point.X);
            }

            // first thing, if our smallest x is greater than most recent bar, we have nothing to do yet.
            // do not try to check Y because lines could cross through stuff
            double firstBarX = values[0].ValueType == ChartAlertValueType.StaticValue ? minLineX : chartControl.GetXByTime(values[0].Time);
            double firstBarY = chartScale.GetYByValue(values[0].Value);

            // dont have to take extension into account as its already handled in min/max line x

            // bars completely passed our line
            if (maxLineX < firstBarX)
            {
                return(false);
            }

            // bars not yet to our line
            if (minLineX > firstBarX)
            {
                return(false);
            }

            // NOTE: normalize line points so the leftmost is passed first. Otherwise, our vector
            // math could end up having the line normal vector being backwards if user drew it backwards.
            // but we dont care the order of anchors, we want 'up' to mean 'up'!
            Point leftPoint  = lineStartPoint.X < lineEndPoint.X ? lineStartPoint : lineEndPoint;
            Point rightPoint = lineEndPoint.X > lineStartPoint.X ? lineEndPoint : lineStartPoint;
            Point barPoint   = new Point(firstBarX, firstBarY);

            // NOTE: 'left / right' is relative to if line was vertical. it can end up backwards too
            MathHelper.PointLineLocation pointLocation = MathHelper.GetPointLineLocation(leftPoint, rightPoint, barPoint);

            // for vertical things, think of a vertical line rotated 90 degrees to lay flat, where it's normal vector is 'up'
            switch (condition)
            {
            case Condition.Greater:                 return(pointLocation == MathHelper.PointLineLocation.LeftOrAbove);

            case Condition.GreaterEqual:    return(pointLocation == MathHelper.PointLineLocation.LeftOrAbove || pointLocation == MathHelper.PointLineLocation.DirectlyOnLine);

            case Condition.Less:                    return(pointLocation == MathHelper.PointLineLocation.RightOrBelow);

            case Condition.LessEqual:               return(pointLocation == MathHelper.PointLineLocation.RightOrBelow || pointLocation == MathHelper.PointLineLocation.DirectlyOnLine);

            case Condition.Equals:                  return(pointLocation == MathHelper.PointLineLocation.DirectlyOnLine);

            case Condition.NotEqual:                return(pointLocation != MathHelper.PointLineLocation.DirectlyOnLine);

            case Condition.CrossAbove:
            case Condition.CrossBelow:
                Predicate <ChartAlertValue> predicate = v =>
                {
                    double barX         = chartControl.GetXByTime(v.Time);
                    double barY         = chartScale.GetYByValue(v.Value);
                    Point  stepBarPoint = new Point(barX, barY);
                    MathHelper.PointLineLocation ptLocation = MathHelper.GetPointLineLocation(leftPoint, rightPoint, stepBarPoint);

                    if (condition == Condition.CrossAbove)
                    {
                        return(ptLocation == MathHelper.PointLineLocation.LeftOrAbove);
                    }
                    return(ptLocation == MathHelper.PointLineLocation.RightOrBelow);
                };

                return(MathHelper.DidPredicateCross(values, predicate));
            }

            return(false);
        }
Esempio n. 3
0
        public override bool IsAlertConditionTrue(AlertConditionItem conditionItem, Condition condition, ChartAlertValue[] values, ChartControl chartControl, ChartScale chartScale)
        {
            // dig up which anchor we are running on to determine line
            ChartAnchor chartAnchor = conditionItem.Tag as ChartAnchor;

            if (chartAnchor == null)
            {
                return(false);
            }

            ChartPanel chartPanel = chartControl.ChartPanels[PanelIndex];
            double     alertY = chartScale.GetYByValue(chartAnchor.Price);
            Point      entryPoint = EntryAnchor.GetPoint(chartControl, chartPanel, chartScale);
            Point      stopPoint = RiskAnchor.GetPoint(chartControl, chartPanel, chartScale);
            Point      targetPoint = RewardAnchor.GetPoint(chartControl, chartPanel, chartScale);
            double     anchorMinX = DrawTarget ? new[] { entryPoint.X, stopPoint.X, targetPoint.X }.Min() : new[] { entryPoint.X, stopPoint.X }.Min();
            double     anchorMaxX = DrawTarget ? new[] { entryPoint.X, stopPoint.X, targetPoint.X }.Max() : new[] { entryPoint.X, stopPoint.X }.Max();
            double     lineStartX = IsExtendedLinesLeft ? chartPanel.X : anchorMinX;
            double     lineEndX   = IsExtendedLinesRight ? chartPanel.X + chartPanel.W : anchorMaxX;

            // first thing, if our smallest x is greater than most recent bar, we have nothing to do yet.
            // do not try to check Y because lines could cross through stuff
            double firstBarX = chartControl.GetXByTime(values[0].Time);
            double firstBarY = chartScale.GetYByValue(values[0].Value);

            if (lineEndX < firstBarX)             // bars passed our drawing tool
            {
                return(false);
            }

            Point lineStartPoint = new Point(lineStartX, alertY);
            Point lineEndPoint   = new Point(lineEndX, alertY);

            Point barPoint = new Point(firstBarX, firstBarY);

            // NOTE: 'left / right' is relative to if line was vertical. it can end up backwards too
            MathHelper.PointLineLocation pointLocation = MathHelper.GetPointLineLocation(lineStartPoint, lineEndPoint, barPoint);
            // for vertical things, think of a vertical line rotated 90 degrees to lay flat, where it's normal vector is 'up'
            switch (condition)
            {
            case Condition.Greater:                 return(pointLocation == MathHelper.PointLineLocation.LeftOrAbove);

            case Condition.GreaterEqual:    return(pointLocation == MathHelper.PointLineLocation.LeftOrAbove || pointLocation == MathHelper.PointLineLocation.DirectlyOnLine);

            case Condition.Less:                    return(pointLocation == MathHelper.PointLineLocation.RightOrBelow);

            case Condition.LessEqual:               return(pointLocation == MathHelper.PointLineLocation.RightOrBelow || pointLocation == MathHelper.PointLineLocation.DirectlyOnLine);

            case Condition.Equals:                  return(pointLocation == MathHelper.PointLineLocation.DirectlyOnLine);

            case Condition.NotEqual:                return(pointLocation != MathHelper.PointLineLocation.DirectlyOnLine);

            case Condition.CrossAbove:
            case Condition.CrossBelow:
                Predicate <ChartAlertValue> predicate = v =>
                {
                    double barX         = chartControl.GetXByTime(v.Time);
                    double barY         = chartScale.GetYByValue(v.Value);
                    Point  stepBarPoint = new Point(barX, barY);
                    // NOTE: 'left / right' is relative to if line was vertical. it can end up backwards too
                    MathHelper.PointLineLocation ptLocation = MathHelper.GetPointLineLocation(lineStartPoint, lineEndPoint, stepBarPoint);
                    if (condition == Condition.CrossAbove)
                    {
                        return(ptLocation == MathHelper.PointLineLocation.LeftOrAbove);
                    }
                    return(ptLocation == MathHelper.PointLineLocation.RightOrBelow);
                };
                return(MathHelper.DidPredicateCross(values, predicate));
            }
            return(false);
        }
Esempio n. 4
0
        public override bool IsAlertConditionTrue(AlertConditionItem conditionItem, Condition condition, ChartAlertValue[] values, ChartControl chartControl, ChartScale chartScale)
        {
            GannAngle gannAngle = conditionItem.Tag as GannAngle;

            if (gannAngle == null)
            {
                return(false);
            }

            ChartPanel chartPanel  = chartControl.ChartPanels[PanelIndex];
            Point      anchorPoint = Anchor.GetPoint(chartControl, chartPanel, chartScale);

            // dig out the line we're running on
            double dx               = gannAngle.RatioX * chartControl.Properties.BarDistance;
            double dVal             = chartScale.GetPixelsForDistance(gannAngle.RatioY * chartControl.Instrument.MasterInstrument.TickSize);
            Point  stepPoint        = GetGannStepPoint(chartScale, anchorPoint.X, Anchor.Price, dx, dVal);
            Point  extendedEndPoint = GetExtendedPoint(anchorPoint, stepPoint);

            if (values[0].ValueType == ChartAlertValueType.StaticTime)
            {
                int checkX = chartControl.GetXByTime(values[0].Time);
                return(stepPoint.X >= checkX || stepPoint.X >= checkX);
            }

            double barX     = chartControl.GetXByTime(values[0].Time);
            double barY     = chartScale.GetYByValue(values[0].Value);
            Point  barPoint = new Point(barX, barY);

            // bars passed our drawing tool line
            if (extendedEndPoint.X < barX)
            {
                return(false);
            }

            // bars not yet to our drawing tool line
            if (stepPoint.X > barY)
            {
                return(false);
            }

            if (condition == Condition.CrossAbove || condition == Condition.CrossBelow)
            {
                Predicate <ChartAlertValue> predicate = v =>
                {
                    // bar x/y
                    double bX           = chartControl.GetXByTime(v.Time);
                    double bY           = chartScale.GetYByValue(v.Value);
                    Point  stepBarPoint = new Point(bX, bY);
                    // NOTE: 'left / right' is relative to if line was vertical. it can end up backwards too
                    MathHelper.PointLineLocation ptLocation = MathHelper.GetPointLineLocation(anchorPoint, extendedEndPoint, stepBarPoint);
                    if (condition == Condition.CrossAbove)
                    {
                        return(ptLocation == MathHelper.PointLineLocation.LeftOrAbove);
                    }
                    return(ptLocation == MathHelper.PointLineLocation.RightOrBelow);
                };
                return(MathHelper.DidPredicateCross(values, predicate));
            }


            MathHelper.PointLineLocation pointLocation = MathHelper.GetPointLineLocation(anchorPoint, extendedEndPoint, barPoint);
            switch (condition)
            {
            case Condition.Greater:                 return(pointLocation == MathHelper.PointLineLocation.LeftOrAbove);

            case Condition.GreaterEqual:    return(pointLocation == MathHelper.PointLineLocation.LeftOrAbove || pointLocation == MathHelper.PointLineLocation.DirectlyOnLine);

            case Condition.Less:                    return(pointLocation == MathHelper.PointLineLocation.RightOrBelow);

            case Condition.LessEqual:               return(pointLocation == MathHelper.PointLineLocation.RightOrBelow || pointLocation == MathHelper.PointLineLocation.DirectlyOnLine);

            case Condition.Equals:                  return(pointLocation == MathHelper.PointLineLocation.DirectlyOnLine);

            case Condition.NotEqual:                return(pointLocation != MathHelper.PointLineLocation.DirectlyOnLine);

            default:                                                return(false);
            }
        }
Esempio n. 5
0
        public override bool IsAlertConditionTrue(AlertConditionItem conditionItem, Condition condition, ChartAlertValue[] values,
                                                  ChartControl chartControl, ChartScale chartScale)
        {
            PriceLevel priceLevel = conditionItem.Tag as PriceLevel;

            if (priceLevel == null)
            {
                return(false);
            }

            ChartPanel chartPanel           = chartControl.ChartPanels[PanelIndex];
            Point      anchorStartPoint     = StartAnchor.GetPoint(chartControl, chartPanel, chartScale);
            Point      anchorEndPoint       = EndAnchor.GetPoint(chartControl, chartPanel, chartScale);
            Point      anchorExtensionPoint = ExtensionAnchor.GetPoint(chartControl, chartPanel, chartScale);
            Point      midPointExtension    = new Point((anchorExtensionPoint.X + anchorEndPoint.X) / 2, (anchorExtensionPoint.Y + anchorEndPoint.Y) / 2);

            if (CalculationMethod == AndrewsPitchforkCalculationMethod.Schiff)
            {
                anchorStartPoint = new Point(anchorStartPoint.X, (anchorStartPoint.Y + anchorEndPoint.Y) / 2);
            }
            else if (CalculationMethod == AndrewsPitchforkCalculationMethod.ModifiedSchiff)
            {
                anchorStartPoint = new Point((anchorEndPoint.X + anchorStartPoint.X) / 2, (anchorEndPoint.Y + anchorStartPoint.Y) / 2);
            }

            double totalPriceRange = EndAnchor.Price - ExtensionAnchor.Price;
            double startPrice      = ExtensionAnchor.Price;

            double levelPrice = (startPrice + ((priceLevel.Value / 100) * totalPriceRange));
            float  pixelY     = chartScale.GetYByValue(levelPrice);
            float  pixelX     = anchorExtensionPoint.X > anchorEndPoint.X ?
                                (float)(anchorExtensionPoint.X - (Math.Abs((anchorEndPoint.X - anchorExtensionPoint.X) * (priceLevel.Value / 100)))) :
                                (float)(anchorExtensionPoint.X + ((anchorEndPoint.X - anchorExtensionPoint.X) * (priceLevel.Value / 100)));

            Point alertStartPoint = new Point(pixelX, pixelY);
            Point endPoint        = new Point(alertStartPoint.X + (midPointExtension.X - anchorStartPoint.X), alertStartPoint.Y + (midPointExtension.Y - anchorStartPoint.Y));
            Point alertEndPoint   = GetExtendedPoint(alertStartPoint, endPoint);

            double firstBarX = values[0].ValueType == ChartAlertValueType.StaticValue ? pixelX : chartControl.GetXByTime(values[0].Time);
            double firstBarY = chartScale.GetYByValue(values[0].Value);
            Point  barPoint  = new Point(firstBarX, firstBarY);

            // Check bars are not yet to our drawing tool
            if (firstBarX < alertStartPoint.X || firstBarX > alertEndPoint.X)
            {
                return(false);
            }

            // NOTE: 'left / right' is relative to if line was vertical. It can end up backwards too
            MathHelper.PointLineLocation pointLocation = MathHelper.GetPointLineLocation(alertStartPoint, alertEndPoint, barPoint);
            // For vertical things, think of a vertical line rotated 90 degrees to lay flat, where it's normal vector is 'up'
            switch (condition)
            {
            case Condition.Greater:                 return(pointLocation == MathHelper.PointLineLocation.LeftOrAbove);

            case Condition.GreaterEqual:    return(pointLocation == MathHelper.PointLineLocation.LeftOrAbove || pointLocation == MathHelper.PointLineLocation.DirectlyOnLine);

            case Condition.Less:                    return(pointLocation == MathHelper.PointLineLocation.RightOrBelow);

            case Condition.LessEqual:               return(pointLocation == MathHelper.PointLineLocation.RightOrBelow || pointLocation == MathHelper.PointLineLocation.DirectlyOnLine);

            case Condition.Equals:                  return(pointLocation == MathHelper.PointLineLocation.DirectlyOnLine);

            case Condition.NotEqual:                return(pointLocation != MathHelper.PointLineLocation.DirectlyOnLine);

            case Condition.CrossAbove:
            case Condition.CrossBelow:
                Predicate <ChartAlertValue> predicate = v =>
                {
                    double barX         = chartControl.GetXByTime(v.Time);
                    double barY         = chartScale.GetYByValue(v.Value);
                    Point  stepBarPoint = new Point(barX, barY);
                    // NOTE: 'left / right' is relative to if line was vertical. It can end up backwards too
                    MathHelper.PointLineLocation ptLocation = MathHelper.GetPointLineLocation(alertStartPoint, alertEndPoint, stepBarPoint);
                    if (condition == Condition.CrossAbove)
                    {
                        return(ptLocation == MathHelper.PointLineLocation.LeftOrAbove);
                    }
                    return(ptLocation == MathHelper.PointLineLocation.RightOrBelow);
                };
                return(MathHelper.DidPredicateCross(values, predicate));
            }

            return(false);
        }