Beispiel #1
0
        public IEnumerable <ISingleAssetOption> ReadCsv(string filename)
        {
            // open the file "data.csv" which is a CSV file with headers
            using (var inputData =
                       new CsvReader(new StreamReader(filename), true))
            {
                var fieldCount = inputData.FieldCount;

                string[] formats = { "MM/dd/yyyy" };

                ISingleAssetOption returnValue;

                var headers = inputData.GetFieldHeaders();
                while (inputData.ReadNextRecord())
                {
                    try
                    {
                        returnValue = new SingleAssetOption
                        {
                            Underlying = inputData[1],
                            Maturity   = DateTime.ParseExact(inputData[7], formats,
                                                             new CultureInfo("US-us"), DateTimeStyles.AdjustToUniversal),
                            Strike     = double.Parse(inputData[8], new CultureInfo("US-us")),
                            OptionType = (eOptionType)Enum.Parse(typeof(eOptionType),
                                                                 inputData[9]),
                            Nominal    = 10.0 * 100.0,
                            StockPrice = double.Parse(inputData[16], new CultureInfo("US-us")),
                            Premium    = (double.Parse(inputData[11], new CultureInfo("US-us")) +
                                          double.Parse(inputData[12], new CultureInfo("US-us"))) / 2.0,
                            ValuationDate = DateTime.ParseExact(inputData[4], "yyyy-MM-dd", new CultureInfo("US-us"),
                                                                DateTimeStyles.AdjustToUniversal)
                        };
                    }
                    catch (Exception)
                    {
                        //Trace.WriteLine(inputData[1]);
                        returnValue = new SingleAssetOption
                        {
                            ValuationDate = new DateTime(1900, 1, 1),
                            Maturity      = new DateTime(1901, 1, 1),
                            OptionType    = eOptionType.C
                        };
                    }

                    yield return(returnValue);
                }
            }
        }
        private void AddJointOption(
            Dictionary <DateTime, Dictionary <DateTime, Dictionary <double, ISingleAssetOption> > > referenceDictionary,
            ISingleAssetOption referenceOption, ref List <ISingleAssetOption> listOfJointOption)
        {
            if (!referenceDictionary.ContainsKey(referenceOption.ValuationDate) ||
                !referenceDictionary[referenceOption.ValuationDate].ContainsKey(referenceOption.Maturity) ||
                !referenceDictionary[referenceOption.ValuationDate][referenceOption.Maturity]
                .ContainsKey(referenceOption.Strike))
            {
                return;
            }

            SingleAssetOption optionDiffPremium;

            if (referenceOption.OptionType == eOptionType.C)
            {
                optionDiffPremium = new SingleAssetOption
                {
                    StockPrice             = referenceOption.StockPrice,
                    Currency               = referenceOption.Currency,
                    DiscountFactor         = referenceOption.DiscountFactor,
                    DividendDiscountFactor = referenceOption.DividendDiscountFactor,
                    Maturity               = referenceOption.Maturity,
                    Premium = referenceOption.Premium -
                              referenceDictionary[referenceOption.ValuationDate][referenceOption.Maturity][
                        referenceOption.Strike].Premium,
                    Strike = referenceOption.Strike
                }
            }
            ;
            else
            {
                optionDiffPremium = new SingleAssetOption
                {
                    StockPrice             = referenceOption.StockPrice,
                    Currency               = referenceOption.Currency,
                    DiscountFactor         = referenceOption.DiscountFactor,
                    DividendDiscountFactor = referenceOption.DividendDiscountFactor,
                    Maturity               = referenceOption.Maturity,
                    Premium = referenceDictionary[referenceOption.ValuationDate][referenceOption.Maturity][
                        referenceOption.Strike].Premium - referenceOption.Premium,
                    Strike = referenceOption.Strike
                }
            };

            listOfJointOption.Add(optionDiffPremium);
        }
        public ISingleAssetOption LoadOptionDataFromCsvFileFast(string inputLine)
        {
            var inputData = inputLine.Split(',');

            string[] formats = { "MM/dd/yyyy" };

            ISingleAssetOption returnValue;

            try
            {
                returnValue = new SingleAssetOption
                {
                    Underlying = inputData[1],
                    Maturity   = DateTime.ParseExact(inputData[7], formats,
                                                     new CultureInfo("US-us"), DateTimeStyles.AdjustToUniversal),
                    Strike     = double.Parse(inputData[8], new CultureInfo("US-us")),
                    OptionType = (eOptionType)Enum.Parse(typeof(eOptionType),
                                                         inputData[9]),
                    Nominal    = 10.0 * 100.0,
                    StockPrice = double.Parse(inputData[16], new CultureInfo("US-us")),
                    Premium    = (double.Parse(inputData[11], new CultureInfo("US-us")) +
                                  double.Parse(inputData[12], new CultureInfo("US-us"))) / 2.0,
                    ValuationDate = DateTime.ParseExact(inputData[4], "yyyy-MM-dd", new CultureInfo("US-us"),
                                                        DateTimeStyles.AdjustToUniversal)
                };
            }


            catch (Exception)
            {
                //Trace.WriteLine(inputLine);
                returnValue = new SingleAssetOption
                {
                    ValuationDate = new DateTime(1900, 1, 1),
                    Maturity      = new DateTime(1901, 1, 1),
                    OptionType    = eOptionType.C
                };
            }

            return(returnValue);
        }
        /// <summary>
        ///     Loads the option data.
        /// </summary>
        /// <param name="inputLine">The input line.</param>
        /// <param name="columnMapper">The column mapper.</param>
        /// <param name="seperator">The seperator.</param>
        /// <returns></returns>
        public ISingleAssetOption LoadOptionDataFromCsvFile(string inputLine,
                                                            Dictionary <string, int> columnMapper = null,
                                                            char seperator = ',')
        {
            if (columnMapper == null)
            {
                columnMapper = new Dictionary <string, int>
                {
                    { "Underlying", 1 },
                    { "ValuationDate", 14 },
                    { "MaturityDate", 4 },
                    { "Strike", 5 },
                    { "OptionType", 6 },
                    { "PremiumBid", 9 },
                    { "PremiumAsk", 8 },
                    { "StockPrice", 2 }
                }
            }
            ;

            var inputData = inputLine.Split(seperator);

            string[] formats = { "MM/dd/yyyy" };

            var inputSingleAssetOption = new SingleAssetOption();

            inputSingleAssetOption.ValuationDate = new DateTime(1900, 1, 1);
            inputSingleAssetOption.Maturity      = new DateTime(1901, 1, 1);
            inputSingleAssetOption.OptionType    = eOptionType.C;


            try
            {
                inputSingleAssetOption = new SingleAssetOption
                {
                    Underlying = inputData[columnMapper["Underlying"]],
                    Maturity   = DateTime.ParseExact(inputData[columnMapper["MaturityDate"]], formats,
                                                     new CultureInfo("US-us"), DateTimeStyles.AdjustToUniversal),
                    Strike = double.Parse(inputData[columnMapper["Strike"]], new CultureInfo("US-us")),
                    //Currency =
                    //    (eCurrency) Enum.Parse(typeof(eCurrency), inputLine[columnMapper["Currency"]].ToString()),
                    OptionType = (eOptionType)Enum.Parse(typeof(eOptionType),
                                                         inputData[columnMapper["OptionType"]]),
                    Nominal    = 10.0 * 100.0,
                    StockPrice = double.Parse(inputData[columnMapper["StockPrice"]], new CultureInfo("US-us"))
                };

                if (columnMapper.ContainsKey("ImpliedVolatility"))
                {
                    inputSingleAssetOption.ImpliedVolatility =
                        double.Parse(inputData[columnMapper["ImpliedVolatility"]]);
                }

                if (columnMapper.ContainsKey("PremiumAsk") && columnMapper.ContainsKey("PremiumBid"))
                {
                    inputSingleAssetOption.Premium =
                        (double.Parse(inputData[columnMapper["PremiumAsk"]], new CultureInfo("US-us")) +
                         double.Parse(inputData[columnMapper["PremiumAsk"]], new CultureInfo("US-us"))) / 2.0;
                }

                if (columnMapper.ContainsKey("ValuationTime"))
                {
                    inputSingleAssetOption.ValuationDate =
                        DateTime.Parse(inputData[columnMapper["ValuationDate"]] + " " +
                                       inputData[columnMapper["ValuationTime"]]);
                }
                inputSingleAssetOption.ValuationDate =
                    DateTime.Parse(inputData[columnMapper["ValuationDate"]]);
            }
            catch (Exception)
            {
                //Trace.WriteLine(inputLine);
            }

            return(inputSingleAssetOption);
        }