Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Compiles... Then Indicator Disappears

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

    Compiles... Then Indicator Disappears

    I am trying to add some paintbars to an overbought/oversold stochastic with some conditions. Everything works, it compiles, paints the bars as expected... until I insert the last conditional line highlighted below. It still compiles, but then the stochastic and the paintbars disappear when I reload ninjascript. Is there something I am doing wrong with nesting the conditional statements? Admittedly I am not even close to a programmer, just cobbled together various bits over the years. Any help? Thanks!


    Code:
    if ( DUpper[0] > 70) 
                {
                        if ( (SMA(1500)[0] > SMA(1500)[1]) && (DUpper[0] > 80))
                            BarBrush = Brushes.Gold;
                        else if ( SMA(1500)[0] <= SMA(1500)[1] )
                            BarBrush = Brushes.Red;
                }
    
      else if ( DLower[0] < 30) 
                {
                        if ( DLower[0] < 20 )
                            BarBrush = Brushes.Gold; 
    [COLOR=#FF0000]  else if ( SMA(1500)[0] >= SMA(1500)[1])[/COLOR]
                            BarBrush = Brushes.LimeGreen;
                }
    
    
        else BarBrush = null;

    #2
    did you declare High = DUpper[0] and Low=DLower[0]? There is no K, D DLine for stoch.

    if()
    {}
    else if{}
    if{}

    if you want i can give a try.thank you. Is it strategy or indicator?
    Ticks or bars?
    Last edited by Emma1; 03-14-2020, 08:45 AM.

    Comment


      #3
      Thank you for the offer to help! I just put the snippet into regular ole stochastics for simplicity sake.... same difference to me. See the entire code below (almost entire just copy paste from stochastics, then the code from before edited to fit into stochastics and highlighted). This is for an indicator that I use on tick charts. Thank you in advance for any insight!

      Code:
          //
      // Copyright (C) 2019, NinjaTrader LLC <www.ninjatrader.com>.
      // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
      //
      #region Using declarations
      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.ComponentModel.DataAnnotations;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows;
      using System.Windows.Input;
      using System.Windows.Media;
      using System.Xml.Serialization;
      using NinjaTrader.Cbi;
      using NinjaTrader.Gui;
      using NinjaTrader.Gui.Chart;
      using NinjaTrader.Gui.SuperDom;
      using NinjaTrader.Data;
      using NinjaTrader.NinjaScript;
      using NinjaTrader.Core.FloatingPoint;
      using NinjaTrader.NinjaScript.DrawingTools;
      #endregion
      
      // This namespace holds indicators in this folder and is required. Do not change it.
      namespace NinjaTrader.NinjaScript.Indicators
      {
          /// <summary>
          /// The Stochastic Oscillator is made up of two lines that oscillate between
          /// a vertical scale of 0 to 100. The %K is the main line and it is drawn as
          /// a solid line. The second is the %D line and is a moving average of %K.
          /// The %D line is drawn as a dotted line. Use as a buy/sell signal generator,
          /// buying when fast moves above slow and selling when fast moves below slow.
          /// </summary>
          public class StochSlopeBuy : Indicator
          {
              private Series<double>        den;
              private Series<double>        fastK;
              private MIN                    min;
              private MAX                    max;
              private Series<double>        nom;
              private SMA                    smaFastK;
              private SMA                    smaK;
      
              protected override void OnStateChange()
              {
                  if (State == State.SetDefaults)
                  {
                      Description                    = NinjaTrader.Custom.Resource.NinjaScriptIndicatorDescriptionStochastics;
                      Name                        = "StochSlopeBuy";
                      IsSuspendedWhileInactive    = true;
                      PeriodD                        = 13;
                      PeriodK                        = 13;
                      Smooth                        = 5;
      
                      AddPlot(Brushes.DodgerBlue,        NinjaTrader.Custom.Resource.StochasticsD);
                      AddPlot(Brushes.Goldenrod,        NinjaTrader.Custom.Resource.StochasticsK);
      
                      AddLine(Brushes.DarkCyan,    20,    NinjaTrader.Custom.Resource.NinjaScriptIndicatorLower);
                      AddLine(Brushes.DarkCyan,    80,    NinjaTrader.Custom.Resource.NinjaScriptIndicatorUpper);
                  }
                  else if (State == State.DataLoaded)
                  {
                      den            = new Series<double>(this);
                      nom            = new Series<double>(this);
                      fastK        = new Series<double>(this);
                      min            = MIN(Low, PeriodK);
                      max            = MAX(High, PeriodK);
                      smaFastK    = SMA(fastK, Smooth);
                      smaK        = SMA(K, PeriodD);
                  }
              }
      
              protected override void OnBarUpdate()
              {
                  double min0 = min[0];
                  nom[0]        = Close[0] - min0;
                  den[0]        = max[0] - min0;
      
                  if (den[0].ApproxCompare(0) == 0)
                      fastK[0] = CurrentBar == 0 ? 50 : fastK[1];
                  else
                      fastK[0] = Math.Min(100, Math.Max(0, 100 * nom[0] / den[0]));
      
                  // Slow %K == Fast %D
                  K[0] = smaFastK[0];
                  D[0] = smaK[0];
      
      [COLOR=#FF0000]   //PaintBars for Oversold and Overbought with Slope in Favor
                  if ( D[0] > 70) 
                      {
                          if ( (SMA(1500)[0] > SMA(1500)[1]) && (D[0] > 80))
                              BarBrush = Brushes.Gold;
                          else if ( SMA(1500)[0] <= SMA(1500)[1] )
                              BarBrush = Brushes.Red;
                      }
      
                    else if ( D[0] < 30) 
                      {
                          if ((SMA(1500)[0] < SMA(1500)[1]) && D[0] < 20 )
                              BarBrush = Brushes.Gold; 
                            else if ( SMA(1500)[0] >= SMA(1500)[1])
                              BarBrush = Brushes.LimeGreen;
                          }
      
      
                  else BarBrush = null;[/COLOR]
      
      
      
      
              }
      
              #region Properties
              [Browsable(false)]
              [XmlIgnore()]
              public Series<double> D
              {
                  get { return Values[0]; }
              }
      
              [Browsable(false)]
              [XmlIgnore()]
              public Series<double> K
              {
                  get { return Values[1]; }
              }
      
              [Range(1, int.MaxValue), NinjaScriptProperty]
              [Display(ResourceType = typeof(Custom.Resource), Name = "PeriodD", GroupName = "NinjaScriptParameters", Order = 0)]
              public int PeriodD
              { get; set; }
      
              [Range(1, int.MaxValue), NinjaScriptProperty]
              [Display(ResourceType = typeof(Custom.Resource), Name = "PeriodK", GroupName = "NinjaScriptParameters", Order = 1)]
              public int PeriodK
              { get; set; }
      
              [Range(1, int.MaxValue), NinjaScriptProperty]
              [Display(ResourceType = typeof(Custom.Resource), Name = "Smooth", GroupName = "NinjaScriptParameters", Order = 2)]
              public int Smooth
              { get; set; }
              #endregion
          }
      }
      
      #region NinjaScript generated code. Neither change nor remove.
      
      namespace NinjaTrader.NinjaScript.Indicators
      {
          public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
          {
              private StochSlopeBuy[] cacheStochSlopeBuy;
              public StochSlopeBuy StochSlopeBuy(int periodD, int periodK, int smooth)
              {
                  return StochSlopeBuy(Input, periodD, periodK, smooth);
              }
      
              public StochSlopeBuy StochSlopeBuy(ISeries<double> input, int periodD, int periodK, int smooth)
              {
                  if (cacheStochSlopeBuy != null)
                      for (int idx = 0; idx < cacheStochSlopeBuy.Length; idx++)
                          if (cacheStochSlopeBuy[idx] != null && cacheStochSlopeBuy[idx].PeriodD == periodD && cacheStochSlopeBuy[idx].PeriodK == periodK && cacheStochSlopeBuy[idx].Smooth == smooth && cacheStochSlopeBuy[idx].EqualsInput(input))
                              return cacheStochSlopeBuy[idx];
                  return CacheIndicator<StochSlopeBuy>(new StochSlopeBuy(){ PeriodD = periodD, PeriodK = periodK, Smooth = smooth }, input, ref cacheStochSlopeBuy);
              }
          }
      }
      
      namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
      {
          public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
          {
              public Indicators.StochSlopeBuy StochSlopeBuy(int periodD, int periodK, int smooth)
              {
                  return indicator.StochSlopeBuy(Input, periodD, periodK, smooth);
              }
      
              public Indicators.StochSlopeBuy StochSlopeBuy(ISeries<double> input , int periodD, int periodK, int smooth)
              {
                  return indicator.StochSlopeBuy(input, periodD, periodK, smooth);
              }
          }
      }
      
      namespace NinjaTrader.NinjaScript.Strategies
      {
          public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
          {
              public Indicators.StochSlopeBuy StochSlopeBuy(int periodD, int periodK, int smooth)
              {
                  return indicator.StochSlopeBuy(Input, periodD, periodK, smooth);
              }
      
              public Indicators.StochSlopeBuy StochSlopeBuy(ISeries<double> input , int periodD, int periodK, int smooth)
              {
                  return indicator.StochSlopeBuy(input, periodD, periodK, smooth);
              }
          }
      }
      
      #endregion

      Comment


        #4
        It's easier to analyze if you open a NinjaScript Output window, if there is an error it show up, however it's not always very detailed.

        Code:
        else BarBrush = null;
        BarBrush does not need to be reset, it reverts to default automatically on the next bar.

        I'd guess, it's an index out of bounds problem, despite that meaning it would not only be the line you described.
        You could try:
        Code:
            protected override void OnBarUpdate()
            {
                if(CurrentBar < 2) return;
        Last edited by MojoJojo; 03-14-2020, 01:40 PM.

        Comment


          #5
          Hi Mabba, thanks for posting.

          MojoJojo has the correct answer in this thread. If something unexpected goes on when you launch your script make sure to check the log tab of your control center.

          Kind regards.

          Comment


            #6
            Thank you all for the input! I made the tweaks this morning and vwala!

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            648 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            369 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            108 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            572 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            573 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X