Page 1 of 1

Scroll Point and Figure

Posted: Tue Jun 28, 2011 9:57 pm
by 15658023
I am using data stored in a CandleSeries as the source for a Point and Figure function. I need to know how to find out how many columns of X's and O's are returned in the P&F chart. The PFSeries.XValues.Count returns the number of items in the underlying data, not the results of the study.

Here is a chunk of code that will create a sample of the chart I am working with. When you create the chart you can see how the P&F chart is crammed into an unreadable area. I have found that I can set the horizontal and vertical axes to spread it out as needed. I just do not know how to quickly find the P&F column count and the ranges of data in each of the columns. Basically, I will need to be able to calculate both horzontal and vertical axes max and min values so I can scroll a useful chart programatically, meaning I need to know the number of bars and then the visible data range as I scroll it.

Thank you very much for your assistance!

Dave R.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TChart1.Series.Clear()
TChart1.Dock = DockStyle.Fill
TChart1.Aspect.View3D = False
TChart1.Header.Visible = False
TChart1.Legend.Visible = False
TChart1.Axes.Left.Visible = True
TChart1.Axes.Right.Visible = True
TChart1.Axes.Top.Visible = False
TChart1.Axes.Bottom.Visible = True

TChart1.Panel.BorderRound = 0
TChart1.Panel.Transparency = 100
TChart1.Panel.Transparent = True
TChart1.Walls.Back.ImageMode = Steema.TeeChart.Drawing.ImageMode.Normal
TChart1.Panning.Allow = ScrollModes.Both
TChart1.Panning.MouseButton = Windows.Forms.MouseButtons.Right
TChart1.Panel.MarginUnits = PanelMarginUnits.Pixels
TChart1.Panel.MarginLeft = 60
TChart1.Panel.MarginRight = 60
TChart1.Panel.MarginBottom = 13
TChart1.Page.MaxPointsPerPage = 150
TChart1.AutoRepaint = True
Dim GreenAxis As Steema.TeeChart.Axis
Dim BlueAxis As Steema.TeeChart.Axis

GreenAxis = New Steema.TeeChart.Axis(TChart1.Chart)

GreenAxis.OtherSide = False
GreenAxis.AxisPen.Color = Color.Green
GreenAxis.StartPosition = 0
GreenAxis.EndPosition = 100
GreenAxis.Automatic = True
Me.TChart1.Axes.Custom.Add(GreenAxis)

BlueAxis = New Steema.TeeChart.Axis(TChart1.Chart)
BlueAxis.OtherSide = True
BlueAxis.AxisPen.Color = Color.Blue
BlueAxis.StartPosition = 0
BlueAxis.EndPosition = 100
BlueAxis.Automatic = True
Me.TChart1.Axes.Custom.Add(BlueAxis)


Dim CandleSeries1 As New Steema.TeeChart.Styles.Candle(TChart1.Chart)
CandleSeries1.TreatNulls = Steema.TeeChart.Styles.TreatNullsStyle.Ignore
CandleSeries1.Style = Styles.CandleStyles.CandleBar
CandleSeries1.XValues.DateTime = False
CandleSeries1.FillSampleValues(150)
CandleSeries1.CustomVertAxis = GreenAxis
CandleSeries1.UpCloseColor = Color.Blue
TChart1.Series.Add(CandleSeries1)
CandleSeries1.Active = False
GreenAxis.Visible = False

Dim PFLine As New Steema.TeeChart.Styles.PointFigure(TChart1.Chart)
TChart1.Series.Add(PFLine)

PFLine.DataSource = CandleSeries1
PFLine.YValues.DataMember = "Close"
PFLine.BoxSize = 5
PFLine.ReversalAmount = 3
PFLine.HorizAxis = Styles.HorizontalAxis.Bottom
PFLine.CustomVertAxis = BlueAxis
PFLine.CloseValues = CandleSeries1.CloseValues
PFLine.ColorEach = False
PFLine.CheckDataSource()
TChart1.Refresh()

