Not sure what I would use from the condition builder to make this happen?
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Trading off # of candles
Collapse
X
-
Trading off # of candles
How would I go about coding a strategy that entered a trade after a certain number of candles in a row.... like shorting after 6 subsequent red candles.
Not sure what I would use from the condition builder to make this happen?Tags: None
-
Hello ScottieDog,
Thank you for writing in.
If you wish to create a condition that checks for 6 down bars in a row, you can check for the following:
Close[0] < Close[1] // current bar is less than previous
Close[1] < Close[2] // previous bar less than two bars ago
Close[2] < Close[3] // two bars ago less than three bars ago
Close[3] < Close[4] // three bars ago less than four bars ago
Close[4] < Close[5] // four bars ago less than five bars ago
Close[5] < Close[6] // five bars ago less than six bars ago
If all of the above are true, this would denote there were six down bars in a row.Zachary G.NinjaTrader Customer Service
-
You would have to code it from scratch: the Strategy Builder would not be able to handle it.Originally posted by ScottieDog View PostHow would I go about coding a strategy that entered a trade after a certain number of candles in a row.... like shorting after 6 subsequent red candles.
Not sure what I would use from the condition builder to make this happen?
#Variables
#OnBarUpdateCode:private int counter = 0; //class variable to store count
That code takes account of the fact that the data is being streamed and not a static space, and so makes efficient use of the data stream. You could always instead ignore the true nature of your data and use an inefficient "for loop" on every bar, which unfortunately is usually the answer that you would be given.Code:counter = (FirstTickOfBar && Close[0] < Open[0]) ? counter++ : 0; //test for red bar and up counter or reset it as necessary if (counter >= 6) MakeMegaBucks();
Last edited by koganam; 04-09-2016, 12:38 PM.
Comment
-
Thanks.
Is there a way of coding this so that I can have a user set preference for number of bars I want to choose? I´ve set a user defined input INT, but how to apply the conditions to it, for # of bars before an entry? Would I set a user variable for each in the condition builder?
Originally posted by NinjaTrader_ZacharyG View PostHello ScottieDog,
Thank you for writing in.
If you wish to create a condition that checks for 6 down bars in a row, you can check for the following:
Close[0] < Close[1] // current bar is less than previous
Close[1] < Close[2] // previous bar less than two bars ago
Close[2] < Close[3] // two bars ago less than three bars ago
Close[3] < Close[4] // three bars ago less than four bars ago
Close[4] < Close[5] // four bars ago less than five bars ago
Close[5] < Close[6] // five bars ago less than six bars ago
If all of the above are true, this would denote there were six down bars in a row.
Comment
-
Thanks. Saw your reply after my post #4.
I like the idea of the counter. Thanks. Is there somewhere I can read more about this?
Originally posted by koganam View PostYou would have to code it from scratch: the Strategy Builder would not be able to handle it.
#Variables
#OnBarUpdateCode:private int counter = 0; //class variable to store count
That code takes account of the fact that the data is being streamed and not a static space, and so makes efficient use of the data stream. You could always instead ignore the true nature of your data and use an inefficient "for loop" on every bar, which unfortunately is usually the answer that you would be given.Code:counter = (FirstTickOfBar && Close[0] < Open[0]) ? counter++ : 0; //test for red bar and up counter or reset it as necessary if (counter >= 6) MakeMegaBucks();

Comment
-
Originally posted by ScottieDog View PostThanks. Saw your reply after my post #4.
I like the idea of the counter. Thanks. Is there somewhere I can read more about this?
I am not sure what you asking. How to implement a counter?
Such is a concomitant part of almost all data-driven and event-driven programming, which ultimately is all that NT is. It receives data, which receipt is handled by the arrival of a tick event.
All that I have done here is that instead of receiving/accumulating the data, then iterating backward over it (a "for loop"), I have matched my processing to the data model, and handled the data as it came in. Dynamic data, dynamic processing.
ref: https://en.wikipedia.org/wiki/Data-driven_programming
Comment
-
hey, koganam,Originally posted by koganam View PostYou would have to code it from scratch: the Strategy Builder would not be able to handle it.
#Variables
#OnBarUpdateCode:private int counter = 0; //class variable to store count
That code takes account of the fact that the data is being streamed and not a static space, and so makes efficient use of the data stream. You could always instead ignore the true nature of your data and use an inefficient "for loop" on every bar, which unfortunately is usually the answer that you would be given.Code:counter = (FirstTickOfBar && Close[0] < Open[0]) ? counter++ : 0; //test for red bar and up counter or reset it as necessary if (counter >= 6) MakeMegaBucks();

why do you use FirstTickOfBar in this part of code?
counter = (FirstTickOfBar && Close[0] < Open[0]) ? counter++ : 0; //test for red bar and up counter or reset it as necessary
if (counter >= 6) MakeMegaBucks();
thanks
Comment
-
So that each bar processes the counter once and only once, regardless the setting for CalculateOnBarClose.Originally posted by Laimis View Posthey, koganam,
why do you use FirstTickOfBar in this part of code?
counter = (FirstTickOfBar && Close[0] < Open[0]) ? counter++ : 0; //test for red bar and up counter or reset it as necessary
if (counter >= 6) MakeMegaBucks();
thanks
Comment
-
Thanks, but all I get is zeroOriginally posted by koganam View PostSo that each bar processes the counter once and only once, regardless the setting for CalculateOnBarClose.
in variable:
private int counter = 0;
on bar update
counter = (FirstTickOfBar && Close[0] > SMA(period)[0]) ? counter++ : 0;
Print(counter);
i get zero
I really need some advise...
thank you
Comment
-
Hello Laimis,
I would suggest taking a look at the documentation for the ? operator: https://msdn.microsoft.com/en-us/lib...v=vs.100).aspx
Take a look at the statement koganam provided:
In order for the counter to increment, the condition that is being evaluated must be true. If it is not true, it'll reset the counter variable to 0.Code:counter = (FirstTickOfBar && Close[0] > SMA(period)[0]) ? counter++ : 0;
I would suggest placing a print before this statement to check the values of Close[0] and SMA(period)[0].
Example:
Code:if (FirstTickOfBar) { Print("Close[0] " + Close[0]); Print("SMA(period[0] " + SMA(period)[0]); } counter = (FirstTickOfBar && Close[0] > SMA(period)[0]) ? counter++ : 0;Zachary G.NinjaTrader Customer Service
Comment
-
thanks. I tested the code again using your advice.
and it seems, some how counter++ doesn't add to counter = 0. then i changed it to counter+1, it works now.
do you have any ideas why is that so?
nor urgent, but I'm learning C#, so it helps when I understand it
.
thanks again
Comment
-
Hello Laimis,
Thank you for your patience.
It looks like using counter++ would, indeed, not return the expected output in this scenario.
What's happening is that the code is evaluating what counter is (0), increments it by 1 (0 -> 1), but then assigns counter back to its original value (0).
You would be able to use counter + 1 (as you have explained) OR you can do ++counter rather than counter++.Zachary G.NinjaTrader Customer Service
Comment
-
Originally posted by NinjaTrader_ZacharyG View PostHello Laimis,
Thank you for your patience.
It looks like using counter++ would, indeed, not return the expected output in this scenario.
What's happening is that the code is evaluating what counter is (0), increments it by 1 (0 -> 1), but then assigns counter back to its original value (0).
You would be able to use counter + 1 (as you have explained) OR you can do ++counter rather than counter++.
Yes, of course. Unary incrementer. Must increment before assignation.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
612 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
355 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
105 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
561 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
564 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment