Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Best way to use indicator outside OnBar

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

    Best way to use indicator outside OnBar

    If I want to encapsulate a built in indicator like ATR(20)[0] outside OnBarUpdate so that other methods can have access to it what should I do?

    Assigning it to a variable:

    double ATR20 = ATR(20)[0];

    gives me this error:

    'object reference is required for the nonstatic field, method or property Ninjatrader.Strategy.StrategyATR(int)'

    Thanks as always for your help.

    #2
    Hello,

    That line of code is fine. How are you using the variable ATR20? Please post the code.
    DenNinjaTrader Customer Service

    Comment


      #3
      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
      /// <summary>
      /// Enter the description of your strategy here
      /// </summary>
      [Description("Enter the description of your strategy here")]
      public class ATRTEST : Strategy
      {
      #region Variables
      // Wizard generated variables
      private int myInput0 = 1; // Default setting for MyInput0
      // User defined variables (add any user defined variables below)

      double ATRtest = ATR(20)[0];

      #endregion

      /// <summary>
      /// This method is used to configure the strategy and is called once before any strategy method is called.
      /// </summary>
      protected override void Initialize()
      {
      CalculateOnBarClose = true;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      }

      #region Properties
      [Description("")]
      [Category("Parameters")]
      public int MyInput0
      {
      get { return myInput0; }
      set { myInput0 = Math.Max(1, value); }
      }
      #endregion
      }
      }

      Comment


        #4
        Hello,

        I looked at your code really quickly and found one issue right away: you need to put that line of code in question in the OnBarUpdate code block so it is called and updated each time an bar event happens.
        DenNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

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