Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Is it possible to use a Custom Indicator (built in the editor) with a Strategy?

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

    Is it possible to use a Custom Indicator (built in the editor) with a Strategy?

    I've built my own Indicator. It has 4 plots exposed by friendly names. It works perfectly standalone. When I instantiate it in my Strategy, it fails on looking for High[0]. Attaching the debugger it looks like there's a proxy series or something? Like I'm not getting the current series the strategy is on..

    Here's code of the Indicator:
    IsSuspendedWhileInactive = true;
    AddPlot(Brushes.Orange, "This_Is_Plot1");
    AddPlot(Brushes.Orange, "This_Is_Plot2");
    }
    else if (State == State.Configure)
    {
    }
    }

    protected override void OnBarUpdate()
    {
    for (int x = 0; FVG.Count < 5 && x < 256; x++)
    {
    try
    {
    if (High[x] > _curHigh) FAILS ON THIS CHECK
    _curHigh = High[x];
    if (Low[x] < _curLow)
    _curLow = Low[x];

    bool isDownGap = Low[x + 2] > High[x] && !FVG.ContainsKey(Low[x + 2]) && High[x] > _curLow && Low[x + 2] > _curHigh;
    bool isUpGap = High[x + 2] < Low[x] && !FVG.ContainsKey(High[x + 2]) && Low[x] < _curHigh && High[x + 2] < _curLow;
    //Print(string.Format("IsDownGap: {0} High[x]: {2} Low[x+2]: {1} Current Low{3}", isDownGap, Low[x + 2], High[x], _curLow));
    //Print(string.Format("IsUpGap: {0} Low[x]: {2} High[x+2]: {1} Current High{3}", isUpGap, High[x + 2], Low[x], _curHigh));
    //Print(string.Format("FVG Gap Count: {0}", FVG.Count()));
    // if(FVG.Count==NumberofGaps-1 && (isDownGap || isUpGap))
    // FVG.Remove(FVG.Keys.Last());

    if (isDownGap)
    {
    Print(string.Format("IsDownGap: {0} High[x]: {2} Low[x+2]: {1} Current Low{3}", isDownGap, Low[x + 2], High[x], _curLow));
    Draw.Ray(this, string.Format("DownGapUpper{0}{1}", FVG.Count, Bars.BarsPeriod.Value), x + 2, Low[x + 2], x + 1, Low[x + 2], false, _templateDown);
    Draw.Ray(this, string.Format("DownGapLower{0}{1}", FVG.Count, Bars.BarsPeriod.Value), x + 1, High[x], x, High[x], false, _templateDown);
    Draw.Rectangle(this, string.Format("DownGap{0}{1}", FVG.Count, Bars.BarsPeriod.Value), x + 2, Low[x + 2], x, High[x], false, _templateDown);
    Value[0] = Low[x + 2];
    Value[1] = High[x];
    FVG.Add(Low[x + 2], High[x]);
    }
    }

    Here's the Strat Code:
    else if (State == State.Configure)
    {
    //AddDataSeries(Data.BarsPeriodType.Day, 1);
    }
    else if (State == State.DataLoaded)
    {
    _myfvg = TestFVGIndicator();
    AddChartIndicator(_myfvg);
    }
    }

    protected override void OnBarUpdate()
    {
    if(State == State.Historical) return;​


    Here's the error:

    Exception was thrown in ICTFVG Message: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index, Stack Trace: at System.ThrowHelper.ThrowArgumentOutOfRangeExceptio n(ExceptionArgument argument, ExceptionResource resource)
    at NinjaTrader.Data.BarsSeries.GetLow(Int32 index)
    at NinjaTrader.Data.Bars.GetLow(Int32 index)
    at NinjaTrader.NinjaScript.PriceSeries.get_Item(Int32 barsAgo)
    at NinjaTrader.NinjaScript.Indicators.TestFVGIndicato r.OnBarUpdate() in e:\Documents\NinjaTrader 8\bin\Custom\Indicators\TestFVGIndicator.cs:line 70​


    #2
    Hello gdkimble,

    Welcome to the NinjaTrader forums!

    This is an index out of range error.

    Series use bars ago values for indexes.

    If CurrentBar is 0, and you try and call High[5], this will cause an error because there is no bar 5 bars ago.

    For any index used, ensure that your index is less than the size of the collection.
    https://ninjatrader.com/support/foru...13#post1048513

    Ensure x is less than CurrentBar.

    Also, putting your code in a try and catch, will prevent the error from appearing on the Log tab of the Control Center. We generally advise avoiding these unless absolutely necessary.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Chelsea, is there an example of a strategy using custom indicator I could look at? Because the indicator steadfastly refuses to behave the same inside the strategy as it does on it's own.

      Comment


        #4
        Hello gdkimble,

        An example of any indicator?
        Try the SMA. This is a simple indicator with a plot that can be called in the Strategy Builder.

        Or an example of something specific like looping through the last x bars?

        I think you mean looping and ensuring your index is greater than CurrentBar.

        Attached is an example of that.

        Moving forward, I recommend you create simplified test scripts to test one thing at a time.
        Attached Files
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          Thanks for the reply Chelsea, can I ask why you sent the Close series to the indicator on instantiation?

          Comment


            #6
            Hello gdkimble,

            That's the default when selecting an indicator in the Strategy Builder.

            The Input[0] is not used in this script. It only looks at the price series High[0]. It doesn't matter what the input series is for this test demonstration, the results will always be the same.
            Chelsea B.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Mindset, 04-21-2026, 06:46 AM
            0 responses
            88 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by M4ndoo, 04-20-2026, 05:21 PM
            0 responses
            134 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Started by M4ndoo, 04-19-2026, 05:54 PM
            0 responses
            68 views
            0 likes
            Last Post M4ndoo
            by M4ndoo
             
            Started by cmoran13, 04-16-2026, 01:02 PM
            0 responses
            118 views
            0 likes
            Last Post cmoran13  
            Started by PaulMohn, 04-10-2026, 11:11 AM
            0 responses
            67 views
            0 likes
            Last Post PaulMohn  
            Working...
            X