Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

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

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

  • koganam
    replied
    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}

    Leave a comment:


  • NinjaTrader_ChelseaB
    replied
    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

    Leave a comment:


  • marci02
    replied
    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!

    Leave a comment:


  • koganam
    replied
    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}

    Leave a comment:


  • marci02
    replied
    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!!

    Leave a comment:


  • NinjaTrader_ChelseaB
    replied
    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.

    Leave a comment:


  • marci02
    replied
    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?

    Leave a comment:


  • NinjaTrader_ChelseaB
    replied
    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.

    Leave a comment:


  • marci02
    replied
    Hi Chelsea,

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

    Leave a comment:


  • NinjaTrader_ChelseaB
    replied
    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?

    Leave a comment:


  • marci02
    replied
    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.

    Leave a comment:


  • NinjaTrader_Bertrand
    replied
    Thanks for the assist Stuart, pandyav - that would be our recommend approach as well, can you give this a try in your scenario?

    Leave a comment:


  • GlobalTradingTools_Stuart
    replied
    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

    Leave a comment:


  • 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.

Latest Posts

Collapse

Topics Statistics Last Post
Started by Hwop38, 05-04-2026, 07:02 PM
0 responses
154 views
0 likes
Last Post Hwop38
by Hwop38
 
Started by CaptainJack, 04-24-2026, 11:07 PM
0 responses
307 views
0 likes
Last Post CaptainJack  
Started by Mindset, 04-21-2026, 06:46 AM
0 responses
244 views
0 likes
Last Post Mindset
by Mindset
 
Started by M4ndoo, 04-20-2026, 05:21 PM
0 responses
345 views
0 likes
Last Post M4ndoo
by M4ndoo
 
Started by M4ndoo, 04-19-2026, 05:54 PM
0 responses
176 views
0 likes
Last Post M4ndoo
by M4ndoo
 
Working...
X