Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

First indicator, need help on calling it

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

    First indicator, need help on calling it

    I coded my first and very simple indicator - it calculates the percentage of close relative to bar range.

    Here's a code, basic calculation is ok, I tested 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
    {
    /// <summary>
    /// Calculates the percentage of close relative to bar range.
    /// Indicator is measured on a scale of 0 100.
    /// Red candles closing near lows will have lower values.
    /// Green candles closing near highs will have higher values.
    /// </summary>
    
    public class ClosePct : Indicator
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Calculates the percentage of close relative to bar range.";
    Name = "ClosePct";
    Calculate = Calculate.OnBarClose;
    IsOverlay = false;
    DisplayInDataBox = true;
    DrawOnPricePanel = false;
    DrawHorizontalGridLines = false;
    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;
    
    AddPlot(new Stroke(Brushes.Teal, 2), PlotStyle.Bar, NinjaTrader.Custom.Resource.RangeValue);
    }
    else if (State == State.Configure)
    {
    }
    }
    
    protected override void OnBarUpdate()
    {
    Value[0] = (1-((High[0] - Close[0])/(High[0] - Low[0] + 0.0001))) * 100;
    }
    }
    }
    
    #region NinjaScript generated code. Neither change nor remove.
    
    namespace NinjaTrader.NinjaScript.Indicators
    {
    public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
    {
    private ClosePct[] cacheClosePct;
    public ClosePct ClosePct()
    {
    return ClosePct(Input);
    }
    
    public ClosePct ClosePct(ISeries<double> input)
    {
    if (cacheClosePct != null)
    for (int idx = 0; idx < cacheClosePct.Length; idx++)
    if (cacheClosePct[idx] != null && cacheClosePct[idx].EqualsInput(input))
    return cacheClosePct[idx];
    return CacheIndicator<ClosePct>(new ClosePct(), input, ref cacheClosePct);
    }
    }
    }
    
    namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
    {
    public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
    {
    public Indicators.ClosePct ClosePct()
    {
    return indicator.ClosePct(Input);
    }
    
    public Indicators.ClosePct ClosePct(ISeries<double> input )
    {
    return indicator.ClosePct(input);
    }
    }
    }
    
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
    {
    public Indicators.ClosePct ClosePct()
    {
    return indicator.ClosePct(Input);
    }
    
    public Indicators.ClosePct ClosePct(ISeries<double> input )
    {
    return indicator.ClosePct(input);
    }
    }
    }
    
    #endregion
    However. I have 2 issues:
    1) How can I call it in the strategy? I tried using Prints, calling: "ClosePct[0]", but it returns an error of "Cannot apply indexing with [] to an expression of type 'method group'
    2) I also want to add "Average over selected period" of ClosePct. (Like average ClosePct of recent 14 bars)
    2.1) Should this be added to an indicator or to a strategy?
    2.2) Can somebody help me with the code of it?

    #2
    Hello UltraNIX,

    Thank you for your note.

    I'd recommend using the Strategy Builder initially to use the indicator in a strategy. You should be able to add it to a Strategy in the Builder and then click the "View Code" button to see the actual code to see how the strategy would be set up.

    I'm also providing a link to a publicly accessible pre-recorded video 'Strategy Builder 301' for you to view at your own convenience that should help you in understanding the Strategy Builder.

    Strategy Builder 301

    You can call an indicator either from another indicator or a strategy, and I'd suggest if you want to get the average of a selected period that you use it in tandem with the SMA indicator.



    You can use an indicator as the input for another indicator as well, here's our help guide page on using an indicator as an input for another indicator within the Strategy Builder:



    I'd recommend setting that up in the Strategy Builder as well, that way you can view the code as well.

    Please let us know if we may be of further assistance to you.

    Comment


      #3
      Thanks, Kate,

      I was able to call indicator after using Strategy Builder.

      Now I want to have an average of X bars. I did very simple thing - copied SMA indicator code, created new indicator, pasted text from SMA indicator, and replaced "SMA" with "ClosePctA", so it won't duplicate.

      Now I need help with calculation text.

      Here's OnBarUpdate() section of SMA indicator:
      Code:
       protected override void OnBarUpdate()
      {
      if (BarsArray[0].BarsType.IsRemoveLastBarSupported)
      {
      if (CurrentBar == 0)
      Value[0] = Input[0];
      else
      {
      double last = Value[1] * Math.Min(CurrentBar, Period);
      
      if (CurrentBar >= Period)
      Value[0] = (last + Input[0] - Input[Period]) / Math.Min(CurrentBar, Period);
      else
      Value[0] = ((last + Input[0]) / (Math.Min(CurrentBar, Period) + 1));
      }
      }
      else
      {
      if (IsFirstTickOfBar)
      priorSum = sum;
      
      sum = priorSum + Input[0] - (CurrentBar >= Period ? Input[Period] : 0);
      Value[0] = sum / (CurrentBar < Period ? CurrentBar + 1 : Period);
      }
      }
      Where should I update SMA formula with my ClosePct formula, so it would generate appropriate results?

      Comment


        #4
        Thanks, no longer need help on this.

        First of all, I understood that I have to make a new indicator basing on SMA. But I don't. I have to use indicator SMA and use my newly created indicator for Data Series.

        It all works well!

        Comment


          #5
          Hi, I am interested in this as well. I would like to indicate above/below a bar that closes a certain percentage of its range. ie: 25%, 33% etc. Is there something like this available?
          Thanks

          Comment


            #6
            Hello Chrispy,

            Thank you for your reply.

            There may be an existing indicator out there that does this already, however, I'm not aware of a specific one. When you say "closes a certain percentage of its range" do you mean that the close is below the high or above the low by a certain percentage of the full range of the bar? You could certainly create your own indicator that would do that. You could calculate that and plot text or a drawing object above a bar in your own custom indicator if that's true. For example, you can get the range of the bar using the following:

            double BarRange = High[0] - Low[0];

            You could then calculate the distance between the high and the close or low and the close, and determine the percentage of the range that distance is. If that calculated percentage matches your requirements for percentage, then use a drawing object like Draw.ArrowUp or Draw.ArrowDown to mark above or below the bar.




            Please let us know if we may be of further assistance to you.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            628 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            359 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            105 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            562 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            568 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X