예제 #1
0
        // send an Ameritrde binary request
        private byte[] SendAmeritradeBinaryRequest(string Url)
        {
            byte[] xmlData   = null;
            string lcPostUrl = string.Empty;

            if (LoginStatus)
            {
                XMLHTTP       xmlHttp_  = new XMLHTTP();
                StringBuilder cpostdata = new StringBuilder();

                lcPostUrl = Url;

                xmlHttp_.open("POST", lcPostUrl, false, Username, 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);

                xmlData = (byte[])xmlHttp_.responseBody;

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

                string cResponseHeaders = xmlHttp_.getAllResponseHeaders();
            }

            return(xmlData);
        }
        private void TestService()
        {
            bool    success = false;
            XMLHTTP http    = new XMLHTTP();

            try
            {
                logger.Info("测试接口是否联通?");
                http.open("GET", _serviceUrl, false, null, null);
                http.send(_serviceUrl);
                if (http.status == 200)
                {
                    logger.Info("接口正常!");
                    success = true;
                    this.PrintTimer.Interval = _interval;
                    this.PrintTimer.Enabled  = true;
                    this.PrintTimer.Elapsed += new System.Timers.ElapsedEventHandler(this.Print_Elapse);
                }
                else
                {
                    logger.Error("数据接口不可用!");
                }
            }
            catch
            {
                logger.Error("数据接口不可用!");
            }
            finally
            {
                if (!success)
                {
                    this.Stop();
                }
            }
        }
예제 #3
0
        private bool KeepAlive()
        {
            bool retValue = false;

            if (LoginStatus)
            {
                XMLHTTP       xmlHttp_  = new XMLHTTP();
                StringBuilder cpostdata = new StringBuilder();
                string        lcPostUrl = string.Empty;

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

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

                xmlHttp_.open("POST", lcPostUrl, false, Username, 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);

                retValue = true;
            }
            else
            {
                retValue = false;
            }

            return(retValue);
        }
예제 #4
0
        private static string GetHTML(string url)
        {
            XMLHTTP x = new XMLHTTP();

            x.open("GET", url, false);
            x.send();
            return(x.responseText);
        }
예제 #5
0
        private static string GetHTML(string url)
        {
            Log("Connect:" + url, ConsoleColor.Yellow);
            XMLHTTP x = new XMLHTTP();

            x.open("GET", url, false);
            x.send();
            Log("Web data received !", ConsoleColor.Yellow);
            Byte[] b = (Byte[])x.responseBody;
            string s = System.Text.ASCIIEncoding.UTF8.GetString(b, 0, b.Length);

            return(s);
        }
예제 #6
0
        private bool Logout()
        {
            bool retValue = false;

            if (LoginStatus)
            {
                XMLHTTP       xmlHttp_  = new XMLHTTP();
                StringBuilder cpostdata = new StringBuilder();
                string        lcPostUrl = string.Empty;

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


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

                xmlHttp_.open("POST", lcPostUrl, false, Username, 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);

                retValue = true;
            }
            else
            {
                retValue = false;
            }

            // reset status
            this._result          = string.Empty;
            this._sessionid       = string.Empty;
            this._userid          = string.Empty;
            this._cdi             = string.Empty;
            this._timeout         = string.Empty;
            this._associated_acct = string.Empty;
            this._accountid       = string.Empty;
            this._description     = string.Empty;
            this._company         = string.Empty;
            this._segment         = string.Empty;
            this._margintrading   = string.Empty;

            LoginStatus = false;
            return(retValue);
        }
예제 #7
0
        public static string GetRemoteAdress()
        {
            if (AppConfig.Settings("RemoteAddress_Spare") == null)
            {
                return(AppConfig.Settings("RemoteAddress"));
            }
            XMLHTTP http = new XMLHTTP();

            http.open("GET", AppConfig.Settings("RemoteAddress"), false, null, null);
            http.send(null);
            int status = http.status;

            if (status == 200)
            {
                return(AppConfig.Settings("RemoteAddress"));
            }
            else
            {
                return(AppConfig.Settings("RemoteAddress_Spare"));
            }
        }
