Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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.
    Emily C.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by carnitron, Today, 08:42 PM
    0 responses
    4 views
    0 likes
    Last Post carnitron  
    Started by strategist007, Today, 07:51 PM
    0 responses
    6 views
    0 likes
    Last Post strategist007  
    Started by StockTrader88, 03-06-2021, 08:58 AM
    44 responses
    3,969 views
    3 likes
    Last Post jhudas88  
    Started by rbeckmann05, Today, 06:48 PM
    0 responses
    5 views
    0 likes
    Last Post rbeckmann05  
    Started by rhyminkevin, Today, 04:58 PM
    4 responses
    58 views
    0 likes
    Last Post dp8282
    by dp8282
     
    Working...
    X