Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    Brandon H.NinjaTrader Customer Service

    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.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Thank you.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Jimmyk, 01-26-2018, 05:19 AM
          6 responses
          834 views
          0 likes
          Last Post emuns
          by emuns
           
          Started by jxs_xrj, 01-12-2020, 09:49 AM
          6 responses
          3,290 views
          1 like
          Last Post jgualdronc  
          Started by Touch-Ups, Today, 10:36 AM
          0 responses
          10 views
          0 likes
          Last Post Touch-Ups  
          Started by geddyisodin, 04-25-2024, 05:20 AM
          11 responses
          62 views
          0 likes
          Last Post halgo_boulder  
          Started by Option Whisperer, Today, 09:55 AM
          0 responses
          8 views
          0 likes
          Last Post Option Whisperer  
          Working...
          X