Calculate Space Between two Candles

TeeChart for Microsoft Visual Studio .NET, Xamarin Studio (Android, iOS & Forms) & Monodevelop.
Post Reply
Quant
Advanced
Posts: 142
Joined: Tue Dec 30, 2014 12:00 am

Calculate Space Between two Candles

Post by Quant » Fri Feb 20, 2015 10:01 am

How can we calculate space between two candles?
Please provide us some example to do the same.

Also please provide us some example which shows how to manage Zooming, Scrolling and Paging of TeeChart Candle Series?

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Calculate Space Between two Candles

Post by Christopher » Fri Feb 20, 2015 11:28 am

Quant wrote: How can we calculate space between two candles?
Please provide us some example to do the same.
Do you mean the pixel space between two candles, or the space in values?
Quant wrote: Also please provide us some example which shows how to manage Zooming, Scrolling and Paging of TeeChart Candle Series?
This very much depends on what you want to do. There are a number of examples of these themes in the Features demo under:
%Program Files%\Steema Software\Steema TeeChart for .NET 201X 4.1.201X.XXXXX\Examples\DemoProject\bin\ExecutableDemo\TeeChartNetExamples.exe

Can you please post some code here with what you have done with some clearer instructions on what you are unable to achieve?
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

Quant
Advanced
Posts: 142
Joined: Tue Dec 30, 2014 12:00 am

Re: Calculate Space Between two Candles

Post by Quant » Mon Feb 23, 2015 11:36 am

Hi Christopher,

Have a Look on following code:

Code: Select all

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Steema.TeeChart;
using Steema.TeeChart.Styles;


namespace TeeChartExample
{
    public partial class Form1 : Form
    {
        Candle series = new Candle();

        int totalSampleValues = 1000;
        int displayValues = 100;
        int pageNumber;
        int scrollValue;
        
        int previousMaxMargin = 0, previousMinMargin = 0;
        int currentMaxMargin = 0, currentMinMargin = 0;
        int previousMinimum = 0, previousMaximum = 0;

        double previousLeftMax=0, previousLeftMin = 0;

        private void InitializeComponent()
        {
            this.hScrollBar1 = new System.Windows.Forms.HScrollBar();
            this.tChart1 = new Steema.TeeChart.TChart();
            this.SuspendLayout();

            // hScrollBar1 
            this.hScrollBar1.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.hScrollBar1.Location = new System.Drawing.Point(0, 424);
            this.hScrollBar1.Maximum = 1000;
            this.hScrollBar1.Name = "hScrollBar1";
            this.hScrollBar1.Size = new System.Drawing.Size(722, 16);
            this.hScrollBar1.TabIndex = 0;

            // tChart1
            this.tChart1.Aspect.View3D = false;
            this.tChart1.Axes.Bottom.Grid.Visible = false;
            this.tChart1.Axes.Left.Visible = false;
            this.tChart1.Axes.Right.Grid.Visible = false;
            this.tChart1.Axes.Top.Grid.Visible = false;
            this.tChart1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.tChart1.Header.Lines = new string[] { "" };
            this.tChart1.Legend.Visible = false;
            this.tChart1.Location = new System.Drawing.Point(0, 0);
            this.tChart1.Name = "tChart1";
            this.tChart1.Panel.Brush.Color = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
            this.tChart1.Panel.Brush.Gradient.Visible = false;
            this.tChart1.Panel.MarginBottom = 1;
            this.tChart1.Panel.MarginLeft = 1;
            this.tChart1.Panel.MarginRight = 1;
            this.tChart1.Panel.MarginTop = 1;
            this.tChart1.Size = new System.Drawing.Size(722, 424);
            this.tChart1.TabIndex = 1;
            this.tChart1.Walls.Back.Pen.Visible = false;
            this.tChart1.Walls.Back.Visible = false;
            this.tChart1.Walls.Bottom.Pen.Visible = false;
            this.tChart1.Walls.Bottom.Visible = false;
            this.tChart1.Walls.Left.Pen.Visible = false;
            this.tChart1.Walls.Left.Visible = false;
            this.tChart1.Walls.Visible = false;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(722, 440);
            this.Controls.Add(this.tChart1);
            this.Controls.Add(this.hScrollBar1);
            this.Name = "Scrolling and Paging Example";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        public Form1()
        {
            InitializeComponent();
            InitializeChart();
            this.Load += new EventHandler(Form1_Load);
        }

        void Form1_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            hScrollBar1.Value = hScrollBar1.Maximum-1;
            hScrollBar1_Scroll(sender, new ScrollEventArgs(ScrollEventType.Last, hScrollBar1.Maximum));
            tChart1.Draw();
        }

