I'm working on a strategy that evaluates the first few opening bars(6:30am PST to 7:00am PST, 5 minute chart) on the S&P500. The code below does work. However, I'd like to evaluate the bars at 6:59:00AM. When I change the time to 6:59:00, the strategy won't detect any patterns, it does detect several of them when I leave the time at 7:00:00.
I've tried adding a second data series
AddDataSeries("^SP500", BarsPeriodType.Minute, 1);
So I would like to evaluate the bars on the 5-minute chart, but at 6:59am. Is there any way to do this?
Thank you in advance.
-Madhava
Full code
namespace NinjaTrader.NinjaScript.Strategies { public class DEV_unlocked : Strategy { protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Enter the description for your strategy here."; Name = "Dev_unlocked"; // Give your strategy a unique name // Set other defaults as needed } else if (State == State.Configure) { } } protected override void OnBarUpdate() { // Set up time for checking the pattern DateTime now = Times[0][0]; DateTime checkVPatternTime = new DateTime(now.Year, now.Month, now.Day, 7, 00, 00); // Check if it's the correct time to evaluate the pattern if (now.TimeOfDay == checkVPatternTime.TimeOfDay) { // Index of the first bar int firstBarIndex = 5; // Capture high and low from the first bar at 6:30 AM double firstCandleHigh = High[firstBarIndex]; double firstCandleLow = Low[firstBarIndex]; // Paint the first bar Ivory white BarBrushes[firstBarIndex] = Brushes.Ivory; // Define the allowable deviation double allowableDeviation = 1.5; // Boolean to track if a V pattern is detected bool vPatternDetected = false; // Variable to track if there was at least one or two closes above the first candle's high bool closeAboveFirstCandleHigh = false; // Iterate over the next candles to check for a V pattern for (int i = firstBarIndex - 1; i >= 0; i--) { // Check if the current candle closes above the first candle's high if (Close[i] > firstCandleHigh) { closeAboveFirstCandleHigh = true; // Continue to the next iteration to only check for retracement after this condition is met continue; } // If a candle closed above the first candle's high, look for the retracement if (closeAboveFirstCandleHigh && Low[i] <= firstCandleLow + allowableDeviation && Low[i] >= firstCandleLow - allowableDeviation) { // If retracement to near the low of the first candle is found after a higher close vPatternDetected = true; // Shade the bar where V pattern is confirmed BarBrushes[i] = Brushes.Yellow; Print("V pattern detected on " + now.ToString("MM/dd/yy ")); break; } } } } } }
Comment