/// <summary>
        /// Instantaneous drift of the F_i in  the T_k-forward-measure
        ///-F is a n-dim vector of the LIBOR forward rates
        /// -alpha is a n-dim vector containing the tenors of the LIBOR rates
        /// -k indicates that the T_k forward measure is used
        /// - rho is n x n - Matrix containg the correlation betweeen the BM driving
        /// the LIBOR rates
        /// </summary>
        /// <param name="t"></param>
        /// <param name="forwardRate"></param>
        /// <param name="i"></param>
        /// <param name="k"></param>
        /// <returns></returns>
        private double Drift(double t, Vector <double> forwardRate, int i, int k)
        {
            if (i + 1 == k)
            {
                return(0);
            }

            if (i + 1 > k)
            {
                double sum = 0;
                var    si  = Volatility[i](T[i] - t);
                for (var j = i + 1; j <= k - 1; j++)
                {
                    // TODO: Check volatility of L[j]
                    var sj = Volatility[j](T[j] - t);

                    sum += Alpha[j] * (forwardRate[j] + Delta[j]) / (1 + Alpha[j] * forwardRate[j]) * Correlation[i, j] * si * sj;
                }
                return(-sum);
            }
            else
            {
                double sum = 0;
                var    si  = Volatility[i](T[i] - t);

                for (var j = k; j <= i; j++)
                {
                    // L[t,T[i], T[i+1]] => volatility sigma(T[i]-t)
                    var sj = Volatility[j](T[j] - t);

                    sum += Alpha[j] * (forwardRate[j] + Delta[j]) / (1 + Alpha[j] * forwardRate[j]) * Correlation[i, j] * si * sj;
                }
                return(sum);
            }
        }
示例#2
0
        public override void GetAll(IIndicatorValues Ind)
        {
            int period = (int)this.IndicatorParameters.List[0];

            if (!Ind.Volatility(period).IsPopulated)
            {
                int oldCurrentBar = Ind.Bar.CurrentBar;
                for (int i = 1; i <= Ind.Bar.MaxBar; i++)
                {
                    Ind.Bar.CurrentBar = i;

                    object prototype = null;
                    if (i == 1)
                    {
                        prototype = new Volatility();
                    }
                    else
                    {
                        prototype = (Volatility)Ind.Volatility(period)[1].Clone();
                    }

                    Get(ref prototype, Ind);

                    Ind.Volatility(period)[0] = (Volatility)prototype;

                    Ind.Volatility(period).IsPopulated = true;                     // set here so instance is guaranteed to exits
                }

                Ind.Bar.CurrentBar = oldCurrentBar;
            }
        }
示例#3
0
        private async Task <bool> ProcessAsync(AssetPairSettings assetPair, DateTime date)
        {
            _log.Info($"Start processing {assetPair} {date.ToString("yyyy-MM-dd")}.");

            try
            {
                var candles = (await _candlesRepository.GetAsync(assetPair.AssetPairId, date))
                              .OrderBy(c => c.CandleTimestamp).ToArray();

                if (!candles.Any())
                {
                    _log.Info($"There are no candles to process {assetPair} on {date.ToString("yyyy-MM-dd")}.");
                    return(false);
                }

                if (candles.Length < ChangesGap + MinDeviationCount)
                {
                    _log.Info($"Not enought candles to process {assetPair} on {date.ToString("yyyy-MM-dd")}.");
                    return(false);
                }

                Volatility volatility = Calculate(assetPair, candles);
                await _volatilityRepository.InsertAsync(volatility);

                await _candlesRepository.DeleteAsync(candles);

                _log.Info($"Processed {assetPair} {date.ToString("yyyy-MM-dd")} based on {candles.Length} candles.");
            }
            catch (Exception ex)
            {
                _log.Error(ex, $"Fail processing {assetPair} {date.ToString("yyyy-MM-dd")}.");
            }

            return(true);
        }
示例#4
0
 protected Dice(double min, double max, Volatility volatility)
     : this()
 {
     MinValue = min;
     MaxValue = max;
     Volatility = (double)volatility;
 }
        public Volatility Randomrize(Volatility volatility)
        {
            var randomrizedVolatility = _innerRandomrizer.Randomrize(volatility);

            _volatilities.Add(randomrizedVolatility.Value);

            return(volatility);
        }
