예제 #1
0
        public void LoadEntries(string[] pairs, OHLC.Interval[] intervals)
        {
            if (pairs == null || intervals == null || pairs.Length <= 0 || intervals.Length <= 0)
            {
                return;
            }

            foreach (OHLC.Interval interval in intervals)
            {
                foreach (string pair in pairs)
                {
                    string key     = this.GetKey(pair, interval);
                    var    entries = Entries.Get(key);

                    TickTime last    = new TickTime(0);
                    int      minutes = (int)interval;
                    if (entries != null)
                    {
                        if (entries.Last.AddMinutes(minutes) > TickTime.Now)
                        {
                            continue;
                        }

                        last = entries.Last;
                    }
                    else
                    {
                        entries          = new ArchiveEntriesJson();
                        entries.PairName = pair;
                        entries.Interval = interval;
                        entries.Last     = last;
                        entries.Entries  = new OHLCEntry[0];
                    }

                    if (last > new TickTime(0).AddMinutes(minutes))
                    {
                        last = last.AddMinutes(-minutes);
                    }

                    OHLC ohlc = null;
                    try
                    {
                        ohlc = Manager.GetOHLC(pair, interval, last);
                    }
                    catch
                    {
                        continue;
                    }

                    if (ohlc == null)
                    {
                        continue;
                    }


                    var _old = entries.Entries.ToDistinctKeyDictionary(x => x.Time, x => x);
                    var _new = ohlc.Entries.ToDistinctKeyDictionary(x => x.Time, x => x);
                    _old.AddOrUpdate(_new);

                    List <OHLCEntry> list = _old.Values.ToList();

                    if (list == null || list.Count <= 0)
                    {
                        continue;
                    }

                    entries.Entries = list.ToArray();
                    entries.Last    = list.Last().Time;
                    Entries.Set(key, entries, true);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Default interval = 1
        /// since = return committed OHLC data since given id (optional.  exclusive)
        ///
        /// The last entry in the OHLC array is for the current, not-yet-committed frame and will always be present, regardless of the value of "since".
        /// </summary>
        /// <param name="pair"></param>
        /// <param name="interval"></param>
        /// <param name="since"></param>
        /// <returns></returns>
        public OHLC GetOHLC(string pair, OHLC.Interval interval = OHLC.Interval._1m, string since = null)
        {
            string props = string.Format("pair={0}", pair);

            props += string.Format("&interval={0}", (int)interval);


            if (since != null)
            {
                props += string.Format("&since={0}", since);
            }

            string response = QueryPublic("OHLC", props);

            if (response == null)
            {
                return(null);
            }

            ObjResult result = JsonConvert.DeserializeObject <ObjResult>(response);

            if (result.Error == null || result.Error.Count > 0)
            {
                return(null);
            }

            OHLC ohlc = JsonConvert.DeserializeObject <OHLC>(result.Result.ToString());

            //OHLC ohlc = new OHLC();
            foreach (JProperty property in result.Result.Children())
            {
                try
                {
                    if (property.Name != pair)
                    {
                        continue;
                    }

                    ohlc.PairName = pair;

                    if (property.Value == null)
                    {
                        continue;
                    }

                    decimal[][] value = JsonConvert.DeserializeObject <decimal[][]>(property.Value.ToString());

                    List <OHLCEntry> entries = new List <OHLCEntry>();
                    foreach (decimal[] array in value)
                    {
                        if (array.IsNullOrEmpty())
                        {
                            continue;
                        }

                        OHLCEntry entry = new OHLCEntry();
                        entry.Entry = array;
                        entries.Add(entry);
                    }


                    ohlc.Entries = entries.ToArray();

                    break;
                }
                catch (Exception ex)
                {
                    ex.ToOutput();
                    continue;
                }
            }


            return(ohlc);
        }