Click or drag to resize

Time Series Data Queue

This is an extended version of the SimpleDataQueue container to store not only double numbers but objects of any type. In compliance with destination, this container cannot produce statistics but all other features are the same as the SimpleDataQueue has. Multi-types extension entails slightly different programming interface and syntax but not container facilities and behavior.

Implementation and Usage

An implementation of this data container is the TimeSeriesDataQueueT class; to access it in any code project, the FinancialAnalisys.dll must be referenced and the following using statement should precede the code:

C#
1using FinAnalysis.Base;

To enable custom listeners, the corresponding package should also be referenced:

C#
1using FinAnalysis.Listeners;

The class provides three constructors to initialize:

TimeSeriesDataQueueT - initializes a new instance of the class that contains an empty data queue of initial capacity for 10 elements to work in ManualControl regimen;

TimeSeriesDataQueueT(Int32) - initializes a new instance of the class which contains an empty data queue to store no more than capacity elements and specifies the AutoStatic regimen (or SingleElement if capacity = 1);

TimeSeriesDataQueueT(TimeSpan) - initializes a new instance of the class which contains an empty data queue to keep elements for the period time interval including interval bounds; the queue reserves positions for 64 elements and takes the AutoStatic regimen.

Initializing an instance, user should also specify the type of objects to store using the syntax:

C#
1TimeSeriesDataQueue<type> anInstance = new TimeSeriesDataQueue<type>();

where type should point on some standard (e.g. List, int[], etc.) or user-defined type.

Types (the same) are also required when declaring listeners, for instance:

declaring class-listener:

C#
1public class CustomListener: IDataQueueListener<type>

declaring listener-delegate:

C#
1public void OnDelete(type data, DateTime time)

All the class methods and properties correspond exactly to those of the SimpleDataQueue class except statistics; refer to the Simple Data Queue section for details.

Code Sample

Here is the example of TimeSeriesDataQueue usage:

C#
  1using System;
  2using System.Collections.Generic;
  3using System.Linq;
  4using System.Text;
  5using FinAnalysis.Base;
  6
  7namespace TimeSeriesDataQueueSample
  8{
  9    public class Bar
 10    {
 11        public double open;
 12        public double high;
 13        public double low;
 14        public double close;
 15        public double volume;
 16    }
 17
 18    class TimeSeriesDataQueueSample
 19    {
 20        const int Steps = 0xFF;
 21
 22        static void Main(string[] args)
 23        {
 24            Random r = new Random();
 25
 26            Console.WriteLine("Manual Mode");
 27
 28            TimeSeriesDataQueue<Bar> timeSeriesDataQueue = new TimeSeriesDataQueue<Bar>();
 29
 30            for (int i = 0; i < Steps; ++i)
 31            {
 32                Bar bar = new Bar();
 33                bar.open = r.NextDouble();
 34                bar.high = r.NextDouble();
 35                bar.low = r.NextDouble();
 36                bar.close = r.NextDouble();
 37                bar.volume = r.NextDouble();
 38
 39
 40                timeSeriesDataQueue.PutLast(bar, DateTime.Now);
 41                timeSeriesDataQueue.PutFirst(bar, DateTime.Now);
 42            }
 43
 44
 45            for (int i = 0; i < Steps / 2; ++i)
 46            {
 47                timeSeriesDataQueue.RemoveFirst();
 48                timeSeriesDataQueue.RemoveLast();
 49            }
 50
 51
 52            int index = timeSeriesDataQueue.GetIndexByTime(DateTime.Now);
 53
 54            for(int i = 0; i < Steps; ++i)
 55            {
 56                DateTime dateTime = timeSeriesDataQueue.GetTimeByIndex(i);
 57            }
 58
 59
 60            Console.WriteLine("AutoStatic Mode With Point Window");
 61
 62            TimeSeriesDataQueue<Bar> timeSeriesDataQueueAutoPoint = new TimeSeriesDataQueue<Bar>(12);
 63
 64            for (int i = 0; i < Steps; ++i)
 65            {
 66                Bar bar = new Bar();
 67                bar.open = r.NextDouble();
 68                bar.high = r.NextDouble();
 69                bar.low = r.NextDouble();
 70                bar.close = r.NextDouble();
 71                bar.volume = r.NextDouble();
 72
 73                timeSeriesDataQueueAutoPoint.Put(bar, DateTime.Now);
 74            }
 75
 76            index = timeSeriesDataQueue.GetIndexByTime(DateTime.Now);
 77
 78            for (int i = 0; i < Steps; ++i)
 79            {
 80                DateTime dateTime = timeSeriesDataQueue.GetTimeByIndex(i);
 81            }
 82
 83
 84
 85            Console.WriteLine("AutoStatic Mode With Time Window");
 86
 87            TimeSeriesDataQueue<Bar> timeSeriesDataQueueAutoTime = new TimeSeriesDataQueue<Bar>(12);
 88
 89            for (int i = 0; i < Steps; ++i)
 90            {
 91                Bar bar = new Bar();
 92                bar.open = r.NextDouble();
 93                bar.high = r.NextDouble();
 94                bar.low = r.NextDouble();
 95                bar.close = r.NextDouble();
 96                bar.volume = r.NextDouble();
 97
 98                timeSeriesDataQueueAutoTime.Put(bar, DateTime.Now);
 99            }
100
101            index = timeSeriesDataQueueAutoTime.GetIndexByTime(DateTime.Now);
102
103            for (int i = 0; i < Steps; ++i)
104            {
105                DateTime dateTime = timeSeriesDataQueueAutoTime.GetTimeByIndex(i);
106            }
107        }
108    }
109}