Beispiel #1
0
        private List <StockPrice01M> CreateStockValues1MFromJson(string data)
        {
            var result = new List <StockPrice01M>();

            var jsonString = data;

            // TODO. Esta linea deberia sobrar. Pendiente de comprobacion
            jsonString = jsonString.Substring(0, jsonString.Length - 1);

            try
            {
                JsonTextReader reader = new JsonTextReader(new StringReader(jsonString));

                while (reader.Read())
                {
                    if (reader.Value != null)
                    {
                        if (reader.TokenType == JsonToken.PropertyName)
                        {
                            var cad = reader.Value;

                            var   regex = @"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$";
                            Match match = Regex.Match(reader.Value.ToString(), regex, RegexOptions.IgnoreCase);
                            if (match.Success)
                            {
                                var strDate = reader.Value.ToString();

                                // ATENCION: Vamoa a leer los siguientes registros por orden
                                reader.Read(); reader.Read(); reader.Read();
                                var strOpen = reader.Value.ToString();

                                reader.Read(); reader.Read();
                                var strHigh = reader.Value.ToString();

                                reader.Read(); reader.Read();
                                var strLow = reader.Value.ToString();

                                reader.Read(); reader.Read();
                                var strClose = reader.Value.ToString();

                                reader.Read(); reader.Read();
                                var strVolume = reader.Value.ToString();

                                result.Add(new StockPrice01M()
                                {
                                    Date   = Convert.ToDateTime(strDate),
                                    Open   = Double.Parse(strOpen, CultureInfo.InvariantCulture),
                                    Max    = Double.Parse(strHigh, CultureInfo.InvariantCulture),
                                    Min    = Double.Parse(strLow, CultureInfo.InvariantCulture),
                                    Close  = Double.Parse(strClose, CultureInfo.InvariantCulture),
                                    Volume = Double.Parse(strVolume, CultureInfo.InvariantCulture),
                                });
                            }
                        }
                    }
                }
            }
            catch (Exception error)
            {
                LoggerHelper.LogException(true, error);
            }

            return(result);
        }
Beispiel #2
0
        private List <StockPriceDaily> CreateDailyStockValuesFromJson(string data)
        {
            var result = new List <StockPriceDaily>();

            var jsonString = data;

            jsonString = jsonString.Replace("Meta Data", "Metadata");
            jsonString = jsonString.Replace("Time Series (Daily)", "Timeseries");

            jsonString = jsonString.Replace("1. open", "Open");
            jsonString = jsonString.Replace("2. high", "High");
            jsonString = jsonString.Replace("3. low", "Low");
            jsonString = jsonString.Replace("4. close", "Close");
            jsonString = jsonString.Replace("5. volume", "Volume");

            jsonString = jsonString.Substring(0, jsonString.Length - 1);

            try
            {
                JsonTextReader reader = new JsonTextReader(new StringReader(jsonString));

                while (reader.Read())
                {
                    if (reader.Value != null)
                    {
                        if (reader.TokenType == JsonToken.PropertyName)
                        {
                            var cad = reader.Value;

                            var   regex = @"^\d{4}-\d{2}-\d{2}$";
                            Match match = Regex.Match(reader.Value.ToString(), regex, RegexOptions.IgnoreCase);
                            if (match.Success)
                            {
                                var strDate = reader.Value.ToString();

                                // ATENCION: Vamoa a leer los siguientes registros por orden
                                reader.Read(); reader.Read(); reader.Read();
                                var strOpen = reader.Value.ToString();

                                reader.Read(); reader.Read();
                                var strHigh = reader.Value.ToString();

                                reader.Read(); reader.Read();
                                var strLow = reader.Value.ToString();

                                reader.Read(); reader.Read();
                                var strClose = reader.Value.ToString();

                                reader.Read(); reader.Read();
                                var strVolume = reader.Value.ToString();

                                result.Add(new StockPriceDaily()
                                {
                                    Date   = Convert.ToDateTime(strDate),
                                    Open   = Double.Parse(strOpen, CultureInfo.InvariantCulture),
                                    Max    = Double.Parse(strHigh, CultureInfo.InvariantCulture),
                                    Min    = Double.Parse(strLow, CultureInfo.InvariantCulture),
                                    Close  = Double.Parse(strClose, CultureInfo.InvariantCulture),
                                    Volume = Double.Parse(strVolume, CultureInfo.InvariantCulture),
                                });
                            }
                        }
                    }
                }
            }
            catch (Exception error)
            {
                LoggerHelper.LogException(true, error);
            }

            return(result);
        }