예제 #8
0
        /// <summary>
        /// 向富士康上传接口传送数据
        /// </summary>
        /// <param name="url"></param>
        /// <param name="plantName"></param>
        /// <param name="unitName"></param>
        /// <param name="deviceAddress"></param>
        /// <param name="deviceTypeCode"></param>
        /// <param name="dataCollectTime"></param>
        /// <param name="realDataValueStr"></param>
        private void callwebservice(string url, string plantName, string unitName, string deviceAddress, int deviceTypeCode, string dataCollectTime, string realDataValueStr)
        {
            //Web服务的地址
            //拼接数据

            string data;

            data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
            data = data + "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">";
            data = data + "<soap12:Body>";
            data = data + "<UpLoadEQValue xmlns=\"http://tempuri.org/\">";
            data = data + "<PlantID>" + plantName + "</PlantID>";
            data = data + "<BuildingID>" + unitName + "</BuildingID>";
            data = data + "<EQID>" + unitName + "-" + deviceTypeCode + "-" + StringUtil.getstr(deviceAddress, 3) + "</EQID>";
            data = data + "<EqType>" + deviceTypeCode + "</EqType>";
            data = data + "<DataCollectTime>" + dataCollectTime + "</DataCollectTime>";
            data = data + "<EQValues>" + realDataValueStr + "</EQValues>";
            data = data + "</UpLoadEQValue>";
            data = data + "</soap12:Body>";
            data = data + "</soap12:Envelope>";

            //创建异步对象(XMLHTTP对象在MSXML2下)
            String  res     = "";
            XMLHTTP xmlhttp = new XMLHTTP();

            xmlhttp.open("POST", url, false, null, null);

            xmlhttp.setRequestHeader("Content-Type", "application/soap+xml");

            xmlhttp.send(data);
            res = System.Text.Encoding.UTF8.GetString((byte[])xmlhttp.responseBody);

            FileLogUtil.info("data" + data);
            FileLogUtil.info("res" + res);
            Console.WriteLine("data" + data);
            Console.WriteLine("res" + res);
        }
예제 #9
0
 public static Boolean urlIsReach(String url)
 {
     if (url == null)
     {
         return(false);
     }
     try
     {
         XMLHTTP http = new XMLHTTP();
         try
         {
             http.open("GET", url, false, null, null);
             http.send(url);
             int status = http.status;
             if (status == 200)
             {
                 return(true);
             }
             else
             {
                 Program.WriteLog("不可用status:" + status.ToString());
                 return(false);
             }
         }
         catch
         {
             Console.WriteLine("不可用");
         }
     }
     catch (Exception e)
     {
         Program.WriteLog("异常" + e.ToString());
         return(false);
     }
     return(false);
 }
