Example #1
0
        public Option(string prototype, string description)
        {
            if (prototype == null)
                throw new ArgumentNullException ("prototype");
            if (prototype.Length == 0)
                throw new ArgumentException ("Cannot be the empty string.", "prototype");

            this.prototype   = prototype;
            this.names       = prototype.Split ('|');
            this.description = description;
            this.type        = ValidateNames ();
        }
Example #2
0
        protected Option(string prototype, string description, int maxValueCount)
        {
            if (prototype == null)
            {
                throw new ArgumentNullException("prototype");
            }
            if (prototype.Length == 0)
            {
                throw new ArgumentException("Cannot be the empty string.", "prototype");
            }
            if (maxValueCount < 0)
            {
                throw new ArgumentOutOfRangeException("maxValueCount");
            }

            this.prototype   = prototype;
            this.names       = prototype.Split('|');
            this.description = description;
            this.count       = maxValueCount;
            this.type        = ParsePrototype();

            if (this.count == 0 && type != OptionValueType.None)
            {
                throw new ArgumentException(
                          "Cannot provide maxValueCount of 0 for OptionValueType.Required or " +
                          "OptionValueType.Optional.",
                          "maxValueCount");
            }
            if (this.type == OptionValueType.None && maxValueCount > 1)
            {
                throw new ArgumentException(
                          string.Format("Cannot provide maxValueCount of {0} for OptionValueType.None.", maxValueCount),
                          "maxValueCount");
            }
            if (Array.IndexOf(names, "<>") >= 0 &&
                ((names.Length == 1 && this.type != OptionValueType.None) ||
                 (names.Length > 1 && this.MaxValueCount > 1)))
            {
                throw new ArgumentException(
                          "The default option handler '<>' cannot require values.",
                          "prototype");
            }
        }
		private OptionValueType ParsePrototype()
		{
			char type = '\0';
			List<string> seps = new List<string>();
			for (int i = 0; i < names.Length; ++i)
			{
				string name = names[i];
				if (name.Length == 0)
					throw new ArgumentException("Empty option names are not supported.", "prototype");

				int end = name.IndexOfAny(NameTerminator);
				if (end == -1)
					continue;
				names[i] = name.Substring(0, end);
				if (type == '\0' || type == name[end])
					type = name[end];
				else
					throw new ArgumentException(
						string.Format("Conflicting option types: '{0}' vs. '{1}'.", type, name[end]),
						"prototype");
				AddSeparators(name, end, seps);
			}

			if (type == '\0')
				return OptionValueType.None;

			if (count <= 1 && seps.Count != 0)
				throw new ArgumentException(
					string.Format("Cannot provide key/value separators for Options taking {0} value(s).", count),
					"prototype");
			if (count > 1)
			{
				if (seps.Count == 0)
					this.separators = new string[] { ":", "=" };
				else if (seps.Count == 1 && seps[0].Length == 0)
					this.separators = null;
				else
					this.separators = seps.ToArray();
			}

			return type == '=' ? OptionValueType.Required : OptionValueType.Optional;
		}
// RECALL: void printOneExcel(Vector<double>  x,
//						Vector<double> functionResult,string title)

    public void displayinExcel(OptionValueType yval)
    {
        string text = "Value";

        if (yval == OptionValueType.Delta)
        {
            text = "Delta";
        }
        if (yval == OptionValueType.Gamma)
        {
            text = "Gamma";
        }

        if (yval == OptionValueType.Vega)
        {
            text = "Vega";
        }

        if (yval == OptionValueType.Theta)
        {
            text = "Theta";
        }
        if (yval == OptionValueType.Rho)
        {
            text = "Rho";
        }

        if (yval == OptionValueType.Coc)
        {
            text = "Coc";
        }

        Vector <double> yarr = calculate(yval);
        //	cout << "array ..."; int yy; cin >> yy;
        //	print(yarr);

        ExcelMechanisms excel = new ExcelMechanisms();

        excel.printOneExcel(XARR, yarr, text, text, text, text);
    }
