public void ReadAllLinesToContainer(Stream streamFile, ref IContainerSingleAssetOption singleAssetOptionContainer, IInterestRateTable interestRateTable, int skipRows = 1) { _buffer = new byte[MaxBuffer]; _currentLine = new StringBuilder(); var currentLine = 0; using (var fs = streamFile) { using (var bs = new BufferedStream(fs)) { var maturityCounter = 0; var strikeCounter = 0; var memoryStream = new MemoryStream(_buffer); var stream = new StreamReader(memoryStream); while (bs.Read(_buffer, 0, MaxBuffer) != 0) { memoryStream.Seek(0, SeekOrigin.Begin); while (!stream.EndOfStream) { var line = ReadLineWithAccumulation(stream, _currentLine); if (skipRows > currentLine) { currentLine++; continue; } if (maturityCounter >= 19 && strikeCounter >= 89) { break; } if (line != null) { _optionDataLoader.LoadOptionDataFromCsvFileFast(line, ref singleAssetOptionContainer, interestRateTable); } } } } } }
public void Calculate(ref IContainerSingleAssetOption options) { var valueDateCount = options.StockPrice.Length; for (var valueDateIndex = 0; valueDateIndex < valueDateCount; valueDateIndex++) { for (var maturityDateIndex = 0; maturityDateIndex < options.MaxMaturityIndex + 1; maturityDateIndex++) { var strikeCounter = 0; var premiumDifference = double.PositiveInfinity; var dividendDiscounter = 1.0; for (var strikeIndex = 0; strikeIndex < options.MaxStrikeIndex + 1; strikeIndex++) { if (options.CallOptionBool[valueDateIndex, maturityDateIndex, strikeIndex] && options.PutOptionBool[valueDateIndex, maturityDateIndex, strikeIndex]) { var callOptionPrice = options.CallOptionPremium[valueDateIndex, maturityDateIndex, strikeIndex]; var putOptionPrice = options.PutOptionPremium[valueDateIndex, maturityDateIndex, strikeIndex]; var discountRate = options.RateDiscountFactor[valueDateIndex, maturityDateIndex]; var strike = options.IndexToStrikeDictionary[strikeIndex]; var stockPrice = options.StockPrice[valueDateIndex]; if (callOptionPrice > 0d && putOptionPrice > 0d && Math.Abs(callOptionPrice - putOptionPrice) < premiumDifference) { dividendDiscounter = (callOptionPrice - putOptionPrice + strike * discountRate) / stockPrice; strikeCounter++; premiumDifference = Math.Abs(callOptionPrice - putOptionPrice); } } if (strikeCounter > 0) { options.DividendDiscountFactor[valueDateIndex, maturityDateIndex] = dividendDiscounter; } } } } }
public void ReadAllLinesToContainerStreamReader(string fileName, ref IContainerSingleAssetOption singleAssetOptionContainer, IInterestRateTable interestRateTable, int skipRows = 1) { using (var r = new StreamReader(fileName)) { var line = r.ReadLine(); while (!r.EndOfStream) { line = r.ReadLine(); this._optionDataLoader.LoadOptionDataFromCsvFileFast(line, ref singleAssetOptionContainer, interestRateTable); } r.Dispose(); } }
public void LoadOptionDataFromCsvFileFast(string inputLine, ref IContainerSingleAssetOption singleAssetOptionContainer, IInterestRateTable interestRateTable) { var skipOption = false; try { var inputData = inputLine.Split(','); string[] formats = { "MM/dd/yyyy" }; var maturity = DateTime.ParseExact(inputData[7], formats, new CultureInfo("US-us"), DateTimeStyles.AdjustToUniversal); var strike = double.Parse(inputData[8], new CultureInfo("US-us")); var optionType = (eOptionType)Enum.Parse(typeof(eOptionType), inputData[9]); var stockPrice = double.Parse(inputData[16], new CultureInfo("US-us")); var premium = (double.Parse(inputData[11], new CultureInfo("US-us")) + double.Parse(inputData[12], new CultureInfo("US-us"))) / 2.0; var valuationDate = DateTime.ParseExact(inputData[4], "yyyy-MM-dd", new CultureInfo("US-us"), DateTimeStyles.AdjustToUniversal); var valDateIndex = singleAssetOptionContainer.GetValuationDateIndex(valuationDate); var strikeIndex = 0; if (singleAssetOptionContainer.StrikeToIndexDictionary.ContainsKey(strike)) { strikeIndex = singleAssetOptionContainer.StrikeToIndexDictionary[strike]; } else { if (singleAssetOptionContainer.MaxStrikeIndex >= 89) { skipOption = true; } else { singleAssetOptionContainer.MaxStrikeIndex++; strikeIndex = singleAssetOptionContainer.MaxStrikeIndex; singleAssetOptionContainer.StrikeToIndexDictionary.Add(strike, strikeIndex); singleAssetOptionContainer.IndexToStrikeDictionary.Add(strikeIndex, strike); } } var maturityIndex = 0; if (!skipOption) { if (singleAssetOptionContainer.MaturityToIndexDictionary.ContainsKey(maturity)) { maturityIndex = singleAssetOptionContainer.MaturityToIndexDictionary[maturity]; } else { if (singleAssetOptionContainer.MaxMaturityIndex >= 14) { skipOption = true; } else { singleAssetOptionContainer.MaxMaturityIndex++; maturityIndex = singleAssetOptionContainer.MaxMaturityIndex; singleAssetOptionContainer.MaturityToIndexDictionary.Add(maturity, maturityIndex); singleAssetOptionContainer.IndexToMaturityDictionary.Add(maturityIndex, maturity); } } } if (!skipOption) { singleAssetOptionContainer.StockPrice[valDateIndex] = stockPrice; singleAssetOptionContainer.RateDiscountFactor[valDateIndex, maturityIndex] = interestRateTable.GetDiscountFactor(valuationDate, maturity); switch (optionType) { case eOptionType.C: singleAssetOptionContainer.CallOptionBool[valDateIndex, maturityIndex, strikeIndex] = true; singleAssetOptionContainer.CallOptionPremium[valDateIndex, maturityIndex, strikeIndex] = premium; break; case eOptionType.P: singleAssetOptionContainer.PutOptionBool[valDateIndex, maturityIndex, strikeIndex] = true; singleAssetOptionContainer.PutOptionPremium[valDateIndex, maturityIndex, strikeIndex] = premium; break; } } } catch (Exception) { } }