• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

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 Johnny Santiago, Today, 12:30 PM
                    7 responses
                    35 views
                    0 likes
                    Last Post Johnny Santiago  
                    Started by ttodua, Today, 12:26 PM
                    3 responses
                    8 views
                    0 likes
                    Last Post NinjaTrader_PatrickG  
                    Started by dastaan, 06-26-2020, 04:33 AM
                    6 responses
                    113 views
                    0 likes
                    Last Post NinjaTrader_Jim  
                    Started by RumHam, Today, 12:14 PM
                    1 response
                    17 views
                    0 likes
                    Last Post NinjaTrader_Zachary  
                    Started by Lancer, Today, 12:12 PM
                    4 responses
                    19 views
                    0 likes
                    Last Post NinjaTrader_PatrickG  
                    Working...
                    X