I'm working on a strategy that will detect a gap in the S&P500 index, and then enter a MES long or short order at a specific time. It does seem to work for the most part. However, today it did not. I'm thinking maybe it's something to do with the recent market holidays.
I'm checking for the start of a new session in the S&P by using
// Gap detection logic...
if (Times[2][0].Date != Times[2][1].Date)
Thank you for any guidance. The full code is below. Also see the attached screenshot for output window.
-M
namespace NinjaTrader.NinjaScript.Strategies
{
public class MADJOunlocked : Strategy
{
#region Variables
// User defined variables (add any user defined variables below)
// Set the gapUp/gapDown variables to false
private bool gapUp = false;
private bool gapDown = false;
// Session object
private SessionIterator sessionIterator;
#endregion
protected override void OnStateChange()
{
if (State == State.Historical)
{
// Initialize sessionIterator.
sessionIterator = new SessionIterator(Bars);
}
else if (State == State.Configure)
{
// Add data series...
AddDataSeries("MES 06-24", BarsPeriodType.Second, 3);
AddDataSeries("^SP500", BarsPeriodType.Minute, 5);
}
}
/// <summary>
/// Called on each bar update event
/// </summary>
protected override void OnBarUpdate()
{
// Check for the S&P 500 series to perform gap analysis...
if (BarsInProgress == 2)
{
// Check if it's the start of the session to initialize gapUP and gapDOWN.
if (Bars.IsFirstBarOfSession)
{
// Reset gapUP and gapDOWN to false at the start of the session.
gapUp = false;
gapDown = false;
}
// Check if it's the end of the session to reset gapUP and gapDOWN.
if (Bars.IsLastBarOfSession)
{
gapUp = false;
gapDown = false;
}
// Proceed with gap analysis...
if (CurrentBars[0] < 1 || CurrentBars[2] < 1)
{
Print("Not enough data to analyze." + Times[2][0].ToString());
return; // Not enough data to analyze.
}
// Define a threshold for the minimum gap size to consider it significant
double gapThreshold = 1.5; // Adjust this value according to your criteria
// Gap detection logic...
if (Times[2][0].Date != Times[2][1].Date)
{
double previousCloseHigh = Highs[2][1];
double currentOpenLow = Lows[2][0];
double previousCloseLow = Lows[2][1];
double currentOpenHigh = Highs[2][0];
// Calculate the gap sizes
double gapSizeUp = currentOpenLow - previousCloseHigh;
double gapSizeDown = currentOpenHigh - previousCloseLow;
// Check for gap up
if (gapSizeUp > gapThreshold)
{
gapUp = true; // Sets the flag for a significant gap up condition.
}
// Check for gap down
else if (gapSizeDown < -gapThreshold)
{
gapDown = true; // Sets the flag for a significant gap down condition.
}
else
{
Print("No significant gap detected at " + Times[2][0].ToString() + ".");
}
}
}
// Proceed with your existing logic for entry conditions...
if (BarsInProgress == 1)
{
// Detect the current time. Set the target entry time and the 2 minute target entry time
DateTime now = Times[1][0];
DateTime targetEntryTime = new DateTime(now.Year, now.Month, now.Day, 6, 59, 57);
DateTime twoMinTargetEntryTime = new DateTime(now.Year, now.Month, now.Day, 7, 02, 0);
// First, check if the current time matches the target entry time
if (now.TimeOfDay == targetEntryTime.TimeOfDay)
{
// Next, check if the current day is not Monday and not Friday
if (now.DayOfWeek != DayOfWeek.Monday && now.DayOfWeek != DayOfWeek.Friday)
{
// Check if it's a gap up
if (gapUp)
{
Print("Executing Short for gap up on " + now.ToString("MM/dd/yy ") + now.DayOfWeek.ToString() + " at " + now.TimeOfDay.ToString() + ".");
EnterShort(1, 0, "Short entry");
}
// Check if it's a gap down
else if (gapDown)
{
Print("Executing Long for gap down on " + now.ToString("MM/dd/yy ") + now.DayOfWeek.ToString() + " at " + now.TimeOfDay.ToString() + ".");
EnterLong(1, 0, "Long entry");
}
}
}
// First, check if the current time matches the target entry time
if (now.TimeOfDay == twoMinTargetEntryTime.TimeOfDay)
{
// Next, check if the current day is Monday or Friday
if (now.DayOfWeek == DayOfWeek.Monday || now.DayOfWeek == DayOfWeek.Friday)
{
// Check if it's a gap up
if (gapUp)
{
Print("Executing Short for gap up on " + now.ToString("MM/dd/yy ") + now.DayOfWeek.ToString() + " at " + now.TimeOfDay.ToString() + " (2 Min condition).");
EnterShort(1, 0, "Short entry");
}
// Check if it's a gap down
else if (gapDown)
{
Print("Executing Long for gap down on " + now.ToString("MM/dd/yy ") + now.DayOfWeek.ToString() + " at " + now.TimeOfDay.ToString() + " (2 Min condition).");
EnterLong(1, 0, "Long entry");
}
}
}
}
}
}
}

Comment