예제 #10
0
        // send an Ameritrde generic request
        private XmlDocument SendAmeritradeRequest(string Url)
        {
            XmlDocument XmlDoc    = null;
            string      lcPostUrl = string.Empty;

            if (LoginStatus)
            {
                XMLHTTP       xmlHttp_  = new XMLHTTP();
                StringBuilder cpostdata = new StringBuilder();

                lcPostUrl = Url;

                xmlHttp_.open("POST", lcPostUrl, false, Username, 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();

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

                string cResponseHeaders = xmlHttp_.getAllResponseHeaders();

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

                XmlDoc = new XmlDocument();
                XmlDoc.LoadXml(xmlData);
            }
            return(XmlDoc);
        }
예제 #11
0
        /*/
         * *****************************************************************************
         * TDAmeritrade API Code 
         *   
         *   Cedric Harris
         *   September 25, 2007
         * *****************************************************************************  
        /*/


        public bool TD_brokerLogin(string _userid, string _password, string _source, string _version)
        {
            bool retValue = false;
            XMLHTTP xmlHttp_ = new XMLHTTP();
            StringBuilder cpostdata = new StringBuilder();
            string lcPostUrl = string.Empty;

            cpostdata.Append("userid=" + Encode_URL(_userid));
            cpostdata.Append("&password="******"&source=" + Encode_URL(_source));
            cpostdata.Append("&version=" + Encode_URL(_version));


            lcPostUrl = "https://apis.tdameritrade.com/apps/100/LogIn?source=" + Encode_URL(_source) + "&version=" + Encode_URL(_version);

            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(cpostdata.ToString());


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



            /*/
             * Assign Login values from the response string
             * 
            /*/


            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?");
            }

            // Read the Xml.
            while (xml.Read())
            {
                // Got an element?
                if (xml.NodeType == XmlNodeType.Element)
                {
                    #region Parse the Xml Element returned from TD Ameritrade
                    // Get this node.
                    string name = xml.Name;

                    // Get Result/Status
                    if (name.ToLower().ToString().CompareTo(_result_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        _result = xml.Value;
                    }

                    // Get Session ID
                    if (name.ToLower().ToString().CompareTo(_sessionid_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        _sessionid = xml.Value;
                    }

                    // Get USER ID
                    if (name.ToLower().ToString().CompareTo(_userid_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        _userid = xml.Value;
                    }

                    // Get CDI
                    if (name.ToLower().ToString().CompareTo(_cdi_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        _cdi = xml.Value;
                    }

                    // Get TimeOut
                    if (name.ToLower().ToString().CompareTo(_timeout_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        _timeout = xml.Value;
                    }

                    // Get Associated User Account
                    if (name.ToLower().ToString().CompareTo(_associated_acct_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        _associated_acct = xml.Value;
                    }

                    // Get Account ID
                    if (name.ToLower().ToString().CompareTo(_accountid_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        _accountid = xml.Value;
                    }

                    // Get Description
                    if (name.ToLower().ToString().CompareTo(_description_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        _description = xml.Value;
                    }

                    // Get Company
                    if (name.ToLower().ToString().CompareTo(_company_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        _company = xml.Value;
                    }

                    // Get Segment
                    if (name.ToLower().ToString().CompareTo(_segment_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        _segment = xml.Value;
                    }

                    // Get Margined Account
                    if (name.ToLower().ToString().CompareTo(_margintrading_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        _margintrading = xml.Value;
                    }
                    #endregion
                }

            }

            retValue = (_result.ToUpper().CompareTo("OK") >= 0 ? true : false);
            TD_loginStatus = retValue;

            return retValue;
        }
예제 #12
0
        /// <summary>
        /// 向富士康上传接口传送数据
        /// </summary>
        /// <param name="url"></param>
        /// <param name="plantName"></param>
        /// <param name="unitName"></param>
        /// <param name="deviceAddress"></param>
        /// <param name="deviceTypeCode"></param>
        /// <param name="dataCollectTime"></param>
        /// <param name="realDataValueStr"></param>
        private void callwebservice(string url, string plantName, string unitName, string deviceAddress, int deviceTypeCode, string dataCollectTime, string realDataValueStr)
        {
            //Web服务的地址
            //拼接数据

            string data;
            data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
            data = data + "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">";
            data = data + "<soap12:Body>";
            data = data + "<UpLoadEQValue xmlns=\"http://tempuri.org/\">";
            data = data + "<PlantID>" + plantName + "</PlantID>";
            data = data + "<BuildingID>" + unitName + "</BuildingID>";
            data = data + "<EQID>" + unitName + "-" + deviceTypeCode + "-" + StringUtil.getstr(deviceAddress, 3) + "</EQID>";
            data = data + "<EqType>" + deviceTypeCode + "</EqType>";
            data = data + "<DataCollectTime>" + dataCollectTime + "</DataCollectTime>";
            data = data + "<EQValues>" + realDataValueStr + "</EQValues>";
            data = data + "</UpLoadEQValue>";
            data = data + "</soap12:Body>";
            data = data + "</soap12:Envelope>";

            //创建异步对象(XMLHTTP对象在MSXML2下)
            String res = "";
            XMLHTTP xmlhttp = new XMLHTTP();

            xmlhttp.open("POST", url, false, null, null);

            xmlhttp.setRequestHeader("Content-Type", "application/soap+xml");

            xmlhttp.send(data);
            res = System.Text.Encoding.UTF8.GetString((byte[])xmlhttp.responseBody);

            FileLogUtil.info("data" + data);
            FileLogUtil.info("res" + res);
            Console.WriteLine("data" + data);
            Console.WriteLine("res" + res);
        }
예제 #13
0
        public void TD_geOrderStatusAndHistory(string _userid, string _password, string _source, string _version, string orderList, orderHistoryType OrderyHistoryType, ref List<ATradeArgument.tradeReplyDetails> oReplyDetails)
        {


            if (this.TD_loginStatus == true)
            {

                XMLHTTP xmlHttp_ = new XMLHTTP();
                StringBuilder cpostdata = new StringBuilder();
                string lcPostUrl = string.Empty;

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


                switch (OrderyHistoryType)
                {
                    case orderHistoryType.ALL_ORDERS:

                        cpostdata.Append("&type=" + Encode_URL("all"));
                        cpostdata.Append("&days=" + Encode_URL("0"));

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

                        break;


                    case orderHistoryType.OPEN_ORDERS:

                        cpostdata.Append("orderid=" + Encode_URL(orderList));
                        cpostdata.Append("type=" + Encode_URL("open"));
                        cpostdata.Append("days=" + Encode_URL("0"));

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

                        break;


                    case orderHistoryType.PENDING_ORDERS:

                        cpostdata.Append("orderid=" + Encode_URL(orderList));
                        cpostdata.Append("type=" + Encode_URL("pending"));
                        cpostdata.Append("days=" + Encode_URL("0"));

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

                        break;


                    case orderHistoryType.FILLED:

                        cpostdata.Append("orderid=" + Encode_URL(orderList));
                        cpostdata.Append("type=" + Encode_URL("filled"));
                        cpostdata.Append("days=" + Encode_URL("0"));

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

                        break;


                    case orderHistoryType.CANCELLED_ORDERS:

                        cpostdata.Append("orderid=" + Encode_URL(orderList));
                        cpostdata.Append("type=" + Encode_URL("canceled"));
                        cpostdata.Append("days=" + Encode_URL("0"));

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

                        break;


                }

                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(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?");
                }


                string[,] oFillDetails = new string[1, 2];
                List<string[,]> oFillDetailsList = new List<string[,]>();
                ATradeArgument.tradeReplyDetails oTradeHistoryDetails = new ATradeArgument.tradeReplyDetails();
                string name = string.Empty;


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

                        // Initialize the start of the collection 
                        if (name.ToLower().ToString().CompareTo("orderstatus") == 0)
                        {
                            oTradeHistoryDetails = new ATradeArgument.tradeReplyDetails();
                            oFillDetailsList = new List<string[,]>();

                        }


                        // Get order-number
                        if (name.ToLower().ToString().CompareTo("order-number") == 0)
                        {
                            xml.Read();
                            oTradeHistoryDetails.OrderNumber = xml.Value;
                        }


                        // Get cancelable
                        if (name.ToLower().ToString().CompareTo("cancelable") == 0)
                        {
                            xml.Read();
                            oTradeHistoryDetails.lIsCancelable = Convert.ToBoolean(xml.Value);
                        }

                        // Get editable
                        if (name.ToLower().ToString().CompareTo("editable") == 0)
                        {
                            xml.Read();
                            oTradeHistoryDetails.lIsEditable = Convert.ToBoolean(xml.Value);
                        }

                        // Get enhanced-order
                        if (name.ToLower().ToString().CompareTo("enhanced-order") == 0)
                        {
                            xml.Read();
                            oTradeHistoryDetails.lIsEnhancedOrder = Convert.ToBoolean(xml.Value);
                        }

                        // Get EnhancedOrderType
                        if (name.ToLower().ToString().CompareTo("enhanced-type") == 0)
                        {
                            xml.Read();
                            oTradeHistoryDetails.EnhancedOrderType = xml.Value;
                        }

                        // Get display-status
                        if (name.ToLower().ToString().CompareTo("display-status") == 0)
                        {
                            xml.Read();
                            oTradeHistoryDetails.DisplayStatus = xml.Value;
                        }

                        // Get order-routing-status
                        if (name.ToLower().ToString().CompareTo("order-routing-status") == 0)
                        {
                            xml.Read();
                            oTradeHistoryDetails.OrderRoutingStatus = xml.Value;
                        }

                        // Get order-received-date-time
                        if (name.ToLower().ToString().CompareTo("order-received-date-time") == 0)
                        {
                            xml.Read();
                            oTradeHistoryDetails.OrderReceivedTime = xml.Value;
                        }

                        // Get reported-time
                        if (name.ToLower().ToString().CompareTo("reported-time") == 0)
                        {
                            xml.Read();
                            oTradeHistoryDetails.OrderReportedTime = xml.Value;
                        }


                        // Get remaining-quantity
                        if (name.ToLower().ToString().CompareTo("remaining-quantity") == 0)
                        {
                            xml.Read();
                            oTradeHistoryDetails.OrderSharesRemianing = xml.Value;
                        }

                        // Get symbol
                        if (name.ToLower().ToString().CompareTo("symbol") == 0)
                        {
                            xml.Read();
                            oTradeHistoryDetails.cStockSymbol = xml.Value;
                        }

                        // Get asset-type
                        if (name.ToLower().ToString().CompareTo("asset-type") == 0)
                        {
                            xml.Read();
                            if (xml.Value.ToUpper().CompareTo("E") == 0)
                                oTradeHistoryDetails.AssetType = "Equity";

                            if (xml.Value.ToUpper().CompareTo("F") == 0)
                                oTradeHistoryDetails.AssetType = "Mutual Fund";

                            if (xml.Value.ToUpper().CompareTo("O") == 0)
                                oTradeHistoryDetails.AssetType = "Option";

                            if (xml.Value.ToUpper().CompareTo("B") == 0)
                                oTradeHistoryDetails.AssetType = "Bond";


                        }

                        // Get quantity
                        if (name.ToLower().ToString().CompareTo("quantity") == 0)
                        {
                            xml.Read();
                            oTradeHistoryDetails.OrderShares = xml.Value;
                        }

                        // Get order-id
                        if (name.ToLower().ToString().CompareTo("order-id") == 0)
                        {
                            xml.Read();
                            oTradeHistoryDetails.OrderID = xml.Value;
                        }

                        // Get action
                        if (name.ToLower().ToString().CompareTo("action") == 0)
                        {
                            xml.Read();
                            if (xml.Value.ToUpper().CompareTo("B") == 0)
                                oTradeHistoryDetails.Action = "Buy Long";

                            if (xml.Value.ToUpper().CompareTo("S") == 0)
                                oTradeHistoryDetails.Action = "Sell";

                            if (xml.Value.ToUpper().CompareTo("SS") == 0)
                                oTradeHistoryDetails.Action = "Sell Short";

                            if (xml.Value.ToUpper().CompareTo("BC") == 0)
                                oTradeHistoryDetails.Action = "Buy to Cover";


                        }

                        // Get trade-type
                        if (name.ToLower().ToString().CompareTo("trade-type") == 0)
                        {
                            xml.Read();
                            if (xml.Value.CompareTo("1") == 0)
                                oTradeHistoryDetails.TradeType = "Normal Market";

                            if (xml.Value.CompareTo("2") == 0)
                                oTradeHistoryDetails.TradeType = "External Hour Market";

                            if (xml.Value.CompareTo("4") == 0)
                                oTradeHistoryDetails.TradeType = "German Market";

                            if (xml.Value.CompareTo("8") == 0)
                                oTradeHistoryDetails.TradeType = "AM Session";

                            if (xml.Value.CompareTo("16") == 0)
                                oTradeHistoryDetails.TradeType = "Seamless Session";

                        }

                        // Get order-type
                        if (name.ToLower().ToString().CompareTo("order-type") == 0)
                        {
                            xml.Read();
                            if (xml.Value.ToUpper().CompareTo("M") == 0)
                                oTradeHistoryDetails.OrderType = "Market";

                            if (xml.Value.ToUpper().CompareTo("L") == 0)
                                oTradeHistoryDetails.OrderType = "Limit";

                            if (xml.Value.ToUpper().CompareTo("S") == 0)
                                oTradeHistoryDetails.OrderType = "Stop";

                            if (xml.Value.ToUpper().CompareTo("X") == 0)
                                oTradeHistoryDetails.OrderType = "Stop Limit";

                            if (xml.Value.ToUpper().CompareTo("D") == 0)
                                oTradeHistoryDetails.OrderType = "Debit";

                            if (xml.Value.ToUpper().CompareTo("C") == 0)
                                oTradeHistoryDetails.OrderType = "Credit";

                            if (xml.Value.ToUpper().CompareTo("T") == 0)
                                oTradeHistoryDetails.OrderType = "Trailing Stop";

                            if (xml.Value.ToUpper().CompareTo("EX") == 0)
                                oTradeHistoryDetails.OrderType = "Exercise Option";


                        }


                        // Get Fill(s) - Quantity
                        if (name.ToLower().ToString().CompareTo("fill-quantity") == 0)
                        {
                            xml.Read();
                            oFillDetails = new string[1, 2];
                            oFillDetails[0, 0] = xml.Value;

                        }


                        // Get Fill(s) - Price
                        if (name.ToLower().ToString().CompareTo("fill-price") == 0)
                        {
                            xml.Read();
                            oFillDetails[0, 1] = xml.Value;
                            oFillDetailsList.Add(oFillDetails);

                        }



                        // Get limit-price
                        if (name.ToLower().ToString().CompareTo("limit-price") == 0)
                        {
                            xml.Read();
                            oTradeHistoryDetails.OrderPrice = xml.Value;
                        }

                    }
                    else
                    {
                        if (xml.NodeType == XmlNodeType.EndElement)
                        {

                            if (xml.Name.ToString().ToLower().CompareTo("orderstatus") == 0)
                            {
                                Decimal nTotalDollarsExecuted = 0.00M;
                                Int32 nTotalSharesExecuted = 0;

                                if (oFillDetailsList.Count > 0)
                                {
                                    foreach (string[,] oDetail in oFillDetailsList)
                                    {
                                        nTotalSharesExecuted = nTotalSharesExecuted + Convert.ToInt32(Decimal.Round(Convert.ToDecimal(oDetail[0, 0]), 0));
                                        nTotalDollarsExecuted = nTotalDollarsExecuted + Convert.ToDecimal(Decimal.Round(Convert.ToDecimal(oDetail[0, 0]), 0) * Convert.ToDecimal(oDetail[0, 1]));

                                    }
                                    oTradeHistoryDetails.OrderPriceFilled = Convert.ToString(Decimal.Round(nTotalDollarsExecuted / nTotalSharesExecuted, 2));
                                    oTradeHistoryDetails.OrderQuantityFilled = Convert.ToString(nTotalSharesExecuted);
                                }
                                else
                                {
                                    oTradeHistoryDetails.OrderPriceFilled = "0.00";
                                    oTradeHistoryDetails.OrderQuantityFilled = "0";

                                }

                                oReplyDetails.Add(oTradeHistoryDetails);
                            }
                        }
                    }
                }
            }
        }
예제 #14
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;
        }
예제 #15
0
        public bool TD_getTransactionHistory(string _userid, string _password, string _source, string _version, string _startDate, string _endDate, transactionHistoryType transHistoryType, ref List<AmeritradeBrokerAPI.ATradeArgument.transactionHistoryReplyDetails> oReplyDetails)
        {

            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));
                cpostdata.Append("&type=" + Encode_URL(Convert.ToString((int)transHistoryType)));

                if (_startDate.Length > 0 && _endDate.Length > 0)
                {
                    cpostdata.Append("&startdate=" + Encode_URL(_startDate));
                    cpostdata.Append("&enddate=" + Encode_URL(_endDate));
                }

                lcPostUrl = "https://apis.tdameritrade.com/apps/100/History?" + 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();



                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?");
                }

                AmeritradeBrokerAPI.ATradeArgument.transactionHistoryReplyDetails _otransHistory = new ATradeArgument.transactionHistoryReplyDetails();

                string name = string.Empty;
                string _tAccountID = string.Empty;
                string _tStartDate = string.Empty;
                string _tEndDate = string.Empty;


                // 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("transaction-list") == 0)
                        {
                            _otransHistory = new ATradeArgument.transactionHistoryReplyDetails();

                        }


                        // Get account ID
                        if (name.ToLower().ToString().CompareTo("account-id") == 0)
                        {
                            xml.Read();
                            _tAccountID = xml.Value;

                        }

                        // Get start Date
                        if (name.ToLower().ToString().CompareTo("startdate") == 0)
                        {
                            xml.Read();
                            _tStartDate = xml.Value;

                        }


                        // Get end Date
                        if (name.ToLower().ToString().CompareTo("enddate") == 0)
                        {
                            xml.Read();
                            _tEndDate = xml.Value;

                        }


                        // Get orderDateTime
                        if (name.ToLower().ToString().CompareTo("orderdatetime") == 0 || name.ToLower().ToString().CompareTo("executeddate") == 0)
                        {
                            xml.Read();
                            _otransHistory.orderDate = xml.Value;

                        }

                        // Get type
                        if (name.ToLower().ToString().CompareTo("type") == 0)
                        {
                            xml.Read();
                            _otransHistory.historyReqType = xml.Value;

                        }

                        // Get subType
                        if (name.ToLower().ToString().CompareTo("subtype") == 0)
                        {
                            xml.Read();
                            _otransHistory.subType = xml.Value;

                        }


                        // Get buySellCode
                        if (name.ToLower().ToString().CompareTo("buysellcode") == 0)
                        {
                            xml.Read();
                            _otransHistory.buySellCode = xml.Value;

                        }


                        // Get assetType
                        if (name.ToLower().ToString().CompareTo("assettype") == 0)
                        {
                            xml.Read();
                            _otransHistory.assetType = xml.Value;

                        }


                        // Get symbol
                        if (name.ToLower().ToString().CompareTo("symbol") == 0)
                        {
                            xml.Read();
                            _otransHistory.symbol = xml.Value;

                        }


                        // Get description
                        if (name.ToLower().ToString().CompareTo("description") == 0)
                        {
                            xml.Read();
                            _otransHistory.description = xml.Value;

                        }


                        // Get cusip
                        if (name.ToLower().ToString().CompareTo("cusip") == 0)
                        {
                            xml.Read();
                            _otransHistory.cusip = xml.Value;

                        }


                        // Get price
                        if (name.ToLower().ToString().CompareTo("price") == 0)
                        {
                            xml.Read();
                            _otransHistory.price = xml.Value;

                        }


                        // Get quantity
                        if (name.ToLower().ToString().CompareTo("quantity") == 0)
                        {
                            xml.Read();
                            _otransHistory.quantity = xml.Value;
                            _otransHistory.quantity.Replace(",", "");

                        }


                        // Get transactionId
                        if (name.ToLower().ToString().CompareTo("transactionid") == 0)
                        {
                            xml.Read();
                            _otransHistory.transactionID = xml.Value;

                        }


                        // Get value
                        if (name.ToLower().ToString().CompareTo("value") == 0)
                        {
                            xml.Read();
                            _otransHistory.value = xml.Value;
                            _otransHistory.value.Replace(",", "");

                        }



                        // Get commission
                        if (name.ToLower().ToString().CompareTo("commission") == 0)
                        {
                            xml.Read();
                            _otransHistory.commission = xml.Value;
                            _otransHistory.commission.Replace(",", "");

                        }



                        // Get orderNumber
                        if (name.ToLower().ToString().CompareTo("ordernumber") == 0)
                        {
                            xml.Read();
                            _otransHistory.orderNumber = xml.Value;

                        }



                        // Get fees
                        if (name.ToLower().ToString().CompareTo("fees") == 0)
                        {
                            xml.Read();
                            _otransHistory.fees = (xml.Value.Length > 0 ? xml.Value : "0.00");
                            _otransHistory.fees.Replace(",", "");

                        }



                        // Get additionalFees
                        if (name.ToLower().ToString().CompareTo("additionalfees") == 0)
                        {
                            xml.Read();
                            _otransHistory.additionalFees = (xml.Value.Length > 0 ? xml.Value : "0.00");
                            _otransHistory.additionalFees.Replace(",", "");

                        }


                        // Get cashBalanceEffect
                        if (name.ToLower().ToString().CompareTo("cashbalanceeffect") == 0)
                        {
                            xml.Read();
                            _otransHistory.cashBalanceEffect = xml.Value;

                        }



                        // Get openClose
                        if (name.ToLower().ToString().CompareTo("openclose") == 0)
                        {
                            xml.Read();
                            _otransHistory.openClose = xml.Value;

                        }



                        // Get optionType
                        if (name.ToLower().ToString().CompareTo("optiontype") == 0)
                        {
                            xml.Read();
                            _otransHistory.optionType = xml.Value;

                        }



                        // Get optionStrike
                        if (name.ToLower().ToString().CompareTo("optionstrike") == 0)
                        {
                            xml.Read();
                            _otransHistory.optionStrike = xml.Value;

                        }


                        // Get accruedInterest
                        if (name.ToLower().ToString().CompareTo("accruedinterest") == 0)
                        {
                            xml.Read();
                            _otransHistory.accruedInterest = xml.Value;

                        }



                        // Get parentChildIndicator
                        if (name.ToLower().ToString().CompareTo("parentchildindicator") == 0)
                        {
                            xml.Read();
                            _otransHistory.parentchildIndicator = xml.Value;

                        }



                        // Get sharesBefore
                        if (name.ToLower().ToString().CompareTo("sharesbefore") == 0)
                        {
                            xml.Read();
                            _otransHistory.sharesBefore = xml.Value;

                        }



                        // Get sharesAfter
                        if (name.ToLower().ToString().CompareTo("sharesafter") == 0)
                        {
                            xml.Read();
                            _otransHistory.sharesAfter = xml.Value;

                        }


                        // Get otherCharges
                        if (name.ToLower().ToString().CompareTo("othercharges") == 0)
                        {
                            xml.Read();
                            _otransHistory.otherCharges = xml.Value;
                            _otransHistory.otherCharges.Replace(",", "");

                        }



                        // Get redemptionFee
                        if (name.ToLower().ToString().CompareTo("redemptionfee") == 0)
                        {
                            xml.Read();
                            _otransHistory.redemptionFee = xml.Value;

                        }


                        // Get cdscFee
                        if (name.ToLower().ToString().CompareTo("cdscfee") == 0)
                        {
                            xml.Read();
                            _otransHistory.cdscFee = xml.Value;

                        }


                        // Get bondInterestRate
                        if (name.ToLower().ToString().CompareTo("bondInterestrate") == 0)
                        {
                            xml.Read();
                            _otransHistory.bondInterestRate = 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("transaction-list") == 0)
                            {

                                _otransHistory.accountId = _tAccountID;
                                _otransHistory.searchDateFrom = _tStartDate;
                                _otransHistory.searchDateTo = _tEndDate;

                                oReplyDetails.Add(_otransHistory);

                            }

                        }
                    }
                }

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

            return retValue;

        }
예제 #16
0
        public string TD_CancelOrder(string AccountID, string OrderID, string _userid, string _password, string _source, string _version, ref string cResultValue)
        {

            _result = string.Empty;

            if (this.TD_loginStatus == true)
            {

                XMLHTTP xmlHttp_ = new XMLHTTP();
                StringBuilder cpostdata = new StringBuilder();
                string lcPostUrl = string.Empty;


                cpostdata.Append("source=" + Encode_URL(_source));
                cpostdata.Append("&accountid=" + AccountID);
                cpostdata.Append("&orderid=" + OrderID);

                lcPostUrl = "https://apis.tdameritrade.com/apps/100/OrderCancel?" + 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(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?");
                }

                string cParseStage = string.Empty;
                string name = string.Empty;


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

                        // Get Result/Status
                        if (name.ToLower().ToString().CompareTo(_result_tag.ToLower()) == 0)
                        {
                            xml.Read();
                            _result = xml.Value;

                        }


                        // Get Error Message
                        if (name.ToLower().ToString().CompareTo(_error_tag.ToLower()) == 0)
                        {
                            xml.Read();
                            _error = xml.Value;
                            cResultValue = _error;
                        }

                    }
                }
            }

            return _result;
        }
