Within the strategy I have the condition of
if (CurrentBars[0] < 1)
return;
Here is my code - I used the existing SMA code and simply modified it to create a new indicator.
namespace NinjaTrader.NinjaScript.Indicators
{
public class RMA : Indicator
{
private double prevRMA;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Calculates the Relative Moving Average (Wilder's Moving Average).";
Name = "RMA";
IsOverlay = true;
IsSuspendedWhileInactive = true;
Period = 14;
}
else if (State == State.Configure)
{
}
else if (State == State.DataLoaded)
{
prevRMA = 0;
}
}
protected override void OnBarUpdate()
{
if (CurrentBar == 0)
{
// Initialize RMA with the first input value
prevRMA = Input[0];
Value[0] = Input[0];
}
else
{
// RMA Calculation: RMA = (PrevRMA * (Period - 1) + Input) / Period
Value[0] = (prevRMA * (Period - 1) + Input[0]) / Period;
// Update the previous RMA for the next calculation
prevRMA = Value[0];
}
}
region Properties
[Range(1, int.MaxValue), NinjaScriptProperty]
[Display(ResourceType = typeof(Custom.Resource), Name = "Period", GroupName = "NinjaScriptParameters", Order = 0)]
public int Period
{ get; set; }
#endregion
}
}

Comment