Construct from tick average and variance with Concurrent Collection

Created at 27 Jun 2015, 13:45
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
QU

quasar888

Joined 31.05.2014

Construct from tick average and variance with Concurrent Collection
27 Jun 2015, 13:45


Hello folks, here for you i have started a Robot code to get extended list from tick server, thus we get back average and standard deviation from 10 collections of Ticks. 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Collections.Concurrent;
namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class LamouetteduSpa : Robot
    {
        public LamouetteduSpa c;
        public ConcurrentQueue<double>[] CollectionTick { get; set; }
        public int[] c_nbtick { get; set; }
        protected override void OnStart()
        {
            c = new LamouetteduSpa();
            c.CollectionTick = new ConcurrentQueue<double>[10];
            for (int u = 0; u < 10; u++)
            {
                c.CollectionTick[u] = new ConcurrentQueue<double>();
            }
            c.c_nbtick = new int[10];
            c.VarOnTick = new double[10];
            for (int i = 0; i < 10; i++)
            {
                c.c_nbtick[i] = new int();
                c.c_nbtick[i] = 100 + i * i *125; /*10*10*125 =12 500 ticks  */
                c.VarOnTick[i] = new double();  
            }
        }
        public double sum2 { get; set; }
        public double _av { get; set; }
        double getVariance(ConcurrentQueue<double> coll)
        {
            c.sum2 = new double();
            c.sum2 = 0;
            c._av = new double();
            c._av = 0;
            foreach (double item in coll)
            {
                c.sum2 += item * item;
                c._av += item;
            }
            if (coll.Count != 0)
            {
                c._av = c._av / coll.Count;
            }
            c.sum2 = Math.Sqrt(Math.Abs(((c.sum2) - (coll.Count) * (c._av) * (c._av)) / (coll.Count - 1)));
            return c.sum2;
        }
        void CreationCollection(double Symbol_Bid)
        {
            double firstoutTick;
            for (int y = 0; y < 10; y++)
            {
                switch (y)
                {
                    case 0:
                        if (c.CollectionTick[y].Count < c.c_nbtick[y])
                        {
                            c.CollectionTick[y].Enqueue(Symbol.Bid);
                        }
                        else
                        {
                            c.CollectionTick[y].TryDequeue(out firstoutTick);
                            c.CollectionTick[y].Enqueue(Symbol.Bid);
                        }
                        break;
                    case 1:
                        if (c.CollectionTick[y].Count < c.c_nbtick[y])
                        {
                            c.CollectionTick[y].Enqueue(Symbol.Bid);
                        }
                        else
                        {
                            c.CollectionTick[y].TryDequeue(out firstoutTick);
                            c.CollectionTick[y].Enqueue(Symbol.Bid);
                        }
                        break;
                    case 2:
                        if (c.CollectionTick[y].Count < c.c_nbtick[y])
                        {
                            c.CollectionTick[y].Enqueue(Symbol.Bid);
                        }
                        else
                        {
                            c.CollectionTick[y].TryDequeue(out firstoutTick);
                            c.CollectionTick[y].Enqueue(Symbol.Bid);
                        }
                        break;
                    case 3:
                        if (c.CollectionTick[y].Count < c.c_nbtick[y])
                        {
                            c.CollectionTick[y].Enqueue(Symbol.Bid);
                        }
                        else
                        {
                            c.CollectionTick[y].TryDequeue(out firstoutTick);
                            c.CollectionTick[y].Enqueue(Symbol.Bid);

                        }
                        break;
                    case 4:
                        if (c.CollectionTick[y].Count < c.c_nbtick[y])
                        {
                            c.CollectionTick[y].Enqueue(Symbol.Bid);
                        }
                        else
                        {
                            c.CollectionTick[y].TryDequeue(out firstoutTick);
                            c.CollectionTick[y].Enqueue(Symbol.Bid);
                        }
                        break;
                    case 5:
                        if (c.CollectionTick[y].Count < c.c_nbtick[y])
                        {
                            c.CollectionTick[y].Enqueue(Symbol.Bid);

                        }
                        else
                        {
                            c.CollectionTick[y].TryDequeue(out firstoutTick);
                            c.CollectionTick[y].Enqueue(Symbol.Bid);

                        }
                        break;
                    case 6:
                        if (c.CollectionTick[y].Count < c.c_nbtick[y])
                        {
                            c.CollectionTick[y].Enqueue(Symbol.Bid);

                        }
                        else
                        {
                            c.CollectionTick[y].TryDequeue(out firstoutTick);
                            c.CollectionTick[y].Enqueue(Symbol.Bid);

                        }
                        break;
                    case 7:
                        if (c.CollectionTick[y].Count < c.c_nbtick[y])
                        {
                            c.CollectionTick[y].Enqueue(Symbol.Bid);

                        }
                        else
                        {
                            c.CollectionTick[y].TryDequeue(out firstoutTick);
                            c.CollectionTick[y].Enqueue(Symbol.Bid);

                        }
                        break;
                    case 8:
                        if (c.CollectionTick[y].Count < c.c_nbtick[y])
                        {
                            c.CollectionTick[y].Enqueue(Symbol.Bid);

                        }
                        else
                        {
                            c.CollectionTick[y].TryDequeue(out firstoutTick);
                            c.CollectionTick[y].Enqueue(Symbol.Bid);

                        }
                        break;
                    case 9:
                        if (c.CollectionTick[y].Count < c.c_nbtick[y])
                        {
                            c.CollectionTick[y].Enqueue(Symbol.Bid);

                        }
                        else
                        {
                            c.CollectionTick[y].TryDequeue(out firstoutTick);
                            c.CollectionTick[y].Enqueue(Symbol.Bid);

                        }
                        break;
                    default:
                        break;
                }
            }
        }
        public double[] VarOnTick { get; set; }
        protected override void OnTick()
        {
            CreationCollection(Symbol.Bid+1/2*Symbol.Spread);
            if (c.CollectionTick[9].Count == c.c_nbtick[9])
            {   
                for (int i =0; i < 10 ; i ++)
                {
                c.VarOnTick[i] = getVariance(c.CollectionTick[i]);
                Print(" c.VarOnTick [i] " + i+"  "+ c.VarOnTick[i]); 
                }
               
            }
        }

    }
}


@quasar888