Ejemplo 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));
        }