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

Average Volume for Last Days

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

    Average Volume for Last Days

    Hey guys, how do I get a stock's average volume for either the last 30 days or else the last few trading days?

    #2
    EliteTraderNyc,

    Depending on how you want the average calculated, you could use something like :

    double avgvolume = SMA(Volume,30)[0];

    However this works on the primary series of your chart. If you need strictly days here for any type of chart you may need to do something differently. What sort of chart are you wanting to get this calculation for?
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Hi, my primary chart is by minutes, like 5, 20, 30, but I need the daily volume as a whole.

      Comment


        #4
        EliteTraderNYC,

        There are a couple ways to do this. Probably the easiest is to use a MultiTimeFrame indicator.

        Code:
        private double avgdailyvolume = 0;
        
        protected override void Initialize()
        {
        
        Add(PeriodType.Day, 1);
        
        //other code here
        }
        
        protected override void OnBarUpdate()
        {
        
        if( CurrentBars[BarsInProgress]  < BarsRequired) return;
        
        if ( BarsInProgress == 1 ) //OnBarUpdate called for daily bars
        {
            avgdailyvolume = SMA(Volumes[1],30)[0];
        }
        else if (BarsInProgress == 0) //OnBarUpdate called for primary chart series
        {
        
        //other code here
        
        }
        
        }
        You may want to tweak the BarsRequired part here, and/or separately check for each series. This is just ensuring you have enough bars of either series to calculate the values. Also it would be advised to load enough days on your chart to do the calculation accurately.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          How would one add into this code that the avgdailyvolume is to be greater than 500k?

          Comment


            #6
            Hello zachj,

            You may want to add a check for the "avgdailyvolume" to see if it is greater than that number. Example from above:

            Code:
            if ( BarsInProgress == 1 ) //OnBarUpdate called for daily bars
            {
                avgdailyvolume = SMA(Volumes[1],30)[0];
            
                if (avgdailyvolume > 500000)
                {
                      //Do something when "avgdailyvolume" is greater than 500k
                }
                else
                {
                      //Do something when "avgdailyvolume" is less than 500k
                }
            }
            JCNinjaTrader Customer Service

            Comment


              #7
              What if I have the daily as the 3rd series and 1 minute as the secondary as below (I assume placing the daily after makes it the 3rd series).
              Add(PeriodType.Minute, 1);
              Add(PeriodType.Day, 1);

              Then in addition to what you wrote below I add in another condition for sma crossover with a condition for the 1 min price taking out the high of a previous bar on primary series. With also a time condition in there. Do I have the format correct or do I need an ELSE in there somewhere?

              Code:
               
              {
              if (CurrentBars[0] < BarsRequired || CurrentBars[1] < BarsRequired || CurrentBars[2] < BarsRequired) return;
               
              if ( BarsInProgress == 2 ) //OnBarUpdate called for daily bars
              avgdailyvolume = SMA(Volumes[2],30)[0];
              if ((avgdailyvolume > 500000 && (Closes[2][1] >= 10 && Closes[2][1] <= 200))
               
              if (BarsInProgress == 1) //OnBarUpdate called for 1min bars
              if (ToTime(Time[0]) >= timestart && ToTime(Time[0]) < timeend)
              if (SMA(BarsArray[0],Fast)[2] > SMA(BarsArray[0],Slow)[2]    && SMA(BarsArray[0],Fast)[3] < SMA(BarsArray[0],Slow)[3] && Close[0] > Highs[0][2] + distance ) 
               
              {
              EnterLong(200);
              }
              }

              Comment


                #8
                Hello zachj,

                Thanks for your note.

                With the code you have the following conditions will all need to be true for the EnterLong to take place as they are nested with the exception of the first if statement:

                if ((avgdailyvolume > 500000 && (Closes[2][1] >= 10 && Closes[2][1] <= 200))
                if (BarsInProgress == 1) //OnBarUpdate called for 1min bars
                if (ToTime(Time[0]) >= timestart && ToTime(Time[0]) < timeend)
                if (SMA(BarsArray[0],Fast)[2] > SMA(BarsArray[0],Slow)[2] && SMA(BarsArray[0],Fast)[3] < SMA(BarsArray[0],Slow)[3] && Close[0] > Highs[0][2] + distance )

                This does appear to be correct if you would like this to check that the minute interval is processing, the avgdailyvolume is above 500000, the time is within the start and end time (in seconds), and that the sma conditions are true as well.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Something strange is happening, only reason I added the 3rd daily series is to filter out stocks under 500k when backtesting. I run a backtest just on SPY to test it and the net profit is changing? It shouldn't have any effect as spy is always over 100 million in volume. Any idea?

                  Just changing things around in the code it looks like what's altering it is this line...
                  Add(PeriodType.Day, 1);

                  Comment


                    #10
                    Hello ,

                    I see that you are currently exploring this issue with NinjaTrader Patrick.

                    http://www.ninjatrader.com/support/f...d.php?p=335351

                    Please continue to work with Patrick and I will be closing this inquiry.

                    Thank you.
                    Chelsea B.NinjaTrader Customer Service

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by frankthearm, Today, 09:08 AM
                    8 responses
                    31 views
                    0 likes
                    Last Post frankthearm  
                    Started by NRITV, Today, 01:15 PM
                    2 responses
                    9 views
                    0 likes
                    Last Post NRITV
                    by NRITV
                     
                    Started by maybeimnotrader, Yesterday, 05:46 PM
                    5 responses
                    26 views
                    0 likes
                    Last Post NinjaTrader_ChelseaB  
                    Started by quantismo, Yesterday, 05:13 PM
                    2 responses
                    20 views
                    0 likes
                    Last Post quantismo  
                    Started by adeelshahzad, Today, 03:54 AM
                    5 responses
                    33 views
                    0 likes
                    Last Post NinjaTrader_BrandonH  
                    Working...
                    X