Example #5
0
		protected Option (string prototype, string description, int maxValueCount, bool hidden)
		{
			if (prototype == null)
				throw new ArgumentNullException (nameof(prototype));
			if (prototype.Length == 0)
				throw new ArgumentException ("Cannot be the empty string.", nameof(prototype));
			if (maxValueCount < 0)
				throw new ArgumentOutOfRangeException (nameof(maxValueCount));

			this.prototype   = prototype;
			this.description = description;
			this.count       = maxValueCount;
			this.names       = (this is OptionSet.Category)
				// append GetHashCode() so that "duplicate" categories have distinct
				// names, e.g. adding multiple "" categories should be valid.
				? new[]{prototype + this.GetHashCode ()}
				: prototype.Split ('|');

			if (this is OptionSet.Category)
				return;

			this.type        = ParsePrototype ();
			this.hidden      = hidden;

			if (this.count == 0 && type != OptionValueType.None)
				throw new ArgumentException (
						"Cannot provide maxValueCount of 0 for OptionValueType.Required or " +
							"OptionValueType.Optional.",
						nameof(maxValueCount));
			if (this.type == OptionValueType.None && maxValueCount > 1)
				throw new ArgumentException (
						string.Format ("Cannot provide maxValueCount of {0} for OptionValueType.None.", maxValueCount),
						nameof(maxValueCount));
			if (Array.IndexOf (names, "<>") >= 0 && 
					((names.Length == 1 && this.type != OptionValueType.None) ||
					 (names.Length > 1 && this.MaxValueCount > 1)))
				throw new ArgumentException (
						"The default option handler '<>' cannot require values.",
						nameof(prototype));
		}
Example #6
0
        public IOptionTestDataCreator WithValueType(OptionValueType valueType)
        {
            _valueType = valueType;

            return(this);
        }
Example #7
0
        public static void Initialize(CoffeeAndCodeDbContext context)
        {
            context.Database.EnsureCreated();

            // Look for any users.
            if (context.Users.Any())
            {
                return;   // DB has been seeded
            }

            var users = new User[]
            {
                new User {
                    FirstName = "Danny", LastName = "Brown", CreatedDateTime = DateTime.Now, LastModifiedDateTime = DateTime.Now
                },
                new User {
                    FirstName = "Austin", LastName = "Murphy", CreatedDateTime = DateTime.Now, LastModifiedDateTime = DateTime.Now
                }
            };

            context.Users.AddRange(users);
            context.SaveChanges();


            var questions = new Question[]
            {
                new Question
                {
                    Id   = Guid.Parse("9ac46873-46ca-4619-905a-e26ac8f07744"),
                    Name = "How much coffee did you just drink?",
                    AskedOnlyOnceDaily = false
                }
            };

            context.Questions.AddRange(questions);
            context.SaveChanges();


            var optionValueTypes = new OptionValueType[]
            {
                new OptionValueType {
                    Id = Guid.Parse("ab151c4b-7dff-4150-866b-b190634b6bd6"), Name = "int"
                },
                new OptionValueType {
                    Id = Guid.Parse("2c8fa2f3-9ef6-4f40-b0e0-afbafd674501"), Name = "bool"
                },
                new OptionValueType {
                    Id = Guid.Parse("19b3022a-5b61-4e49-a11d-cf6951ecfbf9"), Name = "enum"
                }
            };

            context.ValueTypes.AddRange(optionValueTypes);
            context.SaveChanges();


            var questionOptions = new QuestionOption[]
            {
                new QuestionOption
                {
                    Id        = Guid.Parse("2e48d4d5-2a9c-4338-8ebf-f3349ce247d6"),
                    Name      = "4 oz",
                    Value     = "4",
                    Unit      = "oz",
                    ValueType = context.ValueTypes.Single(vt => vt.Id == Guid.Parse("ab151c4b-7dff-4150-866b-b190634b6bd6")),
                    Question  = context.Questions.Single(q => q.Id == Guid.Parse("9ac46873-46ca-4619-905a-e26ac8f07744"))
                },
                new QuestionOption
                {
                    Id        = Guid.Parse("88aaf4b7-377d-40ec-8a71-9f4ef86be85e"),
                    Name      = "6 oz",
                    Value     = "6",
                    Unit      = "oz",
                    ValueType = context.ValueTypes.Single(vt => vt.Id == Guid.Parse("ab151c4b-7dff-4150-866b-b190634b6bd6")),
                    Question  = context.Questions.Single(q => q.Id == Guid.Parse("9ac46873-46ca-4619-905a-e26ac8f07744"))
                }
            };

            context.Options.AddRange(questionOptions);
            context.SaveChanges();
        }
