Here I am trying to color an SMA according to its inclination, if the angle is > 10° then it is green and if < 10° it is red otherwise between the two it is neutral with gray.
On my chart the SMA remains green.
Thank you for the help
Code :
namespace NinjaTrader.NinjaScript.Indicators
{
public class SMAAngle : Indicator
{
// Variable declaration
private double inclinaisonMin = 0.5; // Ajustez cette valeur selon votre préférence
private double inclinaisonMax = -0.5; // Ajustez cette valeur selon votre préférence
// Declaration of data series
private Series<double> moyenneMobile;
private double angle; // Nouvelle variable pour stocker l'angle
// Indicator initialization method
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"SMAAngle";
Calculate = Calculate.OnEachTick;
AddPlot(Brushes.Gray, "Moyenne Mobile");
}
else if (State == State.Configure)
{
AddDataSeries(Data.BarsPeriodType.Day, 1); // Période journalière pour la moyenne mobile
}
else if (State == State.DataLoaded)
{
moyenneMobile = new Series<double>(this);
}
}
// Method called on each new bar
protected override void OnBarUpdate()
{
if (CurrentBars[0] < 110)
return;
//Calculating the 110-period simple moving average
double smaValue = SMA(Closes[0], 110)[0];
// Tilt calculation
double inclinaison = (smaValue - SMA(Closes[0], 110)[1]);
// Calculating angle in degrees
angle = Math.Atan(inclinaison) * (180.0 / Math.PI);
// Assign the moving average value to the data series
moyenneMobile[0] = smaValue;
// Change color depending on angle
if (angle > inclinaisonMin)
{
Values[0][0] = smaValue;
Plots[0].Brush = Brushes.Green; // Couleur haussière
}
else if (angle < inclinaisonMax)
{
Values[0][0] = smaValue;
Plots[0].Brush = Brushes.Red; // Couleur baissière
}
else
{
Values[0][0] = smaValue;
Plots[0].Brush = Brushes.Gray; // Couleur neutre
}
}
region Properties
// Property to adjust the minimum tilt
public double InclinaisonMin
{
get { return inclinaisonMin; }
set { inclinaisonMin = value; }
}
// Property to adjust the maximum tilt
public double InclinaisonMax
{
get { return inclinaisonMax; }
set { inclinaisonMax = value; }
}
#endregion
}
}
Comment