Example #1
0
        private ExchangeRateResponse ToResponse(YahooManaged.Base.Response <HistQuotesResult> resp, YCurrencyID[] currencies)
        {
            List <ExchangeRateData> lst = new List <ExchangeRateData>();

            if (resp.Result != null)
            {
                foreach (HistQuotesDataChain hqc in resp.Result.Chains)
                {
                    if (hqc.Count > 0)
                    {
                        YCurrencyID cur = FinanceHelper.YCurrencyIDFromString(hqc.ID);
                        if (cur != null)
                        {
                            QuotesData q = new QuotesData();
                            q.SetID(hqc.ID);
                            HistQuotesData hqd = (HistQuotesData)hqc[0];
                            q.Change             = hqd.Close - hqd.PreviousClose;
                            q.LastTradePriceOnly = hqd.Close;
                            q.LastTradeDate      = hqd.TradingDate;
                            q.Volume             = hqd.Volume;
                            lst.Add(new ExchangeRateData(cur.BaseCurrency, cur.DepCurrency, q));
                        }
                    }
                }
            }
            return(new ExchangeRateResponse(resp.Connection, new ExchangeRateResult(lst.ToArray(), currencies)));
        }
        /// <summary>
        /// Tries to read a list of historic quote periods from a CSV formatted text
        /// </summary>
        /// <param name="csvText">The CSV formatted text</param>
        /// <param name="delimiter">The delimiter of the CSV text</param>
        /// <param name="culture">The used culture for formating dates and numbers. If parameter value is null/Nothing, default Culture will be used.</param>
        /// <returns>The converted historic quote periods or Nothing</returns>
        /// <remarks></remarks>
        public static HistQuotesData[] ToHistQuotesData(string csvText, char delimiter, System.Globalization.CultureInfo culture = null)
        {
            List <HistQuotesData> lst = new List <HistQuotesData>();

            if (csvText != string.Empty)
            {
                System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CurrentCulture;
                if (culture != null)
                {
                    ci = culture;
                }
                string[][] table = MyHelper.CsvTextToStringTable(csvText, delimiter);
                if (table.Length > 1)
                {
                    for (int i = 0; i <= table.Length - 1; i++)
                    {
                        if (table[i].Length == 7)
                        {
                            HistQuotesData  qd = new HistQuotesData();
                            System.DateTime t1;
                            double          t2;
                            double          t3;
                            double          t4;
                            double          t5;
                            double          t6;
                            long            t7;

                            if (System.DateTime.TryParse(table[i][0], culture, System.Globalization.DateTimeStyles.None, out t1) &&
                                double.TryParse(table[i][1], System.Globalization.NumberStyles.Currency, culture, out t2) &&
                                double.TryParse(table[i][2], System.Globalization.NumberStyles.Currency, culture, out t3) &&
                                double.TryParse(table[i][3], System.Globalization.NumberStyles.Currency, culture, out t4) &&
                                double.TryParse(table[i][4], System.Globalization.NumberStyles.Currency, culture, out t5) &&
                                double.TryParse(table[i][6], System.Globalization.NumberStyles.Currency, culture, out t6) &&
                                long.TryParse(table[i][5], System.Globalization.NumberStyles.Integer, culture, out t7))
                            {
                                qd.TradingDate   = t1;
                                qd.Open          = t2;
                                qd.High          = t3;
                                qd.Low           = t4;
                                qd.Close         = t5;
                                qd.CloseAdjusted = t6;
                                qd.Volume        = t7;

                                lst.Add(qd);
                            }
                        }
                    }
                }
            }
            return(lst.ToArray());
        }
