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); }
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); }