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

Checking for volume spikes in last 20 one-minute bars

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

    Checking for volume spikes in last 20 one-minute bars

    Hi all,

    Exactly as a the title suggests, I'm looking to check for the existence of volume spikes of over 150k (150000) in the last 20 one minute bars in a strategy.

    My default is creating 20 unique int variables, assigning vol (number) to each of them, and then checking to see with if and elseif for the volume spikes.

    I'm sure there's a more elegant way of doing this (including doing a loop check). Can anyone give me some direction?

    Thanks in advance!

    Yours,
    Spider

    #2
    Hello,

    Thanks for the note.

    Yes you can use this NT method:



    if (CountIf(delegate {return VOL[0] > 150,000;}, 20) > 1)

    Comment


      #3
      Originally posted by Spiderbird View Post
      Hi all,

      Exactly as a the title suggests, I'm looking to check for the existence of volume spikes of over 150k (150000) in the last 20 one minute bars in a strategy.

      My default is creating 20 unique int variables, assigning vol (number) to each of them, and then checking to see with if and elseif for the volume spikes.

      I'm sure there's a more elegant way of doing this (including doing a loop check). Can anyone give me some direction?

      Thanks in advance!

      Yours,
      Spider
      Are you seeking to know the number of spikes, or just whether there is at least 1 ?

      Comment


        #4
        Hi Koganam,

        It was kind of a two part question, but I only asked about the existence of spikes. I also want to be able to capture the lowest low of any one of the 20 bars I'm checking back on.

        Absent the NT method that NinjaTrader_Brett outlined, would the following code work within an NT strategy?

        -------------------


        int loopNumber = 1;
        int volumeSpikes = 0;
        double lowestLow;

        while ( loopNumber < 20)
        // (While the loop is active, do the following…)
        {
        If vol[loopNumber] >= 150000

        // (If the volume in this historical bar is above 150k, increase the volume spike variable by 1, and set the low variable to the closing low of that bar. )
        {
        volumeSpikes++;
        lowestLow = low[loopNumber];
        }

        loopNumber++;
        }

        Comment


          #5
          Originally posted by Spiderbird View Post
          Hi Koganam,

          It was kind of a two part question, but I only asked about the existence of spikes. I also want to be able to capture the lowest low of any one of the 20 bars I'm checking back on.

          Absent the NT method that NinjaTrader_Brett outlined, would the following code work within an NT strategy?

          -------------------


          int loopNumber = 1;
          int volumeSpikes = 0;
          double lowestLow;

          while ( loopNumber < 20)
          // (While the loop is active, do the following…)
          {
          If vol[loopNumber] >= 150000

          // (If the volume in this historical bar is above 150k, increase the volume spike variable by 1, and set the low variable to the closing low of that bar. )
          {
          volumeSpikes++;
          lowestLow = low[loopNumber];
          }

          loopNumber++;
          }
          There are usually a number of ways to write any construct. I generally avoid loops by using an accumulation method, and finding span by difference. All that is to say that using a while loop on a DataStream means that you are running this code on every Close (that means every tick if COBC = false !) That is pretty resource intensive, I would say.

          After the rant, your code will actually, if it run, set the lowestLow to the low on the earliest spike every run. If you are trying to find the lowestLow, you should be doing a comparison and keeping the lower value. You may want to change:

          Code:
          lowestLow = low[loopNumber];
          to
          Code:
          lowestLow = Math.Min(lowestLow, Low[loopNumber]);
          Note the capitalization: your code would have generated an error as low is invalid in the context. Your vol[x] construct also should be Volume[x].

          Comment


            #6
            Hi Koganam,

            Thanks for the direction and advice! (as well as the code correction). I suspect I'll have to do a few capitalization corrections on the larger bit of code I'm working on.

            One clarification though... in the interest of avoiding creating strategies that are resource-intensive (i.e. using while or for loops), what did you mean by " I generally avoid loops by using an accumulation method, and finding span by difference." Do you have an example you can point/URL to where I can see what you mean?

            Thanks in advance,
            Spider

            Comment


              #7
              Originally posted by Spiderbird View Post
              Hi Koganam,

              Thanks for the direction and advice! (as well as the code correction). I suspect I'll have to do a few capitalization corrections on the larger bit of code I'm working on.

              One clarification though... in the interest of avoiding creating strategies that are resource-intensive (i.e. using while or for loops), what did you mean by " I generally avoid loops by using an accumulation method, and finding span by difference." Do you have an example you can point/URL to where I can see what you mean?

              Thanks in advance,
              Spider
              Take as an example some code that calculates the total volume in the last so many bars.

              You could use a loop, or you could use some construct (DataSeries or Array or List) to hold the series of the accumulated value for all bars. Then the sum over the last x bars is simply the current accumulated value less the accumulated value x bars ago.

              You would accumulate as you stream, by simply adding the current value to the previous accumulated value, so you NEVER use a loop anywhere in the code.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by llanqui, 04-28-2024, 10:32 AM
              7 responses
              35 views
              0 likes
              Last Post llanqui
              by llanqui
               
              Started by geotrades1, Today, 08:33 AM
              6 responses
              19 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Started by dcriador, Today, 10:45 AM
              0 responses
              1 view
              0 likes
              Last Post dcriador  
              Started by llanqui, 04-28-2024, 03:53 AM
              2 responses
              27 views
              0 likes
              Last Post llanqui
              by llanqui
               
              Started by RaddiFX, Yesterday, 09:55 PM
              5 responses
              32 views
              0 likes
              Last Post NinjaTrader_Gaby  
              Working...
              X