private static bool FetchOCAgain(Records recordsObject) { TimeSpan timeAddGap = new TimeSpan(0, 2, 0); TimeSpan timeMktOpenTiming = new TimeSpan(9, 15, 0); TimeSpan timeMktCloseTiming = new TimeSpan(15, 30, 0); bool flag = false; DateTime timeLastFetchedTime = DateTime.ParseExact(recordsObject.timestamp, "dd-MMM-yyyy HH:mm:ss", null); //Few new OC only if current time is between market open and close time and //current time is more than 5 mins from last fetched time and //Current day is a weekday if (DateTime.Now.TimeOfDay > timeLastFetchedTime.TimeOfDay.Add(timeAddGap) && DateTime.Now.TimeOfDay > timeMktOpenTiming && DateTime.Now.TimeOfDay < timeMktCloseTiming && (!DateTime.Now.DayOfWeek.ToString().Equals(DayOfWeek.Saturday) && !DateTime.Now.DayOfWeek.ToString().Equals(DayOfWeek.Sunday))) { flag = true; } return(flag); }
public static DataTable toDataTable(Records recordsObject) { var result = new DataTable(); result = addDataTableColumns(result); foreach (var row in recordsObject.data) { var datarow = result.NewRow(); if (row.CE != null) { datarow["StrikePrice"] = row.CE.strikePrice.ToString(); datarow["ExpiryDate"] = row.CE.expiryDate.ToString(); datarow["CE" + "underlying"] = row.CE.underlying.ToString(); datarow["CE" + "identifier"] = row.CE.identifier.ToString(); datarow["CE" + "openInterest"] = row.CE.openInterest.ToString(); datarow["CE" + "changeinOpenInterest"] = row.CE.changeinOpenInterest.ToString(); datarow["CE" + "pchangeinOpenInterest"] = row.CE.pchangeinOpenInterest.ToString(); datarow["CE" + "totalTradedVolume"] = row.CE.totalTradedVolume.ToString(); datarow["CE" + "impliedVolatility"] = row.CE.impliedVolatility.ToString(); datarow["CE" + "lastPrice"] = row.CE.lastPrice.ToString(); datarow["CE" + "change"] = Math.Round(row.CE.change, 2).ToString(); datarow["CE" + "pChange"] = row.CE.pChange.ToString(); datarow["CE" + "totalBuyQuantity"] = row.CE.totalBuyQuantity.ToString(); datarow["CE" + "totalSellQuantity"] = row.CE.totalSellQuantity.ToString(); datarow["CE" + "bidQty"] = row.CE.bidQty.ToString(); datarow["CE" + "bidprice"] = row.CE.bidprice.ToString(); datarow["CE" + "askQty"] = row.CE.askQty.ToString(); datarow["CE" + "askPrice"] = row.CE.askPrice.ToString(); datarow["CE" + "underlyingValue"] = row.CE.underlyingValue.ToString(); datarow["Contract"] = enumContractType.CE.ToString(); } if (row.PE != null) { datarow["StrikePrice"] = row.PE.strikePrice.ToString(); datarow["ExpiryDate"] = row.PE.expiryDate.ToString(); datarow["PE" + "underlying"] = row.PE.underlying.ToString(); datarow["PE" + "identifier"] = row.PE.identifier.ToString(); datarow["PE" + "openInterest"] = row.PE.openInterest.ToString(); datarow["PE" + "changeinOpenInterest"] = row.PE.changeinOpenInterest.ToString(); datarow["PE" + "pchangeinOpenInterest"] = row.PE.pchangeinOpenInterest.ToString(); datarow["PE" + "totalTradedVolume"] = row.PE.totalTradedVolume.ToString(); datarow["PE" + "impliedVolatility"] = row.PE.impliedVolatility.ToString(); datarow["PE" + "lastPrice"] = row.PE.lastPrice.ToString(); datarow["PE" + "change"] = Math.Round(row.PE.change, 2).ToString(); datarow["PE" + "pChange"] = row.PE.pChange.ToString(); datarow["PE" + "totalBuyQuantity"] = row.PE.totalBuyQuantity.ToString(); datarow["PE" + "totalSellQuantity"] = row.PE.totalSellQuantity.ToString(); datarow["PE" + "bidQty"] = row.PE.bidQty.ToString(); datarow["PE" + "bidprice"] = row.PE.bidprice.ToString(); datarow["PE" + "askQty"] = row.PE.askQty.ToString(); datarow["PE" + "askPrice"] = row.PE.askPrice.ToString(); datarow["PE" + "underlyingValue"] = row.PE.underlyingValue.ToString(); datarow["Contract"] = enumContractType.PE.ToString(); } result.Rows.Add(datarow); } return(result); }
public static DataTable AddRecordsToDataTable(FilterConditions filterConditions) { DataTable dt = new DataTable(); dt.Columns.Add("Stock"); dt.Columns.Add("Identifier"); dt.Columns.Add("TradingSymbol"); dt.Columns.Add("Contract"); dt.Columns.Add("TransactionType"); dt.Columns.Add("StrikePrice"); dt.Columns.Add("LotSize"); dt.Columns.Add("Premium"); dt.Columns.Add("ExpiryDate"); Records recordsObject = GetOC(filterConditions.OcType); int iUpperStrikePriceRange = 0; int iLowerStrikePriceRange = 0; if (filterConditions.PercentageRange > 0) { iUpperStrikePriceRange = RoundTo100(recordsObject.underlyingValue + (DefaultSP(filterConditions.OcType) * filterConditions.PercentageRange / 100)); iLowerStrikePriceRange = RoundTo100(recordsObject.underlyingValue - (DefaultSP(filterConditions.OcType) * filterConditions.PercentageRange / 100)); } else { iUpperStrikePriceRange = filterConditions.SPHigherRange; iLowerStrikePriceRange = filterConditions.SPLowerRange; } //List<int> strikePrices = MySession.Current.RecordsObject.strikePrices; List <int> filteredStrikePrices = new List <int>(); foreach (var item in MySession.Current.RecordsObject.strikePrices) { if (item <= iUpperStrikePriceRange && item >= iLowerStrikePriceRange) { filteredStrikePrices.Add(item); dt.Columns.Add(item.ToString()); } } DataRow datarow; foreach (var row in recordsObject.data) { string formattedDateForTradingSymbol = TradingSymbol_DateFormatter(row.expiryDate); if (row.CE != null && (row.CE.strikePrice <= iUpperStrikePriceRange && row.CE.strikePrice >= iLowerStrikePriceRange) && (row.CE.strikePrice % 100 == 0) && row.CE.lastPrice > 0 && row.expiryDate.Equals(filterConditions.ExpiryDate) && (filterConditions.ContractType.Equals(enumContractType.CE.ToString()) || filterConditions.ContractType.Equals("ALL"))) { //Add CE Buy row datarow = dt.NewRow(); datarow["Stock"] = row.CE.underlying; datarow["Identifier"] = row.CE.identifier; datarow["Contract"] = enumContractType.CE.ToString(); datarow["TransactionType"] = enumTransactionType.BUY.ToString(); datarow["StrikePrice"] = row.CE.strikePrice.ToString(); datarow["LotSize"] = GetLotSize(filterConditions.OcType); datarow["Premium"] = row.CE.lastPrice.ToString(); datarow["ExpiryDate"] = row.CE.expiryDate; datarow["TradingSymbol"] = string.Concat(row.CE.underlying, formattedDateForTradingSymbol, row.CE.strikePrice.ToString(), enumContractType.CE.ToString()); foreach (var item in filteredStrikePrices) { datarow[dt.Columns[item.ToString()].ColumnName] = FO.CallBuy(row.CE.strikePrice, row.CE.lastPrice, Convert.ToDouble(item)); } dt.Rows.Add(datarow); //Add CE Sell row datarow = dt.NewRow(); datarow["Stock"] = row.CE.underlying; datarow["Identifier"] = row.CE.identifier; datarow["Contract"] = enumContractType.CE.ToString(); datarow["TransactionType"] = enumTransactionType.SELL.ToString(); datarow["StrikePrice"] = row.CE.strikePrice.ToString(); datarow["LotSize"] = GetLotSize(filterConditions.OcType); datarow["Premium"] = row.CE.lastPrice.ToString(); datarow["ExpiryDate"] = row.CE.expiryDate; datarow["TradingSymbol"] = string.Concat(row.CE.underlying, formattedDateForTradingSymbol, row.CE.strikePrice.ToString(), enumContractType.CE.ToString()); foreach (var item in filteredStrikePrices) { datarow[dt.Columns[item.ToString()].ColumnName] = FO.CallSell(row.CE.strikePrice, row.CE.lastPrice, Convert.ToDouble(item)); } dt.Rows.Add(datarow); } if (row.PE != null && (row.PE.strikePrice <= iUpperStrikePriceRange && row.PE.strikePrice >= iLowerStrikePriceRange) && (row.PE.strikePrice % 100 == 0) && row.PE.lastPrice > 0 && row.expiryDate.Equals(filterConditions.ExpiryDate) && (filterConditions.ContractType.Equals(enumContractType.PE.ToString()) || filterConditions.ContractType.Equals("ALL"))) { //Add PE Buy row datarow = dt.NewRow(); datarow["Stock"] = row.PE.underlying; datarow["Identifier"] = row.PE.identifier; datarow["Contract"] = enumContractType.PE.ToString(); datarow["TransactionType"] = enumTransactionType.BUY.ToString(); datarow["StrikePrice"] = row.PE.strikePrice.ToString(); datarow["LotSize"] = GetLotSize(filterConditions.OcType); datarow["Premium"] = row.PE.lastPrice.ToString(); datarow["ExpiryDate"] = row.PE.expiryDate; datarow["TradingSymbol"] = string.Concat(row.PE.underlying, formattedDateForTradingSymbol, row.PE.strikePrice.ToString(), enumContractType.PE.ToString()); foreach (var item in filteredStrikePrices) { datarow[dt.Columns[item.ToString()].ColumnName] = FO.PutBuy(row.PE.strikePrice, row.PE.lastPrice, Convert.ToDouble(item)); } dt.Rows.Add(datarow); //Add PE Sell row datarow = dt.NewRow(); datarow["Stock"] = row.PE.underlying; datarow["Identifier"] = row.PE.identifier; datarow["Contract"] = enumContractType.PE.ToString(); datarow["TransactionType"] = enumTransactionType.SELL.ToString(); datarow["StrikePrice"] = row.PE.strikePrice.ToString(); datarow["LotSize"] = GetLotSize(filterConditions.OcType); datarow["Premium"] = row.PE.lastPrice.ToString(); datarow["ExpiryDate"] = row.PE.expiryDate; datarow["TradingSymbol"] = string.Concat(row.PE.underlying, formattedDateForTradingSymbol, row.PE.strikePrice.ToString(), enumContractType.PE.ToString()); foreach (var item in filteredStrikePrices) { datarow[dt.Columns[item.ToString()].ColumnName] = FO.PutSell(row.PE.strikePrice, row.PE.lastPrice, Convert.ToDouble(item)); } dt.Rows.Add(datarow); } } return(dt); }
public static Records GetOC(string ocType) { Records recordsObject = new Records(); if (MySession.Current.RecordsObject != null) { if (MySession.Current.RecordsObject.index == null) { MySession.Current.RecordsNifty = MySession.Current.RecordsObject; } else if (MySession.Current.RecordsObject.index.indexSymbol.Equals("NIFTY BANK")) { MySession.Current.RecordsBankNifty = MySession.Current.RecordsObject; } } if (ocType.Equals(enumOptionChainType.NIFTY.ToString())) { if (MySession.Current.RecordsNifty == null) { JObject jObject = DownloadJSONDataFromURL(ocType); if (jObject != null && jObject.Count > 0) { recordsObject = JsonConvert.DeserializeObject <Records>(jObject["records"].ToString()); MySession.Current.RecordsNifty = recordsObject; } } else { recordsObject = MySession.Current.RecordsNifty; if (FetchOCAgain(recordsObject)) { JObject jObject = DownloadJSONDataFromURL(ocType); if (jObject != null) { recordsObject = JsonConvert.DeserializeObject <Records>(jObject["records"].ToString()); MySession.Current.RecordsNifty = recordsObject; } } } MySession.Current.RecordsObject = MySession.Current.RecordsNifty; } if (ocType.Equals(enumOptionChainType.BANKNIFTY.ToString())) { if (MySession.Current.RecordsBankNifty == null) { JObject jObject = DownloadJSONDataFromURL(ocType); if (jObject != null) { recordsObject = JsonConvert.DeserializeObject <Records>(jObject["records"].ToString()); MySession.Current.RecordsBankNifty = recordsObject; } } else { recordsObject = MySession.Current.RecordsBankNifty; if (FetchOCAgain(recordsObject)) { JObject jObject = DownloadJSONDataFromURL(ocType); if (jObject != null) { recordsObject = JsonConvert.DeserializeObject <Records>(jObject["records"].ToString()); MySession.Current.RecordsBankNifty = recordsObject; } } } MySession.Current.RecordsObject = MySession.Current.RecordsBankNifty; } return(recordsObject); }