示例#6
0
文件: Stock.cs 项目: mskKote/stonks
 public Stock(ulong sharesAmount, CompanyInfo company,
              Volatility volatility = Volatility.Low, bool isDividends = false)
 {
     SharesAmount   = sharesAmount;
     Price          = company.Value / sharesAmount;
     Company        = company;
     VolatilityRate = volatility;
     IsDividends    = isDividends;
 }
        public static double Next(double mean, double standardDeviation, Volatility volatility)
        {
            var v = (int) volatility;
            var total = 0d;

            for (var i = 0; i < v; i++)
            {
                total += SimpleRNG.GetNormal(mean, standardDeviation);
            }

            return total / v;
        }
        /// <summary>
        /// Compute correlation matrix
        /// </summary>
        /// <param name="t"></param>
        /// <param name="dt"></param>
        /// <returns></returns>
        private Matrix <double> CorrelationMatrix(double t, double dt)
        {
            var C = Matrix <double> .Build.Dense(NumRates, NumRates);

            for (var i = 0; i < NumRates; i++)
            {
                for (var j = 0; j <= i; j++)
                {
                    var i1 = i;
                    var j1 = j;

                    Func <double, double> func = x => Correlation[i1, j1] * Volatility[i1](T[i1] - x) * Volatility[j1](T[j1] - x);

                    var corr = Integrate.OnClosedInterval(func, t, t + dt);

                    C[i, j] = corr;
                    C[j, i] = corr;
                }
            }
            return(C);
        }
 public T TryGet <T>(string cacheKey, Func <T> getObjectsForCache, Volatility volatility) where T : class
 {
     try
     {
         return(Get <T>(cacheKey) ?? Set <T>(cacheKey, getObjectsForCache.Invoke(), volatility));
     }
     catch (Exception ex)
     {
         logger.Error(TagGroup, "TryGet", ex);
         return(default(T));
     }
 }
示例#10
0
        protected override string GetPresentDetail(IOutputInstant Instant, IIndicatorValues Data, IndicatorParameters IndicatorParameters)
        {
            Volatility vol = Data.Volatility(IndicatorParameters)[Instant.ExposureDate];

            if (vol != null)
            {
                return(String.Format("{0}|", vol.Value));
            }
            else
            {
                return(String.Format("{0}|", ""));
            }
        }
        private int GetDuration(Volatility volatility)
        {
            switch (volatility)
            {
            case Volatility.Low:
                return(1440);

            case Volatility.Medium:
                return(60);

            case Volatility.High:
                return(10);

            default:
                return(10);
            }
        }
        private double[][] computeRollingFractile(int start, int end, int period)
        {
            double[][] disArr = new double[etfDailyData.Count()][];
            //获取前复权的价格
            double[] etfPrice = new double[etfDailyData.Count()];
            for (int i = 0; i < etfDailyData.Count(); i++)
            {
                etfPrice[i] = etfDailyData[i].close * etfDailyData[i].adjustFactor / etfDailyData.Last().adjustFactor;
            }
            //获取ETF每日年化波动率
            double[] etfVol = new double[etfDailyData.Count()];
            etfVol      = Volatility.HVYearly(etfPrice, step);
            this.etfVol = etfVol;
            //统计每日波动率分位数
            List <double> volList = new List <double>();

            for (int i = start; i < etfPrice.Count(); i++)
            {
                //按周期向前推算历史波动率
                volList = etfVol.ToList().GetRange(start - period + 1, period).OrderBy(x => x).ToList();
                if (i >= start)
                {
                    int L = volList.Count() - 1;
                    disArr[i]     = new double[11];
                    disArr[i][0]  = volList[0];
                    disArr[i][1]  = volList[(int)Math.Ceiling(L * 0.1)];
                    disArr[i][2]  = volList[(int)Math.Ceiling(L * 0.2)];
                    disArr[i][3]  = volList[(int)Math.Ceiling(L * 0.3)];
                    disArr[i][4]  = volList[(int)Math.Ceiling(L * 0.4)];
                    disArr[i][5]  = volList[(int)Math.Ceiling(L * 0.5)];
                    disArr[i][6]  = volList[(int)Math.Ceiling(L * 0.6)];
                    disArr[i][7]  = volList[(int)Math.Ceiling(L * 0.7)];
                    disArr[i][8]  = volList[(int)Math.Ceiling(L * 0.8)];
                    disArr[i][9]  = volList[(int)Math.Ceiling(L * 0.9)];
                    disArr[i][10] = volList[L];
                }
            }
            return(disArr);
        }
        protected override void WriteDataXML(XElement ele, ElderScrollsPlugin master)
        {
            XElement subEle;

            ele.TryPathTo("Sunrise/Begin", true, out subEle);
            subEle.Value = SunriseBegin.ToString();

            ele.TryPathTo("Sunrise/End", true, out subEle);
            subEle.Value = SunriseEnd.ToString();

            ele.TryPathTo("Sunset/Begin", true, out subEle);
            subEle.Value = SunsetBegin.ToString();

            ele.TryPathTo("Sunset/End", true, out subEle);
            subEle.Value = SunsetEnd.ToString();

            ele.TryPathTo("Volatility", true, out subEle);
            subEle.Value = Volatility.ToString();

            ele.TryPathTo("MoonData", true, out subEle);
            MoonData.WriteXML(subEle, master);
        }
        /// <summary>
        /// 计算历史波动率的分位数
        /// </summary>
        /// <returns></returns>
        private double[][] computeFractile(int start, int end)
        {
            double[][] disArr = new double[etfDailyData.Count()][];
            //获取前复权的价格
            double[] etfPrice = new double[etfDailyData.Count()];
            for (int i = 0; i < etfDailyData.Count(); i++)
            {
                etfPrice[i] = etfDailyData[i].close * etfDailyData[i].adjustFactor / etfDailyData.Last().adjustFactor;
            }
            //获取ETF每日年化波动率
            double[] etfVol = new double[etfDailyData.Count()];
            etfVol      = Volatility.HVYearly(etfPrice, step);
            this.etfVol = etfVol;
            //统计每日波动率分位数
            List <double> volList = new List <double>();

            for (int i = 1; i < etfPrice.Count(); i++)
            {
                //按循序依次向数组中插入波动率
                if (volList.Count() == 0)
                {
                    volList.Add(etfVol[i]);
                }
                else
                {
                    if (etfVol[i] < volList[0])
                    {
                        volList.Insert(0, etfVol[i]);
                    }
                    else if (etfVol[i] > volList.Last())
                    {
                        volList.Insert(volList.Count(), etfVol[i]);
                    }
                    else
                    {
                        for (int j = 1; j < volList.Count() - 1; j++)
                        {
                            if (etfVol[i] > volList[j - 1] && etfVol[i] <= volList[j])
                            {
                                volList.Insert(j, etfVol[i]);
                                continue;
                            }
                        }
                    }
                }
                if (i >= start)
                {
                    int L = volList.Count() - 1;
                    disArr[i]     = new double[11];
                    disArr[i][0]  = volList[0];
                    disArr[i][1]  = volList[(int)Math.Ceiling(L * 0.1)];
                    disArr[i][2]  = volList[(int)Math.Ceiling(L * 0.2)];
                    disArr[i][3]  = volList[(int)Math.Ceiling(L * 0.3)];
                    disArr[i][4]  = volList[(int)Math.Ceiling(L * 0.4)];
                    disArr[i][5]  = volList[(int)Math.Ceiling(L * 0.5)];
                    disArr[i][6]  = volList[(int)Math.Ceiling(L * 0.6)];
                    disArr[i][7]  = volList[(int)Math.Ceiling(L * 0.7)];
                    disArr[i][8]  = volList[(int)Math.Ceiling(L * 0.8)];
                    disArr[i][9]  = volList[(int)Math.Ceiling(L * 0.9)];
                    disArr[i][10] = volList[L];
                }
            }
            return(disArr);
        }
