Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Horizontal Lines not showing on chart

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

    Horizontal Lines not showing on chart

    Hi all,

    I've recently been developing an indicator to dynamically draw horizontal lines on the charts based on the values stored in an array of values. Attached below is the logic for drawing the lines and the code compiles fine but strangely, the lines are not showing up on the chart.

    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 TestIndicator : Indicator
        {
            private int Counter = 0;
    
            private double[] HighMaximas;
            private double[] LowMinimas;
            private double[] SlidingWindow;
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"Enter the description for your new custom Indicator here.";
                    Name                                        = "TestIndicator";
                    Calculate                                    = Calculate.OnEachTick;
                    IsOverlay                                    = true;
                    DisplayInDataBox                            = false;
                    DrawOnPricePanel                            = false;
                    DrawHorizontalGridLines                        = true;
                    DrawVerticalGridLines                        = true;
                    PaintPriceMarkers                            = true;
                    ScaleJustification                            = NinjaTrader.Gui.Chart.ScaleJustification.Right;
                    //Disable this property if your indicator requires custom values that cumulate with each new market data event.
                    //See Help Guide for additional information.
                    IsSuspendedWhileInactive                    = true;
                    NumberOfCandles                    = 45;
                    WindowLength                    = 3;
                    NumberOfValues                    = 3;
                }
                else if (State == State.Configure)
                {
                    SlidingWindow = new double[WindowLength];
                    HighMaximas = new double[NumberOfValues];
                    LowMinimas = new double[NumberOfValues];
                }
            }
    
            protected override void OnBarUpdate()
            {
                for(int i = 0; i < NumberOfCandles; i++)
                {
                    SlidingWindow[0] = Highs[0][i];
                    SlidingWindow[1] = Highs[0][i+1];
                    SlidingWindow[2] = Highs[0][i+2];
    
                    if((SlidingWindow[1] > SlidingWindow[0]) && (SlidingWindow[1] > SlidingWindow[2]))
                    {
                        HighMaximas[Counter] = SlidingWindow[1];
                        Counter ++;
                    }
    
                    else if(Counter == NumberOfValues)
                    {
                        Counter = 0;
                        break;
                    }
    
                    else
                    {
                        ;
                    }
                }
    
                for(int i = 0; i < NumberOfCandles; i++)
                {
                    SlidingWindow[0] = Lows[0][i];
                    SlidingWindow[1] = Lows[0][i+1];
                    SlidingWindow[2] = Lows[0][i+2];
    
                    if((SlidingWindow[1] < SlidingWindow[0]) && (SlidingWindow[1] < SlidingWindow[2]))
                    {
                        LowMinimas[Counter] = SlidingWindow[1];
                        Counter ++;
                    }
    
                    else if(Counter == NumberOfValues)
                    {
                        Counter = 0;
                        break;
                    }
    
                    else
                    {
                        ;
                    }
    
                    Draw.HorizontalLine(this, "Value1", HighMaximas[0], Brushes.Red);
                }
            }        [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="NumberOfCandles", Description="Candles to scan for stop zones", Order=1, GroupName="Parameters")]
            public int NumberOfCandles
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="WindowLength", Description="Window length of sliding window", Order=2, GroupName="Parameters")]
            public int WindowLength
            { get; set; }
    
            [NinjaScriptProperty]
            [Range(1, int.MaxValue)]
            [Display(Name="NumberOfValues", Description="Number of values to output", Order=3, GroupName="Parameters")]
            public int NumberValues
            { get; set; }
    
    
    
    
    
    
    
        }
    }
    
    #region NinjaScript generated code. Neither change nor remove.
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
        public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
        {
            private TestIndicator[] cacheTestIndicator;
            public TestIndicator TestIndicator(int numberOfCandles, int windowLength, int numberOfValues)
            {
                return TestIndicator(Input, numberOfCandles, windowLength, numberOfValues);
            }
    
            public TestIndicator TestIndicator(ISeries<double> input, int numberOfCandles, int windowLength, int numberOfValues)
            {
                if (cacheTestIndicator != null)
                    for (int idx = 0; idx < cacheTestIndicator.Length; idx++)
                        if (cacheTestIndicator[idx] != null && cacheTestIndicator[idx].NumberOfCandles == numberOfCandles && cacheTestIndicator[idx].WindowLength == windowLength && cacheTestIndicator[idx].NumberOfValues == numberOfValues && cacheTestIndicator[idx].EqualsInput(input))
                            return cacheTestIndicator[idx];
                return CacheIndicator<TestIndicator>(new TestIndicator(){ NumberOfCandles = numberOfCandles, WindowLength = windowLength, NumberOfValues = numberOfValues }, input, ref cacheTestIndicator);
            }
        }
    }
    
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
        public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
        {
            public Indicators.TestIndicator TestIndicator(int numberOfCandles, int windowLength, int numberOfValues)
            {
                return indicator.TestIndicator(Input, numberOfCandles, windowLength, numberOfValues);
            }
    
            public Indicators.TestIndicator TestIndicator(ISeries<double> input , int numberOfCandles, int windowLength, int numberOfValues)
            {
                return indicator.TestIndicator(input, numberOfCandles, windowLength, numberOfValues);
            }
        }
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
        {
            public Indicators.TestIndicator TestIndicator(int numberOfCandles, int windowLength, int numberOfValues)
            {
                return indicator.TestIndicator(Input, numberOfCandles, windowLength, numberOfValues);
            }
    
            public Indicators.TestIndicator TestIndicator(ISeries<double> input , int numberOfCandles, int windowLength, int numberOfValues)
            {
                return indicator.TestIndicator(input, numberOfCandles, windowLength, numberOfValues);
            }
        }
    }
    
    #endregion
    Am I doing something wrong? Any help would be greatly appreciated.
    Last edited by MrSomebody; 03-15-2020, 11:51 AM.

    #2
    Hi MrSomebody, thanks for your post.

    Do you have any errors in the Log tab of your Control Center after running your script? It seems like you have an indexing error in the script. In OnBarUpdate the code is accessing the High values without checking to make sure there are enough bars on the chart yet:

    Code:
    protected override void OnBarUpdate()
            {
                for(int i = 0; i < NumberOfCandles; i++)
                {
                    SlidingWindow[0] = Highs[0][i];
                    SlidingWindow[1] = Highs[0][i+1];
                    SlidingWindow[2] = Highs[0][i+2];
    e.g.

    Code:
    protected override void OnBarUpdate()
            {
    [B]            if(CurrentBar < NumberOfCandels) return;[/B]
    
                for(int i = 0; i < NumberOfCandles; i++)
                {
                    SlidingWindow[0] = Highs[0][i];
                    SlidingWindow[1] = Highs[0][i+1];
                    SlidingWindow[2] = Highs[0][i+2];
    Kind regards.

    - ChrisL

    Comment


      #3
      Hi Chris,

      Thanks for the guidance and yes, there were errors in the log tab because of that missing line. The lines are now displaying correctly as intended. Thanks again!
      Last edited by MrSomebody; 03-15-2020, 02:40 PM.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Mindset, 04-21-2026, 06:46 AM
      0 responses
      50 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by M4ndoo, 04-20-2026, 05:21 PM
      0 responses
      69 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by M4ndoo, 04-19-2026, 05:54 PM
      0 responses
      36 views
      0 likes
      Last Post M4ndoo
      by M4ndoo
       
      Started by cmoran13, 04-16-2026, 01:02 PM
      0 responses
      97 views
      0 likes
      Last Post cmoran13  
      Started by PaulMohn, 04-10-2026, 11:11 AM
      0 responses
      60 views
      0 likes
      Last Post PaulMohn  
      Working...
      X