Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SMA Angle Color

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

    SMA Angle Color

    Hello, can anyone tell me what is wrong in my code?
    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
    }
    }


    #2
    Hello Luke_Skytrader,

    Thank you for your post and welcome to the NinjaTrader forum community!

    I suggest printing the values used in your conditions to change the plot brush, such as angle and inclinaisonMax, to understand what values are being used in the comparisons and which conditions are evaluating to true and which conditions are false. For more details on using prints to debug unexpected behavior:


    You could also consider using Slope() in your logic if you'd like:


    As mentioned on that page, "Tip: Thinking in degrees, for example a 1 to -1 return range would translate to 45 to -45. To convert you could look into working with this formula - Math.Atan(Slope) * 180 / Math.PI"

    Please let us know if we may be of further assistance.

    Comment

    Latest Posts

    Collapse

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