Exemple #1
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="startDate">开始日期</param>
 /// <param name="maturityDate">到期日</param>
 /// <param name="exercise">行权方式</param>
 /// <param name="optionType">看涨看跌</param>
 /// <param name="spreadType">价差期权类型</param>
 /// <param name="weights">权重</param>
 /// <param name="strike">行权价</param>
 /// <param name="underlyingInstrumentType">标的资产类型</param>
 /// <param name="calendar">交易日历</param>
 /// <param name="dayCount">日期规则</param>
 /// <param name="payoffCcy">收益计算币种</param>
 /// <param name="settlementCcy">结算币种</param>
 /// <param name="exerciseDates">行权日</param>
 /// <param name="observationDates">观察日</param>
 /// <param name="underlyingTickers">标的代码</param>
 /// <param name="notional">名义本金</param>
 /// <param name="settlementGap">结算日规则</param>
 /// <param name="optionPremiumPaymentDate">权利金支付日</param>
 /// <param name="optionPremium">权利金</param>
 /// <param name="hasNightMarket">标的资产是否有夜盘交易</param>
 /// <param name="commodityFuturesPreciseTimeMode">是否启用精确时间计算模式</param>
 public SpreadOption(Date startDate,
                     Date maturityDate,
                     OptionExercise exercise,
                     OptionType optionType,
                     SpreadType spreadType,
                     double[] weights,
                     double strike,
                     InstrumentType underlyingInstrumentType,
                     ICalendar calendar,
                     IDayCount dayCount,
                     CurrencyCode payoffCcy,
                     CurrencyCode settlementCcy,
                     Date[] exerciseDates,
                     Date[] observationDates,
                     string[] underlyingTickers,
                     double notional                      = 1,
                     DayGap settlementGap                 = null,
                     Date optionPremiumPaymentDate        = null,
                     double optionPremium                 = 0,
                     bool hasNightMarket                  = false,
                     bool commodityFuturesPreciseTimeMode = false)
     : base(startDate: startDate, maturityDate: maturityDate, exercise: exercise, optionType: optionType, strike: new double[] { strike },
            underlyingInstrumentType: underlyingInstrumentType, calendar: calendar, dayCount: dayCount,
            settlementCcy: settlementCcy, payoffCcy: payoffCcy, exerciseDates: exerciseDates, observationDates: observationDates,
            notional: notional, settlementGap: settlementGap, optionPremiumPaymentDate: optionPremiumPaymentDate, optionPremium: optionPremium, underlyingTickers: underlyingTickers,
            hasNightMarket: hasNightMarket, commodityFuturesPreciseTimeMode: commodityFuturesPreciseTimeMode)
 {
     SpreadType = spreadType;
     Weights    = weights;
 }
Exemple #2
0
        public static ICollection <TItem> Round <TItem>(
            this ICollection <TItem> items,
            Func <TItem, decimal> getValue,
            Action <TItem, decimal> setValue,
            int precision,
            SpreadType spreadType)
            where TItem : class
        {
            decimal roundedSum       = 0;
            decimal sumOfRounded     = 0;
            decimal accumulatedDelta = 0;
            decimal absoluteAtom     = (decimal)Math.Pow(10, -precision);
            decimal atom;

            foreach (TItem sourceItem in items)
            {
                decimal sourceValue  = getValue(sourceItem);
                decimal roundedValue = Round(sourceValue, precision);
                accumulatedDelta += sourceValue - roundedValue;

                roundedSum   += sourceValue;
                sumOfRounded += roundedValue;

                if (spreadType == SpreadType.Flow)
                {
                    if (Math.Abs(Round(accumulatedDelta, TolerancePrecision)) >= absoluteAtom)
                    {
                        atom              = Math.Sign(accumulatedDelta) * absoluteAtom;
                        roundedValue     += atom;
                        accumulatedDelta -= atom;
                    }
                }
                setValue(sourceItem, roundedValue);
            }

            if (spreadType == SpreadType.Flow)
            {
                return(items);
            }

            roundedSum = Round(roundedSum, precision);

            decimal delta = roundedSum - sumOfRounded;

            if (Math.Abs(Round(delta, TolerancePrecision)) >= absoluteAtom)
            {
                atom = Math.Sign(delta) * absoluteAtom;
                int countOfAdjusted = (int)Round(delta / atom, 0);

                foreach (int index in GetAdjustedIndexes(spreadType, items.Count, countOfAdjusted, delta < 0m))
                {
                    TItem adjustingItem = items.ElementAt(index);
                    setValue(adjustingItem, getValue(adjustingItem) + atom);
                }
            }
            return(items);
        }
