Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Number of bars per session

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

    Number of bars per session

    Hello,

    Is there a variable to know how many bars we have in a day beforehand?



    For example,
    - if using 1 minute bars, in a day we have 390 bars
    - if using 5 minute bars, in a day we have 78 bars
    -etc...

    Thanks,

    #2
    Hello Diego Aburto,

    Thank you for your inquiry.

    There isn't a variable that will do this for you, however, you can have your script count the bars per session manually.

    Ensure CalculateOnBarClose is set to true for this to work.

    Here is a quick example:
    Code:
    private int numberOfBars = 0;
    
    // OnBarUpdate() is called at the close of each bar
    protected override void OnBarUpdate()
    {
         // is the bar that just closed the first bar of the session?
         if (Bars.FirstBarOfSession)
         {
              // print the number of bars counted from the previous session
              Print("Previous session number of bars: " + numberOfBars);
    
              // reset the counter as this is a new session
              numberOfBars = 0; 
         }
    
         // increment numberOfBars on every bar close
         numberOfBars++; 
    }
    More information about Bars.BarsFirstBarOfSession can be found here: https://ninjatrader.com/support/help...rofsession.htm

    Please, let us know if we may be of further assistance.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Create a function to calculate this and pass in the time.

      Comment


        #4
        Calculate on opening bar of session

        I think my question relates to this thread. If I want to set a strategy based on a percentage price change from the opening bar of the session, how would I define this?

        I read the below, but I am not sure this answers my question.


        I had no problem manually doing the math and setting a strategy to that value arrived at manually, but if I wanted to add that calculation to the strategy, how would I define which bar to calculate from. I am using the condition builder to build my price % change. Somebody stop me if condition builder is not the correct way to at least build the condition. But I am having trouble understanding how to define calculating on the opening of the opening bar of the session.

        Comment


          #5
          Hello caroline i,

          Thank you for writing in and welcome to the NinjaTrader Support Forum!

          You could most certainly store the close price of the first bar of session through the Strategy Wizard's Condition Buiider, but you would not be able to calculate the percentage change through the wizard itself. You would need to create custom code through the NinjaScript Editor.

          As an example:
          Code:
          private double firstBarOfSessionPrice = 0;
          private double percentChange = 0;
          protectede override void OnBarUpdate()
          {
               // if the current bar is the first bar of session, assign the firstBarOfSessionPrice to the value of Close[0]
               if (Bars.FirstBarOfSession)
                    firstBarOfSessionPrice = Close[0];
          
               // calculate the percentage change
               percentChange = ((Close[0] - firstBarOfSessionPrice) / firstBarOfSessionPrice) * 100;
          }
          As a reminder, if you haven't already updated to the latest version of NinjaTrader, please note that all NinjaTrader users MUST update to NinjaTrader 7.0.1000.31 by Saturday, February 27th if they wish to continue using NinjaTrader; older versions of NinjaTrader will no longer work after the 27th. We encourage everyone to update now to avoid interruption in service.

          None of your settings, work spaces, indicators, etc. will be affected if you follow the directions below.

          To install the update please follow these instructions:
          1. CRITICAL Shut Down NinjaTrader
          2. Download the update HERE
          3. Double click the file once it has completed downloading and follow the installation prompts on your screen

          For more information about this critical update please click HERE.

          Please, let us know if we may be of further assistance.
          Zachary G.NinjaTrader Customer Service

          Comment


            #6
            I was shown the market analyzer and this calculates percent change also. So now I know to ways to do it. Is one better than the other as far as CPU usage?
            Also, will both methods work on a basket of equities.
            I have the attached code snippet and line 43 has an error. For the life of me I can't determine what the error is. Any help would be appreciated.
            Attached Files

            Comment


              #7
              Hello caroline i,

              In the specifgic example I've provided, this calculation is made within OnBarUpdate(). With the Net Change Market Analyzer column, the calculation is made within OnMarketData().

              OnBarUpdate() is called at the close of each bar, or upon every tick that comes in (dependent on whether you have calculate on bar close set to true or false, respectively): https://ninjatrader.com/support/help...nbarupdate.htm

              OnMarketData() is called for every change in level one market data for the underlying instrument. This is a real-time data stream and can be CPU intensive if your program code is compute intensive: https://ninjatrader.com/support/help...marketdata.htm

              In the picture you have provided, there is a semi-colon after
              Code:
              ....e.MarketData.Opening.Price)
              You'll want to format your condition as
              Code:
              if ((e.Price - e.MarketData.Opening.Price) / e.MarketData.Opening.Price >= .25)
              {
                   .....
              }
              Zachary G.NinjaTrader Customer Service

              Comment


                #8
                Thank you. Seem to be working... I changed the percent to .01 just to get it to execute a trade but so far no trades are executing. ??? I used MSFT as I know that has more than a 4% increase from the opening so a .01 should have executed a short trade....
                Perhaps my error is in the default quantity. Your thoughts?
                // Condition set 1
                if ((e.Price - e.MarketData.Opening.Price) / e.MarketData.Opening.Price >=.01)
                {
                EnterShort(DefaultQuantity, "");
                }

                Comment


                  #9
                  Hello caroline i,

                  You'll want to ensure that you multiply the result by 100 within your if statement before checking if it's greater than or equal to .01.
                  Last edited by NinjaTrader_ZacharyG; 02-26-2016, 02:16 PM.
                  Zachary G.NinjaTrader Customer Service

                  Comment


                    #10
                    ah. Well let me play around with it. Thank you for setting me in the right direction.

                    Comment


                      #11
                      Going back to post #2, thank you for the code I have created an indicator to show the bars per session picture attached. I would like to take an average of the total bars per session over say the last 5 days. I have tried to pass the total value of each session into an array and then divide by 5 but I am stuck. I see on c# websites there is an array.Sum() function but I don't see this in ninja, any advise with code would be appreciated

                      Thanks in advance
                      Attached Files

                      Comment


                        #12
                        Hello matt_,

                        Thank you for writing in.

                        Sum() is an extension method found in the System.Linq namespace.

                        You will need to ensure to add a using declaration in your script pertaining to System.Linq.

                        Code:
                        using System.Linq;
                        These declarations are found in the Using declarations region at the top of the script.

                        Use the Sum extension method and the selector overload. Include the System.Linq namespace.


                        Please, let us know if we may be of further assistance.
                        Zachary G.NinjaTrader Customer Service

                        Comment


                          #13
                          Thanks Zachary this helps but I'm stuck on how to get the value of the total bars per session into an array. If I read it right the array would have to take the value within the if statement where myArray is noted below. I have tried many ways and I can't get the total session bars.

                          If anyone could advise that would be great I'm not very experienced with arrays, thanks

                          protected override void Initialize()
                          {
                          Add(new Plot(Color.FromKnownColor(KnownColor.Purple), PlotStyle.Line, "Plot0"));
                          Overlay = false;
                          CalculateOnBarClose = true;
                          }

                          protected override void OnBarUpdate()
                          {

                          if (Bars.FirstBarOfSession)
                          {
                          myArray.Add((int)(numberOfBars));
                          Print(Instrument.FullName + " " + Time[0] + " Previous session number of bars: " + numberOfBars + " Array = " + myArray );
                          numberOfBars = 0;
                          }
                          numberOfBars++;
                          Plot0.Set(numberOfBars);

                          }

                          Comment


                            #14
                            Hello matt_,

                            Thank you for your response.

                            Can you attach the full script with the code you posted to your response?

                            Comment


                              #15
                              Hi Patrick
                              File is attached, thanks
                              Attached Files

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by leojimenezp, 04-20-2024, 05:49 PM
                              4 responses
                              47 views
                              0 likes
                              Last Post leojimenezp  
                              Started by nicthe, Today, 09:24 AM
                              1 response
                              5 views
                              0 likes
                              Last Post nicthe
                              by nicthe
                               
                              Started by samish18, Today, 10:13 AM
                              0 responses
                              6 views
                              0 likes
                              Last Post samish18  
                              Started by kenz987, Yesterday, 10:20 AM
                              2 responses
                              13 views
                              0 likes
                              Last Post kenz987
                              by kenz987
                               
                              Started by nicthe, 08-23-2023, 07:53 AM
                              7 responses
                              197 views
                              0 likes
                              Last Post nicthe
                              by nicthe
                               
                              Working...
                              X