locked
Drawing line on Candlestick chart

    Question

  • I have a candlestick chart and I wish to draw a line connecting the lows of 2 of the data points (say data points 5 and 25)How can I do that ? There doesn't seem to be a simple function like Drawline(x1,y1,x2,y2)  Instead there is an object called lineannotation. How can I instantiate such an object and give it the starting x,y coordinates and the ending x,y coordinates?

    I've been racking my brain so that I can just draw a line on a chart.

    One approach that almost worked was:

                DataPoint left = chart.Series[OHLC].Points[index1];
                DataPoint right = chart.Series[OHLC].Points[index2];
                // Set the line annotation
                line.SetAnchor(left, right);
    I got to see the line, however, the line connected the high of point1 to the high of point2. Both points point1 and point2 consist of Open, High, Low and Close values. How can I specify whether I want my line to be connected to the lows or the highs?

    Thank you for your help.


    Henrick Jeanty

    Wednesday, October 31, 2012 2:01 AM

Answers

  • Unfortunately this is not very straightforward.

    I'll just post a code snippet and hope it can speak for itself:

    int index1 = 1;
    int index2 = 4;
    
    DataPoint left = chart.Series[0].Points[index1];
    DataPoint right = chart.Series[0].Points[index2];
    			
    //Init the annotation
    LineAnnotation line = new LineAnnotation();
    line.AxisX = chart.ChartAreas[0].AxisX;
    line.AxisY = chart.ChartAreas[0].AxisY;
    line.IsSizeAlwaysRelative = false;
    
    //Each point in a candlestick series has several y values, 0=high, 1=low, 2=open, 3=close
    line.Y = left.YValues[1]; //low
    line.X = left.XValue;
    //If your data is indexed (your x values are Strings or you've set Series.IsXValueIndexed to true), use the data point index(+1) as the line X coordinate.
    //line.X = index1 + 1;
    
    //Use the width and height properties to determine the end position of the annotation.
    line.Height = right.YValues[1] - left.YValues[1];
    line.Width = right.XValue - left.XValue;
    //Again, use the index if necessary
    //line.Width = index2 - index1;
    			
    chart.Annotations.Add(line);
    • Marked as answer by Cybister Saturday, November 03, 2012 12:22 AM
    Wednesday, October 31, 2012 12:04 PM
    Moderator

All replies

  • Unfortunately this is not very straightforward.

    I'll just post a code snippet and hope it can speak for itself:

    int index1 = 1;
    int index2 = 4;
    
    DataPoint left = chart.Series[0].Points[index1];
    DataPoint right = chart.Series[0].Points[index2];
    			
    //Init the annotation
    LineAnnotation line = new LineAnnotation();
    line.AxisX = chart.ChartAreas[0].AxisX;
    line.AxisY = chart.ChartAreas[0].AxisY;
    line.IsSizeAlwaysRelative = false;
    
    //Each point in a candlestick series has several y values, 0=high, 1=low, 2=open, 3=close
    line.Y = left.YValues[1]; //low
    line.X = left.XValue;
    //If your data is indexed (your x values are Strings or you've set Series.IsXValueIndexed to true), use the data point index(+1) as the line X coordinate.
    //line.X = index1 + 1;
    
    //Use the width and height properties to determine the end position of the annotation.
    line.Height = right.YValues[1] - left.YValues[1];
    line.Width = right.XValue - left.XValue;
    //Again, use the index if necessary
    //line.Width = index2 - index1;
    			
    chart.Annotations.Add(line);
    • Marked as answer by Cybister Saturday, November 03, 2012 12:22 AM
    Wednesday, October 31, 2012 12:04 PM
    Moderator
  • Thank you very much Sipla. Your answer worked perfectly. I did have to use the indexed version as my X axis values are Strings representing the dates of the stock prices.

    Now, I guess I can draw rectangles by using 4 lines (2 horizontal and 2 vertical), but I would prefer to use the rectangle annotation. Could you please show me how to  draw a rectangle with a function that would take the upper left (point1) and lower right (point2) data points of a series.

    Basically I have a stock price series in candlestick format and I would like to draw a rectangle with upper left corner at the High of point1 and lower right coordinate at the Low of point2 (or vice versa).

    Again, thank you


    Henrick Jeanty

    Saturday, November 03, 2012 12:28 AM
  • I would like to draw a rectangle with upper left corner at the High of point1 and lower right coordinate at the Low of point2
    • Change LineAnnotation to RectangleAnnotation
    • Change the .Y parameter to left.YValues[0]
    • Change the .Height parameter to right.YValues[1] - left.YValues[0]
    Tuesday, November 06, 2012 9:34 AM
    Moderator