Click or drag to resize

Min Max Queue class

MinMaxQueue represents a dynamic circle queue for finding Min value on sliding window.

Contains properties:

Extremum - minimum or maximum value on sliding window. Depends on minOrMax constructor parameter.

ExtremumPoint - How many values have been added since extremal value on specified period.

ExtremumDateTime - What time was associated with extremal value.

Methods:

Put(Double) - Store arbitrary data in queue. Do not call this method for MinMaxQueue created on time period.

Put(Double, DateTime) - Store arbitrary data and time in queue.

Consider an example showing how to use MinMaxQueue data container to find minima of 20 recent values of Sma indicator:

C#
 1using System;
 2
 3using FinAnalysis.Base;
 4using FinAnalysis.TA;
 5
 6//Indicator:
 7public Sma sma;
 8
 9//data queue container:
10private MinMaxQueue dataset;
11
12//Init indicator:
13sma = new Sma(20);
14
15//Init data container of size 20 for calculating Min:
16dataset = new MinMaxQueue (20, MinMaxQueue.MinOrMax.CalcMin);
17
18private OnBarClose(Bar bar)
19{
20        //modify indicator with bar close price:
21        sma.Add(bar.Close);
22
23        //modify container with new indicator value 
24        dataSet.Put(sma.SMA);
25
26        //get statistics from data container:
27        Console.WriteLine(dataSet.Extremum.ToString());
28}
Code Sample

The example of MinMaxQueue class usage:

C#
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Text;
 5using FinAnalysis.Base;
 6
 7namespace MinMaxQueueSample
 8{
 9    class MinMaxQueueSample
10    {
11        const int Steps = 0xFF;
12
13        static void Main(string[] args)
14        {
15            Random r = new Random();
16
17            // Min Queue Without Time
18            MinMaxQueue minMaxQueueWithoutTimeMinimum = new MinMaxQueue(12, MinMaxQueue.MinOrMax.CalcMin);
19
20            for (int i = 0; i < Steps; ++i)
21            {
22                minMaxQueueWithoutTimeMinimum.Put(r.NextDouble());
23                Console.WriteLine("Minimum = {0:0.00}, Minimum Point = {1}", minMaxQueueWithoutTimeMinimum.Extremum, minMaxQueueWithoutTimeMinimum.ExtremumPoint);
24            }
25
26
27            // Max Queue Without Time
28            MinMaxQueue minMaxQueueWithoutTimeMaximum = new MinMaxQueue(12, MinMaxQueue.MinOrMax.CalcMax);
29
30            for (int i = 0; i < Steps; ++i)
31            {
32                minMaxQueueWithoutTimeMaximum.Put(r.NextDouble());
33                Console.WriteLine("Maximum = {0:0.00}, Maximum Point = {1}", minMaxQueueWithoutTimeMaximum.Extremum, minMaxQueueWithoutTimeMaximum.ExtremumPoint);
34            }
35
36
37            // Min Queue With Time
38            MinMaxQueue minMaxQueueWithTimeMinimum = new MinMaxQueue(12, MinMaxQueue.MinOrMax.CalcMin);
39
40            for (int i = 0; i < Steps; ++i)
41            {
42                minMaxQueueWithTimeMinimum.Put(r.NextDouble(), DateTime.Now);
43                Console.WriteLine("Minimum = {0:0.00}, Minimum Point = {1}, Minimum Time = {2}", minMaxQueueWithTimeMinimum.Extremum, minMaxQueueWithTimeMinimum.ExtremumPoint, minMaxQueueWithTimeMinimum.ExtremumDateTime);
44            }
45
46
47            // Max Queue With Time
48            MinMaxQueue minMaxQueueWithTimeMaximum = new MinMaxQueue(12, MinMaxQueue.MinOrMax.CalcMax);
49
50            for (int i = 0; i < Steps; ++i)
51            {
52                minMaxQueueWithTimeMaximum.Put(r.NextDouble(), DateTime.Now);
53                Console.WriteLine("Maximum = {0:0.00}, Maximum Point = {1}, Maximum Time = {2}", minMaxQueueWithTimeMaximum.Extremum, minMaxQueueWithTimeMaximum.ExtremumPoint, minMaxQueueWithTimeMaximum.ExtremumDateTime);
54            }
55
56
57        }
58    }
59}