Very much appreciate your help in advance.
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!
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
NinjaTrader
how to code crossover 'within' last x number of bars
Collapse
X
-
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.Tags: None
-
Originally posted by pandyav View PostI 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.
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
-
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
-
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
-
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
-
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
-
Originally posted by marci02 View PostI'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
-
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
-
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.htmChelsea B.NinjaTrader Customer Service
Comment
-
Originally posted by marci02 View PostThanks! 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!
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 judysamnt7, 03-13-2023, 09:11 AM
|
4 responses
57 views
0 likes
|
Last Post
by DynamicTest
Today, 08:25 PM
|
||
Started by ScottWalsh, Today, 06:52 PM
|
4 responses
36 views
0 likes
|
Last Post
by ScottWalsh
Today, 07:40 PM
|
||
Started by olisav57, Today, 07:39 PM
|
0 responses
7 views
0 likes
|
Last Post
by olisav57
Today, 07:39 PM
|
||
Started by trilliantrader, Today, 03:01 PM
|
2 responses
19 views
0 likes
|
Last Post
by helpwanted
Today, 07:25 PM
|
||
Started by cre8able, Today, 07:24 PM
|
0 responses
9 views
0 likes
|
Last Post
by cre8able
Today, 07:24 PM
|
Comment