PDA

View Full Version : Best way to use indicator outside OnBar


Space123
12-06-2009, 06:19 AM
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.

NinjaTrader_Ben
12-06-2009, 11:33 AM
Hello,

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

Space123
12-06-2009, 04:26 PM
// 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
}
}

NinjaTrader_Ben
12-06-2009, 08:03 PM
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.