Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Indicator is not showing on chart

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

    Indicator is not showing on chart

    I'm trying to show an indicator on the chart. I compiled it with no errors and Ninja allows me to apply it to the chart, but nothing shows at all. Any help is appreciated. Here is the code:

    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.Gui.Tools;
    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
    {
    public class BeniFractals : Indicator
    {
    private double fracPriceUp = double.NaN;
    private double fracPriceDown = double.NaN;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Name = "Harko Fractals";
    AddPlot(Brushes.Green, "Up Fractal");
    AddPlot(Brushes.Red, "Down Fractal");
    AddPlot(Brushes.Green, "Up Fractal Price");
    AddPlot(Brushes.Red, "Down Fractal Price");
    }
    }

    protected override void OnBarUpdate()
    {
    double candleMax = Math.Max(Open[0], Close[0]);
    double candleMin = Math.Min(Open[0], Close[0]);
    bool candleIsGreen = Close[0] >= Open[0];
    bool candleIsRed = Close[0] < Open[0];

    // Fractal One
    bool fracOneIsUp = candleIsGreen && Low[1] > High[2] && High[1] > High[2] && Low[0] < High[1];
    bool fracOneIsDown = candleIsRed && High[1] < Low[2] && Low[1] < Low[2] && High[0] > Low[1];

    // Fractal Two
    bool fracTwoIsUp = candleIsGreen && Low[1] > High[3] && Low[2] > High[3] && High[1] > High[3] && Low[0] < High[1] && Low[0] < High[2];
    bool fracTwoIsDown = candleIsRed && High[1] < Low[3] && High[2] < Low[3] && Low[1] < Low[3] && High[0] > Low[1] && High[0] > Low[2];

    bool fracIsUp = fracOneIsUp || fracTwoIsUp;
    bool fracIsDown = fracOneIsDown || fracTwoIsDown;

    fracPriceUp = fracIsUp ? (fracOneIsUp ? High[1] : High[3]) : fracPriceUp;
    fracPriceDown = fracIsDown ? (fracOneIsDown ? Low[1] : Low[3]) : fracPriceDown;

    Values[0][0] = fracIsUp ? High[0] : double.NaN;
    Values[1][0] = fracIsDown ? Low[0] : double.NaN;
    Values[2][0] = fracPriceUp;
    Values[3][0] = fracPriceDown;
    }
    }
    }

    region NinjaScript generated code. Neither change nor remove.

    namespace NinjaTrader.NinjaScript.Indicators
    {
    public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
    {
    private BeniFractals[] cacheBeniFractals;
    public BeniFractals BeniFractals()
    {
    return BeniFractals(Input);
    }

    public BeniFractals BeniFractals(ISeries<double> input)
    {
    if (cacheBeniFractals != null)
    for (int idx = 0; idx < cacheBeniFractals.Length; idx++)
    if (cacheBeniFractals[idx] != null && cacheBeniFractals[idx].EqualsInput(input))
    return cacheBeniFractals[idx];
    return CacheIndicator<BeniFractals>(new BeniFractals(), input, ref cacheBeniFractals);
    }
    }
    }

    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
    public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
    {
    public Indicators.BeniFractals BeniFractals()
    {
    return indicator.BeniFractals(Input);
    }

    public Indicators.BeniFractals BeniFractals(ISeries<double> input )
    {
    return indicator.BeniFractals(input);
    }
    }
    }

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
    {
    public Indicators.BeniFractals BeniFractals()
    {
    return indicator.BeniFractals(Input);
    }

    public Indicators.BeniFractals BeniFractals(ISeries<double> input )
    {
    return indicator.BeniFractals(input);
    }
    }
    }

    #endregion

    #2
    Hello benmuresan,

    Thanks for your post and welcome to the NinjaTrader Forums!

    When testing the code you shared on my end I see an error occurs in the Log tab of the Control Center which is preventing the indicator from plotting values.

    Indicator 'BeniFractals': Error on calling 'OnBarUpdate' method on bar 0: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart.

    This error message indicates that you are trying to access a BarsAgo value that is not valid. A more simple example using one series would be on bar 5 you check for 6 BarsAgo. There are not yet 6 bars so the CurrentBar minus 6 would be a negative number or a non-existent bar.

    A CurrentBar check could be used in your indicator's logic to ensure that a certain number of bars have processed before the indicator begins calculation.

    See the help guide documentation below for more information.

    CurrentBar - https://ninjatrader.com/support/help...currentbar.htm
    Make sure you have enough bars - https://ninjatrader.com/support/help...nough_bars.htm


    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thanks Brandon, I updated as you said and I no longer get that error, but still no plotting. Here is the updated code. Thanks for your help.

      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.Gui.Tools;
      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
      {
      public class BFractals : Indicator
      {
      private double fracPriceUp = double.NaN;
      private double fracPriceDown = double.NaN;

      protected override void OnStateChange()
      {
      if (State == State.SetDefaults)
      {
      Name = "BFractals";
      AddPlot(Brushes.Green, "Up Fractal");
      AddPlot(Brushes.Red, "Down Fractal");
      AddPlot(Brushes.Green, "Up Fractal Price");
      AddPlot(Brushes.Red, "Down Fractal Price");
      }
      }

      protected override void OnBarUpdate()
      {
      // Check if there are enough bars to calculate the indicator
      if (CurrentBar < 25)
      return;

      double candleMax = Math.Max(Open[0], Close[0]);
      double candleMin = Math.Min(Open[0], Close[0]);
      bool candleIsGreen = Close[0] >= Open[0];
      bool candleIsRed = Close[0] < Open[0];

      // Fractal One
      bool fracOneIsUp = candleIsGreen && Low[1] > High[2] && High[1] > High[2] && Low[0] < High[1];
      bool fracOneIsDown = candleIsRed && High[1] < Low[2] && Low[1] < Low[2] && High[0] > Low[1];

      // Fractal Two
      bool fracTwoIsUp = candleIsGreen && Low[1] > High[3] && Low[2] > High[3] && High[1] > High[3] && Low[0] < High[1] && Low[0] < High[2];
      bool fracTwoIsDown = candleIsRed && High[1] < Low[3] && High[2] < Low[3] && Low[1] < Low[3] && High[0] > Low[1] && High[0] > Low[2];

      bool fracIsUp = fracOneIsUp || fracTwoIsUp;
      bool fracIsDown = fracOneIsDown || fracTwoIsDown;

      fracPriceUp = fracIsUp ? (fracOneIsUp ? High[1] : High[3]) : fracPriceUp;
      fracPriceDown = fracIsDown ? (fracOneIsDown ? Low[1] : Low[3]) : fracPriceDown;

      Values[0][0] = fracIsUp ? High[0] : double.NaN;
      Values[1][0] = fracIsDown ? Low[0] : double.NaN;
      Values[2][0] = fracPriceUp;
      Values[3][0] = fracPriceDown;
      }
      }
      }

      region NinjaScript generated code. Neither change nor remove.

      namespace NinjaTrader.NinjaScript.Indicators
      {
      public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
      {
      private BFractals[] cacheBFractals;
      public BFractals BFractals()
      {
      return BFractals(Input);
      }

      public BFractals BFractals(ISeries<double> input)
      {
      if (cacheBFractals != null)
      for (int idx = 0; idx < cacheBFractals.Length; idx++)
      if (cacheBFractals[idx] != null && cacheBFractals[idx].EqualsInput(input))
      return cacheBFractals[idx];
      return CacheIndicator<BFractals>(new BFractals(), input, ref cacheBFractals);
      }
      }
      }

      namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
      {
      public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
      {
      public Indicators.BFractals BFractals()
      {
      return indicator.BFractals(Input);
      }

      public Indicators.BFractals BFractals(ISeries<double> input )
      {
      return indicator.BFractals(input);
      }
      }
      }

      namespace NinjaTrader.NinjaScript.Strategies
      {
      public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
      {
      public Indicators.BFractals BFractals()
      {
      return indicator.BFractals(Input);
      }

      public Indicators.BFractals BFractals(ISeries<double> input )
      {
      return indicator.BFractals(input);
      }
      }
      }

      #endregion

      Comment


        #4
        Hello benmuresan,

        Thanks for your notes.

        When adding the CurrentBar check to the code you shared in post # 1 I see the indicator is plotting values.

        See the attached screenshot.

        Attached is the script used for testing this.

        Make sure that you are removing the indicator from the chart and adding the indicator back to the chart after making changes to the indicator's code and compiling.
        Attached Files
        Brandon H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Segwin, 05-07-2018, 02:15 PM
        14 responses
        1,788 views
        0 likes
        Last Post aligator  
        Started by Jimmyk, 01-26-2018, 05:19 AM
        6 responses
        837 views
        0 likes
        Last Post emuns
        by emuns
         
        Started by jxs_xrj, 01-12-2020, 09:49 AM
        6 responses
        3,293 views
        1 like
        Last Post jgualdronc  
        Started by Touch-Ups, Today, 10:36 AM
        0 responses
        13 views
        0 likes
        Last Post Touch-Ups  
        Started by geddyisodin, 04-25-2024, 05:20 AM
        11 responses
        63 views
        0 likes
        Last Post halgo_boulder  
        Working...
        X