Array to store data on individual stocks

To get data from the au Kabucom API, I use a “Queue”.

It’s a type of array, but it’s a data store that allows for “first in, last out”.

When a logic program requests data from the “kabu station”, it takes time, and if the processing takes, say, 0.5 seconds, the data will have already been thinned out.

Basically, I try to use data pushed from the “kabu station”, but there are times when the processing cannot keep up, so I put the data in a queue and then use “Dequeue()” to take it out from the back of the store after processing.

This is because I place importance on reproducibility.

However, when calculating with logic, for example in the case of MA (moving average), you need to consider how many past data points to average, so there are times when you don’t use the data output by “Dequeue()”.

I found a convenient function for such cases called a “Queue with maximum capacity”, so I’d like to introduce it to you.

public class FixedQueue<T> : IEnumerable<T>
{
    private Queue<T> _queue;

    public int Count => _queue.Count;

    public int Capacity { get; private set; }

    public FixedQueue(int capacity)
    {
        Capacity = capacity;
        _queue = new Queue<T>(capacity);
    }

    public void Enqueue(T item)
    {
        _queue.Enqueue(item);

        if (Count > Capacity) Dequeue();
    }

    public T Dequeue() => _queue.Dequeue();

    public T Peek() => _queue.Peek();

    public IEnumerator<T> GetEnumerator() => _queue.GetEnumerator();

    IEnumerator IEnumerable.GetEnumerator() => _queue.GetEnumerator();
}

To use it, simply add it with “Enqueue()”, and the data will automatically be “Dequeue()”ed from behind.

static void Main(string[] args)
{
    // 最大容量が3のキュー
    var vMA1 = new FixedQueue<double>(21);

    fixedQueue.Enqueue(price1);
    fixedQueue.Enqueue(price2);
    fixedQueue.Enqueue(price3);
    fixedQueue.Enqueue(price4);
    ....
    fixedQueue.Enqueue(price21);
    Console.WriteLine(vMA1.Average()) //1-21平均

    fixedQueue.Enqueue(price22);
    Console.WriteLine(vMA1.Average()) //2-22平均
}

You can use it like this.