/// <summary> /// Creates a new iterator instance. /// </summary> /// <param name="sequence">An existing sequence instance.</param> /// <exception cref="System.ArgumentNullException">if sequence is null.</exception> public QuotesRangeSingleIterator(QuotesRangeSingleSequence sequence) { if (sequence == null) throw new ArgumentNullException(nameof(sequence), "Sequence parameter can not be null."); this.Sequence = sequence; this.iterator = new QuotesSingleIterator(sequence.Storage, sequence.Symbol, sequence.StartTime, sequence.EndTime, sequence.Depth); var range = new Range<Quote>(sequence.LowerBound, sequence.UpperBound); var index = sequence.LowerBound; for (; (index < sequence.UpperBound) && this.iterator.Continue; ++index) { range[index] = this.iterator.Current; this.iterator.NextTick(); } if (index == sequence.UpperBound) { this.Current = range; this.Continue = true; } else { this.Continue = false; } }
MathSequenceStat CalculateSpread(string symbol, DateTime startTime, DateTime endTime) { do { try { var sequence = new QuotesRangeSingleSequence(this.Storage.Online, symbol, startTime, endTime, 1, 2); var timeInterval = (endTime - startTime).TotalSeconds; var totalDuration = 0D; var sumOfSpreads = 0D; var sumOfSpreadsSquares = 0D; var countTick = 0; foreach (var currQuoteRange in sequence) { var currentQuote = currQuoteRange[0]; var nextQuote = currQuoteRange[1]; var spread = currentQuote.Spread; var spreadInPips = this.PipsFromPrice(symbol, spread); var duration = (nextQuote.CreatingTime - currentQuote.CreatingTime).TotalSeconds; if (duration > 3600) continue; totalDuration += duration; var weightCoefficient = duration / timeInterval; sumOfSpreads += weightCoefficient * spread; sumOfSpreadsSquares += weightCoefficient * spread * spread; countTick++; if (currentQuote.CreatingTime.Day != nextQuote.CreatingTime.Day) Console.WriteLine("Now: {3};Symbol: {2};ProcessingTime: {0}; Ticks {1}", nextQuote.CreatingTime.ToString(), countTick, symbol, DateTime.Now); } // we need correction, because initial time interval can be different than actual time interval var correctionCoefficient = timeInterval / totalDuration; sumOfSpreads *= correctionCoefficient; sumOfSpreadsSquares *= correctionCoefficient; var sigma = sumOfSpreadsSquares - sumOfSpreads * sumOfSpreads; if (sigma > 0) sigma = Math.Sqrt(sigma); else sigma = 0; return new MathSequenceStat(this.PipsFromPrice(symbol, sumOfSpreads), this.PipsFromPrice(symbol, sigma), countTick); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.WriteLine("Trying again"); } } while (true); }
MathSequenceStat CalculateSpread(string symbol, DateTime startTime, DateTime endTime) { do { try { var sequence = new QuotesRangeSingleSequence(this.Storage.Online, symbol, startTime, endTime, 1, 2); var timeInterval = (endTime - startTime).TotalSeconds; var totalDuration = 0D; var sumOfSpreads = 0D; var sumOfSpreadsSquares = 0D; var countTick = 0; foreach (var currQuoteRange in sequence) { var currentQuote = currQuoteRange[0]; var nextQuote = currQuoteRange[1]; var spread = currentQuote.Spread; var spreadInPips = this.PipsFromPrice(symbol, spread); var duration = (nextQuote.CreatingTime - currentQuote.CreatingTime).TotalSeconds; if (duration > 3600) { continue; } totalDuration += duration; var weightCoefficient = duration / timeInterval; sumOfSpreads += weightCoefficient * spread; sumOfSpreadsSquares += weightCoefficient * spread * spread; countTick++; if (currentQuote.CreatingTime.Day != nextQuote.CreatingTime.Day) { Console.WriteLine("Now: {3};Symbol: {2};ProcessingTime: {0}; Ticks {1}", nextQuote.CreatingTime.ToString(), countTick, symbol, DateTime.Now); } } // we need correction, because initial time interval can be different than actual time interval var correctionCoefficient = timeInterval / totalDuration; sumOfSpreads *= correctionCoefficient; sumOfSpreadsSquares *= correctionCoefficient; var sigma = sumOfSpreadsSquares - sumOfSpreads * sumOfSpreads; if (sigma > 0) { sigma = Math.Sqrt(sigma); } else { sigma = 0; } return(new MathSequenceStat(this.PipsFromPrice(symbol, sumOfSpreads), this.PipsFromPrice(symbol, sigma), countTick)); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.WriteLine("Trying again"); } } while (true); }