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

Message ListMessage List     Post MessagePost Message

  Error question
Posted by Brian Kim on Apr-05-2019 10:03
Attachments:
1.The window is turned small change (f1 key only)
2.Why are volume values different?


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ChartDirector;

namespace chartExam
{
    public partial class Form1 : Form
    {
        thChart th = new thChart();
        DateTime sDate = DateTime.ParseExact("20190101", "yyyyMMdd", null);
        DateTime eDate = DateTime.Now;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            chartViewer.Visible = true;
            loadData();
            pointerPB.Checked = true;
        }

        private void chartViewer_MouseMovePlotArea(object sender, MouseEventArgs e)
        {
            WinChartViewer viewer = (WinChartViewer)sender;
            th.trackFinance((MultiChart)viewer.Chart, viewer.PlotAreaMouseX, viewer.PlotAreaMouseY);
            viewer.updateDisplay();
        }
        private void loadData()
        {
            th.generateData(chartViewer, "", sDate, eDate, 86400);
            th.drawChart(chartViewer, 1);
        }
        private void pointerPB_CheckedChanged(object sender, EventArgs e)
        {
            if (pointerPB.Checked)
                chartViewer.MouseUsage = WinChartMouseUsage.ScrollOnDrag;
        }

        private void zoomOutPB_CheckedChanged(object sender, EventArgs e)
        {
            if (zoomOutPB.Checked)
                chartViewer.MouseUsage = WinChartMouseUsage.ZoomOut;
        }

        private void zoomInPB_CheckedChanged(object sender, EventArgs e)
        {
            if (zoomInPB.Checked)
                chartViewer.MouseUsage = WinChartMouseUsage.ZoomIn;
        }

        private void chartViewer_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            var chartViewer = sender as WinChartViewer;
            if (e.KeyCode == Keys.F1)
            {
                chartViewer.MouseUsage = WinChartMouseUsage.ScrollOnDrag;
            }
            if (e.KeyCode == Keys.F2)
            {
                chartViewer.MouseUsage = WinChartMouseUsage.ZoomOut;
            }
            if (e.KeyCode == Keys.F3)
            {
                chartViewer.MouseUsage = WinChartMouseUsage.ZoomIn;
            }
        }
        private void chartViewer_ViewPortChanged(object sender, WinViewPortEventArgs e)
        {
            WinChartViewer v = (WinChartViewer)sender;
            if (e.NeedUpdateChart)
            {
                th.drawChart(chartViewer, 1);
            }
        }
    }
}
===============================
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
using ChartDirector;

namespace chartExam
{
    public class thChart
    {
        const int DayComp = 86400;
        public string getName() { return "Finance Chart Custom Symbols"; }
        public string gName = "";
        public int getNoOfCharts() { return 1; }
        public int noOfDays = 30;
        public int extraDays = 30;
        public int extraPoints = 30;
        public int width = 1280;
        public int Height = 500;
        public int g_Vol = 0;
        double highValue = 0;
        double lowValue = 0;
        double openValue = 0;
        double closeValue = 0;
        double volValue = 0;
        int resolution = 0;
        public DateTime[] timeStamps = null;
        public double[] highData;
        public double[] lowData;
        public double[] openData;
        public double[] closeData;
        public double[] volData;

        public bool smaCheck = true;
        public void generateData(WinChartViewer viewer, string ticker, DateTime startDate, DateTime endDate, int r)
        {
            FinanceSimulator db = new FinanceSimulator(ticker, startDate, endDate, r);
            timeStamps = db.getTimeStamps();
            highData = db.getHighData();
            lowData = db.getLowData();
            openData = db.getOpenData();
            closeData = db.getCloseData();
            volData = db.getVolData();
            resolution = r;
        }
        protected void errMsg(WinChartViewer viewer, string msg)
        {
            MultiChart m = new MultiChart(400, 200);
            m.addTitle2(Chart.Center, msg, "Arial", 10).setMaxWidth(m.getWidth());
            viewer.Image = m.makeImage();
        }
        public void drawChart(WinChartViewer viewer, int chartIndex)
        {
            FinanceChart c = new FinanceChart(width);
            c.setPlotAreaStyle(0xc0c0c0, 0xdddddd, 0xdddddd, 0xdddddd, 0xdddddd);
            c.setMargins(80, 50, 80, 50);
            c.setPlotAreaBorder(0xdddddd, 1);

            c.setData(timeStamps, highData, lowData, openData, closeData, volData, extraDays);
            XYChart m = c.addMainChart(Height);
            m.setClipping();
            c.addWeightedMovingAvg(5, 0xffffff).setLineWidth(3);
            c.addWeightedMovingAvg(10, 0x80ffff);
            c.addWeightedMovingAvg(20, 0xffff80).setLineWidth(3);

            c.addCandleStick(0xff0000, 0x0000ff);
            c.addBollingerBand(20, 2.4, 0x9999ff, unchecked((int)0xc06666ff)).setLineWidth(3);
            c.addVolIndicator(75, 0xff0000, 0x0000ff, 0x808080);
            m.addText(500, 520, "vM: " + volData[volData.Length - 5].ToString() + " vL: " + volData[volData.Length - 1].ToString()).setFontSize(20,20);

            this.trackFinance(c, viewer.PlotAreaMouseX, viewer.PlotAreaMouseY);
            viewer.Chart = c;
        }

