Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Adding volume bars in Strategy Builder
Collapse
X
-
Adding volume bars in Strategy Builder
Hello, I need a hint to create an entry for a long position in Strategy Builder if the sum of the last 10 volume bars is larger than a specific numeric value. Please advise. Thank you.Tags: None
-
Hello designer01,
The easiest way to do this would involve creating an indicator, the builder's math would not allow for this type of a calculation.
To make an indicator for use with the builder please do the following:
In the NinjaScript editor right click the indicators folder and click new.
In the wizard go through the pages and give the indicator a name and then also give it a Plot on the plots and lines page.
After creating the indicator you would need just the following lines of code in OnBarUpdate:
The value 1000 would be replaced with whatever the target volume you wanted was. Alternatively you can configure a User Input in the indicator wizard and use that in-place to make it configurable.Code:protected override void OnBarUpdate() { Value[0] = 0; if(CurrentBar < 10) return; long total = Volume[0] + Volume[1] + Volume[2] + Volume[3] + Volume[4] + Volume[5] + Volume[6] + Volume[7] + Volume[8] + Volume[9] + Volume[10]; if(total > 1000) Value[0] = 1; }
After making this indicator you can use it in the strategy builder by selecting:
left: indicators -> IndicatorName
center: equals
right: misc -> numeric value -> 1
Last edited by NinjaTrader_Jesse; 04-11-2022, 10:52 AM.
-
Hello Jesse,
Thank you it works great. If I wanted to increase from 10 to 100 for the bar number I would use a for loop how do you convert it to a for loop?
Thank you again.
Comment
-
Hello designer01,
To make the amount of BarsAgo variable you can make a int user input, that will allow the strategy to configure the amount of bars.
To make a loop like that it would look like this:
in C# plus equals += can be used to accumulate a value on a variable. That is the equivalent of saying:Code:if(CurrentBar < BarsAgoUserInput) return; long total = 0; for(int i = 0; i <= BarsAgoUserInput; i++) { total += Volume[i]; } if(total > 1000) Value[0] = 1;
Code:total = total + Volume[i];
Comment
-
Hi Jesse,
Thanks, It works good. I only had to substitute long for double , I was getting an error "Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)"
Not sure when would you use long? I always like using double for the most part.Code:if(CurrentBar < BarsAgoUserInput) return; double total = 0; for(int i = 0; i <= BarsAgoUserInput; i++) { total += Volume[i]; } if(total > 1000) Value[0] = 1;
Thanks again for the help much appreciated.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by CarlTrading, 03-31-2026, 09:41 PM
|
1 response
80 views
1 like
|
Last Post
|
||
|
Started by CarlTrading, 04-01-2026, 02:41 AM
|
0 responses
40 views
0 likes
|
Last Post
by CarlTrading
04-01-2026, 02:41 AM
|
||
|
Started by CaptainJack, 03-31-2026, 11:44 PM
|
0 responses
64 views
2 likes
|
Last Post
by CaptainJack
03-31-2026, 11:44 PM
|
||
|
Started by CarlTrading, 03-30-2026, 11:51 AM
|
0 responses
64 views
0 likes
|
Last Post
by CarlTrading
03-30-2026, 11:51 AM
|
||
|
Started by CarlTrading, 03-30-2026, 11:48 AM
|
0 responses
54 views
0 likes
|
Last Post
by CarlTrading
03-30-2026, 11:48 AM
|

Comment