/// <summary> /// Gets a copy of this data from a date, to a date. /// </summary> /// <param name="start">Date to start from</param> /// <param name="end">Date to end at</param> /// <returns>New object with the data from the requested dates</returns> public TickerData SubSet(DateTime start, DateTime end) { TickerData copyData = new TickerData(TickerAndExchange); int copyStartIndex = -1; int copyEndIndex = -1; for (int i = 0; i < Dates.Count; i++) { if (copyStartIndex == -1 && Dates[i] >= start) { copyStartIndex = i; } if (copyEndIndex == -1 && Dates[i] >= end) { copyEndIndex = i; } if (copyStartIndex > -1 && copyEndIndex > -1) { break; } } if (copyStartIndex != -1) { if (copyEndIndex == -1 && end > Dates.Last()) { copyEndIndex = Dates.Count - 1; } int amountToCopy = (copyEndIndex - copyStartIndex) + 1; copyData.Dates.AddRange(Dates.GetRange(copyStartIndex, amountToCopy)); copyData.Open.AddRange(Open.GetRange(copyStartIndex, amountToCopy)); copyData.Close.AddRange(Close.GetRange(copyStartIndex, amountToCopy)); copyData.High.AddRange(High.GetRange(copyStartIndex, amountToCopy)); copyData.Low.AddRange(Low.GetRange(copyStartIndex, amountToCopy)); copyData.Volume.AddRange(Volume.GetRange(copyStartIndex, amountToCopy)); // Extras copyData.Typical.AddRange(Typical.GetRange(copyStartIndex, amountToCopy)); copyData.Median.AddRange(Median.GetRange(copyStartIndex, amountToCopy)); copyData.HigherTimeframeTrend.AddRange(HigherTimeframeTrend.GetRange(copyStartIndex, amountToCopy)); for (int i = 0; i < HigherTimeframeValueStrings.Length; i++) { string key = HigherTimeframeValueStrings[i]; copyData.HigherTimeframeValues[key].AddRange(HigherTimeframeValues[key].GetRange(copyStartIndex, amountToCopy)); } copyData.Start = copyData.Dates[0]; copyData.End = copyData.Dates[copyData.Dates.Count - 1]; copyData.NumBars = copyData.Dates.Count; copyData.SaveDates(); } return(copyData); }
/// <summary> /// Appends the other data to the data already in this class. It doesn't overwrite data /// for existing dates, so it can only prepend data to the start or append to the end. /// </summary> /// <param name="otherData">Data to append</param> public void AppendData(TickerData otherData) { // Prepend if (otherData.Start < Start) { // Find the index in the other data where this data starts. int copyEndIndex; for (copyEndIndex = 0; copyEndIndex < otherData.Dates.Count; copyEndIndex++) { if (otherData.Dates[copyEndIndex] >= Start) { break; } } // Insert all the new data at the front of the existing data. Dates.InsertRange(0, otherData.Dates.GetRange(0, copyEndIndex)); Open.InsertRange(0, otherData.Open.GetRange(0, copyEndIndex)); Close.InsertRange(0, otherData.Close.GetRange(0, copyEndIndex)); High.InsertRange(0, otherData.High.GetRange(0, copyEndIndex)); Low.InsertRange(0, otherData.Low.GetRange(0, copyEndIndex)); Volume.InsertRange(0, otherData.Volume.GetRange(0, copyEndIndex)); // Extras Typical.InsertRange(0, otherData.Typical.GetRange(0, copyEndIndex)); Median.InsertRange(0, otherData.Median.GetRange(0, copyEndIndex)); HigherTimeframeTrend.InsertRange(0, otherData.HigherTimeframeTrend.GetRange(0, copyEndIndex)); for (int i = 0; i < HigherTimeframeValueStrings.Length; i++) { string key = HigherTimeframeValueStrings[i]; HigherTimeframeValues[key].InsertRange(0, otherData.HigherTimeframeValues[key].GetRange(0, copyEndIndex)); } } // Append if (otherData.End > End) { // Find the index where the other data passes the end of the existing data. int copyStartIndex; for (copyStartIndex = 0; copyStartIndex < otherData.Dates.Count; copyStartIndex++) { if (otherData.Dates[copyStartIndex] > End) { break; } } // Append the new data to the end of the existing data. Dates.AddRange(otherData.Dates.GetRange(copyStartIndex, otherData.Dates.Count - copyStartIndex)); Open.AddRange(otherData.Open.GetRange(copyStartIndex, otherData.Open.Count - copyStartIndex)); Close.AddRange(otherData.Close.GetRange(copyStartIndex, otherData.Close.Count - copyStartIndex)); High.AddRange(otherData.High.GetRange(copyStartIndex, otherData.High.Count - copyStartIndex)); Low.AddRange(otherData.Low.GetRange(copyStartIndex, otherData.Low.Count - copyStartIndex)); Volume.AddRange(otherData.Volume.GetRange(copyStartIndex, otherData.Volume.Count - copyStartIndex)); // Extras Typical.AddRange(otherData.Typical.GetRange(copyStartIndex, otherData.Typical.Count - copyStartIndex)); Median.AddRange(otherData.Median.GetRange(copyStartIndex, otherData.Median.Count - copyStartIndex)); HigherTimeframeTrend.AddRange(otherData.HigherTimeframeTrend.GetRange(copyStartIndex, otherData.HigherTimeframeTrend.Count - copyStartIndex)); for (int i = 0; i < HigherTimeframeValueStrings.Length; i++) { string key = HigherTimeframeValueStrings[i]; HigherTimeframeValues[key].AddRange(otherData.HigherTimeframeValues[key].GetRange(copyStartIndex, otherData.HigherTimeframeValues[key].Count - copyStartIndex)); } } Start = Dates[0]; End = Dates[Dates.Count - 1]; NumBars = Dates.Count; SaveDates(); }