Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Simple SMA Strategy Help
Collapse
X
-
Simple SMA Strategy Help
I am new to strategy development in NT. I have a very simple strategy that I cannot quite get the logic right on in the strategy builder. I want to create a strategy that goes long if the daily close is above the 5-day simple moving average for 4 days straight. I then want the strategy to go short if the daily close is below the 5 day SMA for 4 days. I can get crossover strategies to work right, but I am not sure how to make sure the close is above or below for 4 daily bars before taking a long or short trade. Any help would be appreciated.Tags: None
-
Hi thebluescout,
Thanks for your post.
Are you checking that the Close[0] is greater than the PriorDayOHLC.PriorClose[0] price?
May I have a screenshot of your condition in the condition builder?
To send a screenshot press Alt + PRINT SCREEN to take a screen shot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save as a jpeg file and send the file as an attachment.
For detailed instructions please visit the following link
http://take-a-screenshot.org/Chelsea B.NinjaTrader Customer Service
-
Comment
-
The problem with the wizard is it limitations. In your particular case, the lack of alternative branching means that you need to evaluate every possibility and set a variable to query. Translate this sequence into the wizard:Originally posted by thebluescout View PostHere is the screenshot. I am not sure how to make the condition to ensure that the close is above the 5 day SMA for 4 consecutive days before taking a long trade, and vice versa for a short trade. Thanks.
- If Close[0] is greater than SMA(5)[0], increase Variable0 by 1.
- If Close[0] is not greater than SMA(5)[0], make Variable0 equal to zero.
- If Variable0 is 4 or more, then enter trade.
In the future, it might help you code better if you will breakdown, in writing, what your high level statement means in processing terms. I started with the statements:
- Accumulate or reset a count
- If the count is sufficient, take a trade.
which is essentially saying, "If something happens this number of times consecutively ...".
After that, translation to what I wrote here, was a simple breakdown to how to accumulate or reset the count, given the condition that you wanted met.Last edited by koganam; 08-15-2014, 10:31 AM.
Comment
-
Yes, I do want a daily data series for the SMA. And the current daily close would have to be above the SMA for 4 days straight. Thanks.
Comment
-
I think I am close to getting the hang of what I need to do. My question is how do I make Variable0 increase. I see in the condition builder where I can make it equal to one iIf Close[0] is greater than SMA(5)[0]. Not sure if I am making it increase though.
Comment
-
I definitely appreciate the help. This is pretty close. It appears that the "count" is not on consecutive bars. Thus, if there are 4 bar closes below the SMA, even if non-consecutive, then it triggers a sell. Conversely, if there are 4 closes above the SMA, even if non-consecutive, it triggers a buy. I would like it to be 4 consecutive bars in order to trigger an order. Not sure if there is an easy way to do this.
Comment
-
Hi thebluescout,
I may have misunderstood what you are trying to do.
The code looks that the current price is greater than the SMA of the daily period today and the current price is greater than the SMA of the daily period yesterday, and the current price is greater than the SMA of the daily period the day before yesterday, etc..
Is this not what you are looking for?Chelsea B.NinjaTrader Customer Service
Comment
-
Probably a problem with the last of your conditional lines:Originally posted by NinjaTrader_ChelseaB View PostHi thebluescout,
I may have misunderstood what you are trying to do.
The code looks that the current price is greater than the SMA of the daily period today and the current price is greater than the SMA of the daily period yesterday, and the current price is greater than the SMA of the daily period the day before yesterday, etc..
Is this not what you are looking for?
You probably want to change that to use the Close instead:Code:&& Close[0] > SMA(PriorDayOHLC().PriorOpen, 5)[3])
Code:&& Close[0] > SMA(PriorDayOHLC().PriorClose, 5)[3])
Last edited by koganam; 08-15-2014, 04:02 PM.
Comment
-
Once again, I definitely appreciate all the help. I'm still having issues though, even with that revised code. For example in entering a long trade, it appears that there are 4 total closes above the SMA to trigger a long position. This "count" does not reset, even if there was an intervening close below the SMA. I would like the 4 closes above or below to be consecutive. I have attached a screenshot of the output I am getting. Please let me know if I need to explain something in greater detail.
Comment
-
Hi thebluescout,
Have you corrected the mistake that kognam found?
Attached is a copy of the indicator with the correction.
This indicator does not use a count that is reset. The code looks that the current price is greater than the SMA of the daily period today and the current price is greater than the SMA of the daily period yesterday, and the current price is greater than the SMA of the daily period the day before yesterday, etc..
Please take a look at the code in the script and let me know if you have questions with this.Attached FilesChelsea B.NinjaTrader Customer Service
Comment
-
Your picture shows that you are using Daily bars. In that case, you can just use the static approach for 4 days.Originally posted by thebluescout View PostOnce again, I definitely appreciate all the help. I'm still having issues though, even with that revised code. For example in entering a long trade, it appears that there are 4 total closes above the SMA to trigger a long position. This "count" does not reset, even if there was an intervening close below the SMA. I would like the 4 closes above or below to be consecutive. I have attached a screenshot of the output I am getting. Please let me know if I need to explain something in greater detail.
The first picture shows what the Strategy Panel looks like; the second shows what values you need to look at in the conditions pane.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
646 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
367 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
108 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
569 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
573 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment