Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Referencing yesterday's from the opening 5 min bar.

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

    Referencing yesterday's from the opening 5 min bar.

    Hi. I am trying to write a code block that will act as a gap filter. Is the gap is too big no trading type thing. I am using 5 mins bars. I have added a secondary series and think I am referencing everything correctly but my backtest won't run at all. It does seem to compile OK with no errors. I have tried so many different things and done so much googling but to no avail.

    I have some code below. Thanks in advance.

    Code:
    if (State == State.Configure)
    {
    AddDataSeries(BarsPeriodType.Day, 1);
    }
    
    else if (State == State.DataLoaded && displayIndicators == true)
    {
    myATR = ATR(ATRPeriod);
    emaFast = EMA(fastPeriod);
    emaSlow = EMA(slowPeriod);
    emaFast.PaintPriceMarkers = false;
    emaSlow.PaintPriceMarkers = false;
    emaFast.Plots[0].Brush = Brushes.LimeGreen;
    emaSlow.Plots[0].Brush = Brushes.Red;
    AddChartIndicator(emaFast);
    AddChartIndicator(emaSlow);
    AddChartIndicator(myATR);
    
    }
    
    
    }
    
    protected override void OnBarUpdate()
    {
    
    // No trading before 8:30. Would be nice to find a way to prevent trading if not using RTH template.
    if (ToTime(Time[0]) < 83000)
    return;
    
    // Gap filter
    if (Bars.BarsSinceNewTradingDay == 0 && Math.Abs(Open[0] - Closes[1][1]) < x)
    canTrade = true;
    ​

    #2
    Hello Red 7 Trading,

    Thanks for your post.

    In the code you shared I do not see that orders are being submitted. If there are no order entry methods being called, no trade results will appear in the Strategy Analyzer.

    Do you see any error messages appear in the Log tab of the Control Center? If so, what do they report?

    Have you added debugging prints to understand how the strategy is behaving?

    If not, debugging prints should be added to the script to understand how it is processing logic. In the strategy add prints (outside of any conditions) that print the values of every variable used in every condition that places an order along with the time of that bar.

    Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).​

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.
    https://ninjatrader.com/support/foru...121#post791121
    <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

    Comment


      #3
      Hi Brandon - thanks for the response. Apologies, I did not paste in the entire script but I have debugged this to the extent that it's definitely the block where I am comparing current close to yesterday close that's the issue. Specifically, it's this exact part of the code below that causes problems.

      Closes[1][1]

      If I remove that everything works just fine. So I guess my question is, knowing that I have added the daily data series is this the correct way to reference the last bar close of that data series i.e. yesterday's close.

      Also, if I am using instrument RTH for trading hours on MNQ on 5 min bars why wouldn't just Math.Abs(Open[0] - Close[1]) work on the very first bar of the session? The previous bar should be the last 5 min bar of the previous day right? Which would then give me the opening gap. Because even that doesn't work.

      I am totally confused here but I will add some print statements and see what comes out if this response hasn't shed more light on my issue for you.

      Alternatively, if you could suggest how you would code an opening gap filter on RTH with 5 min bars that might be helpful rather than trying to clean up my mess.

      Thanks, Red 7.

      Comment


        #4
        OK - so I figured out part of the problem I was not checking to make sure we had at least 1 bar of each series processed. But now things are even wackier. I am posting part of my script below and some print messages. I can't for the life if me figure this out. It should be so simple. All I want to do is check today's open using 5 min bars vs yesterday's close against a gap threshold for RTH session. I can't believe it's this difficult. Surely I am not the only person that's ever tried to do this from an intraday bar series right???

        CODE START

        if (State == State.Configure)

        {
        AddDataSeries(BarsPeriodType.Day, 1);
        }


        else if (State == State.DataLoaded && displayIndicators == true)
        {
        myATR = ATR(ATRPeriod);
        emaFast = EMA(fastPeriod);
        emaSlow = EMA(slowPeriod);
        emaFast.PaintPriceMarkers = false;
        emaSlow.PaintPriceMarkers = false;
        emaFast.Plots[0].Brush = Brushes.LimeGreen;
        emaSlow.Plots[0].Brush = Brushes.Red;
        AddChartIndicator(emaFast);
        AddChartIndicator(emaSlow);
        AddChartIndicator(myATR);

        }


        }

        protected override void OnBarUpdate()
        {
        // This breaks out of the code block if I don't have at least 1 bar of each series processed.
        if (CurrentBars[0] < 1 || CurrentBars[1] < 1)
        return;

        // No trading before 8:30. Would be nice to find a way to prevent trading is not using RTH.
        // if (ToTime(Time[0]) < 83000)
        //return;

        // Gap filter
        if (BarsInProgress == 0 && Bars.IsFirstBarOfSession && Math.Abs(Open[0] - Closes[1][1]) < myATR[0] * gapMultiplier)

        {
        canTrade = true;
        Print(string.Format("The date is {0} and the opening gap is {1} and the gap threshold is {2} and canTrade has been set to {3}",
        ToDay(Time[0]),
        Math.Abs(Open[0] - Closes[1][1]),
        myATR[0] * gapMultiplier,
        canTrade));
        }

        // Long entry
        if (ToTime(Time[0]) > 83000)

        {
        // Long entry
        if (CrossAbove(EMA(fastPeriod), EMA(slowPeriod), 1) && canTrade == true)
        {
        EnterLong(initialContracts);
        }

        // Short entry
        else if (CrossBelow(EMA(fastPeriod), EMA(slowPeriod), 1) && canTrade == true)
        {
        EnterShort(initialContracts);
        }

        }

        CODE END

        Comment


          #5
          Hello Red 7 Trading,

          Thanks for your notes.

          That is correct. A CurrentBars check would need to be added to the script to make sure that you have enough bars loaded for all data series being accessed. A more simple example using one series would be on bar 5 you check for 6 BarsAgo. There are not yet 6 bars so the CurrentBar minus 6 would be a negative number or a non-existent bar.

          CurrentBars - https://ninjatrader.com/support/help...urrentbars.htm
          Make sure you have enough bars - https://ninjatrader.com/support/help...nough_bars.htm

          ​When you mention "But now things are even wackier.", what exactly is the "wacky" behavior you are referring to?

          Does an error message appear in the Log tab of the Control Center? If so, what does it report?

          If the error states something like "You are accessing an index with a value that is invalid since it is out-of-range." I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart, this means that a bar has likely not formed yet for the added daily series.

          You should add debugging prints to the script that prints out CurrentBars[0], CurrentBars[1], and the Time[0] to confirm if this is the case.

          If this is the case, you could add a condition in your script that checks if CurrentBars[1] > 0 before accessing Closes[1][1]. For example:

          if (CurrentBars[1] > 0)
          {
          Print("Open BIP0: " + Open[0]);
          Print("Close BIP1: " + Closes[1][1]);
          }​
          <span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>

          Comment


            #6
            Hi Brandon. Apologies for my "wacky" response. This issue has been frustrating me for a few days now and I wrote the above after beating my head against the wall for a few hours with no progress. Bottom line is I need to use more print statements to the output so I can actually see the logic that NT8 is doing and current values of variables. I am making headway and I think I have solved it. If I continue to have issues I will start a new thread and be really specific about what I am trying to do and what my current issues are. I think for now I have solved the problem after some debugging with print statements. Thank you for your help Brandon.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Yesterday, 05:17 AM
            0 responses
            55 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            132 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
            45 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