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!
See more
See less

Partner 728x90

Collapse

Adding volume bars in Strategy Builder

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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.

    #2
    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;
    }
    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.

    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

    Comment


      #3
      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


        #4
        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;
        in C# plus equals += can be used to accumulate a value on a variable. That is the equivalent of saying:
        Code:
        total = total + Volume[i];



        JesseNinjaTrader Customer Service

        Comment


          #5
          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;
          Not sure when would you use long? I always like using double for the most part.
          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 memonic
          by memonic
           
          Started by merc410, Today, 03:41 AM
          2 responses
          13 views
          0 likes
          Last Post merc410
          by merc410
           
          Started by sugalt, 04-30-2024, 04:02 AM
          2 responses
          13 views
          0 likes
          Last Post sugalt
          by sugalt
           
          Started by Ndakotan1313, 03-14-2024, 05:02 PM
          2 responses
          64 views
          0 likes
          Last Post blaise_code  
          Started by claxxical, 05-30-2017, 12:30 PM
          37 responses
          4,464 views
          0 likes
          Last Post Padan
          by Padan
           
          Working...
          X