Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How do I identify "First bar of day"

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

    #16
    Cumulate value in chart

    Could you give me a hint on how I can cumulate a value/values (ei close) for a specific number of bars (across session boundaries) to be stored in a variable and plotted!?

    Edit: Would that simply be "double cumval=Close[0]+ Close[1]+ Close[2]+ Close[3]+..."

    ...or is there a more convenient way if I wanted to include the last ~200 bars?

    Thanks!
    Last edited by FREEN; 04-23-2010, 02:36 AM.

    Comment


      #17
      Freen, you could take a look at the default EMA indicator how such values could be cumulated.

      Comment


        #18
        Originally posted by NinjaTrader_Bertrand View Post
        Freen, you could take a look at the default EMA indicator how such values could be cumulated.
        Thanks, that´s the hint I needed.


        Would appreciate help why I go wrong here. Why won´t NT accept "sum"? The indicator should cumulate a value (=sum), and plot it with it´s slow SMA and fast EMA. The user defined input variables are "Bars", "PeriodSMA" and "PeriodEMA":

        protectedoverridevoid Initialize()
        {
        Add(new Plot(Color.FromKnownColor(KnownColor.ActiveCaption ), PlotStyle.Line, "Plot0"));
        Add(new Plot(Color.FromKnownColor(KnownColor.Plum), PlotStyle.Line, "Plot1"));
        Add(new Plot(Color.FromKnownColor(KnownColor.Pink), PlotStyle.Line, "Plot2"));
        Add(new Line(Color.FromKnownColor(KnownColor.ActiveCaption ), 0, "Baseline"));
        CalculateOnBarClose = true;
        Overlay = false;
        sum = new DataSeries (this);
        }

        ///<summary>
        /// Called on each bar update event (incoming tick)
        ///</summary>
        protectedoverridevoid OnBarUpdate()
        {
        // Use this method for calculating your indicator values. Assign a value to each
        // plot below by replacing 'Close[0]' with your own formula.

        if (CurrentBar < Bars) return;

        double sum = 0;
        for (int barsAgo = 0; barsAgo < Bars; barsAgo++)
        {
        sum.Set(sum + Input[barsAgo]);
        Plot0.Set(sum[0]);
        Plot1.Set(SMA(sum, PeriodSMA)[0]);
        Plot2.Set(EMA(sum, PeriodEMA)[0]);
        }
        Last edited by FREEN; 04-26-2010, 01:32 PM.

        Comment


          #19
          Hello Freen,

          You are using sum two different ways. Once as a DataSeries and again as a double.

          If you are creating this based on this tutorial, then it's designed to return a double value. This double value cannot be passed into SMA.

          You might look into SUM() method which will add up a series and return a double value.

          If you're trying to create your own DataSeries of SUM, take these steps:

          Make sure it's declared as a DataSeries in the variables region.
          private DataSeries sum;

          Remove the "double sum" declaration in OnBarUpdate()

          The same logic that was used to add a series and return a double won't work in a DataSeries. It might be best to first get the double value using the principles in the tutorial and then Set it with a DataSeries. You have to be sure that you're using unique names for this. sum the double can't also be sum the DataSeries.

          The following tutorial can help with the logic needed to construct you own DataSeries
          http://www.ninjatrader-support.com/H...verview24.html
          Ryan M.NinjaTrader Customer Service

          Comment


            #20
            Doing the math to the double "sumint" and convering it to the data series "sum" as you said worked fine. Thanks lots!


            if
            (CurrentBar < Bars) return;
            double sumint = 0;
            for (int barsAgo = 0; barsAgo < Bars; barsAgo++)
            {
            sumint = sumint + Input[barsAgo];
            }
            Plot0.Set(sumint);
            {
            sum.Set(sumint);
            Plot1.Set(SMA(sum, PeriodSMA)[
            0]);
            Plot2.Set(EMA(sum, PeriodEMA)[
            0]);
            }

            Comment


              #21
              evaluation/calculation sequence

              Would this code be calculated the same...

              if (Bars.FirstBarOfSession==true)
              {
              Bar.Set(
              .37 *(Close[0] - Open[0])*ud);
              Plot0.Set(
              .37 *(Close[0] - Open[0])*ud);
              Plot1.Set(SMA(Bar, PeriodSMA)[
              0]);
              }

              if (Bars.BarsSinceSession==1)
              {
              Bar.Set(
              .48 *(Close[0] - Open[0])*ud);
              Plot0.Set(
              .48 *(Close[0] - Open[0])*ud);
              Plot1.Set(SMA(Bar, PeriodSMA)[
              0]);
              }

              if (Bars.BarsSinceSession==2)
              {
              Bar.Set(
              .6 *(Close[0] - Open[0])*ud);
              Plot0.Set(
              .6 *(Close[0] - Open[0])*ud);
              Plot1.Set(SMA(Bar, PeriodSMA)[
              0]);
              }

              if (Bars.BarsSinceSession==3)
              {
              Bar.Set(
              .67 *(Close[0] - Open[0])*ud);
              Plot0.Set(
              .67 *(Close[0] - Open[0])*ud);
              Plot1.Set(SMA(Bar, PeriodSMA)[
              0]);
              }

              ...as this code:

              if (Bars.FirstBarOfSession==true)
              {
              Bar.Set(
              .37 *(Close[0] - Open[0])*ud);
              Plot0.Set(
              .37 *(Close[0] - Open[0])*ud);
              }

              if (Bars.BarsSinceSession==1)
              {
              Bar.Set(
              .48 *(Close[0] - Open[0])*ud);
              Plot0.Set(
              .48 *(Close[0] - Open[0])*ud);
              }

              if (Bars.BarsSinceSession==2)
              {
              Bar.Set(
              .6 *(Close[0] - Open[0])*ud);
              Plot0.Set(
              .6 *(Close[0] - Open[0])*ud);
              }

              if (Bars.BarsSinceSession==3)
              {
              Bar.Set(
              .67 *(Close[0] - Open[0])*ud);
              Plot0.Set(
              .67 *(Close[0] - Open[0])*ud);
              }
              Plot1.Set(SMA(Bar, PeriodSMA)[0]);

              ...???

              Comment


                #22
                FREEN,

                Unfortunately I would not know. You would need to test this with Print() statements and see what values they actually come out to be.
                Josh P.NinjaTrader Customer Service

                Comment


                  #23
                  Thank´s it plotted the same wich simplified a bit.

                  I´m trying to use a modification of your code inte the SMA tutorial:

                  ------------------------------------------------------
                  if (CurrentBar < Bars) return;
                  double sumint = 0;
                  for (int barsAgo = 0; barsAgo < Bars; barsAgo++)
                  {
                  sumint = sumint + Input[barsAgo];
                  }
                  Plot0.Set(sumint);
                  ------------------------------------------------------


                  ...that in short looks like this:

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

                  if
                  (CurrentBar < Barscum) return;
                  double sumint = 0;

                  if (Bars.FirstBarOfSession==true)
                  {
                  sumint = (.00037 *(Open[0] - Close[0])*ud);
                  }
                  if (Bars.BarsSinceSession==1)
                  {
                  sumint = (.00048 *(Open[0] - Close[0])*ud);
                  }
                  ......


                  double sumintplot = 0;
                  for (int barsAgo = 0; barsAgo < Barscum; barsAgo++);
                  {
                  sumintplot = sumintplot + sumint[barsAgo];
                  }
                  Plot0.Set(sumintplot);
                  ------------------------------------------------------

                  Basicly what I´ve done is to replace "Input[barsAgo]" with the double variable "sumint[barsAgo]".

                  I also tried to convert the variable "sumint" to a DataSeries value, but still get the compilation error ~"the name "barsAgo" is not in it´s right context".

                  Appreciate your help on where I go wrong here, thanks lot´s again!

                  Comment


                    #24
                    FREEN, your sumint is a double, as such there's no history to access with the barsAgo parameter as you've tried. You will need to work with a DataSeries here to be able to access past double values.

                    Comment


                      #25
                      Hello,

                      The tutorial on how to add a data series is pretty good...fyi. This link as well:

                      Comment


                        #26
                        Thanks Bertrand et Mountainclimber. Yes, that part of the NT guide gave me a better insight in the use of DataSeries.

                        Comment


                          #27
                          You are welcome!
                          Last edited by mountainclimber; 05-07-2010, 10:29 AM.

                          Comment


                            #28
                            Would appreciate help on why the double Volbar and the int ud is not accepted in this code:



                            if (Bars.BarsSinceSession==12)
                            {
                            double Volbar = (.00036 *(Open[0] - Close[0]));
                            }
                             

                            //2. VolbarSMA ("PeriodSMA" for SMA calc)
                            VolbarDS.Set(Volbar);
                            double VolbarSMA = (SMA(VolbarDS, PeriodSMA)[0]);



                            //3. Diffbar ("Cutoff" for VolbarSMA level calc)
                            double Diffbar = (Volbar - (VolbarSMA * Cutoff));




                            //4. Filter out neg values and set up/down

                            if (Diffbar < 0)
                            {
                            Diffbar =
                            0;
                            }
                            else
                            {
                            int ud = 1;
                            if ((Opens[1][0]-Closes[1][0])>0)
                            {
                            ud =
                            1;
                            }
                            else
                            {
                            ud = -
                            1;
                            }
                            }

                            DiffbarDS.Set(Diffbar * ud);

                            Comment


                              #29
                              Hello Freen,



                              You're declaring your variables within an if block. Please declare the variable in the variables region.
                              private double Volbar;
                              private int ud;

                              You can then assign its value in that if block without typing the datastructure (double, int).

                              Code:
                               
                              if (Bars.BarsSinceSession==12)
                              {
                              Volbar = (.00036 *(Open[0] - Close[0]));
                              }
                               
                              if (Diffbar < 0) 
                              {
                              Diffbar = 0;
                              }
                              else
                              { 
                              ud = 1;
                              if ((Opens[1][0]-Closes[1][0])>0)
                              {
                              ud = 1;
                              }
                              else
                              {
                              ud = -1;
                              } 
                              }
                              
                              DiffbarDS.Set(Diffbar * ud);



                              Ryan M.NinjaTrader Customer Service

                              Comment


                                #30
                                Tnx! I thought that could be done anywhere within running code.

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                                0 responses
                                607 views
                                0 likes
                                Last Post Geovanny Suaza  
                                Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                                0 responses
                                353 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by Mindset, 02-09-2026, 11:44 AM
                                0 responses
                                105 views
                                0 likes
                                Last Post Mindset
                                by Mindset
                                 
                                Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                                0 responses
                                560 views
                                1 like
                                Last Post Geovanny Suaza  
                                Started by RFrosty, 01-28-2026, 06:49 PM
                                0 responses
                                561 views
                                1 like
                                Last Post RFrosty
                                by RFrosty
                                 
                                Working...
                                X