public static OptionChain GetOpcOptionChain(Security security) { int tries = 5; int currentTry = 1; while (currentTry < tries) { var client = new RestClient("https://www.optionsprofitcalculator.com/ajax/getOptions"); var request = new RestRequest(Method.GET); request.AddParameter("stock", security.Symbol); request.AddParameter("reqId", 1); IRestResponse response = client.Execute(request); try { String contentMassaged = response.Content.Replace(",\"_data_source\":\"c0\"", String.Empty); OpcGetOptionChainResponse opcGetOptionChainResponse = JsonConvert.DeserializeObject <OpcGetOptionChainResponse>(contentMassaged); OptionChain optionChain = new OptionChain(opcGetOptionChainResponse); return(optionChain); } catch (Exception ex) { currentTry += 1; } } return(null); }
public OptionChain(OpcGetOptionChainResponse opcGetOptionChainResponse) : this() { this.Date = DateTime.Now; if (opcGetOptionChainResponse.Dates != null && opcGetOptionChainResponse.Dates.Count > 0) { int dateIndex = (this.Date.DayOfWeek == DayOfWeek.Friday || this.Date.DayOfWeek == DayOfWeek.Friday) ? 1 : 0; KeyValuePair <String, OpcDate> opcDatePair = opcGetOptionChainResponse.Dates.ElementAt(dateIndex); OptionDate optionDate = new OptionDate(); optionDate.ExpiryDate = DateTime.Parse(opcDatePair.Key); foreach (KeyValuePair <String, OpcOptionPriceSpread> callPair in opcDatePair.Value.Calls) { OptionStrike optionStrike = new OptionStrike(); optionStrike.StrikePrice = Decimal.Parse(callPair.Key); Call call = new Call(); call.Bid = callPair.Value.Bid; call.Ask = callPair.Value.Ask; optionStrike.Call = call; OpcOptionPriceSpread putPair = opcDatePair.Value.Puts[callPair.Key]; Put put = new Put(); put.Bid = putPair.Bid; put.Ask = putPair.Ask; optionStrike.Put = put; optionDate.Strikes.Add(optionStrike); } this.Dates.Add(optionDate); } }