void AddBarToChart(ezBarDataPoint barDP) { object[] multipleValues = new object[] { barDP.High, barDP.Low, barDP.Open, barDP.Close }; // We don't use these indexes right now, but the AddXY method returns the index of the point it adds. int index1 = chart1.Series[0].Points.AddXY(barDP.Time, multipleValues); int index2 = chart1.Series[1].Points.AddXY(barDP.Time, barDP.Volume); }
void chartDataSeries_DataSeriesUpdated(object sender, DataSeriesUpdatedEventArgs e) { Spy.Print("DataSeriesUpdated (on thread)"); if (e.TradeBarIndexUpdate == -1) { return; } var update = new ezDataSeriesUpdatedEventArgs(e.ModeIndexUpdate, e.SettlementIndexUpdate, e.TradeBarIndexUpdate); for (int i = e.TradeBarIndexUpdate; i < ctsChartData.TradeBars.Count; i++) { if (i >= chartData.TradeBars.Count) { BarDataPoint bdp = ctsChartData.TradeBars[i] as BarDataPoint; var ezBar = new ezBarDataPoint(); ezBar.Open = bdp.OpenTicks; ezBar.High = bdp.HighTicks; ezBar.Low = bdp.LowTicks; ezBar.Close = bdp.CloseTicks; ezBar.TradeCount = bdp.TradeCount; ezBar.Volume = bdp.Volume; ezBar.TradeDate = bdp.TradeDate; ezBar.Time = bdp.Time; chartData.TradeBars.Add(ezBar); } else { BarDataPoint bdp = ctsChartData.TradeBars[i] as BarDataPoint; //var ezBar = new ezBarDataPoint(bdp.OpenTicks, bdp.HighTicks, bdp.LowTicks, bdp.CloseTicks, bdp.TradeCount, bdp.Volume, bdp.TradeDate, bdp.Time); var ezBar = new ezBarDataPoint(); ezBar.Open = bdp.OpenTicks; ezBar.High = bdp.HighTicks; ezBar.Low = bdp.LowTicks; ezBar.Close = bdp.CloseTicks; ezBar.TradeCount = bdp.TradeCount; ezBar.Volume = bdp.Volume; ezBar.TradeDate = bdp.TradeDate; ezBar.Time = bdp.Time; chartData.UpdateTradeBar(i, ezBar); } } chartData.DataProviderSeriesUpdated(update); }
void chartDataSeries_DataLoadComplete(object sender, DataLoadCompleteEventArgs e) { Spy.Print("DataLoadComplete (on thread)"); for (int i = 0; i < ctsChartData.TradeBars.Count; i++) { BarDataPoint bdp = ctsChartData.TradeBars[i] as BarDataPoint; //var ezBar = new ezBarDataPoint(bdp.OpenTicks, bdp.HighTicks, bdp.LowTicks, bdp.CloseTicks, bdp.TradeCount, bdp.Volume, bdp.TradeDate, bdp.Time); var ezBar = new ezBarDataPoint(); ezBar.Open = bdp.OpenTicks; ezBar.High = bdp.HighTicks; ezBar.Low = bdp.LowTicks; ezBar.Close = bdp.CloseTicks; ezBar.TradeCount = bdp.TradeCount; ezBar.Volume = bdp.Volume; ezBar.TradeDate = bdp.TradeDate; ezBar.Time = bdp.Time; chartData.TradeBars.Add(ezBar); } chartData.DataProviderLoadComplete(); }
public void LoadHistoricalChartData(object chartIdentifier, DateTime startDate, DateTime endDate) { string ticker = chartIdentifier as string; int startYear = startDate.Year; int startMonth = startDate.Month - 1; // have to subtract 1 from the month to get the right data from Yahoo int startDay = startDate.Day; int endYear = endDate.Year; int endMonth = endDate.Month - 1; // have to subtract 1 from the month to get the right data from Yahoo int endDay = endDate.Day; ezDateRange dateRangeRequested = new ezDateRange(); ezDateRange dateRangeProcessed = new ezDateRange(); List <HistoricalStock> historical = YahooHistoricalDownloader.Instance.DownloadData(ticker, startDate, endDate); // Start with an empty set of TradeBars. chartData.TradeBars.Clear(); // Yahoo downloads the data in reverse date order (most recent dates first, then previous day, and so on). So // we need to reverse it when we put it into our chart data. for (int i = historical.Count - 1; i >= 0; i--) { HistoricalStock hs = historical[i]; ezBarDataPoint bdp = new ezBarDataPoint(); bdp.TradeDate = hs.Date; bdp.Open = hs.Open; bdp.High = hs.High; bdp.Low = hs.Low; bdp.Close = hs.Close; bdp.Volume = hs.Volume; chartData.TradeBars.Add(bdp); } if (DataLoadComplete != null) { DataLoadComplete(this, new ezDataLoadCompleteEventArgs(zDataLoadStatus.Success, dateRangeRequested, dateRangeProcessed)); } }
private void // ERROR: Handles clauses are not supported in C# moBarData_DataSeriesUpdated(object sender, ezDataSeriesUpdatedEventArgs e) { // NOTE: This event is raised on it's own thread. If you are updating a GUI application with the chart data (like this example), don't forget that you // MUST marshall this update onto the GUI thread before you modify any GUI components. // (This is the same for all T4 API events.) if (this.InvokeRequired) { // An invoke is required to update the GUI, simply invoke back to this same mthod. this.BeginInvoke(new ezDataSeriesUpdatedEventHandler(moBarData_DataSeriesUpdated), new object[] { sender, e }); // Don't forget to exit now! return; } //On the GUI thread now, update the chart. // Before iterating the data collection, you MUST lock it. This will prevent live trade updates from modifying the collection as you read it. chartDataProvider.Lock(); try { //e.TradeBarIndexUpdate is the index of the first bar that was added or updated since the last update event. for (int i = e.TradeBarIndexUpdate; i <= chartDataProvider.ChartData.TradeBars.Count - 1; i++) { ezBarDataPoint oBar = (ezBarDataPoint)chartDataProvider.ChartData.TradeBars[i]; if (i < chart1.Series[0].Points.Count) { Spy.Print("Updating bar index {0} - close = {1}", i, oBar.Close); // Update the chart series. DataPoint dp1 = chart1.Series[0].Points[i]; dp1.YValues[0] = oBar.High; dp1.YValues[1] = oBar.Low; dp1.YValues[2] = oBar.Open; dp1.YValues[3] = oBar.Close; DataPoint dp2 = chart1.Series[1].Points[i]; dp2.YValues[0] = oBar.Volume; RecalcAllActiveSeries(); chart1.Refresh(); } else { xAxis = chart1.ChartAreas["ChartAreaMain"].AxisX; bool scrollToFurthest = (xAxis.ScaleView.ViewMaximum == xAxis.Maximum); AddBarToChart(oBar); RecalcAllActiveSeries(); chart1.Refresh(); if (scrollToFurthest == true) { xAxis.ScaleView.Scroll(xAxis.Maximum); } } } } catch (Exception ex) { Spy.Print("Error updating chart data: " + ex.ToString()); status.Text = "Error updating chart data: " + ex.ToString(); } finally { // If you don't be sure to unlock the data collection, you probably won't get any live trade updates. chartDataProvider.Unlock(); } }
private void // ERROR: Handles clauses are not supported in C# moBarData_DataLoadComplete(object sender, ezDataLoadCompleteEventArgs e) { // NOTE: This event is raised on it's own thread. If you are updating a GUI application with the chart data (like this example), don't forget that you // MUST marshall this update onto the GUI thread before you modify any GUI components. // (This is the same for all T4 API events.) if (this.InvokeRequired) { // An invoke is required to update the GUI, simply invoke back to this same mthod. this.BeginInvoke(new ezDataLoadCompleteEventHandler(moBarData_DataLoadComplete), new object[] { sender, e }); // Don't forget to exit now! return; } // On the GUI thread now, update the chart. if (e.Status == zDataLoadStatus.Failed) { status.Text = "ERROR: Chart data request failed."; Spy.Print("IChartDataRequest_ChartDataComplete: Chart data request failed."); return; } else { // Sometimes, you won't get all the historical data you requested. This could happen if there isn't chart data available all the back to the start date you requested. Spy.Print("IChartDataRequest_ChartDataComplete: Status: {0}, Dates Requested: {1}, Dates Received: {2}", e.Status, e.DateRangeRequested, e.DateRangeProcessed); } status.Text = "To add an indicator to the chart, choose a chart indicator from the indicator dropdown list."; // Date/time label formats are different for Day than they are for "shorter than day" periods. if (chartDataProvider.BarInterval.Interval == zChartInterval.Day) { chart1.ChartAreas["ChartAreaMain"].AxisX.LabelStyle.Format = "ddd M/d"; chart1.ChartAreas["ChartAreaBottom"].AxisX.LabelStyle.Format = "ddd M/d"; } else { chart1.ChartAreas["ChartAreaMain"].AxisX.LabelStyle.Format = "ddd h:mmtt"; chart1.ChartAreas["ChartAreaBottom"].AxisX.LabelStyle.Format = "M/d h:mmtt"; } chart1.Series["candlestickSeries"].Points.Clear(); chart1.Series["candlestickVolumeSeries"].Points.Clear(); // Remove all but the two main series. for (int i = 2; i < chart1.Series.Count; i++) { chart1.Series.Remove(chart1.Series[i]); } double dataPointHigh = double.MinValue; double dataPointLow = double.MaxValue; // Before iterating the data collection, you MUST lock it. This will prevent live trade updates from modifying the collection as you read it. chartDataProvider.Lock(); try { /*for (int i = 0; i <= chartBarData.TradeBars.Count - 1; i++) { * ezBarDataPoint oBar = (ezBarDataPoint)chartBarData.TradeBars[i]; * * Trace.WriteLine(string.Format("TradeDate: {6}, Time: {5}, Open: {0}, High: {1}, Low: {2}, Close: {3}, Volume: {4}", oBar.OpenTicks, oBar.HighTicks, oBar.LowTicks, oBar.CloseTicks, oBar.Volume, oBar.Time, oBar.TradeDate)); * * moTable.Rows.Add(i, oBar.TradeDate, oBar.Time, oBar.OpenTicks, oBar.HighTicks, oBar.LowTicks, oBar.CloseTicks, oBar.Volume); * * // Add candlestick to chart (high, low, open, close). * object[] multipleValues = new object[] { oBar.HighTicks, oBar.LowTicks, oBar.OpenTicks, oBar.CloseTicks }; * chart1.Series[0].Points.AddXY(oBar.Time, multipleValues); * chart1.Series[1].Points.AddXY(oBar.Time, oBar.Volume); * * // See if our maximum and minimum values on the chart have changed (we will use this for axis scaling). * dataPointHigh = Math.Max(dataPointHigh, oBar.HighTicks); * dataPointLow = Math.Min(dataPointLow, oBar.LowTicks); * }*/ for (int i = 0; i <= chartDataProvider.ChartData.TradeBars.Count - 1; i++) { ezBarDataPoint oBar = (ezBarDataPoint)chartDataProvider.ChartData.TradeBars[i]; // Add candlestick to chart (high, low, open, close). AddBarToChart(oBar); // See if our maximum and minimum values on the chart have changed (we will use this for axis scaling). dataPointHigh = Math.Max(dataPointHigh, oBar.High); dataPointLow = Math.Min(dataPointLow, oBar.Low); } chart1.ChartAreas["ChartAreaMain"].AxisY.Minimum = dataPointLow; chart1.ChartAreas["ChartAreaMain"].AxisY.Maximum = dataPointHigh; //this.Text = "Monkey Chart - " + chartDataProvider.Name + string.Format(" - {0} {1}", chartDataProvider.BarInterval.Period, chartDataProvider.BarInterval.Interval); } catch (Exception ex) { Spy.Print("Error loading chart data: " + ex.ToString()); status.Text = "Error loading chart data: " + ex.ToString(); } finally { // If you don't be sure to unlock the data collection, you probably won't get any live trade updates. chartDataProvider.Unlock(); } }