Exemplo n.º 1
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 );
        }
Exemplo n.º 2
0
            private IndicatorResult GenerateResult(IPriceSeries shortTermPoints, IPriceSeries longTermPoints)
            {
                var signals = Indicator.SignalGenerationStrategy.Generate(shortTermPoints, longTermPoints);

                var reportData = new GenericIndicatorReport.Data()
                {
                    SignalOfDayUnderAnalysis = GetCurrentSignal(signals, longTermPoints),
                    Prices  = myPrices,
                    Signals = signals
                };

                reportData.Points[Indicator.ShortTerm.Name] = shortTermPoints;
                reportData.Points[Indicator.LongTerm.Name]  = longTermPoints;

                var result = new IndicatorResult(Indicator.Name, Stock, reportData.SignalOfDayUnderAnalysis);

                if (Context.GenerateHistoricSignals)
                {
                    result.Signals = signals;
                }

                result.Report = new DoubleCrossoverReport(Indicator, Stock, reportData);

                return(result);
            }
Exemplo n.º 3
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);
            }
        }
Exemplo n.º 4
0
        public StockPriceChart( string name, StockHandle stock, IPriceSeries prices )
        {
            Name = name;
            Stock = stock;
            Prices = new PriceSeries( prices );

            IndicatorPoints = new Dictionary<string, IPriceSeries>();
            Signals = SignalSeries.Null;
        }
Exemplo n.º 5
0
        public StockPriceChart(string name, StockHandle stock, IPriceSeries prices)
        {
            Name   = name;
            Stock  = stock;
            Prices = new PriceSeries(prices);

            IndicatorPoints = new Dictionary <string, IPriceSeries>();
            Signals         = SignalSeries.Null;
        }
Exemplo n.º 6
0
        private IPriceSeries ApplyOperators(IPriceSeries series)
        {
            foreach (var op in Operators)
            {
                series = op.Apply(series);
            }

            return(series);
        }
Exemplo n.º 7
0
        private IPriceSeries ApplyOperators( IPriceSeries series )
        {
            foreach ( var op in Operators )
            {
                series = op.Apply( series );
            }

            return series;
        }
Exemplo n.º 8
0
            public IndicatorResult Calculate()
            {
                myPrices = Indicator.Prices.ForStock(Stock);

                var points = CalculatePoints();

                var result = GenerateResult(points);

                return(result);
            }
Exemplo n.º 9
0
            public IndicatorResult Calculate()
            {
                myPrices = Indicator.Prices.ForStock(Stock);

                var shortTermPoints = Indicator.ShortTerm.Calculate(myPrices);
                var longTermPoints  = Indicator.LongTerm.Calculate(myPrices);

                var result = GenerateResult(shortTermPoints, longTermPoints);

                return(result);
            }
Exemplo n.º 10
0
        public SystemResult( string system, StockHandle stock, IPriceSeries prices, IndicatorResult indicatorResult )
        {
            System = system;
            Stock = stock;
            Prices = prices;

            Signal = indicatorResult.Signal;
            ExpectedGain = indicatorResult.ExpectedGain;
            GainRiskRatio = indicatorResult.GainRiskRatio;
            Signals = indicatorResult.Signals;
        }
Exemplo n.º 11
0
        // expect: series already sorted by date
        public Curve AddCurve(string name, IPriceSeries points)
        {
            var curve = new Curve(name, points);

            curve.ViewPort = myViewPort;
            curve.PreRenderingOperators.AddRange(mySettings.CurvesPreRenderingOperators);

            myCurves.Add(curve);

            return(curve);
        }
Exemplo n.º 12
0
        public SystemResult(string system, StockHandle stock, IPriceSeries prices, IndicatorResult indicatorResult)
        {
            System = system;
            Stock  = stock;
            Prices = prices;

            Signal        = indicatorResult.Signal;
            ExpectedGain  = indicatorResult.ExpectedGain;
            GainRiskRatio = indicatorResult.GainRiskRatio;
            Signals       = indicatorResult.Signals;
        }
Exemplo n.º 13
0
        public IPriceSeries Apply(IPriceSeries series)
        {
            if (!series.Any())
            {
                return(series);
            }

            var descriptor = new ObjectDescriptor("InterpolatedMissingDates");
            var seriesId   = series.Identifier.Modify(descriptor);

            return(PriceSeries.FromSortedSet(seriesId, FillMissingDates(series)));
        }
