Пример #1
0
        protected override Signal GenerateSignal(Security security, DateTime AsOf)
        {
            security.SetSwingPointsAndTrends(BarCount, PriceBarSize.Weekly);

            // Only generate signals after the last close of the week has finalized this bar
            if (AsOf != Calendar.LastTradingDayOfWeek(AsOf))
            {
                return(null);
            }

            // Today's weekly trend
            PriceBar weeklyBar = security.GetPriceBar(AsOf, PriceBarSize.Weekly);

            if (weeklyBar == null)
            {
                return(null);
            }

            TrendQualification weeklyTrend = weeklyBar.GetTrendType(BarCount);

            // Get the weekly trend from the prior week
            PriceBar priorWeek = weeklyBar.PriorBar;

            TrendQualification priorWeekTrend = priorWeek?.GetTrendType(BarCount) ?? TrendQualification.NotSet;

            if (weeklyTrend == priorWeekTrend)
            {
                return(null);
            }

            if (weeklyTrend == TrendQualification.AmbivalentSideways ||
                weeklyTrend == TrendQualification.ConfirmedSideways ||
                weeklyTrend == TrendQualification.SuspectSideways)
            {
                return(new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.CloseIfOpen, 1));
            }

            if (weeklyTrend == TrendQualification.SuspectBullish ||
                weeklyTrend == TrendQualification.ConfirmedBullish)
            {
                return(new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Buy, 1));
            }

            if (weeklyTrend == TrendQualification.SuspectBearish ||
                weeklyTrend == TrendQualification.ConfirmedBearish)
            {
                return(new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Sell, 1));
            }


            return(null);
        }
Пример #2
0
 public void SetTrendType(int bars, TrendQualification value)
 {
     if (!Security.AreSwingPointsSet(this.BarSize, bars))
     {
         this.Security.SetSwingPointsAndTrends(bars, this.BarSize);
     }
     //throw new UnknownErrorException() { message = "Must Initialize prior to access" };
     if (!SwingPointTrendInfo.ContainsKey(bars))
     {
         this.SwingPointTrendInfo.Add(bars, new TrendInfo());
     }
     SwingPointTrendInfo[bars].TrendType = value;
 }
Пример #3
0
        protected override Signal GenerateSignal(Security security, DateTime AsOf)
        {
            TrendQualification sectorTrend = GetSectorTrend(security, AsOf);

            security.SetSwingPointsAndTrends(BarCount, BarSize);

            var bar = security.GetPriceBar(AsOf, BarSize);

            if (bar == null || bar.PriorBar == null)
            {
                return(null);
            }

            // If this bar does not represent a transition point, no signal
            var priorTrend = bar.PriorBar.GetTrendType(BarCount);

            if (bar.GetTrendType(BarCount) == priorTrend)
            {
                return(null);
            }

            switch (bar.GetTrendType(BarCount))
            {
            case TrendQualification.NotSet:
            case TrendQualification.AmbivalentSideways:
            case TrendQualification.SuspectSideways:
            case TrendQualification.ConfirmedSideways:
                //return new Signal(security, AsOf, SignalAction.CloseIfOpen);
                return(null);

            case TrendQualification.SuspectBullish:
                if (priorTrend != TrendQualification.ConfirmedBullish && GetTrendAlignment(sectorTrend, TrendQualification.SuspectBullish) == TrendAlignment.Bullish)
                {
                    return(new Signal(security, BarSize, AsOf, SignalAction.Buy));
                }
                break;

            case TrendQualification.ConfirmedBullish:
                if (GetTrendAlignment(sectorTrend, TrendQualification.ConfirmedBullish) == TrendAlignment.Bullish)
                {
                    return(new Signal(security, BarSize, AsOf, SignalAction.Buy));
                }
                break;

            case TrendQualification.SuspectBearish:
                if (priorTrend != TrendQualification.ConfirmedBearish && GetTrendAlignment(sectorTrend, TrendQualification.SuspectBullish) == TrendAlignment.Bearish)
                {
                    return(new Signal(security, BarSize, AsOf, SignalAction.Sell));
                }
                break;

            case TrendQualification.ConfirmedBearish:
                if (GetTrendAlignment(sectorTrend, TrendQualification.ConfirmedBearish) == TrendAlignment.Bearish)
                {
                    return(new Signal(security, BarSize, AsOf, SignalAction.Sell));
                }
                break;
            }

            return(null);
        }
