Exemplo n.º 1
0
        public static Equity Transform(this Equity sourceEquity, PeriodOption targetPeriod)
        {
            if (sourceEquity.Period == targetPeriod)
            {
                return(sourceEquity);
            }

            if (!IsTransformationValid(sourceEquity.Period, targetPeriod))
            {
                throw new InvalidTransformationException(sourceEquity.Period, targetPeriod);
            }

            var candles        = new List <Candle>();
            var periodInstance = targetPeriod.CreateInstance();

            DateTime startTime = sourceEquity.First().DateTime;

            while (startTime <= sourceEquity.Last().DateTime)
            {
                var nextStartTime = periodInstance.NextTimestamp(startTime);
                if (periodInstance.IsTimestamp(startTime))
                {
                    var candle = ComputeCandle(sourceEquity, startTime, nextStartTime);
                    if (candle != null)
                    {
                        candles.Add(candle);
                    }
                }
                startTime = nextStartTime;
            }

            return(new Equity(sourceEquity.Name, candles, targetPeriod));
        }
Exemplo n.º 2
0
        public void AddChange(double change, DateTime?dt = null)
        {
            if (Equity.Count == 0)
            {
                Returns.Add(0);
                Equity.Add(change);
            }
            else
            {
                Returns.Add(Equity.Last() != 0 ? change / Equity.Last() : 0);
                Equity.Add(Equity[Equity.Count - 1] + change);
            }

            Changes.Add(change);
            _maxEquity = Math.Max(_maxEquity, Equity.Last());
            DrawdownPct.Add(_maxEquity == 0 ? 0 : Equity.Last() / _maxEquity - 1);
            DrawdownAmt.Add(Equity.Last() - _maxEquity);

            if (dt != null)
            {
                AddDrawdownLengths(dt.Value);
            }

            Dates.Add(dt);
        }
Exemplo n.º 3
0
        public void AddValue(double value, DateTime?dt = null)
        {
            if (Equity.Count == 0)
            {
                Returns.Add(0);
                Changes.Add(0);
            }
            else
            {
                Returns.Add(Equity.Last() != 0 ? value / Equity.Last() - 1 : 0);
                Changes.Add(value - Equity[Equity.Count - 1]);
            }

            Equity.Add(value);
            _maxEquity = Math.Max(_maxEquity, value);
            DrawdownPct.Add(_maxEquity == 0 ? 0 : value / _maxEquity - 1);
            DrawdownAmt.Add(Equity.Last() - _maxEquity);

            if (dt != null)
            {
                AddDrawdownLengths(dt.Value);
            }

            Dates.Add(dt);
        }
Exemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ret">Net Return</param>
        public void AddReturn(double ret, DateTime?dt = null)
        {
            if (Equity.Count == 0)
            {
                Equity.Add(1);
                Changes.Add(0);
            }
            else
            {
                Equity.Add(Equity.Last() * (1 + ret));
                Changes.Add(Equity[Equity.Count - 1] - Equity[Equity.Count - 2]);
            }

            _maxEquity = Math.Max(_maxEquity, Equity.Last());
            Returns.Add(ret);
            DrawdownPct.Add(_maxEquity == 0 ? 0 : Equity.Last() / _maxEquity - 1);
            DrawdownAmt.Add(Equity.Last() - _maxEquity);

            if (dt != null)
            {
                AddDrawdownLengths(dt.Value);
            }

            Dates.Add(dt);
        }