示例#1
0
        private static void CalculateAvgFromHighs(HistQuotesDataChain chain, QuoteFiveDay quoteFiveDayItem)
        {
            var lows = chain.Select(x => x.High).ToList();

            //lows.Remove(lows.OrderByDescending(c => c).First());
            quoteFiveDayItem.LowsAverage            = lows.Average(x => x);
            quoteFiveDayItem.LastFourDaysAvgPctDiff = (quoteFiveDayItem.Last - lows.Average(x => x)) / quoteFiveDayItem.Last;
        }
示例#2
0
        protected override HistQuotesResult ConvertResult(Base.ConnectionInfo connInfo, System.IO.Stream stream, Base.SettingsBase settings)
        {
            HistQuotesDownloadSettings s = (HistQuotesDownloadSettings)settings;
            string text = MyHelper.StreamToString(stream, s.TextEncoding);

            HistQuotesDataChain[] quotes = new HistQuotesDataChain[-1 + 1];
            if (connInfo.State == Base.ConnectionState.Success)
            {
                if (s.JSON)
                {
                    quotes = this.ConvertJSON(text, s.IDs);
                }
                else
                {
                    quotes = this.ConvertCSV(text, s.IDs[0]);
                }
            }
            return(new HistQuotesResult(quotes, s));
        }
        public IList <Tuple <DateTime, double> > Download()
        {
            Console.WriteLine("Extracting tickers from {0} till today, monthly", fromDate.Date);
            Console.WriteLine("Ticker: {0}", ticker);
            Response <HistQuotesResult> response = downloader.Download(ticker, fromDate, DateTime.Now, HistQuotesInterval.Monthly);

            if (response.Connection.State == MaasOne.Base.ConnectionState.Success)
            {
                HistQuotesResult    result = response.Result;
                HistQuotesDataChain chain  = result.Chains[0];
                IList <Tuple <DateTime, double> > tickerResults = new List <Tuple <DateTime, double> >();
                foreach (var item in chain)
                {
                    Tuple <DateTime, double> periodResult = new Tuple <DateTime, double>(item.TradingDate, item.Close);
                    tickerResults.Add(periodResult);
                }
                return(tickerResults);
            }
            else
            {
                Console.WriteLine("WARNING: Error downloading stock ticker {0}. {1} ", ticker, response.Connection.State);
                return(null);
            }
        }
示例#4
0
        private HistQuotesDataChain[] ConvertJSON(string text, string[] ids)
        {
            List <HistQuotesDataChain> quotes = new List <HistQuotesDataChain>();
            HistQuotesDataChain        chain  = new HistQuotesDataChain();
            Regex           reg     = new Regex("{\"col0\":\".*?\",\"col1\":\".*?\",\"col2\":\".*?\",\"col3\":\".*?\",\"col4\":\".*?\",\"col5\":\".*?\",\"col6\":\".*?\"}");
            MatchCollection matches = reg.Matches(text);

            if (matches.Count > 0)
            {
                foreach (Match m in matches)
                {
                    if (m.Success)
                    {
                        string[] columns = m.Value.Replace("{", "").Replace("}", "").Split(new char[] { ',' });

                        if (columns.Length == 7)
                        {
                            if (columns[0] == "\"col0\":\"Date\"")
                            {
                                if (chain.Count > 0)
                                {
                                    string id = string.Empty;
                                    if (quotes.Count <= ids.Length - 1)
                                    {
                                        id = ids[quotes.Count];
                                    }
                                    chain.SetID(id);
                                    quotes.Add(chain);
                                    chain = new HistQuotesDataChain();
                                }
                            }
                            else
                            {
                                HistQuotesData hq = new HistQuotesData();
                                foreach (string col in columns)
                                {
                                    string[] values = col.Replace("\"", "").Split(':');
                                    if (values.Length == 2)
                                    {
                                        switch (values[0])
                                        {
                                        case "col0":
                                            DateTime t1;
                                            if (System.DateTime.TryParse(values[1], FinanceHelper.DefaultYqlCulture, System.Globalization.DateTimeStyles.AdjustToUniversal, out t1))
                                            {
                                                hq.TradingDate = t1;
                                            }
                                            break;

                                        case "col1":
                                            double t2;
                                            if (double.TryParse(values[1], System.Globalization.NumberStyles.Any, FinanceHelper.DefaultYqlCulture, out t2))
                                            {
                                                hq.Open = t2;
                                            }
                                            break;

                                        case "col2":
                                            double t3;
                                            if (double.TryParse(values[1], System.Globalization.NumberStyles.Any, FinanceHelper.DefaultYqlCulture, out t3))
                                            {
                                                hq.High = t3;
                                            }
                                            break;

                                        case "col3":
                                            double t4;
                                            if (double.TryParse(values[1], System.Globalization.NumberStyles.Any, FinanceHelper.DefaultYqlCulture, out t4))
                                            {
                                                hq.Low = t4;
                                            }
                                            break;

                                        case "col4":
                                            double t5;
                                            if (double.TryParse(values[1], System.Globalization.NumberStyles.Any, FinanceHelper.DefaultYqlCulture, out t5))
                                            {
                                                hq.Close = t5;
                                            }
                                            break;

                                        case "col5":
                                            long t6;
                                            if (long.TryParse(values[1], System.Globalization.NumberStyles.Any, FinanceHelper.DefaultYqlCulture, out t6))
                                            {
                                                hq.Volume = t6;
                                            }
                                            break;

                                        case "col6":
                                            double t7;
                                            if (double.TryParse(values[1], System.Globalization.NumberStyles.Any, FinanceHelper.DefaultYqlCulture, out t7))
                                            {
                                                hq.CloseAdjusted = t7;
                                            }
                                            break;
                                        }
                                    }
                                }
                                chain.Add(hq);
                            }
                        }
                    }
                }

                if (chain.Count > 0)
                {
                    string id = string.Empty;
                    if (quotes.Count <= ids.Length - 1)
                    {
                        id = ids[quotes.Count];
                    }
                    chain.SetID(id);
                    quotes.Add(chain);
                }
                chain = null;
            }
            return(quotes.ToArray());
        }