Exemplo n.º 14
0
        public IPriceSeries Apply( IPriceSeries series )
        {
            double groupInterval = (double)series.Count / (double)myMaxPoints;
            if ( groupInterval <= 1 )
            {
                return series;
            }

            var descriptor = new ObjectDescriptor( "ThinOut", ObjectDescriptor.Param( "MaxCount", myMaxPoints ) );
            var seriesId = series.Identifier.Modify( descriptor );
            return PriceSeries.FromSortedSet( seriesId, GroupPointsByAverage( series, (int)Math.Ceiling( groupInterval ) ) );
        }
Exemplo n.º 15
0
        public IPriceSeries Apply(IPriceSeries series)
        {
            double groupInterval = (double)series.Count / (double)myMaxPoints;

            if (groupInterval <= 1)
            {
                return(series);
            }

            var descriptor = new ObjectDescriptor("ThinOut", ObjectDescriptor.Param("MaxCount", myMaxPoints));
            var seriesId   = series.Identifier.Modify(descriptor);

            return(PriceSeries.FromSortedSet(seriesId, GroupPointsByAverage(series, (int)Math.Ceiling(groupInterval))));
        }
Exemplo n.º 16
0
        public IPriceSeries Calculate( IPriceSeries prices )
        {
            var points = new List<SimplePrice>();

            for ( int i = NumDays; i < prices.Count; ++i )
            {
                var pricesRange = new PriceSeriesRange( prices, ClosedInterval.FromOffsetLength( i - NumDays, NumDays ) );

                double value = pricesRange.Average( p => p.Value );

                var point = new SimplePrice( prices[ i ].Time, value );
                points.Add( point );
            }

            var descriptor = new ObjectDescriptor( "SMA", ObjectDescriptor.Param( "NumDays", NumDays ) );
            var seriesId = prices.Identifier.Derive( descriptor );
            return new PriceSeries( seriesId, points );
        }
Exemplo n.º 17
0
        public IPriceSeries Calculate(IPriceSeries prices)
        {
            var points = new List <SimplePrice>();

            for (int i = NumDays; i < prices.Count; ++i)
            {
                var pricesRange = new PriceSeriesRange(prices, ClosedInterval.FromOffsetLength(i - NumDays, NumDays));

                double value = pricesRange.Average(p => p.Value);

                var point = new SimplePrice(prices[i].Time, value);
                points.Add(point);
            }

            var descriptor = new ObjectDescriptor("SMA", ObjectDescriptor.Param("NumDays", NumDays));
            var seriesId   = prices.Identifier.Derive(descriptor);

            return(new PriceSeries(seriesId, points));
        }
Exemplo n.º 18
0
        private static IEnumerable<TimedValue<DateTime, double>> GroupPointsByAverage( IPriceSeries series, int averageInterval )
        {
            var pointGroup = new List<TimedValue<DateTime, double>>();
            foreach ( var point in series )
            {
                pointGroup.Add( point );

                if ( pointGroup.Count == averageInterval )
                {
                    yield return Average( pointGroup );
                    pointGroup.Clear();
                }
            }

            if ( pointGroup.Any() )
            {
                yield return Average( pointGroup );
            }
        }
Exemplo n.º 19
0
            protected Signal GetCurrentSignal(ISignalSeries signals, IPriceSeries indicatorPoints)
            {
                if (indicatorPoints.Last().Time < Context.DateUnderAnalysis)
                {
                    // no data for the DUA
                    return(Signal.None);
                }

                // we had data so default is "neutral"
                Signal currentSignal = new NeutralSignal();

                var currentTimedSignal = signals.FirstOrDefault(s => s.Time == Context.DateUnderAnalysis);

                if (currentTimedSignal != null)
                {
                    // found a "better" signal for the DUA - take this one
                    currentSignal = currentTimedSignal.Value;
                }

                return(currentSignal);
            }
Exemplo n.º 20
0
            private IndicatorResult GenerateResult(IPriceSeries points)
            {
                var signals = Indicator.SignalGenerationStrategy.Generate(myPrices, points);

                var reportData = new GenericIndicatorReport.Data()
                {
                    SignalOfDayUnderAnalysis = GetCurrentSignal(signals, points),
                    Prices  = myPrices,
                    Signals = signals
                };

                reportData.Points[Indicator.Name] = points;

                var result = new IndicatorResult(Indicator.Name, Stock, reportData.SignalOfDayUnderAnalysis);

                if (Context.GenerateHistoricSignals)
                {
                    result.Signals = signals;
                }

                result.Report = new SmaReport(Indicator, Stock, reportData);

                return(result);
            }
