ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Adding Scatter Layer to demo code FrmZoomScrollTrack2.cs
Posted by Robby Stamper on Apr-09-2022 03:13
Hi,

I'm using C# and version and ChartDirector 7.1.

I copied the code from FrmZoomScrollTrack2.cs and modified it slightly to chart three lines.  (Price, 200 day High, and 200 day Low)

That works great.   Now, I want to add a scatter layer that displays buy and sell points.

Here is the beginning of my method, drawChart()

        private void drawChart(WinChartViewer viewer)
        {
            // Get the start date and end date that are visible on the chart.
            DateTime viewPortStartDate = Chart.NTime(viewer.getValueAtViewPort("x", viewer.ViewPortLeft));
            DateTime viewPortEndDate = Chart.NTime(viewer.getValueAtViewPort("x", viewer.ViewPortRight));

            // Get the array indexes that corresponds to the visible start and end dates
            int startIndex = (int)Math.Floor(Chart.bSearch(timeStamps, viewPortStartDate));
            int endIndex = (int)Math.Ceiling(Chart.bSearch(timeStamps, viewPortEndDate));
            int noOfPoints = endIndex - startIndex + 1;

            // Extract the part of the data array that are visible.
            DateTime[] viewPortTimeStamps = Chart.arraySlice(timeStamps, startIndex, noOfPoints);
            double[] viewPortDataSeriesA = Chart.arraySlice(dataSeriesA, startIndex, noOfPoints);
            double[] viewPortDataSeriesB = Chart.arraySlice(dataSeriesB, startIndex, noOfPoints);
            double[] viewPortDataSeriesC = Chart.arraySlice(dataSeriesC, startIndex, noOfPoints);

            chartTrades.ExtractTrades(out buyXs, out buyYs, timeStamps, ChartTrades.GET_LONG_BUY_TRADES, startIndex, startIndex + noOfPoints - 1);
            chartTrades.ExtractTrades(out sellXs, out sellYs, timeStamps, ChartTrades.GET_LONG_SELL_TRADES, startIndex, startIndex + noOfPoints - 1);





Please note that those last two lines are objects and methods of mine to get the the buyXs, buyYs, sellXs, and sellYs.  I wrote that code to do the same as your Chart.arraySlice() method and only get those that are in the viewPort.  There are 7498 data points in my dataSeriesA.  There are 1501 data points in videwPortDataSeriesA.
noOfPoints is 1501.   I have two items in the buyXs array.  They are 1352 and 1329.   I have two items in the buyYs Array.  They are 1388 and 1479.   The viewPortDataSeriesA has prices from 700 to 2200.   So, my buyXs are within the range of 0-1500 and the viewport has 1501 data points.  Yet, my scatter plot does not plot.  The scatter plot items are in the legend but not on the chart.

This is the section of drawChart() responsible for adding the layers.


            // Add a line layer for the lines, using a line width of 2 pixels
            LineLayer layer = c.addLineLayer2();
            layer.setLineWidth(2);

            // In this demo, we do not have too many data points. In real code, the chart may contain a lot
            // of data points when fully zoomed out - much more than the number of horizontal pixels in this
            // plot area. So it is a good idea to use fast line mode.
            layer.setFastLineMode();

            // Now we add the 3 data series to a line layer, using the color red (ff33333), green (008800)
            // and blue (3333cc)
            layer.setXData(viewPortTimeStamps);
            if (alphaCB.Checked)
                layer.addDataSet(viewPortDataSeriesA, 0xff3333, "Price");
            if (betaCB.Checked)
                layer.addDataSet(viewPortDataSeriesB, 0x008800, "Breakout High");
            if (gammaCB.Checked)
                layer.addDataSet(viewPortDataSeriesC, 0x3333cc, "Breakout Low");

            // Add scatter layer, using 11 pixels green (33ff33) circle symbols
            c.addScatterLayer(sellXs, sellYs, "Long - Sells", Chart.ArrowPointer, 20, 0x33ff33);

            // Add scatter layer, using 11 pixels blue (3333ff) triangle symbols
            c.addScatterLayer(buyXs, buyYs, "Long - Buys", Chart.TriangleSymbol, 20, 0x3333ff);

Thanks in advance for any help possible.  Have a great weekend everyone.

Robby Stamper
James River Capital Corp
804-249-1066

  Re: Adding Scatter Layer to demo code FrmZoomScrollTrack2.cs
Posted by Peter Kwan on Apr-09-2022 19:55
Hi Robby,

May be the x-coordinates you use for the scatter layer is not consistent with the x-coordinates of the line layer.

The line layer uses viewPortTimeStamps as the x-coordinates. From your description, it seems the scatter layer x-coordinates are the array indexes. It needs to use the same x-coordinates as the line layer to make it consistent.

for (int i = 0; i < buyXs.Length; ++i)
    buyXs[i] = Chart.CTime(viewPortTimeStamps[buyXs[i]]);

In most financial charts, the timestamps should be treated as somewhat "random and unpredictable" and should not be used as x-coordinates. All our financial chart sample code uses the "trading session number", which can be treated as the same as the array index in programming, as the x-coordinates. See:

https://www.chartdir.com/forum/download_thread.php?bn=chartdir_support&thread=1606079475#N1606157135

The FrmZoomScrollTrack2 sample code is not specifically designed for financial charts, and it uses the timestamps as x-coordinates. If this is suitable for your case, then you can use timestamps as the x-coordinates of the scatter layer too.

In ChartDirector 7, the "Interactive Financial Chart" sample code also supports zooming and scrolling. You can use it as a reference on how to zoom/scroll using array indexes as x-coordinates.

Regards
Peter Kwan

  Re: Adding Scatter Layer to demo code FrmZoomScrollTrack2.cs
Posted by Robby Stamper on Apr-11-2022 22:36
Thanks Peter,

I will try out the financial chart.

Robby

  Re: Adding Scatter Layer to demo code FrmZoomScrollTrack2.cs
Posted by Robby Stamper on Apr-12-2022 02:33
Hi Peter,

I've tried frmFinanceDemo.cs and frmTrackFinance.cs.  Neither seem to have the scrolling capability of frmZoomScrollTrack2.  I am using WinForms, so I am only looking at files found under ChartDirector.7.1ChartDirectorNetWinCharts

Is there other Finance demo source code that I should be using if I want a scrollable finance chart?

Thanks.

Robby

  Re: Adding Scatter Layer to demo code FrmZoomScrollTrack2.cs
Posted by Peter Kwan on Apr-12-2022 11:25
Attachments:
Hi Robby,

It is the "Finance Chart / Interactive Financial Chart" example (frmFinancedemo.cs).

Regards
Peter Kwan
CSharpChartExplorer_20220412112401.png