Dim ddd As Double = PFLine.XValues.Count 'This is the wrong data.

End Sub

Re: Scroll Point and Figure

Posted: Fri Jul 01, 2011 9:26 am
by narcis
Hi Dave,

Find below an example doing what you requested. You'll need to add TChart and ListBox components into a form to get it working. This example is based on the algorithm PointFigure series uses to calculate columns. If you need VB code you can convert automatically using those tools:

http://www.carlosag.net/Tools/CodeTranslator/
http://authors.aspalliance.com/aldotnet ... slate.aspx
http://www.developerfusion.com/tools/co ... arp-to-vb/
http://converter.telerik.com/

Code: Select all

    public Form1()
    {
      InitializeComponent();
      InitializeChart();
    }

    private void InitializeChart()
    {
      listBox1.Dock = DockStyle.Right;

      tChart1.Dock = DockStyle.Left;
      tChart1.Aspect.View3D = false;
      tChart1.Legend.Visible = false;

      Steema.TeeChart.Styles.Candle candle1 = new Steema.TeeChart.Styles.Candle(tChart1.Chart);
      Steema.TeeChart.Styles.PointFigure pointFigure1 = new Steema.TeeChart.Styles.PointFigure(tChart1.Chart);

      pointFigure1.DataSource = candle1;

      candle1.Style = Steema.TeeChart.Styles.CandleStyles.CandleBar;
      candle1.UpCloseColor = Color.Green;
      candle1.FillSampleValues();

      candle1.VertAxis = Steema.TeeChart.Styles.VerticalAxis.Right;
      candle1.HorizAxis = Steema.TeeChart.Styles.HorizontalAxis.Top;

      tChart1.Axes.Left.StartPosition = 50;
      tChart1.Axes.Right.EndPosition = 50;
      
      tChart1.Draw();
      CalcColumns(pointFigure1);
      DisplayInfo();
    }

    private void AddLines(string text, System.Collections.ArrayList array)
    {
      listBox1.Items.Add("");
      listBox1.Items.Add(text);

      double[] tmpRange = new double[2];

      for (int i = 0; i < array.Count; i++)
      {
        tmpRange = (double[])array[i];
        listBox1.Items.Add(i.ToString() + ": " + tmpRange[0].ToString("#.##") + " - " + tmpRange[1].ToString("#.##"));
      }
    }

    private void DisplayInfo()
    {
      listBox1.Items.Clear();
      listBox1.Items.Add("Up (X): " + upRanges.Count.ToString() + ", Down (O): " + downRanges.Count.ToString());

      AddLines("Up ranges: ", upRanges);
      AddLines("Down ranges: ", downRanges);
    }

    public enum Direction
    {
      Up,
      Down
    };

    System.Collections.ArrayList upRanges = new System.Collections.ArrayList();
    System.Collections.ArrayList downRanges = new System.Collections.ArrayList();
    System.Collections.ArrayList upX = new System.Collections.ArrayList();
    System.Collections.ArrayList downX = new System.Collections.ArrayList();

    private void ColumnData(Steema.TeeChart.Styles.PointFigure s, Direction direction, bool newCol, double FromValue, double ToValue, int tmpX)
    {
      double[] range = new double[2] {FromValue, ToValue};

      switch (direction)
      {
        case Direction.Up:
          if (newCol)
          {
            upX.Add(tmpX);
            upRanges.Add(range);
          }
          else
          {
            int i = upX.IndexOf(tmpX);
            range = (double[])upRanges[i];
            if (range[0] > FromValue) { range[0] = FromValue; }
            if (range[1] < ToValue) { range[1] = ToValue; }
          }
          break;
        case Direction.Down:
          if (newCol)
          {
            downX.Add(tmpX);
            downRanges.Add(range);
          }
          else
          {
            int i = downX.IndexOf(tmpX);
            range = (double[])downRanges[i];
            if (range[0] > FromValue) { range[0] = FromValue; }
            if (range[1] < ToValue) { range[1] = ToValue; }
          }
          break;
        default:
          break;
      }
    }

    private void CalcColumns(Steema.TeeChart.Styles.PointFigure s)
    {
      if (s.Count > 0)
      {
        double tmpDistance = s.ReversalAmount * s.BoxSize;

        double tmpLow = s.LowValues[0];
        double tmpHigh = s.HighValues[0];
        int tmpCol = 0;
        double tmp;
        int tmpX = 0;

        tmpX = s.CalcXPosValue(tmpCol);
        ColumnData(s, Direction.Down, true, tmpLow, tmpHigh, tmpX);

        bool tmpIsDown = true;

        for (int t = 1; t < s.Count; t++)
        {

          if (tmpIsDown)
          {
            tmp = s.LowValues[t];
            if (tmp <= (tmpLow - s.BoxSize))
            {
              ColumnData(s, Direction.Down, false, tmp, tmpLow - s.BoxSize, tmpX);
              tmpLow = tmp;
            }
            else
            {
              tmp = s.HighValues[t];
              if (tmp >= (tmpLow + tmpDistance))
              {
                tmpCol++;
                tmpHigh = tmp;

                tmpX = s.CalcXPosValue(tmpCol);
                ColumnData(s, Direction.Up, true, tmpLow + s.BoxSize, tmpHigh, tmpX);

                tmpIsDown = false;
              }
            }
          }
          else
          {
            tmp = s.HighValues[t];
            if (tmp >= (tmpHigh + s.BoxSize))
            {
              ColumnData(s, Direction.Up, false, tmpHigh + s.BoxSize, tmp, tmpX);
              tmpHigh = tmp;
            }
            else
            {
              tmp = s.LowValues[t];
              if (tmp <= (tmpHigh - tmpDistance))
              {
                tmpCol++;
                tmpLow = tmp;

                tmpX = s.CalcXPosValue(tmpCol);
                ColumnData(s, Direction.Down, true, tmpLow, tmpHigh - s.BoxSize, tmpX);

                tmpIsDown = true;
              }
            }
          }
        }

      }
    }

