Пример #1
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));
        }
Пример #2
0
        /// <summary>
        /// Writes this factor file data to an enumerable of csv lines
        /// </summary>
        /// <returns>An enumerable of lines representing this factor file</returns>
        public IEnumerable <string> ToCsvLines()
        {
            if (FactorFileMinimumDate != null)
            {
                var min = SortedFactorFileData.First().Value;
                yield return($"{FactorFileMinimumDate:yyyyMMdd},{min.PriceFactor},{min.SplitFactor}");
            }

            foreach (var kvp in SortedFactorFileData)
            {
                yield return($"{kvp.Key:yyyyMMdd},{kvp.Value.PriceFactor},{kvp.Value.SplitFactor}");
            }
        }