Exemplo n.º 1
0
        /// <summary>
        /// Gets the historic trades for a symbol.
        /// </summary>
        /// <param name="startTimeStamp">If using the last timestamp, that timestamp will be returned</param>
        public static List <Trade> GetHistoricTrades(string symbol, int year, int month, int day, int limit = -1, long startTimeStamp = -1, long endTimeStampLimit = -1)
        {
            List <Trade> result = new List <Trade>();

            // symbol is case sensitive
            symbol = symbol.ToUpper();

            NameValueCollection qs = new NameValueCollection();

            qs.Add("apiKey", Config.Key);

            if (limit != -1)
            {
                qs.Add("limit", limit.ToString());
            }
            if (startTimeStamp != -1)
            {
                qs.Add("timestamp", startTimeStamp.ToString());
            }
            if (endTimeStampLimit != -1)
            {
                qs.Add("timestampLimit", endTimeStampLimit.ToString());
            }

            string url = String.Format("https://api.polygon.io/v2/ticks/stocks/trades/{0}/{1}-{2}-{3}?{4}", symbol, year, month.ToString("D2"), day.ToString("D2"), UC.NameValueCollectionToString(qs));

            string payload = UC.DownloadStringAsync(url);

            var obj = JSON.DeserializeDynamic(payload);

            for (int n = 0; n < obj["results"].Count; n++)
            {
                Trade t = new Trade();

                var resultItem = obj["results"][n];

                if (resultItem.ContainsKey("c"))
                {
                    for (int m = 0; m < resultItem["c"].Count; m++)
                    {
                        t.Conditions += resultItem["c"][m].ToString() + (m == resultItem["c"].Count - 1 ? "" : ",");
                    }
                }
                else
                {
                    t.Conditions = "";
                }

                t.ExchangeId = (int)resultItem["x"];

                t.Price = (decimal)resultItem["p"];

                if (resultItem.ContainsKey("z"))
                {
                    t.Tape = (int)resultItem["z"];
                }

                t.Timestamp = (long)resultItem["t"];

                t.Volume = (int)obj["results"][n]["s"];

                result.Add(t);
            }

            result = result.OrderBy(t => t.Timestamp).ToList();

            return(result);
        }