示例#1
0
        private IEnumerable <TimedValue <DateTime, double> > FillMissingDates(IPriceSeries series)
        {
            yield return(series.First());

            var expectedPoint = series.First();

            foreach (var point in series.Skip(1))
            {
                while (true)
                {
                    expectedPoint = new TimedValue <DateTime, double>(expectedPoint.Time.AddDays(1), expectedPoint.Value);
                    if (expectedPoint.Time == point.Time)
                    {
                        break;
                    }

                    if (expectedPoint.Time.DayOfWeek == DayOfWeek.Saturday ||
                        expectedPoint.Time.DayOfWeek == DayOfWeek.Sunday)
                    {
                        // no trading at weekends usually
                        continue;
                    }

                    // missing price at this date - take over the last one we have
                    yield return(expectedPoint);
                }

                expectedPoint = point;
                yield return(point);
            }
        }
示例#2
0
        public ISignalSeries Generate( IPriceSeries referencePrices, IPriceSeries indicatorPoints )
        {
            var signals = new List<TimedSignal>();

            var prevPrice = referencePrices.First();
            TimedValue<DateTime, double> prevIndicatorPoint = null;
            foreach ( var price in referencePrices.Skip( 1 ) )
            {
                var indicatorPoint = indicatorPoints.TryGet( price.Time );
                if ( indicatorPoint == null )
                {
                    signals.Add( new TimedSignal( price.Time, Signal.None ) );
                    continue;
                }

                if ( prevIndicatorPoint == null )
                {
                    prevIndicatorPoint = indicatorPoint;
                    continue;
                }

                if ( prevPrice.Value < prevIndicatorPoint.Value && price.Value > indicatorPoint.Value )
                {
                    signals.Add( new TimedSignal( indicatorPoint.Time, new BuySignal() ) );
                }
                else if ( prevPrice.Value > prevIndicatorPoint.Value && price.Value < indicatorPoint.Value )
                {
                    signals.Add( new TimedSignal( indicatorPoint.Time, new SellSignal() ) );
                }

                prevPrice = price;
            }

            return new SignalSeries( referencePrices, indicatorPoints.Identifier, signals );
        }
        public ISignalSeries Generate(IPriceSeries referencePrices, IPriceSeries indicatorPoints)
        {
            var signals = new List <TimedSignal>();

            var prevPrice = referencePrices.First();
            TimedValue <DateTime, double> prevIndicatorPoint = null;

            foreach (var price in referencePrices.Skip(1))
            {
                var indicatorPoint = indicatorPoints.TryGet(price.Time);
                if (indicatorPoint == null)
                {
                    signals.Add(new TimedSignal(price.Time, Signal.None));
                    continue;
                }

                if (prevIndicatorPoint == null)
                {
                    prevIndicatorPoint = indicatorPoint;
                    continue;
                }

                if (prevPrice.Value < prevIndicatorPoint.Value && price.Value > indicatorPoint.Value)
                {
                    signals.Add(new TimedSignal(indicatorPoint.Time, new BuySignal()));
                }
                else if (prevPrice.Value > prevIndicatorPoint.Value && price.Value < indicatorPoint.Value)
                {
                    signals.Add(new TimedSignal(indicatorPoint.Time, new SellSignal()));
                }

                prevPrice = price;
            }

            return(new SignalSeries(referencePrices, indicatorPoints.Identifier, signals));
        }
        public ISignalSeries Generate(IPriceSeries referencePrices, IPriceSeries indicatorPoints)
        {
            var signals            = new List <TimedSignal>();
            var signalDaysInterval = ClosedInterval.FromOffsetLength(MinDaysAfterCutForSignal, KeepSignalForMaxDays);

            var prevPrice = referencePrices.First();
            TimedValue <DateTime, double> prevIndicatorPoint = null;
            TimedSignal activeSignal = null;

            foreach (var price in referencePrices.Skip(1))
            {
                var indicatorPoint = indicatorPoints.TryGet(price.Time);
                if (indicatorPoint == null)
                {
                    signals.Add(new TimedSignal(price.Time, Signal.None));
                    continue;
                }

                if (prevIndicatorPoint == null)
                {
                    prevIndicatorPoint = indicatorPoint;
                    continue;
                }

                if (prevPrice.Value < prevIndicatorPoint.Value && price.Value > indicatorPoint.Value)
                {
                    var signal = new TimedSignal(indicatorPoint.Time, new BuySignal());

                    if (signalDaysInterval.IsEmpty)
                    {
                        signals.Add(signal);
                    }
                    else
                    {
                        activeSignal = signal;
                    }
                }
                else if (prevPrice.Value > prevIndicatorPoint.Value && price.Value < indicatorPoint.Value)
                {
                    var signal = new TimedSignal(indicatorPoint.Time, new SellSignal());

                    if (signalDaysInterval.IsEmpty)
                    {
                        signals.Add(signal);
                    }
                    else
                    {
                        activeSignal = signal;
                    }
                }

                if (activeSignal != null)
                {
                    // we have a cut signal -> handle it
                    int daysSinceCut = (int)Math.Round((price.Time - activeSignal.Time).TotalDays);

                    // if we are in defined range -> add the signal
                    if (signalDaysInterval.Includes(daysSinceCut))
                    {
                        signals.Add(new TimedSignal(indicatorPoint.Time, activeSignal.Value));
                    }

                    if (daysSinceCut > signalDaysInterval.Max)
                    {
                        // left the interval -> reset the signal
                        activeSignal = null;
                    }
                }

                prevPrice          = price;
                prevIndicatorPoint = indicatorPoint;
            }

            return(new SignalSeries(referencePrices, indicatorPoints.Identifier, signals));
        }
        public ISignalSeries Generate( IPriceSeries referencePrices, IPriceSeries indicatorPoints )
        {
            var signals = new List<TimedSignal>();
            var signalDaysInterval = ClosedInterval.FromOffsetLength( MinDaysAfterCutForSignal, KeepSignalForMaxDays );

            var prevPrice = referencePrices.First();
            TimedValue<DateTime,double> prevIndicatorPoint = null;
            TimedSignal activeSignal = null;
            foreach ( var price in referencePrices.Skip( 1 ) )
            {
                var indicatorPoint = indicatorPoints.TryGet( price.Time );
                if ( indicatorPoint == null )
                {
                    signals.Add( new TimedSignal( price.Time, Signal.None ) );
                    continue;
                }

                if ( prevIndicatorPoint == null )
                {
                    prevIndicatorPoint = indicatorPoint;
                    continue;
                }

                if ( prevPrice.Value < prevIndicatorPoint.Value && price.Value > indicatorPoint.Value )
                {
                    var signal = new TimedSignal( indicatorPoint.Time, new BuySignal() );

                    if ( signalDaysInterval.IsEmpty )
                    {
                        signals.Add( signal );
                    }
                    else
                    {
                        activeSignal = signal;
                    }
                }
                else if ( prevPrice.Value > prevIndicatorPoint.Value && price.Value < indicatorPoint.Value )
                {
                    var signal = new TimedSignal( indicatorPoint.Time, new SellSignal() );

                    if ( signalDaysInterval.IsEmpty )
                    {
                        signals.Add( signal );
                    }
                    else
                    {
                        activeSignal = signal;
                    }
                }

                if ( activeSignal != null )
                {
                    // we have a cut signal -> handle it
                    int daysSinceCut = (int)Math.Round( ( price.Time - activeSignal.Time ).TotalDays );

                    // if we are in defined range -> add the signal
                    if ( signalDaysInterval.Includes( daysSinceCut ) )
                    {
                        signals.Add( new TimedSignal( indicatorPoint.Time, activeSignal.Value ) );
                    }

                    if ( daysSinceCut > signalDaysInterval.Max )
                    {
                        // left the interval -> reset the signal
                        activeSignal = null;
                    }
                }

                prevPrice = price;
                prevIndicatorPoint = indicatorPoint;
            }

            return new SignalSeries( referencePrices, indicatorPoints.Identifier, signals );
        }