Beispiel #1
0
        public Bar InsertBar(int indexBefore, BarParams barParams)
        {
            var bar = new Bar(this, indexBefore)
            {
                Params = barParams
            };

            if (indexBefore == Bars.Count)
            {
                Bars.Add(bar);
                bar.UpdateTimings();
            }
            else
            {
                Bars.Insert(indexBefore, bar);
                bar.UpdateTimings();

                foreach (var b in Bars.Skip(indexBefore + 1))
                {
                    ++b.Index;
                    b.UpdateTimings();
                }
            }
            return(bar);
        }
        public Bars GetBars(string symbol, PeriodType period, ref int count, int offset)
        {
            string rc = Command(string.Format("BR {0} {1} {2} {3}", symbol, (int) period, offset, count));
            if (!ResponseOK(rc) || rc.Length < 3)
                return null;

            string[] reply = rc.Substring(3).Split(new[] {' '});
            if (reply.Length < 5 || reply[0] != symbol)
                return null;

            int rperiod, rbars, roffset, rcount;
            try
            {
                rperiod = int.Parse(reply[1]);
                rbars = int.Parse(reply[2]);
                roffset = int.Parse(reply[3]);
                rcount = int.Parse(reply[4]);
            }
            catch (FormatException)
            {
                return null;
            }
            if (rperiod != (int) period || roffset != offset || rcount*6 != reply.Length - 5)
                return null;

            count = rbars;
            var bars = new Bars(symbol, period);
            for (int i = 5; i < reply.Length; i += 6)
            {
                try
                {
                    DateTime time = FromTimestamp(int.Parse(reply[i]));
                    double open = StringToDouble(reply[i + 1]);
                    double high = StringToDouble(reply[i + 2]);
                    double low = StringToDouble(reply[i + 3]);
                    double close = StringToDouble(reply[i + 4]);
                    int volume = int.Parse(reply[i + 5]);

                    bars.Insert(time, open, high, low, close, volume);
                }
                catch (FormatException)
                {
                    return null;
                }
            }
            return bars;
        }