예제 #17
0
        public bool TD_Logout(string _userid, string _password, string _source, string _version)
        {
            bool retValue = false;

            if (this.TD_loginStatus == true)
            {

                XMLHTTP xmlHttp_ = new XMLHTTP();
                StringBuilder cpostdata = new StringBuilder();
                string lcPostUrl = string.Empty;

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


                lcPostUrl = "https://apis.tdameritrade.com/apps/100/LogOut?" + 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);

                retValue = true;
            }
            else
                retValue = false;

            return retValue;

        }
예제 #18
0
        public bool TD_GetStreamerInfo(string _userid, string _password, string _source, string _version)
        {

            bool retValue = false;

            if (this.TD_loginStatus == true)
            {

                XMLHTTP xmlHttp_ = new XMLHTTP();
                StringBuilder cpostdata = new StringBuilder();
                string lcPostUrl = string.Empty;

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


                lcPostUrl = "https://apis.tdameritrade.com/apps/100/StreamerInfo?" + 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();



                /*/
                 * Assign Login values from the response string
                 * 
                /*/


                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?");
                }

                // Read the Xml.
                while (xml.Read())
                {
                    // Got an element?
                    if (xml.NodeType == XmlNodeType.Element)
                    {
                        #region Parse the Xml Element returned from TD Ameritrade
                        // Get this node.
                        string name = xml.Name;

                        // Get Streamer URL
                        if (name.ToLower().ToString().CompareTo(_streamerurl_tag.ToLower()) == 0)
                        {
                            xml.Read();
                            _streamerurl = xml.Value;
                        }

                        // Get Token
                        if (name.ToLower().ToString().CompareTo(_token_tag.ToLower()) == 0)
                        {
                            xml.Read();
                            _token = xml.Value;
                        }

                        // Get TimeStamp
                        if (name.ToLower().ToString().CompareTo(_timestamp_tag.ToLower()) == 0)
                        {
                            xml.Read();
                            _timestamp = xml.Value;
                        }

                        // Get cdDomain
                        if (name.ToLower().ToString().CompareTo(_cdDomain_tag.ToLower()) == 0)
                        {
                            xml.Read();
                            _cdDomain = xml.Value;
                        }

                        // Get User Group
                        if (name.ToLower().ToString().CompareTo(_usergroup_tag.ToLower()) == 0)
                        {
                            xml.Read();
                            _usergroup = xml.Value;
                        }

                        // Get Access Level
                        if (name.ToLower().ToString().CompareTo(_accesslevel_tag.ToLower()) == 0)
                        {
                            xml.Read();
                            _accesslevel = xml.Value;
                        }

                        // Get ACL
                        if (name.ToLower().ToString().CompareTo(_acl_tag.ToLower()) == 0)
                        {
                            xml.Read();
                            _acl = xml.Value;
                        }

                        // Get Application ID
                        if (name.ToLower().ToString().CompareTo(_appid_tag.ToLower()) == 0)
                        {
                            xml.Read();
                            _appid = xml.Value;
                        }

                        // Get Authorized
                        if (name.ToLower().ToString().CompareTo(_authorized_tag.ToLower()) == 0)
                        {
                            xml.Read();
                            _authorized = xml.Value;
                        }

                        // Get Error Message
                        if (name.ToLower().ToString().CompareTo(_errormsg_tag.ToLower()) == 0)
                        {
                            xml.Read();
                            _errormsg = xml.Value;
                        }
                        #endregion
                    }

                }

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

            return retValue;
        }
