Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Annual chart

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

    Annual chart

    I get the annual volatility in daily for the last year, but I want that in the annual chart also for the last 15 years, but it is not possible for me, as I need 252 candles per year and the annual chart only gets 10 or 15 candles.
    Any ideas?​​

    #2
    Hello:
    I have used this method and I can get in annual the daily data, but it piles up the data.
    I have created a condition in the update method but it doesn't fix it

    else if (State == State.Configure)
    {
    //Add the daily series to work on a yearly
    AddDataSeries(BarsPeriodType.Day, 1);
    AddDataSeries(BarsPeriodType.Day, 252);
    }

    Code:
    //This namespace holds Indicators in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Indicators.Favoritos
    {
        public class VariacionAnual : Indicator
        {
            double maxAno;
            double minAno;
            double Apertura;
            double Cierre;
            private Series<double> rendimientosDiarios;
            int corre;
            
            protected override void OnStateChange()
            {
                
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Escriba la descripción de su nuevo Indicador personalizado aquí.";
                    Name                                        = "VariacionAnual";
                    //IsVisible = false;
                    IsOverlay = true;
                    Calculate = Calculate.OnPriceChange;
                }
                else if (State == State.DataLoaded)
                {
                   rendimientosDiarios = new Series<double>(this);
                }
                
                else if (State == State.Configure)
                {
                    //Añadimos la serie diaria para trabajar en anual
                    AddDataSeries(BarsPeriodType.Day, 1);
                    AddDataSeries(BarsPeriodType.Day, 252);
                }
            }
    
            protected override void OnBarUpdate()
            {
                 // Aproximadamente 252 días hábiles en un año          
                 if (CurrentBar < 253)
                 return;
                 if (BarsInProgress == 2)
                 {
                // Calcula la volatilidad anual
                // Calcula el rendimiento diario
                  double rendimientoDiario = (Close[0] - Close[1]) / Close[1];
                  rendimientosDiarios[0] = rendimientoDiario;          
                  double suma = 0;
                  double sumaCuadrados = 0;
    
                 for ( int i = 0; i < 252; i++)
                {
                suma += rendimientosDiarios[i];
                sumaCuadrados += Math.Pow(rendimientosDiarios[i], 2);
                }
                
               double media = suma / 252;
               double varianza = (sumaCuadrados / 252) - Math.Pow(media, 2);
               double desviacionEstandarDiaria = Math.Sqrt(varianza);
               double volatilidadAnual = desviacionEstandarDiaria * Math.Sqrt(252);
                }
                // Calcular los valores máximos y mínimos del año
                 maxAno = High[0];
                 minAno = Low[0];
                 Apertura= Open[0];
                 Cierre = Close[0];
                 NinjaTrader.Gui.Tools.SimpleFont myFont = new NinjaTrader.Gui.Tools.SimpleFont("Courier New", 12) { Size = 15, Bold = true };
                 double variAnual = ((maxAno - minAno)/minAno)  * 100;  
                 double RendimientoAnual = ((Cierre - Apertura)/Apertura)  * 100;
            
                 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.Text(this, "Volatilidad" + CurrentBar, String.Format("{0:0.##}",volatilidadAnual), 0, Open[0], Brushes.Blue);
                 Draw.TextFixed(this, "Text" , corre.ToString(),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);
                 }
            }
        
    }​ [ATTACH=JSON]{"data-align":"none","data-size":"medium","data-attachmentid":1318315}[/ATTACH]
    Attached Files

    Comment


      #3
      Hello Gibranes,

      With:
      AddDataSeries(BarsPeriodType.Day, 252);

      You would be adding 1 bar that spans 252 days.

      I think you want to just be using a 1 day series. Weekends and holidays will automatically be excluded by the Trading hours template.
      Chelsea B.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      43 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      26 views
      0 likes
      Last Post PaulMohn  
      Started by CarlTrading, 03-31-2026, 09:41 PM
      1 response
      163 views
      1 like
      Last Post NinjaTrader_ChelseaB  
      Started by CarlTrading, 04-01-2026, 02:41 AM
      0 responses
      98 views
      1 like
      Last Post CarlTrading  
      Started by CaptainJack, 03-31-2026, 11:44 PM
      0 responses
      158 views
      2 likes
      Last Post CaptainJack  
      Working...
      X