Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Code help for current bar higher than the last bar

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

    Code help for current bar higher than the last bar

    Hello,

    I am trying a simple strategy to go long if the current bar becomes higher than the last bar. I have tried these codes as separate conditions and was wondering if my syntax is right for both or not. In both cases method is OnBarUpdate(). Can you please verify this code is right, it compiles fine? Thanks

    (CurrentBar > Bars.Count - 1)) //current bar is higher than the last bar.
    (CurrentBar > Close[1])) //current bar is higher than the last closed bar.

    John

    #2
    Hello John,

    Thanks for your post.

    To clarify, are you wanting to enter a long position if the Close price of the current bar is higher than the Close price of the previous bar?

    If so, you could compare the current Close price to the previous Close price using the code below.

    Code:
    if (Close[0] > Close[1])
        EnterLong();
    See this help guide page for more information about accessing the Close PriceSeries: https://ninjatrader.com/support/help.../nt8/close.htm

    Let me know if I may assist further.
    <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
      Thank you, I am extending this strategy to incorporate more logic. So what I would like is for the EMA9 to to cross EMA21 and the current bar to be higher than the last one, so the chart will go up, then for the bars to come down and do a retest of the EMA9. So how would I know that the CurrentBar is down at EMA9 again? So far I have tried this but I don't think this code is right:

      if (CrossAbove(emaFast, emaSlow, 1) && (Close[0] > Close[1]) ) //ema crossed and current bar is higher than the last bar
      {
      //All of these attempts failed to see if the current bar is doing a retest of EMA9 again after going up.
      //if(Close[0] ==EMA(EMA9)[0] )
      // if (Close[0] == EMA9[0] )
      //if (Close[0] > ma[0]
      //if(CurrentBar == EMA(Close,EMA9)[1])​
      }

      Thanks again for you help.
      John

      Comment


        #4
        Hello John,

        Thanks for your note.

        "if (CrossAbove(emaFast, emaSlow, 1) && (Close[0] > Close[1]) ) //ema crossed and current bar is higher than the last bar"

        The code you shared would be the correct way to check if the emaFast crosses above the emaSlow on the previous bar and checks if the current Close price is greater than the previous Close price.

        To check if the current Close price is less than the previous Close price, you would create a condition that checks if (Close[0] < Close[1]).

        Note the Strategy Build could be used to set up conditions and you could view the generated syntax for that code by clicking the 'View code' button.

        See this help guide page about creating Conditions: https://ninjatrader.com/support/help...on_builder.htm

        Note that CurrentBar will only return the number of the current bar on the chart. For example, if you have 10 bars on the chart, CurrentBar will return 10.

        See this help guide page about CurrentBar: https://ninjatrader.com/support/help...currentbar.htm

        If the strategy is not behaving as expected, debugging prints should be added to the script to see exactly how it is evaluating logic.

        Below is a link to a forum post that demonstrates how to use prints to understand behavior.

        https://ninjatrader.com/support/foru...121#post791121

        Let me know if I may assist further.
        <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


          #5
          Thank you.

          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