Example #3
0
        private HistQuotesDataChain[] ConvertCSV(string text, string id)
        {
            List <HistQuotesData> lst = new List <HistQuotesData>();

            if (text != string.Empty)
            {
                System.Globalization.CultureInfo ci = FinanceHelper.DefaultYqlCulture;

                string[][] table = MyHelper.CsvTextToStringTable(text, ',');
                if (table.Length > 1)
                {
                    for (int i = 0; i <= table.Length - 1; i++)
                    {
                        if (table[i].Length == 7)
                        {
                            HistQuotesData qd = new HistQuotesData();
                            DateTime       t1;
                            double         t2;
                            double         t3;
                            double         t4;
                            double         t5;
                            double         t6;
                            long           t7;
                            if (System.DateTime.TryParse(table[i][0], ci, System.Globalization.DateTimeStyles.None, out t1) &&
                                double.TryParse(table[i][1], System.Globalization.NumberStyles.Currency, ci, out t2) &&
                                double.TryParse(table[i][2], System.Globalization.NumberStyles.Currency, ci, out t3) &&
                                double.TryParse(table[i][3], System.Globalization.NumberStyles.Currency, ci, out t4) &&
                                double.TryParse(table[i][4], System.Globalization.NumberStyles.Currency, ci, out t5) &&
                                double.TryParse(table[i][6], System.Globalization.NumberStyles.Currency, ci, out t6) &&
                                long.TryParse(table[i][5], System.Globalization.NumberStyles.Integer, ci, out t7))
                            {
                                qd.TradingDate   = t1;
                                qd.Open          = t2;
                                qd.High          = t3;
                                qd.Low           = t4;
                                qd.Close         = t5;
                                qd.CloseAdjusted = t6;
                                qd.Volume        = t7;

                                lst.Add(qd);
                            }
                        }
                    }
                }
            }
            return(new HistQuotesDataChain[] { new HistQuotesDataChain(id, lst.ToArray()) });
        }
        /// <summary>
        /// Writes HistQuoteData to XML format
        /// </summary>
        /// <param name="writer">The used XML writer</param>
        /// <param name="quote">The used HistQuoteData</param>
        /// <param name="culture">The used culture for formating dates and numbers. If parameter value is null/Nothing, default Culture will be used.</param>
        /// <remarks>Creates a main node for all periods</remarks>
        public static void FromHistQuoteData(System.Xml.XmlWriter writer, HistQuotesData quote, System.Globalization.CultureInfo culture = null)
        {
            System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CurrentCulture;
            if (culture != null)
            {
                ci = culture;
            }
            writer.WriteStartElement("HistQuote");

            writer.WriteStartElement(FinanceHelper.NameHistQuoteDate);
            writer.WriteValue(quote.TradingDate.ToShortDateString());
            writer.WriteEndElement();

            writer.WriteStartElement(FinanceHelper.NameHistQuoteOpen);
            writer.WriteValue(MyHelper.ObjectToString(quote.Open, ci));
            writer.WriteEndElement();

            writer.WriteStartElement(FinanceHelper.NameHistQuoteHigh);
            writer.WriteValue(MyHelper.ObjectToString(quote.High, ci));
            writer.WriteEndElement();

            writer.WriteStartElement(FinanceHelper.NameHistQuoteLow);
            writer.WriteValue(MyHelper.ObjectToString(quote.Low, ci));
            writer.WriteEndElement();

            writer.WriteStartElement(FinanceHelper.NameHistQuoteClose);
            writer.WriteValue(MyHelper.ObjectToString(quote.Close, ci));
            writer.WriteEndElement();

            writer.WriteStartElement(FinanceHelper.NameHistQuoteVolume);
            writer.WriteValue(MyHelper.ObjectToString(quote.Volume, ci));
            writer.WriteEndElement();

            writer.WriteStartElement(FinanceHelper.NameHistQuoteAdjClose);
            writer.WriteValue(MyHelper.ObjectToString(quote.CloseAdjusted, ci));
            writer.WriteEndElement();

            writer.WriteEndElement();
        }
        /// <summary>
        /// Writes HistQuoteData to XML format
        /// </summary>
        /// <param name="writer">The used XML writer</param>
        /// <param name="quote">The used HistQuoteData</param>
        /// <param name="culture">The used culture for formating dates and numbers. If parameter value is null/Nothing, default Culture will be used.</param>
        /// <remarks>Creates a main node for all periods</remarks>
        public static void FromHistQuoteData(System.Xml.XmlWriter writer, HistQuotesData quote, System.Globalization.CultureInfo culture = null)
        {
            System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CurrentCulture;
            if (culture != null)
                ci = culture;
            writer.WriteStartElement("HistQuote");

            writer.WriteStartElement(FinanceHelper.NameHistQuoteDate);
            writer.WriteValue(quote.TradingDate.ToShortDateString());
            writer.WriteEndElement();

            writer.WriteStartElement(FinanceHelper.NameHistQuoteOpen);
            writer.WriteValue(MyHelper.ObjectToString(quote.Open, ci));
            writer.WriteEndElement();

            writer.WriteStartElement(FinanceHelper.NameHistQuoteHigh);
            writer.WriteValue(MyHelper.ObjectToString(quote.High, ci));
            writer.WriteEndElement();

            writer.WriteStartElement(FinanceHelper.NameHistQuoteLow);
            writer.WriteValue(MyHelper.ObjectToString(quote.Low, ci));
            writer.WriteEndElement();

            writer.WriteStartElement(FinanceHelper.NameHistQuoteClose);
            writer.WriteValue(MyHelper.ObjectToString(quote.Close, ci));
            writer.WriteEndElement();

            writer.WriteStartElement(FinanceHelper.NameHistQuoteVolume);
            writer.WriteValue(MyHelper.ObjectToString(quote.Volume, ci));
            writer.WriteEndElement();

            writer.WriteStartElement(FinanceHelper.NameHistQuoteAdjClose);
            writer.WriteValue(MyHelper.ObjectToString(quote.CloseAdjusted, ci));
            writer.WriteEndElement();

            writer.WriteEndElement();
        }