        private void InitializeChart()
        {
            //Event
            tChart1.AfterDraw += new PaintChartEventHandler(tChart1_AfterDraw);
            tChart1.DoubleClick += new EventHandler(tChart1_DoubleClick);

            //Initialize Series
            series.DownCloseColor = Color.Black;
            series.VertAxis = VerticalAxis.Both;
            tChart1.Aspect.View3D = false;
            tChart1.Series.Add(series);   
            series.FillSampleValues(totalSampleValues);
            
            //Set Margin
            tChart1.Axes.Left.MaximumOffset = 30;
            tChart1.Axes.Left.MinimumOffset = 30;

            //Enable Paging
            pageNumber = Convert.ToInt32(totalSampleValues / displayValues);
            tChart1.Page.MaxPointsPerPage = this.displayValues;
            tChart1.Page.Current = this.pageNumber;
            tChart1.Page.AutoScale = true;
            tChart1.Draw();

            //Enable Scroliing
            hScrollBar1.Minimum = 0;
            hScrollBar1.Maximum = totalSampleValues - displayValues - 1;
            previousMinMargin = totalSampleValues - displayValues - 1;
            previousMaxMargin = totalSampleValues - 1;
            hScrollBar1.Scroll += new ScrollEventHandler(hScrollBar1_Scroll);
          

            //Custom Bottom Axis
            tChart1.Axes.Bottom.Labels.DateTimeFormat = "dd";
            tChart1.Axes.Bottom.Increment = Utils.GetDateTimeStep(DateTimeSteps.OneWeek);
            tChart1.Panel.MarginBottom = 10;
        }

        public void SetMinMax(int minimum, int maximum)
        {
            try
            {
                this.scrollValue = hScrollBar1.Value;
                this.tChart1.Axes.Bottom.SetMinMax(this.tChart1[0].XValues[minimum], this.tChart1[0].XValues[maximum]);
                this.tChart1.Axes.Bottom.AdjustMaxMin();

                currentMinMargin = minimum;
                currentMaxMargin = maximum;

                if (currentMinMargin < previousMinMargin - displayValues)
                {
                    this.tChart1.Page.Previous();
                    pageNumber = pageNumber - 1;
                    this.tChart1.Page.Current = pageNumber;
                    previousMinMargin = currentMinMargin;
                    previousMaxMargin = currentMaxMargin;
                }
                else if (currentMaxMargin > previousMaxMargin + displayValues)
                {
                    this.tChart1.Page.Next();
                    pageNumber = pageNumber + 1;
                    this.tChart1.Page.Current = pageNumber;
                    previousMinMargin = currentMinMargin;
                    previousMaxMargin = currentMaxMargin;
                }
                this.tChart1.Invalidate();
            }

            catch (Exception ex)
            {
            }
        }

        void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            SetMinMax(e.NewValue-displayValues, e.NewValue);
        }

        void tChart1_DoubleClick(object sender, EventArgs e)
        {
            this.tChart1.ShowEditor();
        }

        void tChart1_AfterDraw(object sender, Steema.TeeChart.Drawing.Graphics3D g)
        {
            Axis bottom = tChart1.Axes.Bottom;
            int fontHeight = 10;
            Rectangle rect = Utils.FromLTRB(bottom.IStartPos, bottom.Position, bottom.IEndPos, bottom.Position + fontHeight * 2);

            g.Pen.Color = Color.Red;
            g.Brush.Visible = false;
            g.Rectangle(rect);

            DateTime minDate = DateTime.FromOADate(bottom.Minimum);
            DateTime maxDate = DateTime.FromOADate(bottom.Maximum);

            for (DateTime date = minDate; date.Date <= maxDate; date = date.AddDays(1))
            {
                if (date.Day == 1)
                {
                    DateTime tmpDate = new DateTime(date.Year, date.Month, date.Day);
                    int tmpX = bottom.CalcPosValue(tmpDate.ToOADate());
                    int tmpY = rect.Bottom + rect.Height;
                    g.VerticalLine(tmpX, rect.Top, tmpY);

                    g.Font.Color = Color.Red;
                    g.TextOut(tmpX, tmpY - fontHeight, tmpDate.ToString("MMMM"));
                }
            }

            //For Scrolling and AutoScale
            previousLeftMin = this.tChart1[0].YValues[currentMinMargin];
            previousLeftMax = previousLeftMin;

            for (int count = currentMinMargin; count < currentMaxMargin; count++)
            {
                if (this.tChart1[0].YValues[count] < previousLeftMin)
                {
                    previousLeftMin = this.tChart1[0].YValues[count];
                }
                else if (this.tChart1[0].YValues[count] > previousLeftMax)
                {
                    previousLeftMax = this.tChart1[0].YValues[count];
                }
            }
            this.tChart1.Axes.Left.SetMinMax(previousLeftMin, previousLeftMax);
            this.tChart1.Axes.Right.SetMinMax(previousLeftMin, previousLeftMax);
            
        }
    }
}

