Exemple #1
0
        /// <summary>
        /// Gets all of the splits and dividends represented by this factor file
        /// </summary>
        /// <param name="symbol">The symbol to ues for the dividend and split objects</param>
        /// <param name="exchangeHours">Exchange hours used for resolving the previous trading day</param>
        /// <returns>All splits and diviends represented by this factor file in chronological order</returns>
        public List <BaseData> GetSplitsAndDividends(Symbol symbol, SecurityExchangeHours exchangeHours)
        {
            var dividendsAndSplits = new List <BaseData>();

            if (SortedFactorFileData.Count == 0)
            {
                Log.Trace($"{symbol} has no factors!");
                return(dividendsAndSplits);
            }

            var futureFactorFileRow = SortedFactorFileData.Last().Value;

            for (var i = SortedFactorFileData.Count - 2; i >= 0; i--)
            {
                var row      = SortedFactorFileData.Values[i];
                var dividend = row.GetDividend(futureFactorFileRow, symbol, exchangeHours);
                if (dividend.Distribution != 0m)
                {
                    dividendsAndSplits.Add(dividend);
                }

                var split = row.GetSplit(futureFactorFileRow, symbol, exchangeHours);
                if (split.SplitFactor != 1m)
                {
                    dividendsAndSplits.Add(split);
                }

                futureFactorFileRow = row;
            }

            return(dividendsAndSplits.OrderBy(d => d.Time.Date).ToList());
        }
Exemple #2
0
        /// <summary>
        /// Creates a new factor file with the specified data applied.
        /// Only <see cref="Dividend"/> and <see cref="Split"/> data types
        /// will be used.
        /// </summary>
        /// <param name="data">The data to apply</param>
        /// <param name="exchangeHours">Exchange hours used for resolving the previous trading day</param>
        /// <returns>A new factor file that incorporates the specified dividend</returns>
        public CorporateFactorProvider Apply(List <BaseData> data, SecurityExchangeHours exchangeHours)
        {
            if (data.Count == 0)
            {
                return(this);
            }

            var factorFileRows = new List <CorporateFactorRow>();
            var firstEntry     = SortedFactorFileData.First().Value.First();
            var lastEntry      = SortedFactorFileData.Last().Value.First();

            factorFileRows.Add(lastEntry);

            var splitsAndDividends = GetSplitsAndDividends(data[0].Symbol, exchangeHours);

            var combinedData = splitsAndDividends.Concat(data)
                               .DistinctBy(e => $"{e.GetType().Name}{e.Time.ToStringInvariant(DateFormat.EightCharacter)}")
                               .OrderByDescending(d => d.Time.Date);

            foreach (var datum in combinedData)
            {
                CorporateFactorRow nextEntry = null;
                var split    = datum as Split;
                var dividend = datum as Dividend;
                if (dividend != null)
                {
                    nextEntry = lastEntry.Apply(dividend, exchangeHours);
                    lastEntry = nextEntry;
                }
                else if (split != null)
                {
                    nextEntry = lastEntry.Apply(split, exchangeHours);
                    lastEntry = nextEntry;
                }

                if (nextEntry != null)
                {
                    // overwrite the latest entry -- this handles splits/dividends on the same date
                    if (nextEntry.Date == factorFileRows.Last().Date)
                    {
                        factorFileRows[factorFileRows.Count - 1] = nextEntry;
                    }
                    else
                    {
                        factorFileRows.Add(nextEntry);
                    }
                }
            }

            var firstFactorFileRow = new CorporateFactorRow(firstEntry.Date, factorFileRows.Last().PriceFactor, factorFileRows.Last().SplitFactor, firstEntry.ReferencePrice == 0 ? 0 : firstEntry.ReferencePrice);
            var existing           = factorFileRows.FindIndex(row => row.Date == firstFactorFileRow.Date);

            if (existing == -1)
            {
                // only add it if not present
                factorFileRows.Add(firstFactorFileRow);
            }

            return(new CorporateFactorProvider(Permtick, factorFileRows, FactorFileMinimumDate));
        }
Exemple #3
0
        /// <summary>
        /// Creates a new factor file with the specified data applied.
        /// Only <see cref="Dividend"/> and <see cref="Split"/> data types
        /// will be used.
        /// </summary>
        /// <param name="data">The data to apply</param>
        /// <param name="exchangeHours">Exchange hours used for resolving the previous trading day</param>
        /// <returns>A new factor file that incorporates the specified dividend</returns>
        public FactorFile Apply(List <BaseData> data, SecurityExchangeHours exchangeHours)
        {
            if (data.Count == 0)
            {
                return(this);
            }

            var factorFileRows = new List <FactorFileRow>();
            var lastEntry      = SortedFactorFileData.Last().Value;

            factorFileRows.Add(lastEntry);

            var combinedData = GetSplitsAndDividends(data[0].Symbol, exchangeHours).Concat(data)
                               .OrderByDescending(d => d.Time.Date);

            foreach (var datum in combinedData)
            {
                FactorFileRow nextEntry = null;
                var           split     = datum as Split;
                var           dividend  = datum as Dividend;
                if (dividend != null)
                {
                    nextEntry = lastEntry.Apply(dividend, exchangeHours);
                    lastEntry = nextEntry;
                }
                else if (split != null)
                {
                    nextEntry = lastEntry.Apply(split, exchangeHours);
                    lastEntry = nextEntry;
                }

                if (nextEntry != null)
                {
                    // overwrite the latest entry -- this handles splits/dividends on the same date
                    if (nextEntry.Date == factorFileRows.Last().Date)
                    {
                        factorFileRows[factorFileRows.Count - 1] = nextEntry;
                    }
                    else
                    {
                        factorFileRows.Add(nextEntry);
                    }
                }
            }

            return(new FactorFile(Permtick, factorFileRows, FactorFileMinimumDate));
        }