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

how to code crossover 'within' last x number of bars

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

    how to code crossover 'within' last x number of bars

    I am trying to come up with a code where I would like to go long if 50SMA crosses above 200 SMA 'within' last 10 bars. Can someone please help me write the code? I know how to put this together if this just one bar ago, but I'd like to go long if the crossover happened on 'any' of the last 10 bars.

    Very much appreciate your help in advance.

    #2
    Originally posted by pandyav View Post
    I am trying to come up with a code where I would like to go long if 50SMA crosses above 200 SMA 'within' last 10 bars. Can someone please help me write the code? I know how to put this together if this just one bar ago, but I'd like to go long if the crossover happened on 'any' of the last 10 bars.

    Very much appreciate your help in advance.
    Hi pandyav

    The line of code below should do what you're after.
    if (CrossAbove(SMA(50), SMA(200), 10))
    {
    Enter your code in here to action when the above condition is met
    }

    The link below will give you some more info too...
    http://www.ninjatrader.com/support/h...sub=crossabove

    Cheers

    Comment


      #3
      Thanks for the assist Stuart, pandyav - that would be our recommend approach as well, can you give this a try in your scenario?
      BertrandNinjaTrader Customer Service

      Comment


        #4
        Current Bar crosses below MA and closes above

        I'm having problems coding an indicator that is similar to the thread. I would like an alert when the currentbar crosses below a moving average but closes above the moving average. I want this to happen on the current bar and not look back so many bars. This is what I've tried and it's not working.
        if ((CrossBelow(Low[0], EMA(14) [0]) && (Close[0] > EMA(14)[0])){

        in place of Low, I've tried CurrentBar and that didn't work either.

        Comment


          #5
          Hello marci02,

          If the code is run at the end of the bar, then the low of the previous bar will need to have been above the ema on the previous bar and then the low on the current bar will need to be below the ema on the current bar, and at the same time the close of the current bar will need to be above the ema on the current bar.

          This could happen with the code you have but this is a pretty specific and probably wouldn't happen often.

          Do you have screenshot that shows this situation occurred but no alert was sent?

          You aren't looking for the low to dip below the ema and then come back up are you?
          Chelsea B.NinjaTrader Customer Service

          Comment


            #6
            Hi Chelsea,

            Thanks for responding so quickly. I'm attaching a picture. Hope this helps and hope you can help me.
            Attached Files

            Comment


              #7
              Hi marci02,

              None of the bars in this screenshot fit the criteria.

              The low has to be above the ema on the previous bar and then below the ema on the current bar. None of the bars with arrows have this.

              On the bars I see that have the low above the ema on the previous bar and below the ema on the current bar, none of those have the close above the ema.
              Chelsea B.NinjaTrader Customer Service

              Comment


                #8
                Thanks ChelseaB for your feedback. So, basically I can't generate an alert on the current bar's close if that bar crossed below the EMA then closed above, is that correct?

                Comment


                  #9
                  Hello marci02,

                  Yes, it would be possible to detect this.

                  if (Close[1] > EMA(14)[1] && Low[0] < EMA(14)[0] && Close[0] > EMA(14)[0])
                  {
                  // trigger alert
                  }

                  This looks for the close of the previous bar being greater than the ema on the previous bar, then the low being lower than the ema on the current bar and the close being greater than the ema on the current bar.

                  This would indicate that the price dipped below the ema and came back up.
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Thanks ChelseaB! it wasn't what I was looking for but I was finally able to figure out how to get what I need by changing close to open and not use previous bars. thanks!!

                    Comment


                      #11
                      Originally posted by marci02 View Post
                      I'm having problems coding an indicator that is similar to the thread. I would like an alert when the currentbar crosses below a moving average but closes above the moving average. I want this to happen on the current bar and not look back so many bars. This is what I've tried and it's not working.
                      if ((CrossBelow(Low[0], EMA(14) [0]) && (Close[0] > EMA(14)[0])){

                      in place of Low, I've tried CurrentBar and that didn't work either.
                      Code:
                      if (Close[1] > EMA(Period)[0] 
                      && Close[0] > EMA(Period)[0]
                      && Low[0] < EMA(Period)[0]) {//Quod Erat Demonstrandum}

                      Comment


                        #12
                        Thanks! Let me try your code and see what the difference is with what I created.

                        I've added this indicator to Market Analyzer and I"m getting an error "Failed to call 'OnBarUpdate' method for market analyzer column" I've changed the number of bar to look back and changed the maximum bars to infinite but no luck. Any help would be appreciated!

                        Comment


                          #13
                          Hello marci02,

                          This may be due to the BarsRequired.

                          If you call Close[1] there will need to be at least one bar on the chart.

                          Try adding BarsRequired = 2; to the Initialize() method of the script.

                          http://www.ninjatrader.com/support/h...rsrequired.htm
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by marci02 View Post
                            Thanks! Let me try your code and see what the difference is with what I created.

                            I've added this indicator to Market Analyzer and I"m getting an error "Failed to call 'OnBarUpdate' method for market analyzer column" I've changed the number of bar to look back and changed the maximum bars to infinite but no luck. Any help would be appreciated!
                            My mistake. I assumed you knew to escape the first bar. I simply gave you the exact code that you requested. Correction below.
                            Code:
                            if (CurrentBar < 1) return;
                            if (Close[1] > EMA(Period)[0] 
                            && Close[0] > EMA(Period)[0]
                            && Low[0] < EMA(Period)[0]) {//Quod Erat Demonstrandum}

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by jaybedreamin, Today, 05:56 PM
                            0 responses
                            7 views
                            0 likes
                            Last Post jaybedreamin  
                            Started by DJ888, 04-16-2024, 06:09 PM
                            6 responses
                            18 views
                            0 likes
                            Last Post DJ888
                            by DJ888
                             
                            Started by Jon17, Today, 04:33 PM
                            0 responses
                            4 views
                            0 likes
                            Last Post Jon17
                            by Jon17
                             
                            Started by Javierw.ok, Today, 04:12 PM
                            0 responses
                            12 views
                            0 likes
                            Last Post Javierw.ok  
                            Started by timmbbo, Today, 08:59 AM
                            2 responses
                            13 views
                            0 likes
                            Last Post bltdavid  
                            Working...
                            X