Пример #4
0
        protected override Signal GenerateSignal(Security security, DateTime AsOf)
        {
            security.SetSwingPointsAndTrends(BarCount, PriceBarSize.Daily);
            security.SetSwingPointsAndTrends(BarCount, PriceBarSize.Weekly);

            // Today's daily trend
            PriceBar           dailyBar   = security.GetPriceBar(AsOf, PriceBarSize.Daily);
            TrendQualification dailyTrend = dailyBar.GetTrendType(BarCount);

            // Today's weekly trend
            // We use last week's value until this week is completed on Friday, then we can start using that bar
            PriceBar weeklyBar = security.GetPriceBar(AsOf, PriceBarSize.Weekly);

            if (AsOf != Calendar.LastTradingDayOfWeek(AsOf))
            {
                weeklyBar = weeklyBar?.PriorBar;
            }

            if (weeklyBar == null)
            {
                return(null);
            }

            TrendQualification weeklyTrend = weeklyBar.GetTrendType(BarCount);

            TrendAlignment currentTrendAlignment = GetTrendAlignment(dailyTrend, weeklyTrend);

            if (currentTrendAlignment == TrendAlignment.Sideways || currentTrendAlignment == TrendAlignment.Opposing)
            {
                return(new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.CloseIfOpen, 1));
            }

            // Get the trend from the last period
            if (dailyBar.PriorBar == null)
            {
                return(null);
            }

            TrendQualification priorDayTrend = dailyBar.PriorBar.GetTrendType(BarCount);
            // Get the weekly trend from the prior day, which will align with yesterday's daily trend
            PriceBar priorWeek = security.GetPriceBar(Calendar.PriorTradingDay(AsOf), PriceBarSize.Weekly);

            if (priorWeek == null)
            {
                return(null);
            }
            TrendQualification priorWeekTrend = priorWeek.GetTrendType(BarCount);

            TrendAlignment priorTrendAlignment = GetTrendAlignment(priorDayTrend, priorWeekTrend);

            if (currentTrendAlignment == priorTrendAlignment)
            {
                return(null);
            }

            if (currentTrendAlignment == TrendAlignment.Bullish)
            {
                if (dailyTrend == TrendQualification.ConfirmedBullish && weeklyTrend == TrendQualification.ConfirmedBullish)
                {
                    return(new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Buy, 1.0));
                }
                //if (dailyTrend == TrendQualification.SuspectBullish && weeklyTrend == TrendQualification.ConfirmedBullish)
                //    return new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Buy, .75);
                //if (dailyTrend == TrendQualification.ConfirmedBullish && weeklyTrend == TrendQualification.SuspectBullish)
                //    return new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Buy, .50);
                //if (dailyTrend == TrendQualification.SuspectBullish && weeklyTrend == TrendQualification.SuspectBullish)
                //    return new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Buy, .25);
            }
            if (currentTrendAlignment == TrendAlignment.Bearish)
            {
                if (dailyTrend == TrendQualification.ConfirmedBearish && weeklyTrend == TrendQualification.ConfirmedBearish)
                {
                    return(new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Sell, 1.0));
                }
                //if (dailyTrend == TrendQualification.SuspectBearish && weeklyTrend == TrendQualification.ConfirmedBearish)
                //    return new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Sell, .75);
                //if (dailyTrend == TrendQualification.ConfirmedBearish && weeklyTrend == TrendQualification.SuspectBearish)
                //    return new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Sell, .50);
                //if (dailyTrend == TrendQualification.SuspectBearish && weeklyTrend == TrendQualification.SuspectBearish)
                //    return new Signal(security, PriceBarSize.Daily, AsOf, SignalAction.Sell, .25);
            }

            return(null);
        }
Пример #5
0
 public NetChangeByTrendType(TrendQualification trendType)
 {
     this.TrendType = trendType;
 }