        public void trackFinance(MultiChart m, int mouseX, int mouseY)
        {
            // Clear the current dynamic layer and get the DrawArea object to draw on it.
            DrawArea d = m.initDynamicLayer();
            // It is possible for a FinanceChart to be empty, so we need to check for it.
            if (m.getChartCount() == 0) return;
            XYChart c = null;
            String legendText = "";
            int xValue = (int)(((XYChart)m.getChart(0)).getNearestXValue(mouseX));
            for (int i = 0; i < m.getChartCount(); ++i)
            {
                c = (XYChart)m.getChart(i);
                int xCoor = c.getXCoor(xValue);
                String ohlcLegend = "";
                PlotArea plotArea = c.getPlotArea();
                int plotAreaLeftX = plotArea.getLeftX() + c.getAbsOffsetX();
                int plotAreaTopY = plotArea.getTopY() + c.getAbsOffsetY();

                for (int j = 0; j < c.getLayerCount(); ++j)
                {
                    Layer layer = c.getLayerByZ(j);
                    int xIndex = layer.getXIndexOf(xValue);
                    int dataSetCount = layer.getDataSetCount();
                    if (dataSetCount == 4)
                    {
                        highValue = layer.getDataSet(0).getValue(xIndex);
                        lowValue = layer.getDataSet(1).getValue(xIndex);
                        openValue = layer.getDataSet(2).getValue(xIndex);
                        closeValue = layer.getDataSet(3).getValue(xIndex);
                        if (i == 0)
                        {
                            if (closeValue != Chart.NoValue)
                            {
                                ohlcLegend = "open: " + c.formatValue(openValue, "{value|P4}") + ", high: " +
                                    c.formatValue(highValue, "{value|P4}") + ", low: " + c.formatValue(lowValue,
                                    "{value|P4}") + ", close: " + c.formatValue(closeValue, "{value|P4}");
                                double lastCloseValue = layer.getDataSet(3).getValue(xIndex - 1);

                                if (lastCloseValue != Chart.NoValue)
                                {
                                    double change = closeValue - lastCloseValue;
                                    double percent = change * 100 / closeValue;
                                    String symbol = ((change >= 0) ?
                                        "<*font,color=008800*><*img=@triangle,width=8,color=008800*>" :
                                        "<*font,color=CC0000*><*img=@invertedtriangle,width=8,color=CC0000*>");
                                    ohlcLegend = ohlcLegend + "  " + symbol + " " + c.formatValue(change, "{value|P4}"
                                        ) + " (" + c.formatValue(percent, "{value|2}") + "%)<*/font*>";
                                }
                                ohlcLegend = "<*block*>" + ohlcLegend + "      <*/*>";
                            }
                        }

                    }
                    if (i == 1)
                    {

                        volValue = layer.getDataSet(0).getValue(xIndex);
                       // volValue = vData[xIndex];
                        //value2 = c.formatValue(volValue, "{value|P0}");
                        var value2 = volValue.ToString();
                        string label2 = "<*block,bgColor=FFFFDD,margin=4,edgeColor=000000*>" + value2 + "   x: " + xIndex + "<*/*>";
                        TTFText t2 = d.text(label2, "Arial Bold", 10);
                        t2.draw(mouseX, (plotArea.getBottomY() + c.getAbsOffsetY()) - 40, 0x000000, Chart.Bottom);
                    }

                    if (dataSetCount == 4)
                    {
                        legendText = "<*block,valign=top,maxWidth=" + (plotArea.getWidth() - 5) + "*><*font=Arial Bold*>["
                                    + c.xAxis().getFormattedLabel(xValue, "yyyy.mm.dd") + "]<*/font*>  " + ohlcLegend + legendText;
                        TTFText t = d.text(legendText, "Arial", 12);
                        t.draw(plotAreaLeftX + 5, plotAreaTopY + 30, 0x000000, Chart.TopLeft);

                        if (plotArea.getBottomY() >= mouseY && plotAreaTopY <= mouseY)
                        {
                            int value = int.Parse(m.formatValue(c.getYValue(mouseY, c.yAxis()), "{value|P0}"));
                            string label = "<*block,bgColor=FFFFDD,margin=4,edgeColor=000000*>" + value.ToString("#,##0") + "<*/*>";
                            TTFText t1 = d.text(label, "Arial Bold", 10);
                            if (smaCheck == true)
                            {
                                t1.draw(plotArea.getLeftX(), mouseY, 0x000000, Chart.Right);
                                t1.draw(plotArea.getRightX(), mouseY, 0x000000, Chart.Left);
                            }
                            else
                            {
                                t1.draw(plotArea.getRightX(), mouseY, 0x000000, Chart.Left);
                            }
                            m.addLine(plotAreaLeftX, mouseY, plotArea.getRightX(), mouseY, m.dashLineColor(0x000000, Chart.DashLine), 1);
                        }
                    }
                    if (i == m.getChartCount() - 1)
                    {
                        string value2 = c.xAxis().getFormattedLabel(xValue, "yyyy.mm.dd");
                        string label2 = "<*block,bgColor=FFFFDD,margin=4,edgeColor=000000*>" + value2 + "<*/*>";
                        TTFText t2 = d.text(label2, "Arial Bold", 10);
                        t2.draw(mouseX, (plotArea.getBottomY() + c.getAbsOffsetY()) + 40, 0x000000, Chart.Bottom);
                    }
                    d.vline(plotAreaTopY, plotAreaTopY + plotArea.getHeight(), c.getXCoor(xValue) + c.getAbsOffsetX(), d.dashLineColor(0x000000, 0x0101));
                }
            }
        }
    }
}
q.png

  Re: Error question
