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 NullPointStrategies, 03-13-2026, 05:17 AM
|
0 responses
89 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
152 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
80 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
53 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
63 views
0 likes
|
Last Post
|

Comment