Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Last value

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

    Last value

    I have this loop in the Onbarupdate method and I see it in the chart with draw.textfixed.
    How do I get the last value, because as I iterate the value changes, while the onbarupdate method is loading the bars.

    for (int i = 0; i < CounterVL; i++)
    {
    mean1=AnnualSumPerformance/CounterVL;
    }
    I need the last value of mean1​

    #2
    Hello Gibranes,

    As this is a single value variable and not a collection like an array or list, whatever value is assigned to the variable when the loop finishes will be the last value.

    for (int i = 0; i < CounterVL; i++)
    {
    mean1=AnnualSumPerformance/CounterVL;
    }​

    Print(mean1); // this prints the last value of this variable.


    If you were using a collection like a Series, array, or list, the last element in the collection would be either

    mean[0] if a series

    or

    mean[mean.Count() - 1] if an array or list or other collection type.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      If I understand that and I see that it is Print (mean1) is the last value.
      But I need mean2 to be equal to mean1(ultima).
      How do I do it?
      I can create an array I think it will be the easiest for me.​

      Comment


        #4
        Hello Gibranes,

        I'm not sure what mean1(ultima) is.

        Are you trying to save a value for every bar of the chart.

        Are you trying to save a non-determined collection of values?

        Are you trying to save a determined collection of values?

        Are you trying to create an indicator?

        Do you want the mean of the bar?
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          I want to get the last value of loop media1 to develop with it another formula

          protected override void OnBarUpdate()
          {
          if (BarsInProgress == 1) // Serie de datos mensual
          {
          // Calcular los valores máximos y mínimos del ańo
          maxAno = Highs[1][0];
          minAno = Lows[1][0];
          Apertura= Opens[1][0];
          Cierre = Closes[1][0];

          variAnual = ((maxAno - minAno)/minAno) * 100;
          RendimientoAnual = ((Cierre - Apertura)/Apertura) * 100;
          SumaRendimientoAnual= SumaRendimientoAnual+RendimientoAnual;

          if (CurrentBar==Contador)
          {
          rendimientosDiarios[Contador]=RendimientoAnual;
          Desviacion[Contador]=rendimientosDiarios[Contador];
          Contador++;
          }
          int ContadorVL=rendimientosDiarios.Count;


          for (int i = 0; i < ContadorVL; i++)
          {
          media1=SumaRendimientoAnual/ContadorVL;
          }

          // DEBO SABER CUANDO EL MEDIA1 HA TERMINADO DE CARGAR TODOS LOS DATOS
          //REPRESENTACION TEXTO

          Draw.Text(this, "Rendimiento" + CurrentBar, String.Format("{0:0.##}",RendimientoAnual), 0, High[0] , Brushes.Green);
          Draw.Text(this, "Diferencia" + CurrentBar, String.Format("{0:0.##}",variAnual), 0, Low[0]-2 * TickSize, Brushes.Red);
          Draw.TextFixed(this, "Text" , "Rentabilidad:" +String.Format("{0:0.##}", media1),TextPosition.TopLeft,Brushes.Green, myFont, Brushes.Transparent, Brushes.Transparent, 100);
          Draw.TextFixed(this, "Text1" , "\nVariacion",TextPosition.TopLeft,Brushes.Red , myFont, Brushes.Transparent, Brushes.Transparent, 100);
          Draw.TextFixed(this, "V", ""+ String.Format("{0:0.##}",SumaRendimientoAnual), TextPosition.TopRight, Brushes.Green, myFont, Brushes.Transparent, Brushes.Transparent, 100);
          Print (media1);
          }​


          Comment


            #6
            Hello Gibranes,

            "I want to get the last value of loop media1 to develop with it another formula"

            Your variable is a single value. Since it's just one value it would be the last (and the first) value..

            Are you expecting media1 to have multiple values like a collection?
            Where are you adding multiple values?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              I want to assign the last value of 1.05 here
              Desviacion[i]=Desviacion[i]-media1;

              Comment


                #8
                Hello Gibranes,

                You want to assign the last value of Desviacion collection to the media1 variable?

                Desviacion[0] = media1;

                or

                Desviacion[Desviacion.Count() - 1] = media1;

                Depending on what type of collection Desviacion is.
                Chelsea B.NinjaTrader Customer Service

                Comment


                  #9
                  Maybe I am not making myself clear.
                  The final value of the average1 is 1.06 and I want it to subtract it from the high value of the bar and to paint it in red under the bar.
                  What I am subtracting are the steps of the loop until I reach 1.06.
                  For example first bar : 1.47 (green) -1.06 should not be 1.35​

                  Click image for larger version

Name:	imagen_2024-09-26_182428583.png
Views:	116
Size:	46.8 KB
ID:	1319394

                  Comment


                    #10
                    This the codigo
                    protected override void OnBarUpdate()
                    {

                    if (BarsInProgress == 1) // Serie de datos mensual
                    {
                    // Calcular los valores máximos y mínimos del ańo
                    maxAno = Highs[1][0];
                    minAno = Lows[1][0];
                    Apertura= Opens[1][0];
                    Cierre = Closes[1][0];

                    variAnual = ((maxAno - minAno)/minAno) * 100;
                    RendimientoAnual = ((Cierre - Apertura)/Apertura) * 100;
                    SumaRendimientoAnual= SumaRendimientoAnual+RendimientoAnual;

                    //if (CurrentBar==Contador)
                    //{
                    rendimientosDiarios[Contador]=RendimientoAnual;
                    Desviacion[Contador]=rendimientosDiarios[Contador];
                    Contador++;
                    //}
                    int ContadorVL=rendimientosDiarios.Count;

                    for (int i = 0; i < ContadorVL; i++)
                    {
                    media1=SumaRendimientoAnual/ContadorVL;
                    Desviacion[i]=Desviacion[i]-media1;
                    }

                    // DEBO SABER CUANDO EL MEDIA1 HA TERMINADO DE CARGAR TODOS LOS DATOS
                    //REPRESENTACION TEXTO

                    Draw.Text(this, "Rendimiento" + CurrentBar, String.Format("{0:0.##}",RendimientoAnual), 0, High[0] , Brushes.Green);
                    Draw.Text(this, "Diferencia" + CurrentBar, String.Format("{0:0.##}",Desviacion[Paso]), 0, Low[0]-2 * TickSize, Brushes.Red);
                    //Draw.Text(this, "Diferencia" + CurrentBar, String.Format("{0:0.##}",variAnual), 0, Low[0]-2 * TickSize, Brushes.Red);
                    Draw.TextFixed(this, "Text" , "Media1:" +String.Format("{0:0.##}",media1),TextPosition.Top Left,Brushes.Green, myFont, Brushes.Transparent, Brushes.Transparent, 100);
                    Draw.TextFixed(this, "Text1" , "\nVariacion",TextPosition.TopLeft,Brushes.Red , myFont, Brushes.Transparent, Brushes.Transparent, 100);
                    Draw.TextFixed(this, "V", ""+ String.Format("{0:0.##}",SumaRendimientoAnual), TextPosition.TopRight, Brushes.Green, myFont, Brushes.Transparent, Brushes.Transparent, 100);
                    ++Paso;
                    }​

                    Comment


                      #11
                      Hello Gibranes,

                      You want to subtract the value of media1 from the High[0], and draw this price as text under the bar.

                      Draw.Text(this, "myText" + CurrentBar, (High[0] - media1).ToString(), 0, Low[0] - 1, Brushes.Red);
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        I already do that and it is not what I want.
                        I want to subtract the value of mean1 when all the bars have been loaded not in the currentbar, since the value of mean1 is different until all the bars are loaded.
                        Currentbar 1 (average1) =1

                        Currentbar 2 (mean1) =0.5

                        Currentbar 3 (mean1) =1.5

                        Currentbar Ultima (mean1) = 3 is the sum of all currentbars.
                        I want to get the value 3 and put under the bar or save the variable

                        Comment


                          #13
                          Hello Gibranes,

                          New bars can keep appearing in real-time as the market trades.. there would never be a time all real-time bars have loaded unless the instrument has stopped trading on the exchange.

                          Do you mean all historical bars have been processed in OnBarUpdate()?

                          if (State == State.Realtime || (State == State.Historical && CurrentBar == Count - 2))
                          {
                          Draw.Text(this, "myText" + CurrentBar, (High[0] - media1)​.ToString(), 0, Low[0] - 1, Brushes.Red);​
                          }
                          Chelsea B.NinjaTrader Customer Service

                          Comment


                            #14
                            if (State == State.Realtime || (State.Historical && CurrentBar == Count - 2))
                            {br data-dl-uid="163"> Draw.Text(this, "myText" + CurrentBar, (High[0] - media1).ToString(), 0, Low[0] - 1, Brushes.Red);
                            }
                            This code not work (Operator && cannot be applied to operands of type state and bool)​

                            Comment


                              #15
                              Hello Gibranes,

                              Apologies that should be State == State.Historical.

                              Corrected above.
                              Chelsea B.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              556 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              324 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              101 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              545 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              547 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X