Esempio n. 1
0
        public OutputBaseClass GenerateToken()
        {
            OutputBaseClass res = new OutputBaseClass();

            res.status    = true;
            res.http_code = "200";
            AngelTokenResponse restoken = new AngelTokenResponse();

            try
            {
                AngelToken Token = this.Token;
                if (Token != null)
                {
                    if (ValidateToken(Token))
                    {
                        string URL = APIURL + "/rest/auth/angelbroking/jwt/v1/generateTokens";

                        string PostData = "{\"refreshToken\":\"" + Token.refreshToken + "\"}";

                        string json = POSTWebRequest(Token, URL, PostData);
                        if (!json.Contains("PostError:"))
                        {
                            restoken          = JsonConvert.DeserializeObject <AngelTokenResponse>(json);
                            res.TokenResponse = restoken.data;
                            res.status        = restoken.status;
                            res.http_error    = restoken.message;
                            res.http_code     = restoken.errorcode;
                            this.Token        = restoken.data;
                        }
                        else
                        {
                            res.status     = false;
                            res.http_code  = "404";
                            res.http_error = json.Replace("PostError:", "");
                        }
                    }
                    else
                    {
                        res.status     = false;
                        res.http_code  = "404";
                        res.http_error = "The token is invalid";
                    }
                }
                else
                {
                    res.status     = false;
                    res.http_code  = "404";
                    res.http_error = "The token is invalid";
                }
            }
            catch (Exception ex)
            {
                res.status     = false;
                res.http_code  = "404";
                res.http_error = ex.Message;
            }
            return(res);
        }
Esempio n. 2
0
        public OutputBaseClass convertPosition(ConvertPositionRequest Request)
        {
            OutputBaseClass res = new OutputBaseClass();

            res.status    = true;
            res.http_code = "200";
            try
            {
                AngelToken Token = this.Token;
                if (Token != null)
                {
                    if (ValidateToken(Token))
                    {
                        string URL = APIURL + "/rest/secure/angelbroking/order/v1/convertPosition";

                        string PostData = JsonConvert.SerializeObject(Request);

                        string json = POSTWebRequest(Token, URL, PostData);
                        if (!json.Contains("PostError:"))
                        {
                            PositionConversionResponse pc = JsonConvert.DeserializeObject <PositionConversionResponse>(json);
                            res.PositionConversionResponse = pc;
                            res.status     = pc.status;
                            res.http_error = pc.message;
                            res.http_code  = pc.errorcode;
                        }
                        else
                        {
                            res.status     = false;
                            res.http_code  = "404";
                            res.http_error = json.Replace("PostError:", "");
                        }
                    }
                    else
                    {
                        res.status     = false;
                        res.http_code  = "404";
                        res.http_error = "The token is invalid";
                    }
                }
                else
                {
                    res.status     = false;
                    res.http_code  = "404";
                    res.http_error = "The token is invalid";
                }
            }
            catch (Exception ex)
            {
                res.status     = false;
                res.http_code  = "404";
                res.http_error = ex.Message;
            }
            return(res);
        }
Esempio n. 3
0
        public OutputBaseClass GetLTPData(LTPDataRequest req)
        {
            OutputBaseClass res = new OutputBaseClass();

            res.status    = true;
            res.http_code = "200";
            try
            {
                AngelToken Token = this.Token;
                if (Token != null)
                {
                    if (ValidateToken(Token))
                    {
                        string URL = APIURL + "/rest/secure/angelbroking/order/v1/getLtpData";

                        string PostData = JsonConvert.SerializeObject(req);

                        string Json = POSTWebRequest(Token, URL, PostData);
                        if (!Json.Contains("PostError:"))
                        {
                            GetLTPDataResponse ltp = JsonConvert.DeserializeObject <GetLTPDataResponse>(Json);
                            res.GetLTPDataResponse = ltp;
                            res.status             = ltp.status;
                            res.http_error         = ltp.message;
                            res.http_code          = ltp.errorcode;
                        }
                        else
                        {
                            res.status     = false;
                            res.http_code  = "404";
                            res.http_error = Json.Replace("PostError:", "");
                        }
                    }
                    else
                    {
                        res.status     = false;
                        res.http_code  = "404";
                        res.http_error = "The token is invalid";
                    }
                }
                else
                {
                    res.status     = false;
                    res.http_code  = "404";
                    res.http_error = "The token is invalid";
                }
            }
            catch (Exception ex)
            {
                res.status     = false;
                res.http_code  = "404";
                res.http_error = ex.Message;
            }
            return(res);
        }
