Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Calling a custom indicator in a Strategy

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

    Calling a custom indicator in a Strategy

    If I have written an indicator in that is based on Day bars and I call it in a strategy running on 5 min bars, will the indicator stay based on Day bars?

    This is my indicator,

    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Gui.Chart;
    #endregion

    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    [Description("Enter the description of your new custom indicator here")]
    public class PivotAverage : Indicator
    {

    private int pivotLookBack = 7;
    private int sessionCount = 0;
    private DataSeries ppDS;

    private bool debug = false;




    protected override void Initialize()
    {
    MaximumBarsLookBack = MaximumBarsLookBack.Infinite;


    Add(PeriodType.Day, 1); //Add Day Bars to Index location 1

    Add(new Plot(Color.FromKnownColor(KnownColor.Black), PlotStyle.Dot, "PPAvg"));


    Overlay = true;
    BarsRequired = 2;
    }

    protected override void OnBarUpdate()
    {
    if(ppDS == null) // Only to sync the ppDS DataSeries to Day Bars
    {
    ppDS = new DataSeries(SMA(BarsArray[1], 2));
    }
    if(CurrentBars[0] <= BarsRequired || CurrentBars[1] <= BarsRequired)
    return;

    if(Bars.SessionBreak)// If there is a new session add one to session count.
    {
    sessionCount++;
    }


    if(BarsInProgress == 1)
    {
    ppDS.Set(Math.Round((Highs[1][0] + Lows[1][0] + Closes[1][0])/3,2));

    if(CurrentBar < (BarsRequired + pivotLookBack))
    {
    PPAvg.Set(ppDS[0]);
    }
    else
    {
    PPAvg.Set(SMA(ppDS, pivotLookBack)[0]);
    }


    if(Rising(PPAvg))
    Print("Rising");

    if(Falling(PPAvg))
    Print("Falling");

    if(Debug)
    {
    Print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    Print(Time[0]);
    Print("Yesterdays Pivot Point is " + ppDS[1]);
    Print("Pivot Point is " + ppDS[0]);
    Print("Pivot " + pivotLookBack + "Period SMA is " + PPAvg[0]);
    Print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

    }
    }



    }

    #region Properties
    [Browsable(false)] // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
    [XmlIgnore()] // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
    public DataSeries PPAvg
    {
    get { return Values[0]; }
    }

    [Description("No of days to calculate the average")]
    [GridCategory("Parameters")]
    public int PivotLookBack
    {
    get { return pivotLookBack; }
    set { pivotLookBack = Math.Max(1, value); }
    }

    [Description("Display pivot values to the output window if true")]
    [GridCategory("Parameters")]
    public bool Debug
    {
    get { return debug; }
    set { debug = value; }
    }
    #endregion
    }
    }

    #region NinjaScript generated code. Neither change nor remove.
    // This namespace holds all indicators and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    public partial class Indicator : IndicatorBase
    {
    private PivotAverage[] cachePivotAverage = null;

    private static PivotAverage checkPivotAverage = new PivotAverage();

    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    public PivotAverage PivotAverage(bool debug, int pivotLookBack)
    {
    return PivotAverage(Input, debug, pivotLookBack);
    }

    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    public PivotAverage PivotAverage(Data.IDataSeries input, bool debug, int pivotLookBack)
    {
    if (cachePivotAverage != null)
    for (int idx = 0; idx < cachePivotAverage.Length; idx++)
    if (cachePivotAverage[idx].Debug == debug && cachePivotAverage[idx].PivotLookBack == pivotLookBack && cachePivotAverage[idx].EqualsInput(input))
    return cachePivotAverage[idx];

    lock (checkPivotAverage)
    {
    checkPivotAverage.Debug = debug;
    debug = checkPivotAverage.Debug;
    checkPivotAverage.PivotLookBack = pivotLookBack;
    pivotLookBack = checkPivotAverage.PivotLookBack;

    if (cachePivotAverage != null)
    for (int idx = 0; idx < cachePivotAverage.Length; idx++)
    if (cachePivotAverage[idx].Debug == debug && cachePivotAverage[idx].PivotLookBack == pivotLookBack && cachePivotAverage[idx].EqualsInput(input))
    return cachePivotAverage[idx];

    PivotAverage indicator = new PivotAverage();
    indicator.BarsRequired = BarsRequired;
    indicator.CalculateOnBarClose = CalculateOnBarClose;
    #if NT7
    indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
    indicator.MaximumBarsLookBack = MaximumBarsLookBack;
    #endif
    indicator.Input = input;
    indicator.Debug = debug;
    indicator.PivotLookBack = pivotLookBack;
    Indicators.Add(indicator);
    indicator.SetUp();

    PivotAverage[] tmp = new PivotAverage[cachePivotAverage == null ? 1 : cachePivotAverage.Length + 1];
    if (cachePivotAverage != null)
    cachePivotAverage.CopyTo(tmp, 0);
    tmp[tmp.Length - 1] = indicator;
    cachePivotAverage = tmp;
    return indicator;
    }
    }
    }
    }

    // This namespace holds all market analyzer column definitions and is required. Do not change it.
    namespace NinjaTrader.MarketAnalyzer
    {
    public partial class Column : ColumnBase
    {
    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.PivotAverage PivotAverage(bool debug, int pivotLookBack)
    {
    return _indicator.PivotAverage(Input, debug, pivotLookBack);
    }

    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    public Indicator.PivotAverage PivotAverage(Data.IDataSeries input, bool debug, int pivotLookBack)
    {
    return _indicator.PivotAverage(input, debug, pivotLookBack);
    }
    }
    }

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    public partial class Strategy : StrategyBase
    {
    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.PivotAverage PivotAverage(bool debug, int pivotLookBack)
    {
    return _indicator.PivotAverage(Input, debug, pivotLookBack);
    }

    /// <summary>
    /// Enter the description of your new custom indicator here
    /// </summary>
    /// <returns></returns>
    public Indicator.PivotAverage PivotAverage(Data.IDataSeries input, bool debug, int pivotLookBack)
    {
    if (InInitialize && input == null)
    throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

    return _indicator.PivotAverage(input, debug, pivotLookBack);
    }
    }
    }
    #endregion



    And I am calling it in my Strategy like this:-

    if(Falling(PivotAverage(false, iPivotAvgPeriod)))

    #2
    Hello GKonheiser,

    Yes, your Indicator will still be based on Day bars since that is how your indicator is coded. The only thing that you want to keep in mind is that your Indicators is going to use the settings of your Strategy that is calling it. For example the indicator will use the "number of days to load" that your strategy is set to so if your strategy is set to load 5 days of data then your Indicator will only have access to 5 days worth of daily data as well.

    Let us know if you have any questions.
    JCNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
    0 responses
    650 views
    0 likes
    Last Post Geovanny Suaza  
    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
    0 responses
    370 views
    1 like
    Last Post Geovanny Suaza  
    Started by Mindset, 02-09-2026, 11:44 AM
    0 responses
    109 views
    0 likes
    Last Post Mindset
    by Mindset
     
    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
    0 responses
    574 views
    1 like
    Last Post Geovanny Suaza  
    Started by RFrosty, 01-28-2026, 06:49 PM
    0 responses
    577 views
    1 like
    Last Post RFrosty
    by RFrosty
     
    Working...
    X