public static bool TryFindBarExactOrOneAfter(this IBars target, BarPositionDescriptor barPositionDescriptor, out int barIndex, out bool isExactMatch) { int firstIndex, lastIndex; TimeStamp timeStampFound; if (!target.TryFindTimeStampAtOrAfter(barPositionDescriptor.TimeStamp, out timeStampFound, out isExactMatch, out firstIndex, out lastIndex)) { barIndex = -1; return(false); } if (!isExactMatch) { barIndex = firstIndex; return(true); } isExactMatch = target.TryGetIndexOfNthClose(firstIndex, lastIndex, barPositionDescriptor.Close, barPositionDescriptor.SequenceNumber, out barIndex); if (isExactMatch) { return(true); } barIndex = Math.Min(target.Count - 1, lastIndex + 1); return(true); }
/// <summary> /// Searches an IBars series to find the index of a bar exactly matching the given barPositionDescriptor, returning true if the exactly-matching bar was found. /// If bars at the given timestamp and close do exist, but not enough to reach the barPositionDescriptor.SequenceNumber, 'barIndex' will contain the index of /// the last bar with the right timestamp and close, even though 'false' is returned. /// </summary> /// <param name="barIndex">If the method returns true, barIndex is the index of the bar exactly matching the barPositionDescriptor. /// If the method returns false, barIndex will be the index of the last bar with the correct timestamp and close, if there were not enough bars existing to match barPositionDescriptor.SequenceNumber. /// If there were no bars with the correct timestamp and close, barIndex will be -1</param> public static bool TryFindBarExact(this IBars target, BarPositionDescriptor barPositionDescriptor, out int barIndex) { barIndex = -1; int firstIndex, lastIndex; if (!target.TryFindTimeStampAt(barPositionDescriptor.TimeStamp, out firstIndex, out lastIndex)) { return(false); } return(target.TryGetIndexOfNthClose(firstIndex, lastIndex, barPositionDescriptor.Close, barPositionDescriptor.SequenceNumber, out barIndex)); }
/// <summary> /// Gets a BarPositionDescriptor that can be used to search for the equivalent-position bar in another IBars sequence. /// </summary> public static BarPositionDescriptor GetBarPositionDescriptor(this IBars target, int barIndex) { var result = new BarPositionDescriptor { TimeStamp = target.GetTimeStamp(barIndex), Close = target.GetClose(barIndex), SequenceNumber = 0, }; for (barIndex--; barIndex >= 0 && target.GetTimeStamp(barIndex) == result.TimeStamp; barIndex--) { if (target.GetClose(barIndex) == result.Close) { result.SequenceNumber++; } } return(result); }