Exemplo n.º 21
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));
        }
Exemplo n.º 22
0
        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 );
        }
Exemplo n.º 23
0
 public IPriceSeries Apply(IPriceSeries series)
 {
     return(PriceSeries.Null);
 }
Exemplo n.º 24
0
 public PriceSeries(IPriceSeries series)
     : base(series)
 {
 }
Exemplo n.º 25
0
 public StockPriceChart(StockHandle stock, IPriceSeries prices)
     : this("StockPrices", stock, prices)
 {
 }
Exemplo n.º 26
0
 public PriceSeriesRange(IPriceSeries series, ClosedInterval <int> interval)
     : this(series, interval.Min, interval.Max)
 {
 }
Exemplo n.º 27
0
 public bool ContainsEnoughData( IPriceSeries prices )
 {
     return myCalculator.ContainsEnoughData( prices );
 }
Exemplo n.º 28
0
 public IndicatorPointsSection( string name, IPriceSeries series )
     : base(name)
 {
     Series = series;
 }
        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 IPriceSeries Calculate(IPriceSeries prices)
 {
     return(myCalculator.Calculate(prices));
 }
Exemplo n.º 31
0
 public IndicatorPointsSection(string name, IPriceSeries series)
     : base(name)
 {
     Series = series;
 }
Exemplo n.º 32
0
 protected SignalSeries(IPriceSeries prices, SeriesIdentifier identifier, IEnumerable <TimedValue <DateTime, Signal> > set, bool sortRequired)
     : base(identifier, set, sortRequired)
 {
     Reference = prices;
 }
Exemplo n.º 33
0
 public bool ContainsEnoughData( IPriceSeries prices )
 {
     return prices.Count() >= NumDays;
 }
Exemplo n.º 34
0
 public IPriceSeries Calculate( IPriceSeries prices )
 {
     return myCalculator.Calculate( prices );
 }
Exemplo n.º 35
0
 public StockPriceChart( StockHandle stock, IPriceSeries prices )
     : this("StockPrices", stock, prices)
 {
 }
Exemplo n.º 36
0
        // expect: series already sorted by date
        public Curve AddCurve( string name, IPriceSeries points )
        {
            var curve = new Curve( name, points );
            curve.ViewPort = myViewPort;
            curve.PreRenderingOperators.AddRange( mySettings.CurvesPreRenderingOperators );

            myCurves.Add( curve );

            return curve;
        }
Exemplo n.º 37
0
        private static IEnumerable <TimedValue <DateTime, double> > GroupPointsByAverage(IPriceSeries series, int averageInterval)
        {
            var pointGroup = new List <TimedValue <DateTime, double> >();

            foreach (var point in series)
            {
                pointGroup.Add(point);

                if (pointGroup.Count == averageInterval)
                {
                    yield return(Average(pointGroup));

                    pointGroup.Clear();
                }
            }

            if (pointGroup.Any())
            {
                yield return(Average(pointGroup));
            }
        }
Exemplo n.º 38
0
 public PriceSeriesRange(IPriceSeries series, int from, int to)
     : base(series, from, to)
 {
 }
Exemplo n.º 39
0
 public bool ContainsEnoughData(IPriceSeries prices)
 {
     return(prices.Count() >= NumDays);
 }
Exemplo n.º 40
0
 public SignalSeries(IPriceSeries prices, SeriesIdentifier identifier, IEnumerable <TimedValue <DateTime, Signal> > set)
     : this(prices, identifier, set, true)
 {
 }
Exemplo n.º 41
0
 public IPriceSeries Apply( IPriceSeries series )
 {
     return PriceSeries.Null;
 }
Exemplo n.º 42
0
 public static ISignalSeries FromSortedSet(IPriceSeries prices, SeriesIdentifier identifier, IEnumerable <TimedValue <DateTime, Signal> > series)
 {
     return(new SignalSeries(prices, identifier, series, false));
 }
 public bool ContainsEnoughData(IPriceSeries prices)
 {
     return(myCalculator.ContainsEnoughData(prices));
 }