Example #6
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());
        }
        /// <summary>
        /// Tries to read a list of historic quote periods from a CSV formatted text
        /// </summary>
        /// <param name="csvText">The CSV formatted text</param>
        /// <param name="delimiter">The delimiter of the CSV text</param>
        /// <param name="culture">The used culture for formating dates and numbers. If parameter value is null/Nothing, default Culture will be used.</param>
        /// <returns>The converted historic quote periods or Nothing</returns>
        /// <remarks></remarks>
        public static HistQuotesData[] ToHistQuotesData(string csvText, char delimiter, System.Globalization.CultureInfo culture = null)
        {
            List<HistQuotesData> lst = new List<HistQuotesData>();
            if (csvText != string.Empty)
            {
                System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CurrentCulture;
                if (culture != null)
                    ci = culture;
                string[][] table = MyHelper.CsvTextToStringTable(csvText, delimiter);
                if (table.Length > 1)
                {
                    for (int i = 0; i <= table.Length - 1; i++)
                    {
                        if (table[i].Length == 7)
                        {
                            HistQuotesData qd = new HistQuotesData();
                            System.DateTime t1;
                            double t2;
                            double t3;
                            double t4;
                            double t5;
                            double t6;
                            long t7;

                            if (System.DateTime.TryParse(table[i][0], culture, System.Globalization.DateTimeStyles.None, out t1) &&
                                double.TryParse(table[i][1], System.Globalization.NumberStyles.Currency, culture, out t2) &&
                                double.TryParse(table[i][2], System.Globalization.NumberStyles.Currency, culture, out t3) &&
                                double.TryParse(table[i][3], System.Globalization.NumberStyles.Currency, culture, out t4) &&
                                double.TryParse(table[i][4], System.Globalization.NumberStyles.Currency, culture, out t5) &&
                                double.TryParse(table[i][6], System.Globalization.NumberStyles.Currency, culture, out t6) &&
                                long.TryParse(table[i][5], System.Globalization.NumberStyles.Integer, culture, out t7))
                            {
                                qd.TradingDate = t1;
                                qd.Open = t2;
                                qd.High = t3;
                                qd.Low = t4;
                                qd.Close = t5;
                                qd.CloseAdjusted = t6;
                                qd.Volume = t7;

                                lst.Add(qd);
                            }
                        }
                    }
                }
            }
            return lst.ToArray();
        }
 /// <summary>
 /// Tries to read a HistQuoteData from XML
 /// </summary>
 /// <param name="node">The XML node of HistQuoteData</param>
 /// <param name="culture">The used culture for formating dates and numbers. If parameter value is null/Nothing, default Culture will be used.</param>
 /// <returns>The converted historic quote data or Nothing</returns>
 /// <remarks></remarks>
 public static HistQuotesData ToHistQuoteData(XElement node, System.Globalization.CultureInfo culture = null)
 {
     if (node != null && node.Name.LocalName.ToLower() == "histquote" && MyHelper.EnumToArray(node.Elements()).Length > 0)
     {
         System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CurrentCulture;
         if (culture != null)
             ci = culture;
         XElement[] elm = MyHelper.EnumToArray(node.Elements());
         HistQuotesData qd = new HistQuotesData();
         foreach (XElement cNode in node.Elements())
         {
             switch (cNode.Name.LocalName)
             {
                 case FinanceHelper.NameHistQuoteDate:
                     System.DateTime t1;
                     if (System.DateTime.TryParse(elm[0].Value, out t1)) qd.TradingDate = t1;
                     break;
                 case FinanceHelper.NameHistQuoteOpen:
                     double t2;
                     if (double.TryParse(elm[1].Value, System.Globalization.NumberStyles.Currency, ci, out t2)) qd.Open = t2;
                     break;
                 case FinanceHelper.NameHistQuoteHigh:
                     double t3;
                     if (double.TryParse(elm[2].Value, System.Globalization.NumberStyles.Currency, ci, out t3)) qd.High = t3;
                     break;
                 case FinanceHelper.NameHistQuoteLow:
                     double t4;
                     if (double.TryParse(elm[3].Value, System.Globalization.NumberStyles.Currency, ci, out t4)) qd.Low = t4;
                     break;
                 case FinanceHelper.NameHistQuoteClose:
                     double t5;
                     if (double.TryParse(elm[4].Value, System.Globalization.NumberStyles.Currency, ci, out t5)) qd.Close = t5;
                     break;
                 case FinanceHelper.NameHistQuoteAdjClose:
                     double t6;
                     if (double.TryParse(elm[6].Value, System.Globalization.NumberStyles.Currency, ci, out t6)) qd.CloseAdjusted = t6;
                     break;
                 case FinanceHelper.NameHistQuoteVolume:
                     long t7;
                     if (long.TryParse(elm[5].Value, System.Globalization.NumberStyles.Integer, ci, out t7)) qd.Volume = t7;
                     break;
             }
         }
         return qd;
     }
     else
     {
         return null;
     }
 }
        /// <summary>
        /// Tries to read a HistQuoteData from XML
        /// </summary>
        /// <param name="node">The XML node of HistQuoteData</param>
        /// <param name="culture">The used culture for formating dates and numbers. If parameter value is null/Nothing, default Culture will be used.</param>
        /// <returns>The converted historic quote data or Nothing</returns>
        /// <remarks></remarks>
        public static HistQuotesData ToHistQuoteData(XElement node, System.Globalization.CultureInfo culture = null)
        {
            if (node != null && node.Name.LocalName.ToLower() == "histquote" && MyHelper.EnumToArray(node.Elements()).Length > 0)
            {
                System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CurrentCulture;
                if (culture != null)
                {
                    ci = culture;
                }
                XElement[]     elm = MyHelper.EnumToArray(node.Elements());
                HistQuotesData qd  = new HistQuotesData();
                foreach (XElement cNode in node.Elements())
                {
                    switch (cNode.Name.LocalName)
                    {
                    case FinanceHelper.NameHistQuoteDate:
                        System.DateTime t1;
                        if (System.DateTime.TryParse(elm[0].Value, out t1))
                        {
                            qd.TradingDate = t1;
                        }
                        break;

                    case FinanceHelper.NameHistQuoteOpen:
                        double t2;
                        if (double.TryParse(elm[1].Value, System.Globalization.NumberStyles.Currency, ci, out t2))
                        {
                            qd.Open = t2;
                        }
                        break;

                    case FinanceHelper.NameHistQuoteHigh:
                        double t3;
                        if (double.TryParse(elm[2].Value, System.Globalization.NumberStyles.Currency, ci, out t3))
                        {
                            qd.High = t3;
                        }
                        break;

                    case FinanceHelper.NameHistQuoteLow:
                        double t4;
                        if (double.TryParse(elm[3].Value, System.Globalization.NumberStyles.Currency, ci, out t4))
                        {
                            qd.Low = t4;
                        }
                        break;

                    case FinanceHelper.NameHistQuoteClose:
                        double t5;
                        if (double.TryParse(elm[4].Value, System.Globalization.NumberStyles.Currency, ci, out t5))
                        {
                            qd.Close = t5;
                        }
                        break;

                    case FinanceHelper.NameHistQuoteAdjClose:
                        double t6;
                        if (double.TryParse(elm[6].Value, System.Globalization.NumberStyles.Currency, ci, out t6))
                        {
                            qd.CloseAdjusted = t6;
                        }
                        break;

                    case FinanceHelper.NameHistQuoteVolume:
                        long t7;
                        if (long.TryParse(elm[5].Value, System.Globalization.NumberStyles.Integer, ci, out t7))
                        {
                            qd.Volume = t7;
                        }
                        break;
                    }
                }
                return(qd);
            }
            else
            {
                return(null);
            }
        }