We achieved following in above code:
1] Scrolling Using External Control (like HScrollBar)
2] Paging with Autoscale=True

What we needed now:
1] Zooming Candle Width according to TotalSampleValues, DisplayValues and Chart Size (i.e. Width & Height)
Note: TotalSampleValues=Total No. of Sample Values Plotted on the Chart
DisplayValues= Max Points Per Page

2] Zooming with Paging
For example after zooming, it should automatically calculate new DisplayValues and new No. of Pages, and work accordingly.

3] One of the major issue we are having right now is Candle Chart showing blank spaces for saturday and Sunday (or Trading Holidays). Instead of skipping this holidays it shows us blank spaces, which causes TeeChart .Net to show wrong chart. If we used your given workaround of assigning labels, it may have problem while setting Bottom Axis Min Max.

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Calculate Space Between two Candles

Post by Christopher » Mon Feb 23, 2015 2:39 pm

Quant wrote: What we needed now:
1] Zooming Candle Width according to TotalSampleValues, DisplayValues and Chart Size (i.e. Width & Height)
Note: TotalSampleValues=Total No. of Sample Values Plotted on the Chart
DisplayValues= Max Points Per Page
If I'm not mistaken, we covered this issue here.
Quant wrote: 2] Zooming with Paging
For example after zooming, it should automatically calculate new DisplayValues and new No. of Pages, and work accordingly.
DisplayValues is not part of the TeeChart API (the calculation is not TeeChart's responsibility, as it were) - what do you mean be "recalculating No. of Pages after zoom and work accordingly"? Could you please explain?
Quant wrote: 3] One of the major issue we are having right now is Candle Chart showing blank spaces for saturday and Sunday (or Trading Holidays). Instead of skipping this holidays it shows us blank spaces, which causes TeeChart .Net to show wrong chart. If we used your given workaround of assigning labels, it may have problem while setting Bottom Axis Min Max.
Setting bottom axis minimum and maximum should not be a problem - all it means is converting the DateTime value to the integer value.
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

Quant
Advanced
Posts: 142
Joined: Tue Dec 30, 2014 12:00 am

Re: Calculate Space Between two Candles

Post by Quant » Tue Feb 24, 2015 4:19 am

DisplayValues is not part of the TeeChart API (the calculation is not TeeChart's responsibility, as it were) - what do you mean be "recalculating No. of Pages after zoom and work accordingly"? Could you please explain?
Yes, DisplayValues is not the part of TeeChart API. But we already mentioned DisplayValues=MaxPointsPerPage.
Currently we are handling paging and scrolling efficiently, but the problem comes when we zoom Charts. It disturbs MaxPointsPerPage settings, which in turn disturbs scrolling.

Christopher
Guru
Posts: 1603
Joined: Fri Nov 15, 2002 12:00 am

Re: Calculate Space Between two Candles

Post by Christopher » Tue Feb 24, 2015 10:34 am

Quant wrote: Yes, DisplayValues is not the part of TeeChart API. But we already mentioned DisplayValues=MaxPointsPerPage.
Currently we are handling paging and scrolling efficiently, but the problem comes when we zoom Charts. It disturbs MaxPointsPerPage settings, which in turn disturbs scrolling.
Does the following code behave as you expect?

Code: Select all

    Line series = new Line();
    private void InitializeChart()
    {
      tChart1.Aspect.View3D = false;
      tChart1.Series.Add(series);
      tChart1.Zoom.Direction = ZoomDirections.Horizontal;
      series.FillSampleValues(100);

      tChart1.Page.MaxPointsPerPage = 10;
      tChart1.Tools.Add(typeof(PageNumber));

      tChart1.Zoomed += tChart1_Zoomed;
    }

    void tChart1_Zoomed(object sender, EventArgs e)
    {
      tChart1.Draw();
      int range = series.LastVisibleIndex - series.FirstVisibleIndex;
      tChart1.Page.MaxPointsPerPage = range;
      tChart1.Axes.Bottom.Automatic = true;
      tChart1.Draw();
    }

    private void button6_Click(object sender, EventArgs e)
    {
      tChart1.Page.Next();
    }

    private void button5_Click(object sender, EventArgs e)
    {
      tChart1.Page.Previous();
    }
Best Regards,
Christopher Ireland / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Instructions - How to post in this forum

Post Reply