Exemplo n.º 1
0
        void OnSearchButtonClick(object sender, EventArgs e)
        {
            Execute(() =>
            {
                SearchBox.Enabled    = false;
                SearchButton.Enabled = false;

                SearchResultsGrid.Rows.Clear();

                // TODO: May add query asnyc?
                var results = GoogleFinanceHelper.SearchTicker(SearchBox.Text);

                foreach (var item in results)
                {
                    SearchResultsGrid.Rows.Add(item.Name, item.Symbol, item.Exchange);
                }

                if (SearchResultsGrid.Rows.Count > 0)
                {
                    SearchResultsGrid.Select();
                    SearchResultsGrid.Focus();
                }
            }, () =>
            {
                SearchBox.Enabled    = true;
                SearchButton.Enabled = true;

                if (SearchResultsGrid.Rows.Count == 0)
                {
                    SearchBox.SelectAll();
                    SearchBox.Focus();
                }
            });
        }
Exemplo n.º 2
0
 void OnLoad(object sender, EventArgs e)
 {
     Execute(() =>
     {
         JsonBox.Text            = _ticker == "?" ? string.Empty : GoogleFinanceHelper.GetTickerData(_ticker);
         JsonBox.SelectionStart  = 0;
         JsonBox.SelectionLength = 0;
         JsonBox.Focus();
     });
 }
Exemplo n.º 3
0
        static object PSQFunction(string Symbol, string InfoCode)
        {
            if (string.IsNullOrWhiteSpace(Symbol))
            {
                throw new Exception("Symbol must be defined.");
            }

            if (string.IsNullOrWhiteSpace(InfoCode))
            {
                InfoCode = "PRICE";
            }

            var symbol = Symbol.Trim();
            var data   = default(JObjectData);

            try
            {
                if (_cacheTimeout == null)
                {
                    lock (_cacheLock)
                    {
                        _cacheTimeout = new TimeoutWatch(TimeSpan.FromMinutes(1));
                    }
                }
                else if (_cacheTimeout.IsExpired)
                {
                    lock (_cacheLock)
                    {
                        _cache.Clear();
                        _cacheTimeout = new TimeoutWatch(TimeSpan.FromMinutes(1));
                    }
                }

                if (_cache.ContainsKey(symbol))
                {
                    data = _cache[symbol];

                    if (data == null)
                    {
                        return(ExcelError.ExcelErrorNA);
                    }
                }
                else
                {
                    data = GoogleFinanceHelper.GetQuote(symbol);

                    if (data == null)
                    {
                        return(ExcelError.ExcelErrorNA);
                    }

                    lock (_cacheLock)
                    {
                        if (_cache.ContainsKey(symbol))
                        {
                            _cache[symbol] = data;
                        }
                        else
                        {
                            _cache.Add(symbol, data);
                        }
                    }
                }

                InfoCode = InfoCode.Trim();

                switch (InfoCode.ToLowerInvariant())
                {
                case "price":
                case "close":
                case "l":
                case "last":
                case "rate":
                    return(data.GetValueAsDouble("l"));

                case "name":
                    return(data.GetValueAsString("name"));

                case "date":
                case "datelocal":
                    // Google Finance API is not returning the last trade date
                    return(DateTime.Now.ToShortDateString());

                case "time":
                case "timelocal":
                    // Google Finance API is not returning the last trade time
                    return(DateTime.Now.ToShortTimeString());

                case "op":
                case "open":
                    return(data.GetValueAsDouble("op"));

                case "lo":
                case "low":
                    return(data.GetValueAsDouble("lo"));

                case "hi":
                case "high":
                    return(data.GetValueAsDouble("hi"));

                case "lo52":
                case "low52":
                    return(data.GetValueAsDouble("lo52"));

                case "hi52":
                case "high52":
                    return(data.GetValueAsDouble("hi52"));

                case "v":
                case "vo":
                case "volume":
                    return(data.GetValueAsString("vo"));

                case "change":
                    return(data.GetValueAsDouble("c"));

                case "cp":
                case "changein%":
                case "changepercentage":
                    return(data.GetValueAsDouble("cp"));

                case "t":
                case "symbol":
                case "ticker":
                    return(data.GetValueAsString("t", "symbol"));

                case "e":
                case "exchange":
                    return(data.GetValueAsString("e", "exchange"));

                default:
                    return(data.GetValueFromPath(InfoCode));
                }
            }
            catch (Exception e)
            {
                throw new Exception("Couldn't retrieve and process data from Google Finance service. Internal message: " + e.Message);
            }
        }