diff --git a/BytLabs.BackendPackages.sln b/BytLabs.BackendPackages.sln index e8ea4d2..8e4772f 100644 --- a/BytLabs.BackendPackages.sln +++ b/BytLabs.BackendPackages.sln @@ -66,6 +66,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BytLabs.States.Domain", "sr EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BytLabs.States.Domain.Tests", "src\BytLabs.States\BytLabs.States.Domain.Tests\BytLabs.States.Domain.Tests.csproj", "{DA2EBC02-539C-428D-9B35-5FF250A81ADE}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "BytLabs.Trading", "BytLabs.Trading", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BytLabs.FivePaisa", "src\BytLabs.Trading\BytLabs.FivePaisa\BytLabs.FivePaisa.csproj", "{A2EB8847-733C-C9DE-1191-2B3F8F3FD273}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -132,6 +136,10 @@ Global {DA2EBC02-539C-428D-9B35-5FF250A81ADE}.Debug|Any CPU.Build.0 = Debug|Any CPU {DA2EBC02-539C-428D-9B35-5FF250A81ADE}.Release|Any CPU.ActiveCfg = Release|Any CPU {DA2EBC02-539C-428D-9B35-5FF250A81ADE}.Release|Any CPU.Build.0 = Release|Any CPU + {A2EB8847-733C-C9DE-1191-2B3F8F3FD273}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {A2EB8847-733C-C9DE-1191-2B3F8F3FD273}.Debug|Any CPU.Build.0 = Debug|Any CPU + {A2EB8847-733C-C9DE-1191-2B3F8F3FD273}.Release|Any CPU.ActiveCfg = Release|Any CPU + {A2EB8847-733C-C9DE-1191-2B3F8F3FD273}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -160,6 +168,8 @@ Global {CEFA2EE5-D4A7-4DA1-BF46-19B353C13C92} = {89A2D4EB-9E98-43ED-87F7-3C5A0D06C3BF} {55480AF2-D287-4189-91E9-7F8455371114} = {CEFA2EE5-D4A7-4DA1-BF46-19B353C13C92} {DA2EBC02-539C-428D-9B35-5FF250A81ADE} = {CEFA2EE5-D4A7-4DA1-BF46-19B353C13C92} + {02EA681E-C7D8-13C7-8484-4AC65E1B71E8} = {89A2D4EB-9E98-43ED-87F7-3C5A0D06C3BF} + {A2EB8847-733C-C9DE-1191-2B3F8F3FD273} = {02EA681E-C7D8-13C7-8484-4AC65E1B71E8} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {04DF4232-3FEF-4056-8058-76FC3291805F} diff --git a/src/BytLabs.Trading/BytLabs.FivePaisa/5PaisaAPI.cs b/src/BytLabs.Trading/BytLabs.FivePaisa/5PaisaAPI.cs new file mode 100644 index 0000000..77594d2 --- /dev/null +++ b/src/BytLabs.Trading/BytLabs.FivePaisa/5PaisaAPI.cs @@ -0,0 +1,824 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Mail; +using System.Net; +using System.Text; +using System.Threading.Tasks; +using static System.Net.WebRequestMethods; +using System.Diagnostics; +using Microsoft.VisualBasic; +using static System.Net.Mime.MediaTypeNames; +using System.IO; +using Newtonsoft.Json.Linq; +using System.Collections; +using System.Dynamic; + +namespace BytLabs.FivePaisa +{ + public class _5PaisaAPI + { + private string _root = "https://Openapi.5paisa.com/VendorsAPI/Service1.svc/"; + private string _apiKey; + private string EncryptionKey; + private string encryptUserId; + Token Token { get; set; } + public _5PaisaAPI(string apiKey, string encryptionKey, string encryptUserId,string Root = null) + { + + _apiKey = apiKey; + EncryptionKey = encryptionKey; + this.encryptUserId = encryptUserId; + + } + /* Makes a POST request */ + private string POSTWebRequest(Token agr, string URL, string Data) + { + try + { + HttpWebRequest httpWebRequest = null; + httpWebRequest = (HttpWebRequest)WebRequest.Create(URL); + if (agr != null) + httpWebRequest.Headers.Add("Authorization", "Bearer " + agr.AccessToken); + httpWebRequest.Headers.Add("5Paisa-API-Uid", "nosniff"); + 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(); + + using (dataStream = response.GetResponseStream()) + { + + StreamReader reader = new StreamReader(dataStream); + // Read the content. + Json = reader.ReadToEnd(); + } + return Json; + } + catch (Exception ex) + { + return "PostError:" + ex.Message; + } + } + + private string GETWebRequest(Token agr, string URL) + { + 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.AccessToken); + httpWebRequest.Headers.Add("5Paisa-API-Uid", "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 = "GET"; + httpWebRequest.ContentType = "application/json"; + httpWebRequest.Accept = "application/json"; + + string Json = ""; + 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 (Stream 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 "GetError:" + ex.Message; + } + } + + #region OuthLogin + public OutputBaseClass GetOuthLogin(string RequestToken) + { + OutputBaseClass res = new OutputBaseClass(); + + res.http_code = "200"; + try + { + TokenResponse agr = new TokenResponse(); + string URL = _root + "GetAccessToken"; + var dataStringSession = JsonConvert.SerializeObject(new + { + head = new { Key = _apiKey }, + //body = new { ClientCode= ClientCode, JWTToken = Token, Key= VendorKey, AllowMap = Allowmap } + body = new { RequestToken = RequestToken, EncryKey = EncryptionKey, UserId = encryptUserId } + + }); + var json = POSTWebRequest(null, URL, dataStringSession); + agr = JsonConvert.DeserializeObject(json); + if (agr.body.Status == "0") + { + + res.TokenResponse = agr.body; + res.status = agr.body.Status; + res.http_error = agr.body.Message; + res.http_code = agr.errorcode; + this.Token = agr.body; + } + else + { + + res.status = agr.body.Status; + res.http_error = agr.body.Message; + } + } + catch (Exception ex) + { + + res.http_error = ex.Message.ToString(); + } + return res; + } + #endregion + + #region TOTPLogin + public OutputBaseClass TOTPLogin(string _EmailId,string _TOTP,string _Pin) + + { + OutputBaseClass res = new OutputBaseClass(); + + + try + { + TokenResponse agr = new TokenResponse(); + string URL = _root + "TOTPLogin"; + var dataStringSession = JsonConvert.SerializeObject(new + { + head = new { Key = _apiKey }, + body = new { Email_ID = _EmailId, TOTP = _TOTP, PIN = _Pin } + + }); + var json = POSTWebRequest(null, URL, dataStringSession); + agr = JsonConvert.DeserializeObject(json); + if (agr.body.Status == "0") + { + res.TokenResponse = agr.body; + res.status = agr.body.Status; + res.http_error = agr.body.Message; + this.Token = agr.body; + } + else + { + res.status = agr.body.Status; + res.http_error = agr.body.Message; + + } + } + catch (Exception ex) + { + //res.status = false; + + res.http_error = ex.Message; + } + return res; + } + #endregion + + #region PlaceOrder/ModifyOrder/CancelOrder + + public OutputBaseClass placeOrder(OrderInfo order) + { + OutputBaseClass res = new OutputBaseClass(); + + try + { + Token Token = this.Token; + if (Token != null) + { + if (ValidateToken(Token)) + { + string URL = _root + "V1/PlaceOrderRequest"; + var dataStringSession = JsonConvert.SerializeObject(new + { + head = new { key = _apiKey }, + body = new + { + Exchange = order.Exchange, + ExchangeType = order.ExchangeType, + ScripCode = order.ScripCode, + ScripData = order.ScripData, + Price = order.Price, + OrderType = order.OrderType, + Qty = order.Qty, + DisQty = order.DisQty, + StopLossPrice = order.StopLossPrice, + IsIntraday = order.IsIntraday, + iOrderValidity = order.iOrderValidity, + AppSource = order.AppSource, + RemoteOrderID = order.RemoteOrderID + + } + + }); + + string Json = POSTWebRequest(Token, URL, dataStringSession); + OrderResponse pres = JsonConvert.DeserializeObject(Json); + if (pres.body.Status=="0") + { + // OrderResponse pres = JsonConvert.DeserializeObject(Json); + res.PlaceOrderResponse = pres; + res.status = pres.body.Status; + res.http_error = pres.body.Message; + + } + else + { + res.status = pres.body.Status; + res.http_error = pres.body.Message; + //res.http_error = Json.Replace("PostError:", ""); + } + } + else + { + res.status ="-1"; + res.http_error = "Token not exist"; + } + } + else + { + res.status = "-1"; + res.http_error = "Token not exist"; + } + } + catch (Exception ex) + { + //res.status = false; + //res.http_code = "404"; + res.http_error = ex.Message; + } + return res; + + } + + public OutputBaseClass ModifyOrder(OrderInfo order) + { + OutputBaseClass res = new OutputBaseClass(); + + + try + { + Token Token = this.Token; + if (Token != null) + { + if (ValidateToken(Token)) + { + string URL = _root + "V1/ModifyOrderRequest"; + var dataStringSession = JsonConvert.SerializeObject(new + { + head = new { key = _apiKey }, + body = new + { + + Price = order.Price==null?0: order.Price, + Qty = order.Qty==null?0:order.Qty, + StopLossPrice = order.StopLossPrice==null?0:order.StopLossPrice, + ExchOrderID=order.ExchOrderID + } + + }); + + string Json = POSTWebRequest(Token, URL, dataStringSession); + OrderResponse pres = JsonConvert.DeserializeObject(Json); + if (pres.body.Status == "0") + { + // OrderResponse pres = JsonConvert.DeserializeObject(Json); + res.PlaceOrderResponse = pres; + res.status = pres.body.Status; + res.http_error = pres.body.Message; + + } + else + { + res.status = pres.body.Status; + res.http_error = pres.body.Message; + //res.http_error = Json.Replace("PostError:", ""); + } + } + else + { + res.status = "false"; + res.http_error = "Token not exist"; + } + } + else + { + res.status = "-1"; + res.http_error = "Token not exist"; + } + } + catch (Exception ex) + { + res.status = "-1"; + res.http_code = "404"; + res.http_error = ex.Message; + } + return res; + + } + + public OutputBaseClass CancelOrder(OrderInfo order) + { + OutputBaseClass res = new OutputBaseClass(); + + res.http_code = "200"; + try + { + Token Token = this.Token; + if (Token != null) + { + if (ValidateToken(Token)) + { + string URL = _root + "V1/CancelOrderRequest"; + var dataStringSession = JsonConvert.SerializeObject(new + { + head = new { key = _apiKey }, + body = new + { + ExchOrderID = order.ExchOrderID, + + } + + }); + + string Json = POSTWebRequest(Token, URL, dataStringSession); + OrderResponse pres = JsonConvert.DeserializeObject(Json); + if (pres.body.Status == "0") + { + // OrderResponse pres = JsonConvert.DeserializeObject(Json); + res.PlaceOrderResponse = pres; + res.status = pres.body.Status; + res.http_error = pres.body.Message; + } + else + { + res.status = pres.body.Status; + res.http_error = pres.body.Message; + //res.http_error = Json.Replace("PostError:", ""); + } + } + else + { + res.status = "-1"; + res.http_error = "Token not exist"; + } + } + else + { + res.status = "-1"; + res.http_error = "Token not exist"; + } + } + catch (Exception ex) + { + //res.status = false; + res.http_code = "404"; + res.http_error = ex.Message; + } + return res; + + } + + #endregion + + #region TradeBook + public OutputBaseClass TradeBook(OrderInfo order) + { + OutputBaseClass res = new OutputBaseClass(); + + res.http_code = "200"; + try + { + Token Token = this.Token; + if (Token != null) + { + if (ValidateToken(Token)) + { + string URL = _root + "V1/TradeBook"; + var dataStringSession = JsonConvert.SerializeObject(new + { + head = new { key = _apiKey }, + body = new + { + ClientCode = order.ClientCode, + + } + }); + + string Json = POSTWebRequest(Token, URL, dataStringSession); + TradeBookResponse pres = JsonConvert.DeserializeObject(Json); + if (pres.body.status == "0") + { + // OrderResponse pres = JsonConvert.DeserializeObject(Json); + res.TradeBook = pres; + res.status = pres.body.status; + res.http_error = pres.body.message; + + } + else + { + res.status = pres.body.status; + res.http_error = pres.body.message; + //res.http_error = Json.Replace("PostError:", ""); + } + } + else + { + res.status = "-1"; + res.http_error = "Token not exist"; + } + } + else + { + res.status = "-1"; + res.http_error = "Token not exist"; + } + } + catch (Exception ex) + { + //res.status = false; + res.http_code = "404"; + res.http_error = ex.Message; + } + return res; + + } + + #endregion + + #region TradeHistory + public OutputBaseClass TradeHistory(OrderInfo order) + { + OutputBaseClass res = new OutputBaseClass(); + + res.http_code = "200"; + try + { + Token Token = this.Token; + if (Token != null) + { + if (ValidateToken(Token)) + { + string URL = _root + "V1/TradeHistory"; + var dataStringSession = JsonConvert.SerializeObject(new + { + head = new { key = _apiKey }, + body = new + { + ClientCode = order.ClientCode, + ExchOrderIDs = order.ExchOrderList + + } + + }); + + string Json = POSTWebRequest(Token, URL, dataStringSession); + TradeHistoryResponse pres = JsonConvert.DeserializeObject(Json); + if (pres.body.Status == "0") + { + // OrderResponse pres = JsonConvert.DeserializeObject(Json); + res.TradeHistory = pres; + res.status = pres.body.Status; + res.http_error = pres.body.Message; + + } + else + { + res.status = pres.body.Status; + res.http_error = pres.body.Message; + //res.http_error = Json.Replace("PostError:", ""); + } + } + else + { + res.status = "-1"; + res.http_error = "Token not exist"; + } + } + else + { + res.status = "-1"; + res.http_error = "Token not exist"; + } + } + catch (Exception ex) + { + res.http_error = ex.Message; + } + return res; + + } + #endregion + + #region OrderBook + public OutputBaseClass OrderBook(OrderInfo order) + { + OutputBaseClass res = new OutputBaseClass(); + + res.http_code = "200"; + try + { + Token Token = this.Token; + if (Token != null) + { + if (ValidateToken(Token)) + { + string URL = _root + "V3/OrderBook"; + var dataStringSession = JsonConvert.SerializeObject(new + { + head = new { key = _apiKey }, + body = new + { + ClientCode = order.ClientCode, + } + + }); + string Json = POSTWebRequest(Token, URL, dataStringSession); + OrderBookResponse pres = JsonConvert.DeserializeObject(Json); + if (pres.body.Status == "0") + { + // OrderResponse pres = JsonConvert.DeserializeObject(Json); + res.OrderBook = pres; + res.status = pres.body.Status; + res.http_error = pres.body.Message; + + } + else + { + res.status = pres.body.Status; + res.http_error = pres.body.Message; + //res.http_error = Json.Replace("PostError:", ""); + } + } + else + { + res.status = "-1"; + res.http_error = "Token not exist"; + } + } + else + { + res.status = "-1"; + res.http_error = "Token not exist"; + } + } + catch (Exception ex) + { + //res.status = false; + // res.http_code = "404"; + res.http_error = ex.Message; + } + return res; + + } + + #endregion + + #region HistoricalData + public OutputBaseClass historical(string Exch,string ExchType,int scripcode,string Day,DateTime fromDate,DateTime ToDate) + { + + OutputBaseClass res = new OutputBaseClass(); + dynamic obj = new ExpandoObject(); + + + try + { + Token Token = this.Token; + if (Token != null) + { + if (ValidateToken(Token)) + { + //string URL = "https://openapi.5paisa.com/V2/historical/N/C/1630/1d?from=2023-02-23&end=2023-04-23"; + string URL = "https://openapi.5paisa.com/" + "V2/historical"+"/"+ Exch+"/"+"/"+ ExchType +"/"+ scripcode + "/" + Day+"?"+"from"+"="+fromDate+"&"+ "end"+"="+ ToDate; + + string Result = GETWebRequest(Token,URL); + var obj1 = JsonConvert.DeserializeObject(Result); + // obj = obj1.data.candles; + res.HistoricalData= obj1.data.candles; + } + else + { + res.status = "-1"; + res.http_error = "Token not exist"; + } + } + else + { + res.status = "-1"; + res.http_error = "Token not exist"; + } + } + catch (Exception ex) + { + //res.status = false; + // res.http_code = "404"; + res.http_error = ex.Message; + } + return res; + + } + #endregion + + #region ScripMaster + public string ScripMaster(string Segment) + { + string ScripOut =null; + try + { + + string URL = _root + "ScripMaster/segment/" + Segment; + ScripOut = GETWebRequest(Token, URL); + + } + catch (Exception ex) + { + ScripOut = ex.Message; + } + return ScripOut; + + } + + #endregion + + #region MarketFeed + public OutputBaseClass MarketFeed(OrderInfo order) + { + OutputBaseClass res = new OutputBaseClass(); + + res.http_code = "200"; + try + { + Token Token = this.Token; + if (Token != null) + { + if (ValidateToken(Token)) + { + string URL = _root + "V1/MarketFeed"; + var dataStringSession = JsonConvert.SerializeObject(new + { + head = new { key = _apiKey }, + body = new + { + MarketFeedData = order.MarketFeedData, + + LastRequestTime = order.LastRequestTime, + RefreshRate = order.RefreshRate + } + + }); ; + + string Json = POSTWebRequest(Token, URL, dataStringSession); + MarketFeedResponse pres = JsonConvert.DeserializeObject(Json); + if (pres.body.Status == 0) + { + // OrderResponse pres = JsonConvert.DeserializeObject(Json); + res.MarketFeed = pres; + res.status = Convert.ToString(pres.body.Status); + res.http_error = pres.body.Message; + + } + else + { + res.status = Convert.ToString(pres.body.Status); + res.http_error = pres.body.Message; + //res.http_error = Json.Replace("PostError:", ""); + } + } + else + { + res.status = "-1"; + res.http_error = "Token not exist"; + } + } + else + { + res.status = "-1"; + res.http_error = "Token not exist"; + } + } + catch (Exception ex) + { + //res.status = false; + // res.http_code = "404"; + res.http_error = ex.Message; + } + return res; + + } + #endregion + + #region NetPositionNetWise + public OutputBaseClass NetPositionNetWise(OrderInfo order) + { + OutputBaseClass res = new OutputBaseClass(); + + res.http_code = "200"; + try + { + Token Token = this.Token; + if (Token != null) + { + if (ValidateToken(Token)) + { + string URL = _root + "V2/NetPositionNetWise"; + var dataStringSession = JsonConvert.SerializeObject(new + { + head = new { key = _apiKey }, + body = new + { + ClientCode = order.ClientCode + } + + }); + + string Json = POSTWebRequest(Token, URL, dataStringSession); + NetPositionNetWiseRes pres = JsonConvert.DeserializeObject(Json); + if (pres.body.Status == 0) + { + // OrderResponse pres = JsonConvert.DeserializeObject(Json); + res.NetPositionNetWise = pres; + res.status = Convert.ToString(pres.body.Status); + res.http_error = pres.body.Message; + + } + else + { + res.status = Convert.ToString(pres.body.Status); + res.http_error = pres.body.Message; + //res.http_error = Json.Replace("PostError:", ""); + } + } + else + { + res.status = "-1"; + res.http_error = "Token not exist"; + } + } + else + { + res.status = "-1"; + res.http_error = "Token not exist"; + } + } + catch (Exception ex) + { + //res.status = false; + // res.http_code = "404"; + res.http_error = ex.Message; + } + return res; + + } + + #endregion + + #region ValidateToken + private bool ValidateToken(Token token) + { + bool result = false; + if (token != null) + { + if (token.AccessToken != "") + { + result = true; + } + } + else + result = false; + + return result; + } + #endregion + } +} + + + diff --git a/src/BytLabs.Trading/BytLabs.FivePaisa/5PaisaModel.cs b/src/BytLabs.Trading/BytLabs.FivePaisa/5PaisaModel.cs new file mode 100644 index 0000000..3b4db9b --- /dev/null +++ b/src/BytLabs.Trading/BytLabs.FivePaisa/5PaisaModel.cs @@ -0,0 +1,468 @@ +using Microsoft.VisualBasic; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.Serialization; +using System.Text; +using System.Threading.Tasks; + +namespace BytLabs.FivePaisa +{ + class _5PaisaModel + { + } + public class Token + { + + public string AccessToken { get; set; } + + public string RequestToken { get; set; } + public string Status { get; set; } + public string Message { get; set; } + + } + + class TokenResponse + { + public bool status { get; set; } + public string Message { get; set; } + public string errorcode { get; set; } + public Token body { get; set; } + } + public class OutputBaseClass + { + public string status { get; set; } + public string http_code { get; set; } + + public string http_error { get; set; } + public Token TokenResponse { get; set; } + + public OrderResponse PlaceOrderResponse { get; set; } + + public TradeBookResponse TradeBook { get; set; } + + public TradeHistoryResponse TradeHistory { get; set; } + + public MarketFeedResponse MarketFeed { get; set; } + + public OrderBookResponse OrderBook { get; set; } + + public NetPositionNetWiseRes NetPositionNetWise { get; set; } + + public HistoricalDataRes HistoricalWiseData { get; set; } + + public object HistoricalData { get; set; } + + } + public class HistoricalDataRes + { + public string Status { get; set; } + + public HistoricalResBody data { get; set; } + } + + public class HistoricalResBody + { + + public ICollection candles { get; set; } + } + public class CandleWiseData + { + public DateTime Datetime { get; set; } + public decimal Open { get; set; } + public decimal High { get; set; } + public decimal Low { get; set; } + public decimal Close { get; set; } + public Int64 Volume { get; set; } + + } + public class NetPositionNetWiseRes + { + public NetPositionNetwiseResBody body { get; set; } + + public Reshead head { get; set; } + } + public class NetPositionNetwiseResBody + { + public int Status { get; set; } + public string Message { get; set; } + public List NetPositionDetail { get; set; } + } + public class NetPositionNetWiseDetails + { + public char Exch { get; set; } + public char ExchType { get; set; } + public int ScripCode { get; set; } + public string ScripName { get; set; } + public int BuyQty { get; set; } + public double BuyAvgRate { get; set; } + public double BuyValue { get; set; } + public int SellQty { get; set; } + public double SellAvgRate { get; set; } + public double SellValue { get; set; } + public int NetQty { get; set; } + public double BookedPL { get; set; } + public double LTP { get; set; } + public char OrderFor { get; set; } + public int BodQty { get; set; } + public double PreviousClose { get; set; } + public double MTOM { get; set; } + public double Multiplier { get; set; } + public double AvgRate { get; set; } + public double CFQty { get; set; } + public double AvgCFQty { get; set; } + public double LotSize { get; set; } + public int ConvertedQty { get; set; } + } + public class MarketFeedResponse + { + + public MarketfeedMain body { get; set; } + + public Reshead head { get; set; } + } + public class MarketfeedMain + { + public int Status { get; set; } + public string Message { get; set; } + public int CacheTime { get; set; } + public DateTime TimeStamp { get; set; } + + public List Data { get; set; } + } + public class MarketFeedDataListRes + { + public char Exch { get; set; } + public char ExchType { get; set; } + public uint Token { get; set; } + public double LastRate { get; set; } + public uint TotalQty { get; set; } + public double High { get; set; } + public double Low { get; set; } + public double PClose { get; set; } + public DateTime TickDt { get; set; } + + public int Time { get; set; } + public float ChgPcnt { get; set; } + public float Chg { get; set; } + + public string Symbol { get; set; } + } + public class OrderBookResponse + { + public OrderBookMain body { get; set; } + + public Reshead head { get; set; } + } + public class OrderBookMain + { + public string Status { get; set; } + public string Message { get; set; } + public List OrderBookDetail { get; set; } + + } + + + public class OrderBookV3 + { + public char RequestType { get; set; } + public int BrokerOrderId { get; set; } + public DateTime BrokerOrderTime { get; set; } + public char Exch { get; set; } + public char ExchType { get; set; } + public int ScripCode { get; set; } + public string ScripName { get; set; } + public char BuySell { get; set; } + public int Qty { get; set; } + public double Rate { get; set; } + public char AtMarket { get; set; } + public char WithSL { get; set; } + public double SLTriggerRate { get; set; } + public char SLTriggered { get; set; } + public string ExchOrderID { get; set; } + public DateTime ExchOrderTime { get; set; } + public string OrderStatus { get; set; } + public char AfterHours { get; set; } + public char AHProcess { get; set; } + public int OldorderQty { get; set; } + public int TradedQty { get; set; } + public int PendingQty { get; set; } + public string OrderRequesterCode { get; set; } + public int DisClosedQty { get; set; } + public int TerminalId { get; set; } + public char DelvIntra { get; set; } + public int OrderValidity { get; set; } + public string OrderValidUpto { get; set; } + public string Reason { get; set; } + public double SMOSLLimitRate { get; set; } + public double SMOSLTriggerRate { get; set; } + public double SMOProfitRate { get; set; } + public double SMOTrailingSL { get; set; } + public double MarketLot { get; set; } + public string RemoteOrderID { get; set; } + public double AveragePrice { get; set; } + } + public class OrderData + { + public Int32 BrokerOrderID { get; set; } + public string ClientCode { get; set; } + public char Exch { get; set; } + public char ExchType { get; set; } + public string ExchOrderID { get; set; } + public Int16 LocalOrderID { get; set; } + public string RemoteOrderID { get; set; } + public int RMSResponseCode { get; set; } + public string Message { get; set; } + public Int32 ScripCode { get; set; } + public string Status { get; set; } + public DateTime Time { get; set; } + + } + public class Reshead + + { + public string responseCode { get; set; } + public string status { get; set; } + + public string statusDescription { get; set; } + } + + public class TradebookData + { + public string status { get; set; } + public string message { get; set; } + public List TradeBookDetail { get; set; } + } + public class TradeBookDetailV1 + { + public char Exch; + public char ExchType; + public int ScripCode; + public string ScripName; + public char BuySell; + public int Qty; + public int PendingQty; + public int OrgQty; + public double Rate; + public string ExchOrderID; + public string ExchangeTradeID; + public DateTime ExchangeTradeTime; + public string TradeType; + public char DelvIntra; + public double Multiplier; + } + public class ScripMasterReposnse + { + public char Exch { get; set; } + public char ExchType { get; set; } + public int ScripCode { get; set; } + + public string Name { get; set; } + public string Expiry { get; set; } + public string ScripType { get; set; } + + public double StrikeRate { get; set; } + public string FullName { get; set; } + + public int LotSize { get; set; } + + public int QtyLimit { get; set; } + + public int ISIN { get; set; } + public string ScripData { get; set; } + public string Series { get; set; } + + public int COBOAllowed { get; set; } + + + + public int Multiplier { get; set; } + } + public class TradeBookResponse + { + public TradebookData body { get; set; } + + public Reshead head { get; set; } + + } + public class TradeHistoryResponse + { + public TradeHistoryDataResponse body { get; set; } + + public Reshead head { get; set; } + } + + public class TradeHistoryDataResponse + { + + public string Status { get; set; } + public string Message { get; set; } + + public List TradeHistoryData { get; set; } + } + public class TradeHistoryDataList + { + + public TradeHistoryDataList() + { + Exchange = ' '; + ExchangeType = ' '; + ScripCode = 0; + Symbol = ""; + Product = ""; + TransactionType = ' '; + TradedQty = 0; + ExchOrderID = ""; + RemoteOrderID = ""; + ExchangeTime = DateTime.Now; + TradePrice = 0d; + TradeID = 0; + OrderType = ""; + Status = 0; + Message = ""; + } + + public char Exchange; + public char ExchangeType; + public int ScripCode; + public string Symbol; + public string Product; + public char TransactionType; + public int TradedQty; + public string ExchOrderID; + public string RemoteOrderID; + public DateTime ExchangeTime; + public double TradePrice; + public int TradeID; + public string OrderType; + public int Status; + public string Message; + } + public class OrderResponse + { + public bool status { get; set; } + public string message { get; set; } + public string errorcode { get; set; } + public OrderData body { get; set; } + + public Reshead head { get; set; } + } + public class OutputBaseTOTPClass + { + public string status { get; set; } + public string http_code { get; set; } + public string http_error { get; set; } + + public string ClientCode { get; set; } + public string Message { get; set; } + public string RedirectURL { get; set; } + public string RequestToken { get; set; } + + public string Status { get; set; } + + public string Userkey { get; set; } + } + + /* Input Classes*/ + public class OrderInfo + { + + public string ClientCode { get; set; } + + public char Exchange { get; set; } + + public char ExchangeType { get; set; } + + public string ScripData { get; set; } + + public double Price { get; set; } + + public string OrderType { get; set; } + + public long Qty { get; set; } + + public long ScripCode { get; set; } + + public long DisQty { get; set; } + + public double StopLossPrice { get; set; } + + public bool IsIntraday { get; set; } + + public OrderValidity iOrderValidity { get; set; } + + public int AppSource { get; set; } + + public string RemoteOrderID { get; set; } + + public char AHPlaced { get; set; } + + public string PublicIP { get; set; } + + public DateTime ValidTillDate { get; set; } + + public string ExchOrderID { get; set; } + + public List ExchOrderList { get; set; } + + public DateTime LastRequestTime { get; set; } + + public string RefreshRate { get; set; } + + public List MarketFeedData { get; set; } + + + } + public class WebsocketConnect + { + + public string Method { get; set; } + public string Operation { get; set; } + public string ClientCode { get; set; } + public List WebsokectMarketFeedData { get; set; } + + } + + public enum OrderValidity : short + { + Day = 0, + GTD = 1, + GTC = 2, + IOC = 3, + EOS = 4, + VTD = 5, + FOK = 6 + } + public partial class WebsocketMarketFeedDataListReq + { + + public string Exch { get; set; } + + public string ExchType { get; set; } + + public int ScripCode { get; set; } + + + + } + public partial class MarketFeedDataListReq + { + + public string Exch { get; set; } + + public string ExchType { get; set; } + + public int ScripCode { get; set; } + + public string ScripData { get; set; } + + + } + public class ExchOrderIDList + { + public string ExchOrderID { get; set; } + } + + +} + diff --git a/src/BytLabs.Trading/BytLabs.FivePaisa/APIRequest.cs b/src/BytLabs.Trading/BytLabs.FivePaisa/APIRequest.cs new file mode 100644 index 0000000..99f9252 --- /dev/null +++ b/src/BytLabs.Trading/BytLabs.FivePaisa/APIRequest.cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Reflection.PortableExecutable; +using System.Security.AccessControl; +using System.Text; +using System.Threading.Tasks; +using static _5PaisaLibrary.Enum; + +namespace _5PaisaLibrary +{ + public class APIRequest + { + public string SynchronizeApiConsume(string url, APIType ObjApiType, APIHeaders ObjApiHeader, string ParamString = "", string AuthorizationKey = "", string CustomHeader1 = "", string CustomHeader2 = "", bool ShouldUseLoginCookie = false, bool VendorCall = false, string CustomHeader3 = "", bool ShouldUseJWTToken = false) + { + string Result = null; + try + { + if (ObjApiType == APIType.GET) + { + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); + request.Method = "GET"; + Stream objstream; + objstream = request.GetResponse().GetResponseStream(); + StreamReader reader = new StreamReader(objstream); + Result = reader.ReadToEnd(); + } + else if (ObjApiType == APIType.POST) + { + CookieContainer container = new CookieContainer(); + HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); + request.Method = "POST"; + request.ContentType = "application/json"; + //Headers Condition + if (ObjApiHeader == APIHeaders.Authorization && AuthorizationKey != "") + { + request.Headers.Add("Authorization", AuthorizationKey); + } + else if (ObjApiHeader == APIHeaders.Custom && CustomHeader1 != "" && CustomHeader2 != "" && CustomHeader3 != "" && CustomHeader1.Contains(',') && CustomHeader2.Contains(',') && CustomHeader3.Contains(',')) + { + request.Headers.Add(CustomHeader1.Split(',')[0], CustomHeader1.Split(',')[1]); + request.Headers.Add(CustomHeader2.Split(',')[0], CustomHeader2.Split(',')[1]); + request.Headers.Add(CustomHeader3.Split(',')[0], CustomHeader3.Split(',')[1]); + } + else if (ObjApiHeader == APIHeaders.Custom && CustomHeader1 != "" && CustomHeader2 != "" && CustomHeader1.Contains(',') && CustomHeader2.Contains(',')) + { + request.Headers.Add(CustomHeader1.Split(',')[0], CustomHeader1.Split(',')[1]); + request.Headers.Add(CustomHeader2.Split(',')[0], CustomHeader2.Split(',')[1]); + } + + request.CookieContainer = container; + //if (ShouldUseLoginCookie) + //{ + // CookieContainer requestCookieContainer = new CookieContainer(); + // if (HttpContext.Current == null) + // { + // var CookieData = (CookieCollection)(HttpRuntime.Cache["ClientSwarajCookie"]); + // if (Convert.ToString(HttpContext.Current.Session["RegeneratedJwtToken"]) != "") + // CookieData[1].Value = Convert.ToString(HttpContext.Current.Session["RegeneratedJwtToken"]); + + // var CookieDataName = (ShouldUseJWTToken ? CookieData[1].Name : CookieData[0].Name); + // var CookieDataVal = (ShouldUseJWTToken ? CookieData[1].Value : CookieData[0].Value); + // Cookie requiredCookie = new Cookie(CookieDataName, CookieDataVal); + // requiredCookie.Domain = (ShouldUseJWTToken ? CookieData[1].Domain : CookieData[0].Domain); + // requestCookieContainer.Add(requiredCookie); + // request.CookieContainer = requestCookieContainer; + // } + // else if (HttpContext.Current.Session["ClientSwarajCookie"] != null) + // { + // var CookieData = (CookieCollection)(HttpContext.Current.Session["ClientSwarajCookie"]); + + // if (Convert.ToString(HttpContext.Current.Session["RegeneratedJwtToken"]) != "") + // CookieData[1].Value = Convert.ToString(HttpContext.Current.Session["RegeneratedJwtToken"]); + + // var CookieDataName = (ShouldUseJWTToken ? CookieData[1].Name : CookieData[0].Name); + // var CookieDataVal = (ShouldUseJWTToken ? CookieData[1].Value : CookieData[0].Value); + // Cookie requiredCookie = new Cookie(CookieDataName, CookieDataVal); + // requiredCookie.Domain = (ShouldUseJWTToken ? TTAPItokenDomain : CookieData[0].Domain); + // requestCookieContainer.Add(requiredCookie); + // request.CookieContainer = requestCookieContainer; + // if (VendorCall == true) + // { + // request.Headers.Add("Cookie", Convert.ToString(CookieData[0])); + // } + // } + //} + request.PreAuthenticate = true; + byte[] data = Encoding.UTF8.GetBytes(ParamString); + Stream dataStream = request.GetRequestStream(); + dataStream.Write(data, 0, data.Length); + dataStream.Close(); + // System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11; + HttpWebResponse response = (HttpWebResponse)request.GetResponse(); + + + Result = new StreamReader(response.GetResponseStream()).ReadToEnd(); + //if (CustomHeader3 == "" && response.Cookies != null && response.Cookies.Count > 0) + // LoginCookie(response.Cookies); + + } + } + catch (Exception ex) + { + throw ex; + } + return Result; + + } + + } +} diff --git a/src/BytLabs.Trading/BytLabs.FivePaisa/BytLabs.FivePaisa.csproj b/src/BytLabs.Trading/BytLabs.FivePaisa/BytLabs.FivePaisa.csproj new file mode 100644 index 0000000..93a9f6f --- /dev/null +++ b/src/BytLabs.Trading/BytLabs.FivePaisa/BytLabs.FivePaisa.csproj @@ -0,0 +1,34 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + + + True + True + Resources.resx + + + + + + ResXFileCodeGenerator + Resources.Designer.cs + + + + diff --git a/src/BytLabs.Trading/BytLabs.FivePaisa/Class1.cs b/src/BytLabs.Trading/BytLabs.FivePaisa/Class1.cs new file mode 100644 index 0000000..b85461e --- /dev/null +++ b/src/BytLabs.Trading/BytLabs.FivePaisa/Class1.cs @@ -0,0 +1,7 @@ +namespace BytLabs.FivePaisa +{ + public class Class1 + { + + } +} \ No newline at end of file diff --git a/src/BytLabs.Trading/BytLabs.FivePaisa/Enum.cs b/src/BytLabs.Trading/BytLabs.FivePaisa/Enum.cs new file mode 100644 index 0000000..fb80989 --- /dev/null +++ b/src/BytLabs.Trading/BytLabs.FivePaisa/Enum.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BytLabs.FivePaisa +{ + public class Enum + { + public enum APIType { GET, POST } + public enum APIHeaders { Null, Authorization, Custom }; + } +} diff --git a/src/BytLabs.Trading/BytLabs.FivePaisa/IWebsocket.cs b/src/BytLabs.Trading/BytLabs.FivePaisa/IWebsocket.cs new file mode 100644 index 0000000..ec2d8c4 --- /dev/null +++ b/src/BytLabs.Trading/BytLabs.FivePaisa/IWebsocket.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BytLabs.FivePaisa +{ + internal interface IWebsocket + { + + bool IsConnected(); + void ConnectForFeed(string jwttoken, string clientcode); + void FetchMarketFeed(WebsocketConnect list); + void Send(string Message); + void Close(bool Abort = false); + + + + } +} diff --git a/src/BytLabs.Trading/BytLabs.FivePaisa/Request.cs b/src/BytLabs.Trading/BytLabs.FivePaisa/Request.cs new file mode 100644 index 0000000..fcfb420 --- /dev/null +++ b/src/BytLabs.Trading/BytLabs.FivePaisa/Request.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BytLabs.FivePaisa +{ + public class ResponseModel + { + // public object RequestData { set; get; } + public object ResponseData { set; get; } + } + public class LoginRequestMobileNewbyTOTP + { + public LoginRequestMobileNewbyOTP body { get; set; } + public Head head { get; set; } + } + public class LoginRequestMobileNewbyOTP + { + private string _Email_id { get; set; } + + private string TOTP { get; set; } + public string PIN { get; set; } + + } + public class GetAccessToken + { + public AccessToken body { get; set; } + public Head head { get; set; } + } + public class AccessToken + { + public string RequestToken { get; set; } + public string EncryKey { get; set; } + public string UserId { get; set; } + + + } + public class Head + { + + public string Key { get; set; } + + } + +} diff --git a/src/BytLabs.Trading/BytLabs.FivePaisa/Response.cs b/src/BytLabs.Trading/BytLabs.FivePaisa/Response.cs new file mode 100644 index 0000000..977d328 --- /dev/null +++ b/src/BytLabs.Trading/BytLabs.FivePaisa/Response.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BytLabs.FivePaisa +{ + internal class Response + { + } +} diff --git a/src/BytLabs.Trading/BytLabs.FivePaisa/Test.cs b/src/BytLabs.Trading/BytLabs.FivePaisa/Test.cs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/BytLabs.Trading/BytLabs.FivePaisa/Test.cs @@ -0,0 +1 @@ + diff --git a/src/BytLabs.Trading/BytLabs.FivePaisa/WebSocket.cs b/src/BytLabs.Trading/BytLabs.FivePaisa/WebSocket.cs new file mode 100644 index 0000000..c35a165 --- /dev/null +++ b/src/BytLabs.Trading/BytLabs.FivePaisa/WebSocket.cs @@ -0,0 +1,117 @@ +using Microsoft.VisualBasic.FileIO; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System.Net.WebSockets; +using Websocket.Client; + +namespace BytLabs.FivePaisa +{ + public class WebSocket : IWebsocket + + { + ManualResetEvent receivedEvent = new ManualResetEvent(false); + int receivedCount = 0; + WebsocketClient _ws; + string _url = "wss://Openfeed.5paisa.com/Feeds/api/chat?Value1="; + public event EventHandler MessageReceived; + + public WebSocket() + { + + } + public void Close(bool Abort = false) + { + if (_ws.IsRunning) + { + if (Abort) + _ws.Stop(WebSocketCloseStatus.NormalClosure, "Close"); + else + { + _ws.Dispose(); + } + } + } + + + + public void FetchMarketFeed(WebsocketConnect list) + { + var dataStringSession = JsonConvert.SerializeObject(new + { + + MarketFeedData = list.WebsokectMarketFeedData, + + Method = list.Method, + Operation = list.Operation, + ClientCode=list.ClientCode, + }) ; + + + + if (_ws.IsStarted) + { + try + { + _ws.Send(dataStringSession); + } + catch (Exception ex) + { + throw ex; + } + } + } + + public bool IsConnected() + { + if (_ws is null) + return false; + + return _ws.IsStarted; + } + public void Receive(string Message) + { + MessageEventArgs args = new MessageEventArgs(); + //args.Message = Helpers.DecodeBase64(Message); + args.Message = Message; + EventHandler handler = MessageReceived; + if (handler != null) + { + handler(this, args); + } + receivedCount++; + if (receivedCount >= 10) + receivedEvent.Set(); + } + public void Send(string Message) + { + if (_ws.IsStarted) + { + try + { + _ws.Send(Message); + } + catch (Exception ex) + { + throw ex; + } + } + } + + public void ConnectForFeed(string jwttoken, string clientcode) + { + string finalurl = _url + jwttoken +"|" + clientcode; + var url = new Uri(finalurl); + + _ws = new WebsocketClient(url); + + _ws.MessageReceived.Subscribe(msg => Receive(msg.Text)); + + _ws.Start(); + } + + public class MessageEventArgs : EventArgs + { + public string Message { get; set; } + } + } +}