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
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:
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.JesseNinjaTrader Customer Service
-
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:
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];
JesseNinjaTrader Customer Service
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?)"
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 memonic, Yesterday, 01:23 PM
|
2 responses
12 views
0 likes
|
Last Post
by memonic
Today, 04:26 AM
|
||
Started by merc410, Today, 03:41 AM
|
2 responses
13 views
0 likes
|
Last Post
by merc410
Today, 04:16 AM
|
||
Started by sugalt, 04-30-2024, 04:02 AM
|
2 responses
13 views
0 likes
|
Last Post
by sugalt
Today, 03:42 AM
|
||
Started by Ndakotan1313, 03-14-2024, 05:02 PM
|
2 responses
64 views
0 likes
|
Last Post
by blaise_code
Today, 03:16 AM
|
||
Started by claxxical, 05-30-2017, 12:30 PM
|
37 responses
4,464 views
0 likes
|
Last Post
by Padan
Today, 02:23 AM
|
Comment