Exemple #3
0
 public DendriteGene(int type, BrainLobeType sourceLobeIndex, SpreadType spread, int fanout, Vector2Int dendriteNumber, Vector2Int ltwRange, Vector2Int strengthRange, DendriteDynamicsGene dynamicsGene)
 {
     Type            = type;
     SourceLobeIndex = sourceLobeIndex;
     Spread          = spread;
     Fanout          = fanout;
     DendriteNumber  = dendriteNumber;
     LTWRange        = ltwRange;
     StrengthRange   = strengthRange;
     Dynamics        = dynamicsGene;
 }
Exemple #4
0
        public static IEnumerable <decimal> Round(
            this IEnumerable <decimal> sourceSequence,
            int precision,
            SpreadType spreadType)
        {
            List <decimal> result = new List <decimal>();

            decimal roundedSum       = 0;
            decimal sumOfRounded     = 0;
            decimal accumulatedDelta = 0;
            decimal absoluteAtom     = (decimal)Math.Pow(10, -precision);
            decimal atom;

            foreach (decimal sourceValue in sourceSequence)
            {
                decimal roundedValue = Round(sourceValue, precision);

                roundedSum   += sourceValue;
                sumOfRounded += roundedValue;

                if (spreadType == SpreadType.Flow)
                {
                    if (Math.Abs(Round(accumulatedDelta, TolerancePrecision)) >= absoluteAtom)
                    {
                        atom              = Math.Sign(accumulatedDelta) * absoluteAtom;
                        roundedValue     += atom;
                        accumulatedDelta -= atom;
                    }
                }
                result.Add(roundedValue);
            }

            if (spreadType == SpreadType.Flow)
            {
                return(result);
            }

            roundedSum = Round(roundedSum, precision);

            decimal delta = roundedSum - sumOfRounded;

            if (Math.Abs(Round(delta, TolerancePrecision)) >= absoluteAtom)
            {
                atom = Math.Sign(delta) * absoluteAtom;
                int countOfAdjusted = (int)Round(delta / atom, 0);

                foreach (int index in GetAdjustedIndexes(spreadType, result.Count, countOfAdjusted, delta < 0m))
                {
                    result[index] += atom;
                }
            }
            return(result);
        }
Exemple #5
0
 private void SetType(Option[] options)
 {
     if (options.Length == 1)
     {
         if (options[0].OptionType == OptionType.Call)
         {
             if (options[0].PositionType == PositionType.Long)
             {
                 _type = SpreadType.LongCall;
             }
         }
         else if (options[0].OptionType == OptionType.Put)
         {
             if (options[0].PositionType == PositionType.Long)
             {
                 _type = SpreadType.LongPut;
             }
             if (options[0].PositionType == PositionType.Short)
             {
                 _type = SpreadType.ShortPut;
             }
         }
     }
     else if ((options.Length == 2))
     {
         if (options[1].Strike == 0)
         {
             if (options[0].OptionType == OptionType.Call)
             {
                 if (options[0].PositionType == PositionType.Long)
                 {
                     _type = SpreadType.LongCall;
                 }
             }
             else if (options[0].OptionType == OptionType.Put)
             {
                 if (options[0].PositionType == PositionType.Long)
                 {
                     _type = SpreadType.LongPut;
                 }
                 if (options[0].PositionType == PositionType.Short)
                 {
                     _type = SpreadType.ShortPut;
                 }
             }
         }
         else
         {
             if (options[0].OptionType == OptionType.Call)
             {
                 int highStikeIndex = options[0].Strike > options[1].Strike ? 0 : 1;
                 if (options[highStikeIndex].PositionType == PositionType.Long)
                 {
                     _type = SpreadType.ShortCallVertical;
                 }
                 else
                 {
                     _type = SpreadType.LongCallVertical;
                 }
             }
             else if (options[0].OptionType == OptionType.Put)
             {
                 int highStikeIndex = options[0].Strike > options[1].Strike ? 0 : 1;
                 if (options[highStikeIndex].PositionType == PositionType.Long)
                 {
                     _type = SpreadType.LongPutVertical;
                 }
                 else
                 {
                     _type = SpreadType.ShortPutVertical;
                 }
             }
         }
     }
 }
