internal override void DrawLineStudy(Types.RectEx rect, LineStatus lineStatus) { rect.Normalize(); _lines.C = C; _lines.Start(); Utils.DrawLine(rect.Left, rect.Top, rect.Left, rect.Bottom, Stroke, StrokeType, StrokeThickness, _lines); Utils.DrawLine(rect.Right, rect.Top, rect.Right, rect.Bottom, Stroke, StrokeType, StrokeThickness, _lines); foreach (double d in _fib) { double x = rect.Left + rect.Width * d; Utils.DrawLine(x, rect.Top, x, rect.Bottom, Stroke, StrokeType, StrokeThickness, _lines); } _lines.Stop(); _internalObjectCreated = true; if (_firstPaint) { _firstPaint = false; _lines.Do(l => { l._line.Tag = this; l.ZIndex = ZIndexConstants.LineStudies1; }); _contextLine = new ContextLine(this); _chartX.InvokeLineStudyCreated(new StockChartX.LineStudyCreatedEventArgs(this)); } }
internal override void DrawLineStudy(Types.RectEx rect, LineStatus lineStatus) { rect.Normalize(); if (lineStatus == LineStatus.StartPaint) { _internalObjectCreated = true; for (int i = 0; i < _ellipses.Length; i++) { _ellipses[i] = new System.Windows.Shapes.Ellipse { Tag = this, Stroke = Stroke, StrokeThickness = StrokeThickness, Fill = Brushes.Transparent }; C.Children.Add(_ellipses[i]); } new ContextLine(this); _chartX.InvokeLineStudyCreated(new StockChartX.LineStudyCreatedEventArgs(this)); return; } double elSize = Math.Max(rect.Width, rect.Height); Rect rc = new Rect(rect.Left, rect.Top, elSize, elSize); for (int i = 0; i < _ellipses.Length; i++) { if (rc.IsEmpty) { break; } #if SILVERLIGHT rc.X -= -elSize * 0.1; rc.Y -= -elSize * 0.1; rc.Width += 2 * (-elSize * 0.1); rc.Height += 2 * (-elSize * 0.1); #endif Canvas.SetLeft(_ellipses[i], rc.Left); Canvas.SetTop(_ellipses[i], rc.Top); _ellipses[i].Height = rc.Width; _ellipses[i].Width = rc.Height; #if WPF rc.Inflate(-elSize * 0.1, -elSize * 0.1); #endif } foreach (System.Windows.Shapes.Ellipse ellipse in _ellipses) { Canvas.SetZIndex(ellipse, ZIndexConstants.LineStudies1); } }
internal override void DrawLineStudy(Types.RectEx rect, LineStatus lineStatus) { // if (_ellipse == null && lineStatus != LineStatus.StartPaint) // DrawLineStudy(rect, LineStatus.StartPaint); // // if (_ellipse == null) // throw new NullReferenceException(); // if (lineStatus == LineStatus.StartPaint) { _ellipse.StrokeThickness = StrokeThickness; _ellipse.Stroke = Stroke; if (_ellipse.Fill == null) { _ellipse.Fill = new SolidColorBrush(Colors.Transparent); } _ellipse.Tag = this; C.Children.Add(_ellipse); if (_contextLine == null) { _contextLine = new ContextLine(this); } _ellipse.MouseLeftButtonDown += (sender, args) => MouseDown(sender, args); _ellipse.MouseEnter += (sender, args) => MouseEnter(sender, args); _ellipse.MouseLeave += (sender, args) => MouseLeave(sender, args); _ellipse.MouseMove += (sender, args) => MouseMove(sender, args); _ellipse.MouseLeftButtonUp += (sender, args) => MouseUp(sender, args); Canvas.SetZIndex(_ellipse, ZIndexConstants.LineStudies1); _chartX.InvokeLineStudyCreated(new StockChartX.LineStudyCreatedEventArgs(this)); return; } rect.Normalize(); Canvas.SetLeft(_ellipse, rect.Left); Canvas.SetTop(_ellipse, rect.Top); _ellipse.Width = rect.Width; _ellipse.Height = rect.Height; }
internal override void DrawLineStudy(Types.RectEx rect, LineStatus lineStatus) { if (Element == null) { Debug.WriteLine("FrameworkElement NULL"); return; } if (lineStatus == LineStatus.StartPaint) { C.Children.Add(Element); Canvas.SetZIndex(Element, ZIndexConstants.LineStudies1); Element.Tag = this; Element.SetBinding(Canvas.TopProperty, this.CreateOneWayBinding("CanvasTop")); Element.SetBinding(Canvas.LeftProperty, this.CreateOneWayBinding("CanvasLeft")); Element.SetBinding(System.Windows.FrameworkElement.WidthProperty, this.CreateOneWayBinding("ElementWidth")); Element.SetBinding(System.Windows.FrameworkElement.HeightProperty, this.CreateOneWayBinding("ElementHeight")); _internalObjectCreated = true; xEnumVisuals(Element); return; } if (Element.Visibility == Visibility.Collapsed) { Element.Visibility = Visibility.Visible; } rect.Normalize(); //Canvas.SetLeft(Element, rect.Left); //Canvas.SetTop(Element, rect.Top); //Element.Width = rect.Width; //Element.Height = rect.Height; CanvasLeft = rect.Left; CanvasTop = rect.Top; ElementWidth = rect.Width; ElementHeight = rect.Height; }
internal override void DrawLineStudy(Types.RectEx rect, LineStatus lineStatus) { if (!_internalObjectCreated && lineStatus != LineStatus.StartPaint) { DrawLineStudy(rect, LineStatus.StartPaint); } if (lineStatus == LineStatus.StartPaint) { _rectangle.StrokeThickness = StrokeThickness; _rectangle.Stroke = Stroke; if (_rectangle.Fill == null) { _rectangle.Fill = new SolidColorBrush(Colors.Transparent); } _rectangle.Tag = this; C.Children.Add(_rectangle); Canvas.SetZIndex(_rectangle, ZIndexConstants.LineStudies1); _rectangle.MouseLeftButtonDown += (sender, args) => MouseDown(sender, args); _rectangle.MouseEnter += (sender, args) => MouseEnter(sender, args); _rectangle.MouseLeave += (sender, args) => MouseLeave(sender, args); _rectangle.MouseMove += (sender, args) => MouseMove(sender, args); _rectangle.MouseLeftButtonUp += (sender, args) => MouseUp(sender, args); new ContextLine(this); _internalObjectCreated = true; _chartX.InvokeLineStudyCreated(new StockChartX.LineStudyCreatedEventArgs(this)); return; } rect.Normalize(); Canvas.SetLeft(_rectangle, rect.Left); Canvas.SetTop(_rectangle, rect.Top); _rectangle.Width = rect.Width; _rectangle.Height = rect.Height; }
internal override void DrawLineStudy(Types.RectEx rect, LineStatus lineStatus) { if (lineStatus == LineStatus.StartPaint) { for (int i = 0; i < _lines.Length; i++) { _lines[i] = new Line { Tag = this }; Canvas.SetZIndex(_lines[i], ZIndexConstants.LineStudies1); C.Children.Add(_lines[i]); } if (_contextLine == null) { _contextLine = new ContextLine(this); _chartX.InvokeLineStudyCreated(new StockChartX.LineStudyCreatedEventArgs(this)); } return; } // **************************************************************** // *Note: This line study requires OHLC series in the owner panel!* // **************************************************************** rect.Normalize(); if (rect.Width == 0) { return; } int revX1 = (int)(_chartX.GetReverseXInternal(rect.Left) + _chartX._startIndex); int revX2 = (int)(_chartX.GetReverseXInternal(rect.Right) + _chartX._startIndex); if (revX1 < 0) { revX1 = 0; } if (revX2 < 0) { revX2 = 0; } if (revX1 == revX2) { return; } // Get the highest high of the high series. // Note: this code makes the vague assumption // that only one symbol exists on this panel. Series sHigh = GetSeriesOHLC(SeriesTypeOHLC.High); if (sHigh == null) { return; } Series sLow = GetSeriesOHLC(SeriesTypeOHLC.Low); if (sLow == null) { return; } double highestHigh = sHigh.MaxFromInterval(ref revX1, ref revX2); double lowestLow = sLow.MinFromInterval(ref revX1, ref revX2); double value = highestHigh + ((highestHigh - lowestLow) / 4); _linesSel.C = C; _linesSel.Start(); for (int i = 0; i < _lines.Length; i++) { value -= ((highestHigh - lowestLow) / 4); rect.Top = _chartPanel.GetY(value); Utils.DrawLine(rect.Left, rect.Top, rect.Right, rect.Top, Stroke, i == 2 ? LinePattern.Dot : StrokeType, StrokeThickness, Opacity, _lines[i]); } if (lineStatus == LineStatus.Moving || lineStatus == LineStatus.Painting) { Utils.DrawLine(rect.Left, 0, rect.Left, C.ActualHeight, Stroke, LinePattern.Dot, StrokeThickness, Opacity, _linesSel); Utils.DrawLine(rect.Right, 0, rect.Right, C.ActualHeight, Stroke, LinePattern.Dot, StrokeThickness, Opacity, _linesSel); } _linesSel.Stop(); _internalObjectCreated = true; _linesSel.Do(l => l.ZIndex = ZIndexConstants.LineStudies1); }
internal override void DrawLineStudy(Types.RectEx rect, LineStatus lineStatus) { if (lineStatus == LineStatus.StartPaint) { return; } rect.Normalize(); if (rect.Width == 0 || rect.Left < 0) { return; } int revX1 = (int)(_chartX.GetReverseXInternal(rect.Left) + _chartX._startIndex); int revX2 = (int)(_chartX.GetReverseXInternal(rect.Right) + _chartX._startIndex); if (revX1 < 0) { revX1 = 0; } if (revX2 < 0) { revX2 = 0; } if (revX1 == revX2) { return; } // Note: this code makes the vague assumption // that only one symbol exists on this panel. // Get the close series Series sClose = GetSeriesOHLC(SeriesTypeOHLC.Close); if (sClose == null) { return; } //Debug.Assert(sClose.RecordCount == _chartX.RecordCount); if (revX1 >= _chartX.RecordCount) { revX1 = _chartX.RecordCount - 1; } if (revX2 >= _chartX.RecordCount) { revX2 = _chartX.RecordCount - 1; } // Get the highest high of the high series. double highestHigh = 0; Series sHigh = GetSeriesOHLC(SeriesTypeOHLC.High); if (sHigh == null) { return; } //Debug.Assert(revX1 <= revX2); for (int i = revX1; i <= revX2; i++) { if (sHigh[i].Value > highestHigh) { highestHigh = sHigh[i].Value.Value; } } //Get the lowest low of the low series. double lowestLow = highestHigh; Series sLow = GetSeriesOHLC(SeriesTypeOHLC.Low); if (sLow == null) { return; } for (int i = revX1; i <= revX2; i++) { if (sLow[i].Value < lowestLow) { lowestLow = sLow[i].Value.Value; } } double range = (highestHigh - lowestLow) * 0.5; if (_rangeScale.HasValue) { range = (highestHigh - lowestLow) * _rangeScale.Value; } // Perform linear regression on the data double xSum = 0, ySum = 0, xSquaredSum = 0, xYSum = 0; int x = revX2 - revX1; int j, n; for (n = 1; n != x + 1; ++n) { j = revX1 + n - 1; xSum += n; ySum += sClose[j].Value.Value; xSquaredSum += (n * n); xYSum += (sClose[j].Value.Value * n); } n = x; double q1 = n != 0 ? (xYSum - ((xSum * ySum) / n)) : 0; double q2 = n != 0 ? (xSquaredSum - ((xSum * xSum) / n)) : 0; double slope = q2 != 0 ? (q1 / q2) : 0; double leftValue = slope != 0 ? (((1 / (double)n) * ySum) - (((int)((double)n / 2)) * slope)) : 0.0; double rightValue = ((n * slope) + leftValue); double inc = (x - 1) != 0 ? (rightValue - leftValue) / (x - 1) : 0; j = 0; double prevVal = 0; double lX1 = rect.Left; //_chartX.GetX(revX1 - _chartX._startIndex); double lX2 = rect.Right; // _chartX.GetX((revX2 - 1) - _chartX._startIndex + 1); if (_linesError.Count == 0) { for (int i = 0; i < 3; i++) { System.Windows.Shapes.Line line = new System.Windows.Shapes.Line { Tag = this }; Canvas.SetZIndex(line, ZIndexConstants.LineStudies1); C.Children.Add(line); _linesError.Add(line); } _contextLine = new ContextLine(this); _chartX.InvokeLineStudyCreated(new StockChartX.LineStudyCreatedEventArgs(this)); } _internalObjectCreated = true; _lines.C = C; _lines.Start(); for (n = revX1; n <= revX2; n++, j++) { double val = leftValue + inc * (j - 1); //double lX1 = _chartX.GetX(n - _chartX._startIndex); //double lX2 = _chartX.GetX(n - _chartX._startIndex + 1); double lY1 = _chartPanel.GetY(prevVal + range); double lY2 = _chartPanel.GetY(val + range); if (prevVal != 0.0) { _errorLines[2].Y1 = lY1; _errorLines[2].Y2 = lY2; Utils.DrawLine(lX1, lY1, lX2, lY2, Stroke, StrokeType, StrokeThickness, _linesError[0]); } lY1 = _chartPanel.GetY(prevVal - range); lY2 = _chartPanel.GetY(val - range); if (prevVal != 0.0) { _errorLines[0].Y1 = lY1; _errorLines[0].Y2 = lY2; Utils.DrawLine(lX1, lY1, lX2, lY2, Stroke, StrokeType, StrokeThickness, _linesError[1]); } lY1 = _chartPanel.GetY(prevVal); lY2 = _chartPanel.GetY(val); if (prevVal != 0.0) { _errorLines[1].Y1 = lY1; _errorLines[1].Y2 = lY2; Utils.DrawLine(lX1, lY1, lX2, lY2, Stroke, StrokeType, StrokeThickness, _linesError[2]); } prevVal = val; } if (lineStatus == LineStatus.Moving || lineStatus == LineStatus.Painting) { Utils.DrawLine(rect.Left, 0, rect.Left, _chartPanel.Height, Stroke, StrokeType, StrokeThickness, _lines); Utils.DrawLine(rect.Right, 0, rect.Right, _chartPanel.Height, Stroke, StrokeType, StrokeThickness, _lines); } _lines.Stop(); _lines.Do(l => l.ZIndex = ZIndexConstants.LineStudies1); _errorLines[0].X1 = rect.Left; _errorLines[0].X2 = rect.Right; _errorLines[1].X1 = rect.Left; _errorLines[1].X2 = rect.Right; _errorLines[2].X1 = rect.Left; _errorLines[2].X2 = rect.Right; if (_contextLine == null && _linesError.Count > 0) { _contextLine = new ContextLine(this); } }
internal override void DrawLineStudy(Types.RectEx rect, LineStatus lineStatus) { if (lineStatus == LineStatus.StartPaint) { for (int i = 0; i < _lines.Length; i++) { _lines[i] = new System.Windows.Shapes.Line { Tag = this }; Canvas.SetZIndex(_lines[i], ZIndexConstants.LineStudies1); C.Children.Add(_lines[i]); } if (_contextLine == null) { _contextLine = new ContextLine(this); _chartX.InvokeLineStudyCreated(new StockChartX.LineStudyCreatedEventArgs(this)); } _internalObjectCreated = true; return; } rect.Normalize(); if (rect.Width == 0) { return; } int revX1 = (int)(_chartX.GetReverseXInternal(rect.Left) + _chartX._startIndex); int revX2 = (int)(_chartX.GetReverseXInternal(rect.Right) + _chartX._startIndex); if (revX1 < 0) { revX1 = 0; } if (revX2 < 0) { revX2 = 0; } if (revX1 == revX2) { return; } // Get the highest high of the high series. // Note: this code makes the vague assumption // that only one symbol exists on this panel. Series sHigh = GetSeriesOHLC(SeriesTypeOHLC.High); if (sHigh == null) { return; } Series sLow = GetSeriesOHLC(SeriesTypeOHLC.Low); if (sLow == null) { return; } Series sClose = GetSeriesOHLC(SeriesTypeOHLC.Close); if (revX1 >= sHigh.RecordCount) { revX1 = sHigh.RecordCount; } if (revX2 >= sHigh.RecordCount) { revX2 = sHigh.RecordCount; } //********** // Perform linear regression on the data double xSum = 0, ySum = 0, xSquaredSum = 0, xYSum = 0; int n, j; if (revX1 > revX2) { revX2 = revX1; } int x = revX2 - revX1; for (n = 1; n != x + 1; ++n) { j = revX1 + n - 1; xSum += n; ySum += sClose[j].Value.Value; xSquaredSum += (n * n); xYSum += (sClose[j].Value.Value * n); } n = x; double q1 = n != 0 ? (xYSum - ((xSum * ySum) / n)) : 0; double q2 = n != 0 ? (xSquaredSum - ((xSum * xSum) / n)) : 0; double slope = q2 != 0 ? (q1 / q2) : 0; double leftValue = slope != 0 ? (((1 / (double)n) * ySum) - (((int)((double)n / 2)) * slope)) : 0.0; double rightValue = ((n * slope) + leftValue); double right = (x - 1); double inc = 0; if (right != 0) { inc = (rightValue - leftValue) / right; } double prevVal = 0.0; j = 0; // Find max distance from linear regression line double lowestLow = sHigh[0].Value.Value; double highestHigh = 0; for (n = revX1; n < revX2; ++n) { j++; double val = leftValue + inc * (j - 1); if (prevVal != 0) { if (sHigh[n].Value.Value - val > highestHigh) { highestHigh = sHigh[n].Value.Value - val; } if (val - sLow[n].Value.Value < lowestLow && val - sLow[n].Value.Value > 0) { lowestLow = val - sLow[n].Value.Value; } } prevVal = val; } if (highestHigh > lowestLow) { lowestLow = highestHigh; } if (lowestLow > highestHigh) { highestHigh = lowestLow; } _linesSel.C = C; double lX1 = _chartX.GetXPixel(revX1 - _chartX._startIndex); double lX2 = _chartX.GetXPixel(revX2 - _chartX._startIndex + 1); j = 0; for (n = revX1; n < revX2; n++, j++) { double val = leftValue + inc * (j - 1); double lY1 = _chartPanel.GetY(prevVal - lowestLow); double lY2 = _chartPanel.GetY(val - lowestLow); if (prevVal != 0) { Utils.DrawLine(lX1, lY1, lX2, lY2, Stroke, StrokeType, StrokeThickness, _lines[0]); } lY1 = _chartPanel.GetY(prevVal); lY2 = _chartPanel.GetY(val); if (prevVal != 0) { Utils.DrawLine(lX1, lY1, lX2, lY2, Stroke, StrokeType, StrokeThickness, _lines[1]); } lY1 = _chartPanel.GetY(prevVal + highestHigh); lY2 = _chartPanel.GetY(val + highestHigh); if (prevVal != 0) { Utils.DrawLine(lX1, lY1, lX2, lY2, Stroke, StrokeType, StrokeThickness, _lines[2]); } prevVal = val; } _linesSel.Start(); if (lineStatus == LineStatus.Moving || lineStatus == LineStatus.Painting) { Utils.DrawLine(lX1, 0, lX1, C.ActualHeight, Stroke, LinePattern.Dot, StrokeThickness, _linesSel); Utils.DrawLine(lX2, 0, lX2, C.ActualHeight, Stroke, LinePattern.Dot, StrokeThickness, _linesSel); } _linesSel.Stop(); _linesSel.Do(l => l.ZIndex = ZIndexConstants.LineStudies1); }
internal override void DrawLineStudy(Types.RectEx rect, LineStatus lineStatus) { int i; if (lineStatus == LineStatus.StartPaint) { for (i = 0; i < LinesCount + 1; i++) { _lines[i] = new Line { Stroke = Stroke, StrokeThickness = StrokeThickness, Tag = this, }; C.Children.Add(_lines[i]); _txts[i] = new TextBlock { Foreground = _chartX.FontForeground, FontFamily = new FontFamily(_chartX.FontFace), FontSize = _chartX.FontSize, Tag = this, }; C.Children.Add(_txts[i]); Canvas.SetZIndex(_lines[i], ZIndexConstants.LineStudies1); Canvas.SetZIndex(_txts[i], ZIndexConstants.LineStudies1); } _handle = new Line { Stroke = Stroke, StrokeThickness = StrokeThickness, Tag = this }; C.Children.Add(_handle); Canvas.SetZIndex(_handle, ZIndexConstants.LineStudies1); if (_contextLine == null) { _contextLine = new ContextLine(this); } _internalObjectCreated = true; _chartX.InvokeLineStudyCreated(new StockChartX.LineStudyCreatedEventArgs(this)); return; } if (!_firstY.HasValue) { _firstY = rect.Top; } bool upsideDown = _firstY > rect.Bottom; if (lineStatus == LineStatus.Moving || lineStatus == LineStatus.Painting) { _handle.Visibility = Visibility.Visible; _handle.X1 = rect.Left; _handle.Y1 = rect.Top; _handle.X2 = rect.Right; _handle.Y2 = rect.Bottom; } else { _handle.Visibility = Visibility.Collapsed; } rect.Normalize(); rect.Right = C.ActualWidth; double max = _chartPanel.GetY(rect.Top); rect.Right -= _chartX.GetTextWidth(string.Format("{0:f2} ", max)); //Comentado por Felipe em 29-08 //if (_params.Count > 1) //{ // upsideDown = _params[0] < _params[1]; //} if ((_params.Count > 5) && (_params[5] == 1)) { upsideDown = false; } else { upsideDown = !upsideDown; } //bloco alterado por Felipe double fibNum = 1.618033; List <double> values = new List <double>(); double minFib = double.MaxValue; double maxFib = double.MinValue; // prepare values to be painted //Thx to Rekhender Dhawan if (upsideDown) { // prepare values to be painted for (i = 0; i < LinesCount; i++) { if (i < _params.Count) { fibNum = _params[i]; } else { fibNum *= 0.618; } values.Add(fibNum); minFib = Math.Min(minFib, fibNum); maxFib = Math.Max(maxFib, fibNum); } } else { for (i = LinesCount - 1; i >= 0; i--) { if (i < _params.Count) { fibNum = _params[i]; } else { fibNum *= 0.618; } values.Add(fibNum); minFib = Math.Min(minFib, fibNum); maxFib = Math.Max(maxFib, fibNum); } } double k = maxFib - minFib; // paint values string formatString = "{0:f" + _chartX.ScalePrecision + "}"; for (i = 0; i < LinesCount && k != 0; i++) { fibNum = values[i]; double norm = (fibNum - minFib) / k; //Comenatado por Felipe em 29-08 //if (upsideDown) // norm = 1 - norm; double textTop = rect.Top + rect.Height * norm; _lines[i].X1 = rect.Left; _lines[i].Y1 = _lines[i].Y2 = textTop; _lines[i].X2 = rect.Right; //string strNum1 = string.Format("{0:0}%", !upsideDown ? (1 - norm) * 100.0 : norm * 100.0); string strNum1 = (!upsideDown) ? (values[(values.Count - 1) - i] * 100.0).ToString("N1") + "%" : (norm * 100.0).ToString("N1") + "%"; double y = _chartPanel.GetReverseY(textTop); string strNum2 = string.Format(formatString, y); _txts[i].Text = string.Format("{0} ({1})", strNum2, strNum1); Canvas.SetLeft(_txts[i], rect.Right + 2); Canvas.SetTop(_txts[i], textTop); } _lines[i].X1 = rect.Left; _lines[i].Y1 = _lines[i].Y2 = rect.Bottom; _lines[i].X2 = rect.Right; }