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

  • koganam
    replied
    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.

    Leave a comment:


  • Spiderbird
    replied
    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

    Leave a comment:


  • koganam
    replied
    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].

    Leave a comment:


  • Spiderbird
    replied
    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++;
    }

    Leave a comment:


  • koganam
    replied
    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 ?

    Leave a comment:


  • NinjaTrader_Brett
    replied
    Hello,

    Thanks for the note.

    Yes you can use this NT method:



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

    Leave a comment:


  • 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

Latest Posts

Collapse

Topics Statistics Last Post
Started by athexinehowley, Today, 02:39 AM
0 responses
8 views
0 likes
Last Post athexinehowley  
Started by dtaylor, Today, 02:24 AM
0 responses
7 views
0 likes
Last Post dtaylor
by dtaylor
 
Started by RosaRichardow, Today, 12:21 AM
1 response
7 views
0 likes
Last Post marcus2300  
Started by ETFVoyageur, Today, 12:45 AM
1 response
11 views
0 likes
Last Post ETFVoyageur  
Started by TradeSaber, Yesterday, 10:33 PM
1 response
13 views
0 likes
Last Post TradeSaber  
Working...
X