Re: Scroll Point and Figure

Posted: Fri Jul 01, 2011 1:51 pm
by 15658023
This looks like it should do the trick!

Thanks!
Dave

Re: Scroll Point and Figure

Posted: Fri Jul 08, 2011 8:42 pm
by 15658023
One more Point and Figure question... Let's say I have a P&F chart running and am adding data to the underlying series. Is there any event or anything that I can watch to know that a new data point caused a new column of X's or O's to appear? I am looking for something I can watch to trigger a horizontal scroll operation at the appropriate time. (With the underlying data, I control which bar I use with each bit of data. I know when I need to scroll the chart for a new bar. With Point and Figure, new columns appear as a result of price action, not new data points. At the point where I am adding data there is no way to know if it will cause a new column to appear.)

Thanks!

Dave

Re: Scroll Point and Figure

Posted: Mon Jul 11, 2011 2:33 pm
by 10050769
Hello Dave,

I suggest that use the code that made Narcis in previous post where is counted the columns and using this code in AfterDraw of Chart or AfterDrawValues of Series Events, you have to be able to identify where a new column starts and if "X" or "O". On the other hand, if you consider that code suggested NarcĂ­s isn't enough, you need know that you can take a look in the code of PointFigure Series with help of reflector, concretely in CalcMaxColumns method of PointsFigure series with the above code is based.

I hope will helps.

Thanks,

Re: Scroll Point and Figure

Posted: Mon Jul 11, 2011 2:36 pm
by 15658023
I will look in to these alternatives. Thank you.

Just as a suggestion, having the control raise an event when a new column is added to a P&F chart would be a handy addition to a future release.

Re: Scroll Point and Figure

Posted: Tue Jul 12, 2011 8:41 am
by 10050769
Hello Dave,

Thanks for your information. I have added your request in wish-list with number [TF02015655] to be consider its inclusion in next maintenance releases of TeeChart.Net.

Thanks,