Example #8
0
 public VerbTypeFactory AddOption(string name, char shortName, bool mandatory, string help,
                                  OptionValueType optionValueType, string setName)
 {
     options.Add(new Option(name, shortName, mandatory, help, optionValueType, setName));
     return(this);
 }
Example #9
0
 public Option(string name, char shortName, bool mandatory, string help, OptionValueType optionValueType, string setName, char separator) : this(name, shortName, mandatory, help, optionValueType, setName)
 {
     Separator = separator;
 }
Example #10
0
 internal void AddOption(string name, char shortName, bool mandatory, string help, OptionValueType valueType, string setName, char separator)
 {
     options.Add(new Option(name, shortName, mandatory, help, valueType, setName, separator));
 }
Example #11
0
        private OptionValueType ParsePrototype()
        {
            char          type = '\0';
            List <string> seps = new List <string>();

            for (int i = 0; i < names.Length; ++i)
            {
                string name = names[i];
                if (name.Length == 0)
                {
                    throw new ArgumentException("不支持空的参数名", "prototype");
                }

                int end = name.IndexOfAny(NameTerminator);
                if (end == -1)
                {
                    continue;
                }
                names[i] = name.Substring(0, end);
                if (type == '\0' || type == name[end])
                {
                    type = name[end];
                }
                else
                {
                    throw new ArgumentException(
                              string.Format("参数类型冲突: '{0}' vs. '{1}'.", type, name[end]),
                              "prototype");
                }
                AddSeparators(name, end, seps);
            }

            if (type == '\0')
            {
                return(OptionValueType.None);
            }

            if (count <= 1 && seps.Count != 0)
            {
                throw new ArgumentException(
                          string.Format("参数无法解析为键值类型 {0} value(s).", count),
                          "prototype");
            }
            if (count > 1)
            {
                if (seps.Count == 0)
                {
                    this.separators = new string[] { ":", "=" }
                }
                ;
                else if (seps.Count == 1 && seps[0].Length == 0)
                {
                    this.separators = null;
                }
                else
                {
                    this.separators = seps.ToArray();
                }
            }

            return(type == '=' ? OptionValueType.Required : OptionValueType.Optional);
        }
Example #12
0
 internal OptionValue(OptionValueType optionValueType, IOptionValueConverter <T> optionValueConverter)
 {
     OptionValueType       = optionValueType;
     _optionValueConverter = optionValueConverter;
     ValueType             = typeof(T);
 }
Example #13
0
    static void Main()

    { // All options are European
        // Call option on a stock
        Option callOption = new Option();

        Console.WriteLine("Option price: {0}", callOption.Price(60.0));

        // Put option on a stock index
        Option myOption = new Option();

        myOption.otyp = "P";
        myOption.K    = 65.0;
        myOption.T    = 0.25;
        myOption.r    = 0.08;
        myOption.sig  = 0.30;

        double g = myOption.Gamma(65.0);

        //double q = 0.0; // 0.05;		// Dividend yield
        //myOption.b = myOption.r - q;
        myOption.b = myOption.r;     // stock

        double S = 60.0;

        //Console.WriteLine("Option type and price: {{0}, {1})", myOption.otyp, myOption.Price(S));
        Console.WriteLine("Option price: {0}", myOption.Price(S));

        // Using extension methods
        myOption.Display(S);

        double lower = 10.0;
        double upper = 100.0;
        int    N     = 90;

        double[] priceArray = myOption.Price(lower, upper, N);

        for (int j = 0; j < priceArray.Length; j++)
        {
            Console.WriteLine(priceArray[j]);
        }


        Range <double> extent      = new Range <double>(0.001, 210.0); // AG: it is better not to start from 0 as gamma is NaN for S=0
        int            NumberSteps = 210;

        OptionPresentation myPresent = new OptionPresentation(myOption, extent, NumberSteps);

        OptionValueType val = OptionValueType.Value;

        myPresent.displayinExcel(val);

        val = OptionValueType.Delta;
        myPresent.displayinExcel(val);

        val = OptionValueType.Gamma;
        myPresent.displayinExcel(val);

        val = OptionValueType.Vega;
        myPresent.displayinExcel(val);

        val = OptionValueType.Theta;
        myPresent.displayinExcel(val);

        val = OptionValueType.Rho;
        myPresent.displayinExcel(val);

        val = OptionValueType.Coc;
        myPresent.displayinExcel(val);
    }