Esempio n. 4
0
        public OutputBaseClass cancelOrder(OrderInfo order)
        {
            OutputBaseClass res = new OutputBaseClass();

            res.status    = true;
            res.http_code = "200";
            try
            {
                AngelToken Token = this.Token;
                if (Token != null)
                {
                    if (ValidateToken(Token))
                    {
                        string URL = APIURL + "/rest/secure/angelbroking/order/v1/cancelOrder";

                        string PostData = "{\"variety\":\"" + order.variety + "\",\"orderid\":\"" + order.orderid + "\"}";

                        string Json = POSTWebRequest(Token, URL, PostData);
                        if (!Json.Contains("PostError:"))
                        {
                            OrderResponse pres = JsonConvert.DeserializeObject <OrderResponse>(Json);
                            res.CancelOrderResponse = pres;
                            res.status     = pres.status;
                            res.http_error = pres.message;
                            res.http_code  = pres.errorcode;
                        }
                        else
                        {
                            res.status     = false;
                            res.http_code  = "404";
                            res.http_error = Json.Replace("PostError:", "");
                        }
                    }
                    else
                    {
                        res.status     = false;
                        res.http_code  = "404";
                        res.http_error = "The token is invalid";
                    }
                }
                else
                {
                    res.status     = false;
                    res.http_code  = "404";
                    res.http_error = "The token is invalid";
                }
            }
            catch (Exception ex)
            {
                res.status     = false;
                res.http_code  = "404";
                res.http_error = ex.Message;
            }
            return(res);
        }
Esempio n. 5
0
        /*Logout Calls */
        public OutputBaseClass LogOut(string clientcode)
        {
            OutputBaseClass res = new OutputBaseClass();

            res.status    = true;
            res.http_code = "200";
            try
            {
                AngelToken Token = this.Token;
                if (Token != null)
                {
                    if (ValidateToken(Token))
                    {
                        string URL = APIURL + "/rest/secure/angelbroking/user/v1/logout";

                        string PostData = "{\"clientcode\":\"" + clientcode + "\"}";

                        string Json = POSTWebRequest(Token, URL, PostData);
                        if (!Json.Contains("PostError:"))
                        {
                            LogOutResponse logres = JsonConvert.DeserializeObject <LogOutResponse>(Json);
                            res.LogOutResponse = logres;
                            res.status         = logres.status;
                            res.http_error     = logres.message;
                            res.http_code      = logres.errorcode;
                        }
                        else
                        {
                            res.status     = false;
                            res.http_code  = "404";
                            res.http_error = Json.Replace("PostError:", "");
                        }
                    }
                    else
                    {
                        res.status     = false;
                        res.http_code  = "404";
                        res.http_error = "The token is invalid";
                    }
                }
                else
                {
                    res.status     = false;
                    res.http_code  = "404";
                    res.http_error = "The token is invalid";
                }
            }
            catch (Exception ex)
            {
                res.status     = false;
                res.http_code  = "404";
                res.http_error = ex.Message;
            }
            return(res);
        }
Esempio n. 6
0
        /*Historical Data*/
        public OutputBaseClass GetCandleData(CandleRequest Request)
        {
            OutputBaseClass res = new OutputBaseClass();

            res.status    = true;
            res.http_code = "200";
            try
            {
                AngelToken Token = this.Token;
                if (Token != null)
                {
                    if (ValidateToken(Token))
                    {
                        string URL = APIURL + "/rest/secure/angelbroking/historical/v1/getCandleData";

                        string PostData = JsonConvert.SerializeObject(Request);

                        string json = POSTWebRequest(Token, URL, PostData);
                        if (!json.Contains("PostError:"))
                        {
                            CandleDataResponse cd = JsonConvert.DeserializeObject <CandleDataResponse>(json);
                            res.GetCandleDataResponse = cd;
                            res.status     = cd.status;
                            res.http_error = cd.message;
                            res.http_code  = cd.errorcode;
                        }
                        else
                        {
                            res.status     = false;
                            res.http_code  = "404";
                            res.http_error = json.Replace("PostError:", "");
                        }
                    }
                    else
                    {
                        res.status     = false;
                        res.http_code  = "404";
                        res.http_error = "The token is invalid";
                    }
                }
                else
                {
                    res.status     = false;
                    res.http_code  = "404";
                    res.http_error = "The token is invalid";
                }
            }
            catch (Exception ex)
            {
                res.status     = false;
                res.http_code  = "404";
                res.http_error = ex.Message;
            }
            return(res);
        }
