Ejemplo n.º 1
0
 protected void Reset()
 {
     this.IsAuthenticated    = false;
     this.UserID             = null;
     this.AvailableQuotes    = new AvailableQuotes();
     this.sessionID          = null;
     this.UserAccount        = null;
     this.UserAccounts       = new List <UserAccount>().AsReadOnly();
     this.UserExchangeStatus = UserExchangeStatus.Unknown;
     this.UserAuthorizations = new Dictionary <string, bool>();
 }
Ejemplo n.º 2
0
        public async Task <bool> LogIn(string userName, string password)
        {
            if (string.IsNullOrWhiteSpace(userName))
            {
                throw new ArgumentException(string.Format(Errors.CannotBeNullOrWhitespace, "userName"), "userName");
            }

            if (string.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentException(string.Format(Errors.CannotBeNullOrWhitespace, "password"), "password");
            }

            var response = await this.http.PostAsync(
                "/apps/300/LogIn?source=" + Uri.EscapeDataString(this.key) + "&version=" + Uri.EscapeDataString(this.version),
                new FormUrlEncodedContent(new[]
            {
                new KeyValuePair <string, string>("userid", userName),
                new KeyValuePair <string, string>("password", password),
                new KeyValuePair <string, string>("source", this.key),
                new KeyValuePair <string, string>("version", this.version)
            }));

            userName = password = null;
            var text = await response.Content.ReadAsStringAsync();

            var xml = XDocument.Parse(text);

            if (this.IsAuthenticated = xml.Root.Element("result").Value == "OK")
            {
                var node = xml.Root.Element("xml-log-in");
                this.UserID    = node.Element("user-id").Value;
                this.sessionID = node.Element("session-id").Value;
                this.timeout   = TimeSpan.FromMinutes(int.Parse(node.Element("timeout").Value));
                var associatedAccountID = node.Element("associated-account-id").Value;
                this.AvailableQuotes = new AvailableQuotes();
                this.AvailableQuotes.Add(Markets.NYSE, node.Element("nyse-quotes").Value == "realtime");
                this.AvailableQuotes.Add(Markets.NASDAQ, node.Element("nasdaq-quotes").Value == "realtime");
                this.AvailableQuotes.Add(Markets.OPRA, node.Element("opra-quotes").Value == "realtime");
                this.AvailableQuotes.Add(Markets.AMEX, node.Element("amex-quotes").Value == "realtime");
                this.AvailableQuotes.Add(Markets.CME, node.Element("cme-quotes").Value == "realtime");
                this.AvailableQuotes.Add(Markets.ICE, node.Element("ice-quotes").Value == "realtime");
                this.AvailableQuotes.Add(Markets.FOREX, node.Element("forex-quotes").Value == "realtime");
                this.UserAuthorizations = node.Element("authorizations").Elements().ToDictionary(x => x.Name.LocalName, x => x.Value == "true");

                switch (node.Element("exchange-status").Value)
                {
                case "non-professional":
                    this.UserExchangeStatus = UserExchangeStatus.NonProfessional;
                    break;

                case "professional":
                    this.UserExchangeStatus = UserExchangeStatus.Professional;
                    break;

                default:
                    this.UserExchangeStatus = UserExchangeStatus.Unknown;
                    break;
                }

                this.UserAccounts = node.Element("accounts").Elements().Select(n =>
                {
                    var account                                         = new UserAccount();
                    account.AccountID                                   = n.Element("account-id").Value;
                    account.DisplayName                                 = n.Element("display-name").Value;
                    account.Description                                 = n.Element("description").Value;
                    account.IsAssociatedAccount                         = n.Element("associated-account").Value == "true";
                    account.Company                                     = n.Element("company").Value;
                    account.Segment                                     = n.Element("segment").Value;
                    account.IsUnified                                   = n.Element("unified").Value == "true";
                    var prefNode                                        = n.Element("preferences");
                    account.Preferences.ExpressTrading                  = prefNode.Element("express-trading").Value == "true";
                    account.Preferences.OptionDirectRouting             = prefNode.Element("option-direct-routing").Value == "true";
                    account.Preferences.StockDirectRouting              = prefNode.Element("stock-direct-routing").Value == "true";
                    account.Preferences.DefaultStockAction              = prefNode.Element("default-stock-action").Value;
                    account.Preferences.DefaultStockOrderType           = prefNode.Element("default-stock-order-type").Value;
                    account.Preferences.DefaultStockQuantity            = prefNode.Element("default-stock-quantity").Value;
                    account.Preferences.DefaultStockExpiration          = prefNode.Element("default-stock-expiration").Value;
                    account.Preferences.DefaultStockSpecialInstructions = prefNode.Element("default-stock-special-instructions").Value;
                    account.Preferences.DefaultStockRouting             = prefNode.Element("default-stock-routing").Value;
                    account.Preferences.DefaultStockDisplaySize         = prefNode.Element("default-stock-display-size").Value;
                    account.Preferences.StockTaxLotMethod               = prefNode.Element("stock-tax-lot-method").Value;
                    account.Preferences.OptionTaxLotMethod              = prefNode.Element("option-tax-lot-method").Value;
                    account.Preferences.MutualFundTaxLotMethod          = prefNode.Element("mutual-fund-tax-lot-method").Value;
                    account.Preferences.DefaultAdvancedToolLaunch       = prefNode.Element("default-advanced-tool-launch").Value;
                    var authNode                                        = n.Element("authorizations");
                    account.Authorizations.Apex                         = authNode.Element("apex").Value == "true";
                    account.Authorizations.Level2                       = authNode.Element("level2").Value == "true";
                    account.Authorizations.StockTrading                 = authNode.Element("stock-trading").Value == "true";
                    account.Authorizations.MarginTrading                = authNode.Element("margin-trading").Value == "true";
                    account.Authorizations.StreamingNews                = authNode.Element("streaming-news").Value == "true";
                    switch (authNode.Element("option-trading").Value)
                    {
                    case "long": account.Authorizations.OptionTrading = OptionTradingType.Long; break;

                    case "covered": account.Authorizations.OptionTrading = OptionTradingType.Covered; break;

                    case "spread": account.Authorizations.OptionTrading = OptionTradingType.Spread; break;

                    case "full": account.Authorizations.OptionTrading = OptionTradingType.Full; break;

                    default: account.Authorizations.OptionTrading = OptionTradingType.None; break;
                    }

                    account.Authorizations.Streamer       = authNode.Element("streamer").Value == "true";
                    account.Authorizations.AdvancedMargin = authNode.Element("advanced-margin").Value == "true";

                    if (account.AccountID == associatedAccountID)
                    {
                        this.UserAccount = account;
                    }

                    return(account);
                }).ToList().AsReadOnly();

                return(true);
            }

            this.Reset();
            return(false);
        }