Example #14
0
        public IOptionValue Create(OptionValueType optionValueType)
        {
            switch (optionValueType)
            {
            case OptionValueType.String:
                return(new OptionValue <string>(optionValueType, new OptionValueConverter <string>()));

            case OptionValueType.Byte:
                return(new OptionValue <byte>(optionValueType, new OptionValueConverter <byte>()));

            case OptionValueType.Int16:
                return(new OptionValue <short>(optionValueType, new OptionValueConverter <short>()));

            case OptionValueType.UInt16:
                return(new OptionValue <ushort>(optionValueType, new OptionValueConverter <ushort>()));

            case OptionValueType.Int32:
                return(new OptionValue <int>(optionValueType, new OptionValueConverter <int>()));

            case OptionValueType.UInt32:
                return(new OptionValue <uint>(optionValueType, new OptionValueConverter <uint>()));

            case OptionValueType.Int64:
                return(new OptionValue <long>(optionValueType, new OptionValueConverter <long>()));

            case OptionValueType.UInt64:
                return(new OptionValue <ulong>(optionValueType, new OptionValueConverter <ulong>()));

            case OptionValueType.Guid:
                return(new OptionValue <Guid>(optionValueType, new OptionValueConverter <Guid>()));

            case OptionValueType.Bool:
                return(new OptionValue <bool>(optionValueType, new OptionValueConverter <bool>()));

            case OptionValueType.Decimal:
                return(new OptionValue <decimal>(optionValueType, new OptionValueConverter <decimal>()));

            case OptionValueType.DateTime:
            case OptionValueType.Date:
            case OptionValueType.Time:
                return(new OptionValue <DateTime>(optionValueType, new OptionValueConverter <DateTime>()));

            case OptionValueType.TimeSpan:
                return(new OptionValue <TimeSpan>(optionValueType, new OptionValueConverter <TimeSpan>()));

            case OptionValueType.NullableByte:
                return(new OptionValue <byte?>(optionValueType, new OptionValueConverter <byte?>()));

            case OptionValueType.NullableInt16:
                return(new OptionValue <short?>(optionValueType, new OptionValueConverter <short?>()));

            case OptionValueType.NullableUInt16:
                return(new OptionValue <ushort?>(optionValueType, new OptionValueConverter <ushort?>()));

            case OptionValueType.NullableInt32:
                return(new OptionValue <int?>(optionValueType, new OptionValueConverter <int?>()));

            case OptionValueType.NullableUInt32:
                return(new OptionValue <uint?>(optionValueType, new OptionValueConverter <uint?>()));

            case OptionValueType.NullableInt64:
                return(new OptionValue <long?>(optionValueType, new OptionValueConverter <long?>()));

            case OptionValueType.NullableUInt64:
                return(new OptionValue <ulong?>(optionValueType, new OptionValueConverter <ulong?>()));

            case OptionValueType.NullableGuid:
                return(new OptionValue <Guid?>(optionValueType, new OptionValueConverter <Guid?>()));

            case OptionValueType.NullableBool:
                return(new OptionValue <bool?>(optionValueType, new OptionValueConverter <bool?>()));

            case OptionValueType.NullableDecimal:
                return(new OptionValue <decimal?>(optionValueType, new OptionValueConverter <decimal?>()));

            case OptionValueType.NullableDateTime:
            case OptionValueType.NullableDate:
            case OptionValueType.NullableTime:
                return(new OptionValue <DateTime?>(optionValueType, new OptionValueConverter <DateTime?>()));

            case OptionValueType.NullableTimeSpan:
                return(new OptionValue <TimeSpan?>(optionValueType, new OptionValueConverter <TimeSpan?>()));

            case OptionValueType.ByteArray:
                return(new OptionValue <byte[]>(optionValueType, new OptionValueConverter <byte[]>()));

            default:
                throw new InvalidEnumArgumentException(nameof(optionValueType), (int)optionValueType, typeof(OptionValueType));
            }
        }
    public Vector <double> calculate(OptionValueType yval)
    {
        // Contains value at end-points
        Vector <double> result = new Vector <double>(nSteps + 1, 1);

        if (yval == OptionValueType.Value)
        {
            for (int j = XARR.MinIndex; j <= XARR.MaxIndex; j++)
            {
                result[j] = curr.Price(XARR[j]);
            }
        }

        if (yval == OptionValueType.Delta)
        {
            for (int j = XARR.MinIndex; j <= XARR.MaxIndex; j++)
            {
                result[j] = curr.Delta(XARR[j]);
            }
        }

        if (yval == OptionValueType.Gamma)
        {
            for (int j = XARR.MinIndex; j <= XARR.MaxIndex; j++)
            {
                result[j] = curr.Gamma(XARR[j]);
            }
            //	print(result);
            ///	int yy; cin >> yy;
        }

        if (yval == OptionValueType.Vega)
        {
            for (int j = XARR.MinIndex; j <= XARR.MaxIndex; j++)
            {
                result[j] = curr.Vega(XARR[j]);
            }
        }

        if (yval == OptionValueType.Theta)
        {
            for (int j = XARR.MinIndex; j <= XARR.MaxIndex; j++)
            {
                result[j] = curr.Theta(XARR[j]);
            }
        }

        if (yval == OptionValueType.Rho)
        {
            for (int j = XARR.MinIndex; j <= XARR.MaxIndex; j++)
            {
                result[j] = curr.Rho(XARR[j]);
            }
        }

        if (yval == OptionValueType.Coc)
        {
            for (int j = XARR.MinIndex; j <= XARR.MaxIndex; j++)
            {
                result[j] = curr.Rho(XARR[j]);
            }
        }

        return(result);
    }