Пример #6
0
        public static List <NetChangeByTrendType> GetNetChangeByTrendType(this Security me, PriceBarSize priceBarSize, int barCount, DateTime?start = null, DateTime?end = null)
        {
            List <PriceBar> PriceBarsUsed;

            switch (priceBarSize)
            {
            case PriceBarSize.Weekly:
            {
                PriceBarsUsed = me.WeeklyPriceBarData;
                if (!start.HasValue)
                {
                    start = me.GetFirstBar(PriceBarSize.Weekly).BarDateTime;
                    end   = me.GetLastBar(PriceBarSize.Weekly).BarDateTime;
                }
                if (start.HasValue && !end.HasValue)
                {
                    throw new UnknownErrorException()
                          {
                              message = "Invalid input dates"
                          }
                }
                ;
                start = FirstTradingDayOfWeek(start.Value);
                end   = FirstTradingDayOfWeek(end.Value);
            }
            break;

            case PriceBarSize.Monthly:
            {
                PriceBarsUsed = me.MonthlyPriceBarData;
                if (!start.HasValue)
                {
                    start = me.GetFirstBar(PriceBarSize.Monthly).BarDateTime;
                    end   = me.GetLastBar(PriceBarSize.Monthly).BarDateTime;
                }
                if (start.HasValue && !end.HasValue)
                {
                    throw new UnknownErrorException()
                          {
                              message = "Invalid input dates"
                          }
                }
                ;
                start = FirstTradingDayOfMonth(start.Value);
                end   = FirstTradingDayOfMonth(end.Value);
            }
            break;

            case PriceBarSize.Quarterly:
            {
                PriceBarsUsed = me.QuarterlyPriceBarData;
                if (!start.HasValue)
                {
                    start = me.GetFirstBar(PriceBarSize.Quarterly).BarDateTime;
                    end   = me.GetLastBar(PriceBarSize.Quarterly).BarDateTime;
                }
                if (start.HasValue && !end.HasValue)
                {
                    throw new UnknownErrorException()
                          {
                              message = "Invalid input dates"
                          }
                }
                ;
                start = FirstTradingDayOfQuarter(start.Value);
                end   = FirstTradingDayOfQuarter(end.Value);
            }
            break;

            case PriceBarSize.Daily:
            default:
            {
                PriceBarsUsed = me.DailyPriceBarData;
                if (!start.HasValue)
                {
                    start = me.GetFirstBar(PriceBarSize.Daily).BarDateTime;
                    end   = me.GetLastBar(PriceBarSize.Daily).BarDateTime;
                }
                if (start.HasValue && !end.HasValue)
                {
                    throw new UnknownErrorException()
                          {
                              message = "Invalid input dates"
                          }
                }
                ;
            }
            break;
            }


            if (PriceBarsUsed.Count == 0)
            {
                return(null);
            }

            // Create a return list of all trends except NotSet
            var ret = new List <NetChangeByTrendType>();

            foreach (var trend in Enum.GetValues(typeof(TrendQualification)))
            {
                if ((TrendQualification)trend != TrendQualification.NotSet)
                {
                    ret.Add(new Analysis.NetChangeByTrendType((TrendQualification)trend));
                }
            }

            // Set swingpoints and trends if not done already
            me.SetSwingPointsAndTrends(barCount, priceBarSize);

            // Get the first bar, ignore the starting ambilavent trend
            PriceBar currentBar = me.GetPriceBar(start.Value, priceBarSize);

            // Track the current trend as we go through each bar
            TrendQualification currentTrend = currentBar.GetTrendType(barCount);

            while (currentBar != null && currentBar.GetTrendType(barCount) == currentTrend)
            {
                currentBar = currentBar.NextBar;
            }

            PriceBar firstBarOfTrend = null;
            PriceBar lastBarOfTrend  = null;

            while (currentBar != null)
            {
                // Since the trend change is based on the ending value for a bar, we track a trend from the second instance through the first bar of the next trend
                if (currentTrend != currentBar.GetTrendType(barCount))
                {
                    lastBarOfTrend = currentBar;
                    if (firstBarOfTrend != null)
                    {
                        var trendResult = ret.Find(x => x.TrendType == currentTrend);
                        trendResult.AddValues(firstBarOfTrend, lastBarOfTrend);
                    }

                    firstBarOfTrend = currentBar.NextBar;
                    currentTrend    = currentBar.GetTrendType(barCount);
                }
                currentBar = currentBar.NextBar;
            }

            return(ret);
        }