示例#1
0
 /// <summary>
 /// Clears this instance of <see cref="InteractiveBrokersAccountData"/>
 /// </summary>
 public void Clear()
 {
     AccountProperties.Clear();
     CashBalances.Clear();
     AccountHoldings.Clear();
     FinancialAdvisorConfiguration.Clear();
 }
示例#2
0
        public void TD_getAcctBalancesAndPositions(string _userid, string _password, string _source, string _version, ref List<CashBalances> oCashBalances, ref List<Positions> oPositions)
        {

            bool retValue = false;
            string lcPostUrl = string.Empty;

            if (this.TD_loginStatus == true)
            {

                XMLHTTP xmlHttp_ = new XMLHTTP();
                StringBuilder cpostdata = new StringBuilder();


                cpostdata.Append("source=" + Encode_URL(_source));

                lcPostUrl = "https://apis.tdameritrade.com/apps/100/BalancesAndPositions?" + cpostdata.ToString();


                xmlHttp_.open("POST", lcPostUrl, false, _userid, _password);
                xmlHttp_.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xmlHttp_.setRequestHeader("Accept", "Accept	image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*");
                xmlHttp_.send(null);

                string xmlData = xmlHttp_.responseText.ToString();
                string cResponseHeaders = xmlHttp_.getAllResponseHeaders();


                /*/
                 * Test Code : Use the following line to test the parsing of balances and positions
                 * 
                /*/
                // StringReader reader = new StringReader( getTestBalancesAndPositions() );

                StringReader reader = new StringReader(xmlData);
                XmlTextReader xml = new XmlTextReader(reader);


                // Check for errors.
                if (null == xmlData || "" == xmlData)
                {
                    // Throw an exception.
                    throw new Exception("Failed to connect, check username and password?");
                }

                CashBalances _oCashBalances = new CashBalances();
                Positions _oPositions = new Positions();
                string cParseStage = string.Empty;
                string name = string.Empty;
                bool lReadingPositionsElement = false;



                // Read the Xml.
                while (xml.Read())
                {
                    // Got an element?
                    if (xml.NodeType == XmlNodeType.Element)
                    {
                        // Get this node.
                        name = xml.Name;

                        if (name.ToLower().ToString().CompareTo("balance") == 0)
                        {
                            cParseStage = "balances";
                            _oCashBalances = new CashBalances();

                        }


                        if (name.ToLower().ToString().CompareTo("account-value") == 0)
                        {
                            cParseStage = "account-value";
                        }


                        if (name.ToLower().ToString().CompareTo("position") == 0 && lReadingPositionsElement == false)
                        {
                            cParseStage = "positions";
                            lReadingPositionsElement = true;
                            _oPositions = new Positions();
                        }



                        // Get Round Trips
                        if (name.ToLower().ToString().CompareTo("round-trips") == 0) // && cParseStage.CompareTo("balances") == 0)
                        {
                            xml.Read();
                            _oCashBalances.DayTradingRoundTrips = xml.Value;

                        }

                        // Get Stock Buying Power
                        if (name.ToLower().ToString().CompareTo("stock-buying-power") == 0)// && cParseStage.CompareTo("balances") == 0)
                        {
                            xml.Read();
                            _oCashBalances.StockBuyingPower = xml.Value;

                        }

                        // Get DayTrading Buying Power
                        if (name.ToLower().ToString().CompareTo("day-trading-buying-power") == 0) //&& cParseStage.CompareTo("balances") == 0)
                        {
                            xml.Read();
                            _oCashBalances.DayTradingBuyingPower = xml.Value;

                        }

                        // Get Available Funds For Trading
                        if (name.ToLower().ToString().CompareTo("available-funds-for-trading") == 0) // && cParseStage.CompareTo("balances") == 0)
                        {
                            xml.Read();
                            _oCashBalances.AvailableFundsForTrading = xml.Value;

                        }



                        // Get Initial Cash Balance
                        if (name.ToLower().ToString().CompareTo("initial") == 0 && cParseStage.CompareTo("account-value") == 0)
                        {
                            xml.Read();
                            _oCashBalances.InitialCashBalance = xml.Value;
                        }

                        // Get Current Cash Balance
                        if (name.ToLower().ToString().CompareTo("current") == 0 && cParseStage.CompareTo("account-value") == 0)
                        {
                            xml.Read();
                            _oCashBalances.CurrentCashBalance = xml.Value;
                        }

                        // Get Change In Cash Balance
                        if (name.ToLower().ToString().CompareTo("change") == 0 && cParseStage.CompareTo("account-value") == 0)
                        {
                            xml.Read();
                            _oCashBalances.ChangeInCashBalance = xml.Value;
                        }


                        // Get Positions - Symbol
                        if (name.ToLower().ToString().CompareTo("symbol") == 0 && lReadingPositionsElement == true)
                        {
                            xml.Read();
                            _oPositions.StockSymbol = xml.Value;
                        }


                        // Get Positions - Quantity
                        if (name.ToLower().ToString().CompareTo("quantity") == 0 && lReadingPositionsElement == true)
                        {
                            xml.Read();
                            _oPositions.Quantity = xml.Value;
                        }


                        // Get Positions - Description
                        if (name.ToLower().ToString().CompareTo("description") == 0 && lReadingPositionsElement == true)
                        {
                            xml.Read();
                            _oPositions.description = xml.Value;
                        }


                        // Get Positions - Account Type
                        if (name.ToLower().ToString().CompareTo("account-type") == 0 && lReadingPositionsElement == true)
                        {
                            xml.Read();
                            _oPositions.AccountType = xml.Value;
                        }


                        // Get Positions - Asset Type
                        if (name.ToLower().ToString().CompareTo("asset-type") == 0 && lReadingPositionsElement == true)
                        {
                            xml.Read();
                            _oPositions.AssetType = xml.Value;
                        }

                        // Get Positions - Closing Price
                        if (name.ToLower().ToString().CompareTo("close-price") == 0 && lReadingPositionsElement == true)
                        {
                            xml.Read();
                            _oPositions.ClosePrice = xml.Value;
                        }

                        // Get Positions - Position Type
                        if (name.ToLower().ToString().CompareTo("position-type") == 0 && lReadingPositionsElement == true)
                        {
                            xml.Read();
                            _oPositions.PositionType = xml.Value;
                        }

                        // Get Positions - Average Price
                        if (name.ToLower().ToString().CompareTo("average-price") == 0 && lReadingPositionsElement == true)
                        {
                            xml.Read();
                            _oPositions.AveragePric = xml.Value;
                        }


                        // Get Positions - Current Value
                        if (name.ToLower().ToString().CompareTo("current-value") == 0 && lReadingPositionsElement == true)
                        {
                            xml.Read();
                            _oPositions.CurrentValue = xml.Value;

                        }



                        // Get Error Message
                        if (name.ToLower().ToString().CompareTo(_errormsg_tag.ToLower()) == 0)
                        {
                            xml.Read();
                            _errormsg = xml.Value;
                        }
                    }
                    else
                    {
                        if (xml.NodeType == XmlNodeType.EndElement)
                        {

                            if (xml.Name.ToString().ToLower().CompareTo("balance") == 0)
                            {
                                oCashBalances.Add(_oCashBalances);
                            }


                            if (xml.Name.ToString().ToLower().CompareTo("position") == 0)
                            {
                                lReadingPositionsElement = false;
                                oPositions.Add(_oPositions);
                            }


                            if (cParseStage.CompareTo(xml.Name.ToString().ToLower()) == 0)
                            {
                                cParseStage = string.Empty;
                            }

                        }
                    }
                }

                retValue = (_errormsg.Length == 0 ? true : false);
            }
            else
                retValue = false;
        }