Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

moving average that changes colors

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

    moving average that changes colors

    hello everyone,


    i have been trying to code an indicator that will change colors according to two different data sets but i have run into a lot of trouble with nt's regions, variables, data sets and other minutiae.

    i'd like to plot a 9 period sma. this 9 period sma would be plotted orange if a 10 period sma and an 18 period sma are both rising. it would be plotted purple if the 10 period and 18 period sma's are both falling and it would be plotted black in any other case.

    i have managed to create an sma that changes colors if it is rising or falling, but i haven't had success in plotting one sma according to how two other sma's are trending. maybe someone with more extensive experience coding in nt could help me.


    thanks.

    #2
    Hello,
    Thank you for your post.

    You can have the 8 period SMA change colors based off of the two other SMA's by doing something similar to the following:
    Code:
    if(CurrentBar < 1)
        return;
    	
    mySMA.Set(SMA(8)[0]);
    			
    if( Rising(SMA(18)) && Rising(SMA(10)) )
    {
    	PlotColors[0][0] = Color.Orange;	
    }
    else if( Falling(SMA(18)) && Falling(SMA(10)) )
    {
    	PlotColors[0][0] = Color.Purple;
    }
    else 
    {
    	PlotColors[0][0] = Color.Black;
    }
    If we can be of any other assistance please let us know.
    Cody B.NinjaTrader Customer Service

    Comment


      #3
      Cody,


      thanks for your super fast reply.


      i have tried to paste the code you suggested into a copy of the sma indicator but it won't compile.

      i get several error messages, and the one i haven't been able to get past reads something like this:

      operator && cannot be applied to operands of type boole and nt.indicator.sma

      i see that it will take some time and reading to at least get the basics for coding in nt. is there any kind of manual for beginners that would be recommendable?


      thanks.

      Comment


        #4
        Originally posted by rtwave View Post
        Cody,


        thanks for your super fast reply.


        i have tried to paste the code you suggested into a copy of the sma indicator but it won't compile.

        i get several error messages, and the one i haven't been able to get past reads something like this:

        operator && cannot be applied to operands of type boole and nt.indicator.sma

        i see that it will take some time and reading to at least get the basics for coding in nt. is there any kind of manual for beginners that would be recommendable?


        thanks.
        ref: http://ninjatrader.com/support/forum...37&postcount=1

        Comment


          #5
          Codys code was more meant to be its own indicator rather than a change to the original sma. His code already includes everything u need for it to work, ecxept adding the actual plot

          here would be his code implemented in a full script. just take ur copied sma file delete everything, paste this code and compile, should work like a charm.

          EDIT:you would find this indicator as "coloredSMA" not what ur filename would say. just to prevent confusion.

          Code:
          #region Using declarations
          using System;
          using System.ComponentModel;
          using System.Diagnostics;
          using System.Drawing;
          using System.Drawing.Drawing2D;
          using System.Xml.Serialization;
          using NinjaTrader.Cbi;
          using NinjaTrader.Data;
          using NinjaTrader.Gui.Chart;
          #endregion
          
          namespace NinjaTrader.Indicator
          {
          
              [Description("")]
              public class ColoredSMA : Indicator
              {
                  #region Variables
          
                  #endregion
          
          
                  protected override void Initialize()
                  {
                      Add(new Plot(Color.Orange, "ColoredSMA"));
                      Overlay                = true;
                  }
          
          
                  protected override void OnBarUpdate()
                  {
                      if(CurrentBar < 1)
                      return;
                      
                      Value.Set(SMA(9)[0]);
                      if( Rising(SMA(18)) && Rising(SMA(10)) )
                      {
                          PlotColors[0][0] = Color.Orange;    
                      }
                      else if( Falling(SMA(18)) && Falling(SMA(10)) )
                      {
                          PlotColors[0][0] = Color.Purple;
                      }
                      else 
                      {
                          PlotColors[0][0] = Color.Black;
                      }
                  }
          
                  #region Properties
                  [Browsable(false)]    // this line prevents the data series from being displayed in the indicator properties dialog, do not remove
                  [XmlIgnore()]        // this line ensures that the indicator can be saved/recovered as part of a chart template, do not remove
                  public DataSeries Default
                  {
                      get { return Values[0]; }
                  }
                  #endregion
              }
          }
          Last edited by BigRo; 12-11-2015, 05:37 AM.

          Comment


            #6
            Hello rtwave,

            BigRo is correct here. koganam also refers to the Help Guide documentation that you can find at the following link: http://ninjatrader.com/support/helpG..._resources.htm

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
            0 responses
            579 views
            0 likes
            Last Post Geovanny Suaza  
            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
            0 responses
            334 views
            1 like
            Last Post Geovanny Suaza  
            Started by Mindset, 02-09-2026, 11:44 AM
            0 responses
            101 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
            0 responses
            554 views
            1 like
            Last Post Geovanny Suaza  
            Started by RFrosty, 01-28-2026, 06:49 PM
            0 responses
            551 views
            1 like
            Last Post RFrosty
            by RFrosty
             
            Working...
            X