Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy not executing today

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Strategy not executing today

    Hello,

    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
    Code:
    // Gap detection logic...
            if (Times[2][0].Date != Times[2][1].Date)​
    . Is there another way I should be doing this?

    Thank you for any guidance. The full code is below. Also see the attached screenshot for output window.

    -M




    Code:
    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");
                    }
                }
    }
        }
    }
    
    
    }
    }​
    Attached Files

    #2
    Hello devatechnologies,

    Thank you for your post.

    What about the behavior of the strategy today did not work as expected? What you are doing is comparing if the date of the timestamp for the current bar does not equal the date of the timestamp of the previous bar. This would tell you if the current bar is starting a new day or not. You could also consider using Bars.IsFirstBarOfSession:


    This would be based on the Trading Hours for the data series used, which may or may not include holiday sessions (as you mentioned holidays). For more information on Trading Hours:


    Ultimately, you may need to add prints to your script to understand why it was not behaving as expected. For more information on using prints for debugging, please see the help article here:


    Please let us know if we may be of further assistance.

    Comment


      #3
      Hi Emily,

      Thank you for the prompt reply. So I've got to the bottom of this issue. It seems there is an issue with the data for the symbol ^SP500 with my data providers feed. When I connected to the Simulated Data Feed, the strategy ran as expected and detected a "no-gap" for today April 1st. That's a pretty cruel April Fools joke. They really got me with that one.

      That being said I do need to add more prints.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      54 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      130 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      73 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      44 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      49 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X