Esempio n. 7
0
        public OutputBaseClass ruleDetails(string RuleID)
        {
            OutputBaseClass res = new OutputBaseClass();

            res.status    = true;
            res.http_code = "200";
            try
            {
                AngelToken Token = this.Token;
                if (Token != null)
                {
                    if (ValidateToken(Token))
                    {
                        string URL = APIURL + "/rest/secure/angelbroking/gtt/v1/ruleDetails";

                        string PostData = "{\"id\":\"" + RuleID + "\"}";

                        string json = POSTWebRequest(Token, URL, PostData);
                        if (!json.Contains("PostError:"))
                        {
                            RuleDetailsResponse rr = JsonConvert.DeserializeObject <RuleDetailsResponse>(json);
                            res.RuleDetailsResponse = rr;
                            res.status     = rr.status;
                            res.http_error = rr.message;
                            res.http_code  = rr.errorcode;
                        }
                        else
                        {
                            res.status     = false;
                            res.http_code  = "404";
                            res.http_error = json.Replace("PostError:", "");
                        }
                    }
                    else
                    {
                        res.status     = false;
                        res.http_code  = "404";
                        res.http_error = "The token is invalid";
                    }
                }
                else
                {
                    res.status     = false;
                    res.http_code  = "404";
                    res.http_error = "The token is invalid";
                }
            }
            catch (Exception ex)
            {
                res.status     = false;
                res.http_code  = "404";
                res.http_error = ex.Message;
            }
            return(res);
        }
Esempio n. 8
0
        public OutputBaseClass getTradeBook()
        {
            OutputBaseClass res = new OutputBaseClass();

            res.status    = true;
            res.http_code = "200";
            try
            {
                AngelToken Token = this.Token;
                if (Token != null)
                {
                    if (ValidateToken(Token))
                    {
                        string URL = APIURL + "/rest/secure/angelbroking/order/v1/getTradeBook";


                        string Json = GETWebRequest(Token, URL);
                        if (!Json.Contains("GetError:"))
                        {
                            GetTradeBookResponse tres = JsonConvert.DeserializeObject <GetTradeBookResponse>(Json);
                            res.GetTradeBookResponse = tres;
                            res.status     = tres.status;
                            res.http_error = tres.message;
                            res.http_code  = tres.errorcode;
                        }
                        else
                        {
                            res.status     = false;
                            res.http_code  = "404";
                            res.http_error = Json.Replace("GetError:", "");
                        }
                    }
                    else
                    {
                        res.status     = false;
                        res.http_code  = "404";
                        res.http_error = "The token is invalid";
                    }
                }
                else
                {
                    res.status     = false;
                    res.http_code  = "404";
                    res.http_error = "The token is invalid";
                }
            }
            catch (Exception ex)
            {
                res.status     = false;
                res.http_code  = "404";
                res.http_error = ex.Message;
            }
            return(res);
        }
Esempio n. 9
0
        /* Makes a POST request */
        private string POSTWebRequest(AngelToken agr, string URL, string Data)
        {
            try
            {
                //ServicePointManager.SecurityProtocol = (SecurityProtocolType)48 | (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;
                HttpWebRequest httpWebRequest = null;
                httpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
                if (agr != null)
                {
                    httpWebRequest.Headers.Add("Authorization", "Bearer " + agr.jwtToken);
                }
                httpWebRequest.Headers.Add("X-Content-Type-Options", "nosniff");
                httpWebRequest.Headers.Add("X-UserType", USER);
                httpWebRequest.Headers.Add("X-SourceID", SourceID);
                httpWebRequest.Headers.Add("X-ClientLocalIP", ClientLocalIP);
                httpWebRequest.Headers.Add("X-ClientPublicIP", ClientPublicIP);
                httpWebRequest.Headers.Add("X-MACAddress", MACAddress);
                httpWebRequest.Headers.Add("X-PrivateKey", PrivateKey);
                httpWebRequest.Method      = "POST";
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Accept      = "application/json";

                byte[] byteArray = Encoding.UTF8.GetBytes(Data);
                httpWebRequest.ContentLength = byteArray.Length;
                string Json = "";

                Stream dataStream = httpWebRequest.GetRequestStream();
                // Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.
                dataStream.Close();

                WebResponse response = httpWebRequest.GetResponse();
                // Display the status.
                //Console.WriteLine(((HttpWebResponse)response).StatusDescription);

                // Get the stream containing content returned by the server.
                // The using block ensures the stream is automatically closed.
                using (dataStream = response.GetResponseStream())
                {
                    // Open the stream using a StreamReader for easy access.
                    StreamReader reader = new StreamReader(dataStream);
                    // Read the content.
                    Json = reader.ReadToEnd();
                }
                return(Json);
            }
            catch (Exception ex)
            {
                return("PostError:" + ex.Message);
            }
        }