Example #16
0
        private OptionValueType ValidateNames()
        {
            char type = '\0';
            for (int i = 0; i < names.Length; ++i) {
                string name = names [i];
                if (name.Length == 0)
                    throw new ArgumentException ("Empty option names are not supported.", "prototype");

                int end = name.IndexOfAny (NameTerminator);
                if (end > 0) {
                    names [i] = name.Substring (0, end);
                    if (type == '\0' || type == name [end])
                        type = name [end];
                    else
                        throw new ArgumentException (
                            string.Format ("Conflicting option types: '{0}' vs. '{1}'.", type, name [end]),
                            "prototype");
                }
            }
            if (type == '\0')
                return OptionValueType.None;
            return type == '=' ? OptionValueType.Required : OptionValueType.Optional;
        }
Example #17
0
        public OptionSet Add(string prototype, string description, Action <string> action, OptionValueType optionValueType)
        {
            if (action == null)
            {
                throw new ArgumentNullException("action");
            }
            Option p = new ActionOption(prototype, description, 1,
                                        delegate(OptionValueCollection v) { action(v[0]); });

            p.OptionValueType = optionValueType;
            base.Add(p);
            return(this);
        }
Example #18
0
        public void Parse_ShouldReturnCorrectOptionValueType_ForAllOptionValueTypeEnum([Values] OptionValueType optionValueType)
        {
            (string name, XDocument optionSet) = CreateOptionSetWithOneOption(tdc => tdc.WithValueType(optionValueType));

            var actual = Sut.Parse(CreateReader(optionSet), ThrowOptionSetValidator);

            Assert.That(actual.Options[name].Behaviour, Is.TypeOf <SimpleOptionBehaviour>());

            var simpleOptionBehaviour = (SimpleOptionBehaviour)actual.Options[name].Behaviour;

            Assert.That(simpleOptionBehaviour.OptionValueType, Is.EqualTo(optionValueType));
        }