Exemple #6
0
            public static Spread GetSpread(Stock stock, SpreadType type, double strikeLow, double spreadWidth, DateTime experationDate, DateTime date)
            {
                Option[] options;
                if (type == SpreadType.LongCall || type == SpreadType.LongPut || type == SpreadType.ShortPut)
                {
                    options = new Option[1];
                }
                else
                {
                    options = new Option[2];
                }

                switch (type)
                {
                case SpreadType.LongCall:
                    options[0] = Option.GetOption(stock, OptionType.Call, PositionType.Long, strikeLow, experationDate, date);
                    break;

                case SpreadType.LongPut:
                    options[0] = Option.GetOption(stock, OptionType.Put, PositionType.Long, strikeLow, experationDate, date);
                    break;

                case SpreadType.ShortPut:
                    options[0] = Option.GetOption(stock, OptionType.Put, PositionType.Short, strikeLow, experationDate, date);
                    break;

                case SpreadType.LongCallVertical:
                    options[0] = Option.GetOption(stock, OptionType.Call, PositionType.Long, strikeLow, experationDate, date);
                    options[1] = Option.GetOption(stock, OptionType.Call, PositionType.Short, strikeLow + spreadWidth, experationDate, date);
                    break;

                case SpreadType.ShortCallVertical:
                    options[0] = Option.GetOption(stock, OptionType.Call, PositionType.Short, strikeLow, experationDate, date);
                    options[1] = Option.GetOption(stock, OptionType.Call, PositionType.Long, strikeLow + spreadWidth, experationDate, date);
                    break;

                case SpreadType.LongPutVertical:
                    options[0] = Option.GetOption(stock, OptionType.Put, PositionType.Short, strikeLow, experationDate, date);
                    options[1] = Option.GetOption(stock, OptionType.Put, PositionType.Long, strikeLow + spreadWidth, experationDate, date);
                    break;

                case SpreadType.ShortPutVertical:
                    options[0] = Option.GetOption(stock, OptionType.Put, PositionType.Long, strikeLow, experationDate, date);
                    options[1] = Option.GetOption(stock, OptionType.Put, PositionType.Short, strikeLow + spreadWidth, experationDate, date);
                    break;
                }

                if (options.Length == 2)
                {
                    if (options[0].Strike == options[1].Strike)
                    {
                        OptionChain chain     = OptionDataBase.Get(options[1].Stock, date);
                        double      newStrike = chain.FindNextStrike(options[1].Strike, options[1].ExpirationDate);
                        options[1] = Option.GetOption(options[1].Stock, options[1].OptionType, options[1].PositionType,
                                                      newStrike, options[1].ExpirationDate, date);
                    }
                }

                Spread spread = new Spread(options);

                return(spread);
            }