示例#15
0
        private void computeVol(string code, int period, ref List <HistoricalVol> volList)
        {
            var list      = getHistoricalDailyData(code);
            var mdata     = (from x in list where x.close > 0 select x).ToList();
            var timelist  = mdata.Select(x => x.time).ToArray();
            var closelist = mdata.Select(x => x.close).ToArray();
            var vol       = Volatility.HVYearly(closelist, period);
            int start     = volList.Count() - vol.Count();

            string[] monthList = code.Split('.');
            string   month     = monthList[0].Substring(monthList[0].Length - 2, 2);

            for (int i = start; i < volList.Count(); i++)
            {
                if (month == "01" && period == period1)
                {
                    volList[i].M01_1 = vol[i - start];
                }
                else if (month == "01" && period == period2)
                {
                    volList[i].M01_2 = vol[i - start];
                }
                else if (month == "03" && period == period1)
                {
                    volList[i].M03_1 = vol[i - start];
                }
                else if (month == "03" && period == period2)
                {
                    volList[i].M03_2 = vol[i - start];
                }
                else if (month == "05" && period == period1)
                {
                    volList[i].M05_1 = vol[i - start];
                }
                else if (month == "05" && period == period2)
                {
                    volList[i].M05_2 = vol[i - start];
                }
                else if (month == "07" && period == period1)
                {
                    volList[i].M07_1 = vol[i - start];
                }
                else if (month == "07" && period == period2)
                {
                    volList[i].M07_2 = vol[i - start];
                }
                else if (month == "08" && period == period1)
                {
                    volList[i].M08_1 = vol[i - start];
                }
                else if (month == "08" && period == period2)
                {
                    volList[i].M08_2 = vol[i - start];
                }
                else if (month == "09" && period == period1)
                {
                    volList[i].M09_1 = vol[i - start];
                }
                else if (month == "09" && period == period2)
                {
                    volList[i].M09_2 = vol[i - start];
                }
                else if (month == "11" && period == period1)
                {
                    volList[i].M11_1 = vol[i - start];
                }
                else if (month == "11" && period == period2)
                {
                    volList[i].M11_2 = vol[i - start];
                }
                else if (month == "12" && period == period1)
                {
                    volList[i].M12_1 = vol[i - start];
                }
                else if (month == "12" && period == period2)
                {
                    volList[i].M12_2 = vol[i - start];
                }
            }
        }
 public T Set <T>(string cacheKey, T cacheObject, Volatility volatility) where T : class
 {
     return(cacheRepository.Set <T>(cacheKey, cacheObject, GetDuration(volatility)));
 }
 public Volatility Randomrize(Volatility volatility)
 {
     _calledTimes++;
     return(volatility);
 }
示例#18
0
 public WholeDice(double min, double max, Volatility volatility)
     : base(min,max,volatility)
 {
 }