/// <summary> /// Called to initialize the example /// </summary> /// <param name="chartControl"></param> public override void Create() { // set a chart title NLabel title = new NLabel("Real Time Stock"); title.TextStyle.FontStyle = new NFontStyle("Times New Roman", 18, System.Drawing.FontStyle.Italic); nChartControl1.Panels.Add(title); m_Chart = (NCartesianChart)nChartControl1.Charts[0]; m_Chart.BoundsMode = BoundsMode.Stretch; // add interlace stripes NLinearScaleConfigurator linearScale = m_Chart.Axis(StandardAxis.PrimaryY).ScaleConfigurator as NLinearScaleConfigurator; NScaleStripStyle stripStyle = new NScaleStripStyle(new NColorFillStyle(Color.Beige), null, true, 0, 0, 1, 1); stripStyle.Interlaced = true; stripStyle.ShowAtWalls = new ChartWallType[] { ChartWallType.Back }; linearScale.StripStyles.Add(stripStyle); m_Chart.Axis(StandardAxis.PrimaryX).ScaleConfigurator = new NRangeTimelineScaleConfigurator(); // enable range selection NRangeSelection rangeSelection = new NRangeSelection(); rangeSelection.VerticalValueSnapper = new NAxisRulerMinMaxSnapper(); m_Chart.RangeSelections.Add(rangeSelection); // enable zooming and scrolling m_Chart.Axis(StandardAxis.PrimaryX).ScrollBar.Visible = true; m_Chart.Axis(StandardAxis.PrimaryX).PagingView = new NDateTimeAxisPagingView(); m_LineSMA = new NLineSeries(); // create a line series for the simple moving average m_Chart.Series.Add(m_LineSMA); m_LineSMA.Name = "SMA(20)"; m_LineSMA.DataLabelStyle.Visible = false; m_LineSMA.BorderStyle.Color = Color.DarkOrange; m_LineSMA.UseXValues = true; // create the stock series m_Stock = new NStockSeries(); m_Chart.Series.Add(m_Stock); m_Stock.DisplayOnAxis(StandardAxis.PrimaryX, true); m_Stock.Name = "Stock Data"; m_Stock.Legend.Mode = SeriesLegendMode.None; m_Stock.DataLabelStyle.Visible = false; m_Stock.CandleStyle = CandleStyle.Bar; m_Stock.CandleWidth = new NLength(0.8f, NRelativeUnit.ParentPercentage); m_Stock.InflateMargins = true; m_Stock.UseXValues = true; m_Stock.UpFillStyle = new NColorFillStyle(LightGreen); m_Stock.UpStrokeStyle.Color = Color.Black; m_Stock.DownFillStyle = new NColorFillStyle(Red); m_Stock.DownStrokeStyle.Color = Color.Black; m_Stock.OpenValues.Name = "open"; m_Stock.CloseValues.Name = "close"; m_Stock.HighValues.Name = "high"; m_Stock.LowValues.Name = "low"; nChartControl1.Controller.Tools.Add(new NPanelSelectorTool()); nChartControl1.Controller.Tools.Add(new NAxisScrollTool()); nChartControl1.Controller.Tools.Add(new NDataZoomTool()); nChartControl1.Controller.Tools.Add(new NDataPanTool()); // apply layout ConfigureStandardLayout(m_Chart, title, null); NumberOfDataPointsComboBox.Items.Add("1000"); NumberOfDataPointsComboBox.Items.Add("5000"); NumberOfDataPointsComboBox.Items.Add("10000"); NumberOfDataPointsComboBox.SelectedIndex = 1; AddData(); UseHardwareAccelerationCheckBox.IsChecked = true; nChartControl1.Settings.RenderSurface = RenderSurface.Window; StartTimer(); }
public override void Initialize() { base.Initialize(); const int nNumberOfWeeks = 20; const int nWorkDaysInWeek = 5; const int nDaysInWeek = 7; const int nTotalWorkDays = nNumberOfWeeks * nWorkDaysInWeek; const int nTotalDays = nNumberOfWeeks * nDaysInWeek; const int nHistoricalDays = 20; NLabel title = nChartControl1.Labels.AddHeader("Financial Chart"); title.TextStyle.FontStyle = new NFontStyle("Times New Roman", 18, FontStyle.Italic); title.TextStyle.FillStyle = new NColorFillStyle(GreyBlue); title.ContentAlignment = ContentAlignment.BottomCenter; title.Location = new NPointL(new NLength(50, NRelativeUnit.ParentPercentage), new NLength(2, NRelativeUnit.ParentPercentage)); // setup chart m_Chart = nChartControl1.Charts[0]; m_Chart.BoundsMode = BoundsMode.Fit; m_Chart.Location = new NPointL(new NLength(7, NRelativeUnit.ParentPercentage), new NLength(10, NRelativeUnit.ParentPercentage)); m_Chart.Size = new NSizeL(new NLength(86, NRelativeUnit.ParentPercentage), new NLength(70, NRelativeUnit.ParentPercentage)); m_Chart.Height = 30; m_Chart.Projection.SetPredefinedProjection(PredefinedProjection.Orthogonal); m_Chart.Wall(ChartWallType.Floor).Visible = false; m_Chart.Wall(ChartWallType.Left).Visible = false; m_Chart.Wall(ChartWallType.Back).Width = 0; m_Chart.Wall(ChartWallType.Back).FillStyle = new NColorFillStyle(Color.FromArgb(239, 245, 239)); m_Chart.Axis(StandardAxis.Depth).Visible = false; // setup y axis NLinearScaleConfigurator linearScale = (NLinearScaleConfigurator)m_Chart.Axis(StandardAxis.PrimaryY).ScaleConfigurator; linearScale.MajorGridStyle.LineStyle.Pattern = LinePattern.Dot; linearScale.MajorGridStyle.LineStyle.Color = Color.Gray; linearScale.MajorGridStyle.SetShowAtWall(ChartWallType.Left, false); // setup x axis NAxis axisX1 = m_Chart.Axis(StandardAxis.PrimaryX); linearScale = new NLinearScaleConfigurator(); axisX1.ScaleConfigurator = linearScale; linearScale.AutoLabels = false; linearScale.MinorTickCount = 4; linearScale.MajorTickMode = MajorTickMode.CustomStep; linearScale.CustomStep = 5; linearScale.RoundToTickMax = false; linearScale.RoundToTickMin = false; linearScale.OuterMajorTickStyle.Length = new NLength(4, NGraphicsUnit.Point); linearScale.InnerMajorTickStyle.Length = new NLength(0, NGraphicsUnit.Point); linearScale.InnerMinorTickStyle.Length = new NLength(0, NGraphicsUnit.Point); linearScale.OuterMinorTickStyle.Length = new NLength(2, NGraphicsUnit.Point); linearScale.OuterMinorTickStyle.LineStyle.Color = Color.Brown; linearScale.MajorGridStyle.SetShowAtWall(ChartWallType.Back, true); linearScale.MajorGridStyle.SetShowAtWall(ChartWallType.Floor, false); linearScale.MajorGridStyle.LineStyle.Pattern = LinePattern.Dot; linearScale.LabelStyle.ValueScale = 0.2; // create a line series for the simple moving average m_LineSMA = (NLineSeries)m_Chart.Series.Add(SeriesType.Line); m_LineSMA.Name = "SMA(20)"; m_LineSMA.DataLabelStyle.Visible = false; m_LineSMA.BorderStyle.Color = Color.DarkOrange; // create the stock series m_Stock = (NStockSeries)m_Chart.Series.Add(SeriesType.Stock); m_Stock.Name = "Stock Data"; m_Stock.Legend.Mode = SeriesLegendMode.None; m_Stock.DataLabelStyle.Visible = false; m_Stock.CandleStyle = CandleStyle.Bar; m_Stock.CandleWidth = new NLength(5, NGraphicsUnit.Point); m_Stock.InflateMargins = false; m_Stock.UpFillStyle = new NColorFillStyle(LightOrange); m_Stock.UpStrokeStyle.Color = Color.Black; m_Stock.DownFillStyle = new NColorFillStyle(DarkOrange); m_Stock.DownStrokeStyle.Color = Color.Black; m_Stock.DisplayOnAxis(StandardAxis.PrimaryX, true); m_Stock.InflateMargins = true; m_Stock.OpenValues.Name = "open"; m_Stock.CloseValues.Name = "close"; m_Stock.HighValues.Name = "high"; m_Stock.LowValues.Name = "low"; // add the bollinger bands as high low area m_HighLow = (NHighLowSeries)m_Chart.Series.Add(SeriesType.HighLow); m_HighLow.Name = "BB(20, 2)"; m_HighLow.DataLabelStyle.Visible = false; m_HighLow.HighFillStyle = new NColorFillStyle(Color.FromArgb(80, 130, 134, 168)); m_HighLow.HighBorderStyle.Width = new NLength(0, NGraphicsUnit.Pixel); m_HighLow.DisplayOnAxis(StandardAxis.SecondaryX, true); // generate some stock data GenerateOHLCData(m_Stock, 300, nTotalWorkDays + nHistoricalDays); // create a function calculator NFunctionCalculator fc = new NFunctionCalculator(); fc.Arguments.Add(m_Stock.CloseValues); // calculate the bollinger bands fc.Expression = "BOLLINGER(close; 20; 2)"; m_HighLow.HighValues = fc.Calculate(); m_HighLow.HighValues.Name = "BollingerUpper"; fc.Expression = "BOLLINGER(close; 20; -2)"; m_HighLow.LowValues = fc.Calculate(); m_HighLow.LowValues.Name = "BollingerLower"; // calculate the simple moving average fc.Expression = "SMA(close; 20)"; m_LineSMA.Values = fc.Calculate(); // remove data that won't be charted m_Stock.HighValues.RemoveRange(0, nHistoricalDays); m_Stock.LowValues.RemoveRange(0, nHistoricalDays); m_Stock.OpenValues.RemoveRange(0, nHistoricalDays); m_Stock.CloseValues.RemoveRange(0, nHistoricalDays); m_HighLow.HighValues.RemoveRange(0, nHistoricalDays); m_HighLow.LowValues.RemoveRange(0, nHistoricalDays); m_LineSMA.Values.RemoveRange(0, nHistoricalDays); GenerateDateLabels(nTotalDays); CandleStyleCombo.Items.Add("Candle"); CandleStyleCombo.Items.Add("Stick"); CandleStyleCombo.SelectedIndex = 0; NRangeSelection rangeSelection = new NRangeSelection(); rangeSelection.VerticalValueSnapper = new NAxisRulerMinMaxSnapper(); ((NCartesianChart)m_Chart).RangeSelections.Add(rangeSelection); nChartControl1.Controller.Tools.Add(new NPanelSelectorTool()); nChartControl1.Controller.Tools.Add(new NAxisScrollTool()); nChartControl1.Controller.Tools.Add(new NDataZoomTool()); nChartControl1.Controller.Tools.Add(new NDataPanTool()); }
private void AddData(NCartesianChart chart, NWorkCalendar calendar) { const int nNumberOfWeeks = 20; const int nWorkDaysInWeek = 5; const int nTotalWorkDays = nNumberOfWeeks * nWorkDaysInWeek; const int nHistoricalDays = 20; NLineSeries lineSMA = new NLineSeries(); lineSMA.Name = "SMA(20)"; lineSMA.DataLabelStyle.Visible = false; lineSMA.BorderStyle.Color = Color.DarkOrange; lineSMA.UseXValues = true; // create the stock series NStockSeries stock = new NStockSeries(); chart.Series.Add(stock); stock.DisplayOnAxis(StandardAxis.PrimaryX, true); stock.Name = "Stock Data"; stock.Legend.Mode = SeriesLegendMode.None; stock.DataLabelStyle.Visible = false; stock.CandleStyle = CandleStyle.Bar; stock.CandleWidth = new NLength(0.8f, NRelativeUnit.ParentPercentage); stock.InflateMargins = true; stock.UseXValues = true; stock.UpFillStyle = new NColorFillStyle(Green); stock.UpStrokeStyle.Color = Color.Black; stock.DownFillStyle = new NColorFillStyle(DarkOrange); stock.DownStrokeStyle.Color = Color.Black; stock.OpenValues.Name = "open"; stock.CloseValues.Name = "close"; stock.HighValues.Name = "high"; stock.LowValues.Name = "low"; int period = 20; // add the bollinger bands as high low area NHighLowSeries highLow = new NHighLowSeries(); chart.Series.Add(highLow); highLow.DisplayOnAxis(StandardAxis.SecondaryX, true); highLow.Name = "BB(" + period.ToString() + ", 2)"; highLow.DataLabelStyle.Visible = false; highLow.HighFillStyle = new NColorFillStyle(Color.FromArgb(80, 130, 134, 168)); highLow.HighBorderStyle.Width = new NLength(0, NGraphicsUnit.Pixel); highLow.UseXValues = true; // generate some stock data GenerateData(stock, calendar, 300, nTotalWorkDays + nHistoricalDays); // create a function calculator NFunctionCalculator fc = new NFunctionCalculator(); fc.Arguments.Add(stock.CloseValues); // calculate the bollinger bands fc.Expression = "BOLLINGER(close;" + period.ToString() + "; 2)"; highLow.HighValues = fc.Calculate(); highLow.HighValues.Name = "BollingerUpper"; fc.Expression = "BOLLINGER(close; " + period.ToString() + "; -2)"; highLow.LowValues = fc.Calculate(); highLow.LowValues.Name = "BollingerLower"; highLow.XValues.InsertRange(0, stock.XValues); // calculate the simple moving average fc.Expression = "SMA(close; " + period.ToString() + ")"; lineSMA.Values = fc.Calculate(); lineSMA.XValues.InsertRange(0, stock.XValues); // remove first period from line SMA lineSMA.Values.RemoveRange(0, period); lineSMA.XValues.RemoveRange(0, period); // remove first period from high low highLow.XValues.RemoveRange(0, period); highLow.HighValues.RemoveRange(0, period); highLow.LowValues.RemoveRange(0, period); // remove first period from stock stock.OpenValues.RemoveRange(0, period); stock.HighValues.RemoveRange(0, period); stock.LowValues.RemoveRange(0, period); stock.CloseValues.RemoveRange(0, period); stock.XValues.RemoveRange(0, period); }