示例#1
0
        private void  // ERROR: Handles clauses are not supported in C#
        moBarData_DataSeriesUpdated(object sender, T4.API.ChartData.DataSeriesUpdatedEventArgs 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 T4.API.ChartData.DataSeriesUpdatedEventHandler(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.
            moBarData.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 <= moBarData.TradeBars.Count - 1; i++)
                {
                    T4.API.ChartData.BarDataPoint oBar = (T4.API.ChartData.BarDataPoint)moBarData.TradeBars[i];


                    if (i < moTable.Rows.Count)
                    {
                        // Update the chart series.
                        DataRow oRow = moTable.Rows[i];
                        oRow.ItemArray = new object[] {
                            i,
                            oBar.TradeDate,
                            oBar.Time,
                            oBar.OpenTicks,
                            oBar.HighTicks,
                            oBar.LowTicks,
                            oBar.CloseTicks,
                            oBar.Volume
                        };
                    }
                    else
                    {
                        // Add a new bar.
                        moTable.Rows.Add(i, oBar.TradeDate, oBar.Time, oBar.OpenTicks, oBar.HighTicks, oBar.LowTicks, oBar.CloseTicks, oBar.Volume);
                    }
                }
            } catch (Exception ex) {
                Trace.WriteLine("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.
                moBarData.Unlock();
            }
        }
示例#2
0
        private void  // ERROR: Handles clauses are not supported in C#
        moBarData_DataLoadComplete(object sender, T4.API.ChartData.DataLoadCompleteEventArgs 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 T4.API.ChartData.DataLoadCompleteEventHandler(moBarData_DataLoadComplete), new object[] {
                    sender,
                    e
                });

                // Don't forget to exit now!
                return;
            }

            // On the GUI thread now, update the chart.

            if (e.Status == DataLoadStatus.Failed)
            {
                Trace.WriteLine("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.
                Trace.WriteLine(string.Format("IChartDataRequest_ChartDataComplete: Status: {0}, Dates Requested: {1}, Dates Received: {2}", e.Status, e.DateRangeRequested, e.DateRangeProcessed));
            }

            chart1.Series["candlestickSeries"].Points.Clear();
            chart1.Series["candlestickVolumeSeries"].Points.Clear();
            chart1.Series["movingAverageSeries"].Points.Clear();
            int dataPointHigh = int.MinValue;
            int dataPointLow  = int.MaxValue;

            // Before iterating the data collection, you MUST lock it. This will prevent live trade updates from modifying the collection as you read it.
            moBarData.Lock();


            try {
                for (int i = 0; i <= moBarData.TradeBars.Count - 1; i++)
                {
                    T4.API.ChartData.BarDataPoint oBar = (T4.API.ChartData.BarDataPoint)moBarData.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);
                }

                DataGridView1.AutoResizeColumns();
                chart1.ChartAreas[0].AxisY.Minimum = dataPointLow;
                chart1.ChartAreas[0].AxisY.Maximum = dataPointHigh;

                /*
                 * IList<DateTime> listx = new List<DateTime>();
                 * IList<double> listy = new List<double>();
                 *
                 * //iterate and add your values to the two lists: listx and listy
                 *
                 * //when you're done, bind the lists to the points collection
                 * yourSeries.Points.DataBindXY(listx, listy);
                 */
            } catch (Exception ex) {
                Trace.WriteLine("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.
                moBarData.Unlock();
            }
        }