Posted by Peter Kwan on Apr-05-2019 15:09
Hi Brian,

The "Finance Chart Track Line" provides an example on how to obtain the values. In brief, there are 4 types of values:

(a) If a layer has 4 data sets, they are "high-low-open-close" values. (So the layer should be a candlestick or HLOC (or OHLC) layer.)

(b) If a layer has 3 data sets, it is for volume bars. The 3 data sets are for up/down/flat bars.

(c) If a layer has 2 data sets, it is a "band" type layer, such as Bollinger Band, Donchian Channel, etc.. The 2 data sets are the high/low bounds of the bands.

(d) If a layer has 1 data set, it is a regular indicator, such as moving averages.

In your code, you use "if (i == 1)" to detect the volume bars, and it takes only the value for 1 data set. This is actually the value for the red bars, so it becomes zero for the blue bars. the suggested code should be:

if (dataSetCount == 3) {
    // The actual volume is the sum of the 3 data sets.
    volValue = layer.getDataSet(0).getValue(xIndex) + layer.getDataSet(1
        ).getValue(xIndex) + layer.getDataSet(2).getValue(xIndex);

    ... display the value as usual ...
}

Hope this can help.

Regards
Peter Kwan

  Re: Error question
Posted by Brian Kim on Apr-05-2019 15:30
Thank you.
The second problem has been solved.

First problem
PreviewKeyDown f1 key (problem of changing screen size)

  Re: Error question
Posted by Peter Kwan on Apr-05-2019 17:42
Hi Brian,

Is this the same as the following issue?

https://www.chartdir.com/forum/download_thread.php?site=chartdir&bn=chartdir_support&thread=1553411565#N1553658266

In the above, you seem to mention the issue is resolved.

ChartDirector does not use the F1 key. I have tried using the ChartDirector sample code, and the window size does not change when pressing the F1 key.

Is it possible to provide a simple example (may be by modifying the sample code) that can show the problem, and then email the entire Visual Studio solution to me so I can trouble-shoot the issue? My email is pkwan@advsofteng.net

Regards
Peter Kwan