예제 #19
0
        private bool Login()
        {
            bool          retValue  = false;
            XMLHTTP       xmlHttp_  = new XMLHTTP();
            StringBuilder cpostdata = new StringBuilder();
            string        lcPostUrl = string.Empty;

            cpostdata.Append("userid=" + Encode_URL(Username));
            cpostdata.Append("&password="******"&source=" + Encode_URL(SourceId));
            cpostdata.Append("&version=" + Encode_URL(Version));

            lcPostUrl = "https://apis.tdameritrade.com/apps/100/LogIn?source=" + Encode_URL(SourceId) + "&version=" + Encode_URL(Version);

            xmlHttp_.open("POST", lcPostUrl, false, Username, 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(cpostdata.ToString());


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

            /*/
             * Assign Login values from the response string
             *
             * /*/

            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?");
            }

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

                    // Get Result/Status
                    if (name.ToLower().ToString().CompareTo(_result_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        this._result = xml.Value;
                    }

                    // Get Session ID
                    if (name.ToLower().ToString().CompareTo(_sessionid_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        this._sessionid = xml.Value;
                    }

                    // Get USER ID
                    if (name.ToLower().ToString().CompareTo(_userid_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        this._userid = xml.Value;
                    }

                    // Get CDI
                    if (name.ToLower().ToString().CompareTo(_cdi_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        this._cdi = xml.Value;
                    }

                    // Get TimeOut
                    if (name.ToLower().ToString().CompareTo(_timeout_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        this._timeout = xml.Value;
                    }

                    // Get Associated User Account
                    if (name.ToLower().ToString().CompareTo(_associated_acct_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        this._associated_acct = xml.Value;
                    }

                    // Get Account ID
                    if (name.ToLower().ToString().CompareTo(_accountid_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        this._accountid = xml.Value;
                    }

                    // Get Description
                    if (name.ToLower().ToString().CompareTo(_description_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        this._description = xml.Value;
                    }

                    // Get Company
                    if (name.ToLower().ToString().CompareTo(_company_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        this._company = xml.Value;
                    }

                    // Get Segment
                    if (name.ToLower().ToString().CompareTo(_segment_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        this._segment = xml.Value;
                    }

                    // Get Margined Account
                    if (name.ToLower().ToString().CompareTo(_margintrading_tag.ToLower()) == 0)
                    {
                        xml.Read();
                        this._margintrading = xml.Value;
                    }
                }
            }

            retValue = (_result.ToUpper().CompareTo("OK") >= 0 ? true : false);

            LoginStatus = retValue;
            return(retValue);
        }