Exemple #7
0
        public void Shoot(SpreadType spreadType, bool comeFromEnemy)
        {
            _spreadType = spreadType;
            _bulletsToShoot.Clear();
            if (!_canShoot)
            {
                return;
            }
            _canShoot = false;

            switch (spreadType.Type)
            {
            default:
                foreach (var spreadTypeOffset in spreadType.Offsets)
                {
                    var bullet = GetBullet();
                    if (bullet == null)
                    {
                        continue;
                    }

                    bullet.Sin       = spreadType.Sin;
                    bullet.SinCoef.y = spreadType.VelocityCoef * spreadTypeOffset.y;
                    bullet.SinCoef.x = spreadTypeOffset.x;

                    bullet.transform.position = _transform.position + (bullet.Sin ? Vector3.zero : spreadTypeOffset);
                    switch (spreadType.Type)
                    {
                    case Spreads.LockOn:
                        bullet.LockOnTarget = GetClosestEnemy();
                        if (bullet.LockOnTarget)
                        {
                            bullet.StartingVelocity = bullet.LockOnTarget.transform.position - _transform.position;
                            bullet.BulletVelocity   = Weapons[_selectedWeapon].BulletVelocity;
                        }
                        else
                        {
                            bullet.StartingVelocity.y = spreadTypeOffset.y * spreadType.VelocityCoef;
                            bullet.StartingVelocity.x = Weapons[_selectedWeapon].BulletVelocity;
                        }
                        break;

                    case Spreads.AutoAim:
                        var target = comeFromEnemy ? GameObject.FindGameObjectWithTag("Player") : GetClosestEnemy();
                        bullet.StartingVelocity = target.transform.position - _transform.position;
                        bullet.BulletVelocity   = Weapons[_selectedWeapon].BulletVelocity;
                        break;

                    default:
                        bullet.StartingVelocity.y = spreadTypeOffset.y * spreadType.VelocityCoef;
                        bullet.StartingVelocity.x = Weapons[_selectedWeapon].BulletVelocity;
                        break;
                    }
                    bullet.StartingVelocity.Normalize();
                    bullet.StartingVelocity *= Weapons[_selectedWeapon].BulletVelocity;
                    bullet.gameObject.SetActive(true);
                    bullet.CanHurtPlayer = comeFromEnemy;
                    if (spreadType.Type == Spreads.Bomb)
                    {
                        StartCoroutine(ExplodeBomb(bullet, bullet.CanHurtPlayer));
                    }
                    _bulletsToShoot.Add(bullet);
                }
                break;

            case Spreads.Explosion:
                foreach (var spreadTypeOffset in spreadType.Offsets)
                {
                    var bullet = GetBullet();
                    if (bullet == null)
                    {
                        continue;
                    }

                    bullet.transform.position = _transform.position;
                    bullet.StartingVelocity   = spreadTypeOffset;
                    bullet.StartingVelocity.Normalize();
                    bullet.StartingVelocity *= Weapons[_selectedWeapon].BulletVelocity;
                    bullet.gameObject.SetActive(true);
                    bullet.CanHurtPlayer = comeFromEnemy;
                    _bulletsToShoot.Add(bullet);
                }
                break;
            }

            foreach (var bullet in _bulletsToShoot)
            {
                bullet.Damage = spreadType.Damage;
                if (comeFromEnemy &&
                    spreadType.Type != Spreads.AutoAim &&
                    spreadType.Type != Spreads.LockOn)
                {
                    bullet.StartingVelocity.x *= -1f;
                }
                bullet.ShootTheBullet();
            }

            StartCoroutine(RateLimiter(spreadType.RateOfFire.Value));
        }
Exemple #8
0
        private static IEnumerable <int> GetAdjustedIndexes(SpreadType spreadType, int length, int countOfAdjusted, bool invertSpread)
        {
            // Contracts
            if (countOfAdjusted > length / 2)
            {
                throw new ArgumentOutOfRangeException(nameof(countOfAdjusted));
            }
            // end of contracts

            if (invertSpread)
            {
                switch (spreadType)
                {
                case SpreadType.Top:
                    spreadType = SpreadType.Bottom;
                    break;

                case SpreadType.Bottom:
                    spreadType = SpreadType.Top;
                    break;

                case SpreadType.First:
                    spreadType = SpreadType.Last;
                    break;

                case SpreadType.Last:
                    spreadType = SpreadType.First;
                    break;
                }
            }

            Random        randomizer        = new Random();
            HashSet <int> usedRandomIndexes = new HashSet <int>();

            for (int i = 0; i < countOfAdjusted; i++)
            {
                switch (spreadType)
                {
                case SpreadType.Top:
                    yield return(i);

                    break;

                case SpreadType.Bottom:
                    yield return(length - i - 1);

                    break;

                case SpreadType.Evenly:
                    yield return((int)Round((decimal)length / countOfAdjusted * i, 0));

                    break;

                case SpreadType.Random:
                    int index;
                    while (usedRandomIndexes.Contains(index = randomizer.Next(length)))
                    {
                    }
                    usedRandomIndexes.Add(index);
                    yield return(index);

                    break;

                case SpreadType.First:
                    yield return(0);

                    break;

                case SpreadType.Last:
                    yield return(length - 1);

                    break;

                default:
                    throw new NotImplementedException();
                }
            }
        }