Esempio n. 10
0
        /* Validate Token data internally */
        private bool ValidateToken(AngelToken token)
        {
            bool result = false;

            if (token != null)
            {
                if (token.jwtToken != "" && token.refreshToken != "")
                {
                    result = true;
                }
            }
            else
            {
                result = false;
            }

            return(result);
        }
Esempio n. 11
0
        /*User Calls*/
        public OutputBaseClass GenerateSession(string clientcode, string password)
        {
            OutputBaseClass res = new OutputBaseClass();

            res.status    = true;
            res.http_code = "200";
            try
            {
                AngelTokenResponse agr = new AngelTokenResponse();

                string URL = APIURL + "/rest/auth/angelbroking/user/v1/loginByPassword";

                string PostData = "{\"clientcode\":\"" + clientcode + "\",\"password\":\"" + password + "\"}";

                string json = POSTWebRequest(null, URL, PostData);
                if (!json.Contains("PostError:"))
                {
                    agr = JsonConvert.DeserializeObject <AngelTokenResponse>(json);
                    res.TokenResponse = agr.data;
                    res.status        = agr.status;
                    res.http_error    = agr.message;
                    res.http_code     = agr.errorcode;
                    this.Token        = agr.data;
                }
                else
                {
                    res.status     = false;
                    res.http_code  = "404";
                    res.http_error = json.Replace("PostError:", "");
                }
            }
            catch (Exception ex)
            {
                res.status     = false;
                res.http_code  = "404";
                res.http_error = ex.Message;
            }
            return(res);
        }
Esempio n. 12
0
        public SmartApi(string _PrivateKey, string _jwtToken = "", string _refreshToken = "")
        {
            PrivateKey = _PrivateKey;

            this.Token              = new AngelToken();
            this.Token.jwtToken     = _jwtToken;
            this.Token.refreshToken = _refreshToken;
            this.Token.feedToken    = "";

            ClientPublicIP = Helpers.GetPublicIPAddress();
            if (ClientPublicIP == "")
            {
                ClientPublicIP = Helpers.GetPublicIPAddress();
            }

            if (ClientPublicIP == "")
            {
                ClientPublicIP = "106.193.147.98";
            }

            ClientLocalIP = Helpers.GetLocalIPAddress();

            if (ClientLocalIP == "")
            {
                ClientLocalIP = "127.0.0.1";
            }

            if (Helpers.GetMacAddress() != null)
            {
                MACAddress = Helpers.GetMacAddress().ToString();
            }
            else
            {
                MACAddress = "fe80::216e:6507:4b90:3719";
            }
        }
Esempio n. 13
0
        public OutputBaseClass modifyOrder(OrderInfo order)
        {
            OutputBaseClass res = new OutputBaseClass();

            res.status    = true;
            res.http_code = "200";
            try
            {
                AngelToken Token = this.Token;
                if (Token != null)
                {
                    if (ValidateToken(Token))
                    {
                        string URL = APIURL + "/rest/secure/angelbroking/order/v1/modifyOrder";

                        if (order.triggerprice == null || order.triggerprice == "")
                        {
                            order.triggerprice = "0";
                        }
                        if (order.squareoff == null || order.squareoff == "")
                        {
                            order.squareoff = "0";
                        }
                        if (order.stoploss == null || order.stoploss == "")
                        {
                            order.stoploss = "0";
                        }
                        if (order.trailingStopLoss == null || order.trailingStopLoss == "")
                        {
                            order.trailingStopLoss = "0";
                        }
                        if (order.disclosedquantity == null || order.disclosedquantity == "")
                        {
                            order.disclosedquantity = "0";
                        }
                        if (order.ordertag == null)
                        {
                            order.ordertag = "";
                        }

                        string PostData = JsonConvert.SerializeObject(order);

                        string Json = POSTWebRequest(Token, URL, PostData);
                        if (!Json.Contains("PostError:"))
                        {
                            OrderResponse pres = JsonConvert.DeserializeObject <OrderResponse>(Json);
                            res.ModifyOrderResponse = pres;
                            res.status     = pres.status;
                            res.http_error = pres.message;
                            res.http_code  = pres.errorcode;
                        }
                        else
                        {
                            res.status     = false;
                            res.http_code  = "404";
                            res.http_error = Json.Replace("PostError:", "");
                        }
                    }
                    else
                    {
                        res.status     = false;
                        res.http_code  = "404";
                        res.http_error = "The token is invalid";
                    }
                }
                else
                {
                    res.status     = false;
                    res.http_code  = "404";
                    res.http_error = "The token is invalid";
                }
            }
            catch (Exception ex)
            {
                res.status     = false;
                res.http_code  = "404";
                res.http_error = ex.Message;
            }
            return(res);
        }