Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Coloring Chart Background Based on CrossOver

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

    Coloring Chart Background Based on CrossOver

    hi all, thanks in advance.

    can anyone guide me on what code would look like coloring the background of a chart based on a simple average cross over? For example, when the 4 period SMA crosses above the 15 period SMA, the background turns green, and when it crosses under, the background turns red.

    thanks

    -osa

    #2



    Originally posted by outstretchedarm View Post
    hi all, thanks in advance.

    can anyone guide me on what code would look like coloring the background of a chart based on a simple average cross over? For example, when the 4 period SMA crosses above the 15 period SMA, the background turns green, and when it crosses under, the background turns red.

    thanks

    -osa

    Comment


      #3
      Hello osa,
      Please use the BackColor property to color the chart background.


      @sledge - Thanks for you input.
      JoydeepNinjaTrader Customer Service

      Comment


        #4
        OK let me experiment with this a bit and then report back.

        thanks for the link

        Comment


          #5
          Hmmm, not being a proficient coder by any means, I am running into tons of errors with the following code I put together:

          Code:
          		#region Variables
          		private int					period	= 14;
          		#endregion
          
          		/// <summary>
          		/// This method is used to configure the indicator and is called once before any bar data is loaded.
          		/// </summary>
          		protected override void Initialize()
          		{
          			Add(new Plot(Color.Orange, "BackColor1"));
          		}
          
          		/// <summary>
          		/// Called on each bar update event (incoming tick)
          		/// </summary>
          		protected override void OnBarUpdate()
          		{
          			if (SMA(5)[0] > SMA(15)[0])
          			{
          			BackColor = Color.Green;
          			}
          		}
          #region Properties
          		/// <summary>
          		/// </summary>
          		[Description("Numbers of bars used for calculations")]
          		[GridCategory("Parameters")]
          		public int Period
          		{
          			get { return period; }
          			set { period = Math.Max(2, value); }
          		}
          		#endregion
          	}
          }
          Any advice?

          Comment


            #6
            Hello outstretchedarm,
            Please refer to the attached code which paints the background green, when SMA 5 is greater than SMA 15.

            Please let me know if I can assist you any further.
            Attached Files
            JoydeepNinjaTrader Customer Service

            Comment


              #7
              Sorry for the slow reply. Much thanks for this. I will experiment with it and if I learn anything, I will report back.

              Comment


                #8
                this test code works one the price panel, but what do i change to get it to print on panel 2 if i put an indicator there?

                Comment


                  #9
                  Hello zoom zoom 126,
                  You can use BackColorAll to color all the panes. Please refer to our help guided to know more about it.
                  JoydeepNinjaTrader Customer Service

                  Comment


                    #10
                    I finally got around to experimenting with this, and I got the code that you provided to work. Thanks!

                    I thought I would get fancy and add an additional condition. I coded:

                    Code:
                    	if ((SMA(300)[0] > SMA(900)[0])
                    				& (SMA(1950)[0] > SMA(5850)[0]))
                    				BackColor = Color.Green;
                    However, it doesn't backcolor at all (I tried it on quite a few different instruments; one of them should have had a data history that met these two conditions).

                    Furthermore, I tried it with both the & and the && operators, and neither backcolored.

                    Any idea why?

                    Thanks in advance for any input. You guys are the greatest at customer service.

                    Comment


                      #11
                      Are you sure that condition is true? You might want to add some Print() statements to that block to see if that is returned to the Tools--> Output window


                      Also is that the exact code you're using? There are some syntax issues with it which I've corrected below:



                      Code:
                      			 	if (SMA(300)[0] > SMA(900)[0] && SMA(1950)[0] > SMA(5850)[0])
                      				{
                      				Print("Condition is true");
                      				BackColor = Color.Green;
                      				}
                      MatthewNinjaTrader Product Management

                      Comment


                        #12
                        Is there something about the SMA indicator that it won't work about a certain value?

                        I'm running:

                        if (SMA(1950)[0] > SMA(5850)[0])
                        BackColor = Color.Green;

                        and am never getting results. it is that parameters of over 1,000 are too high, and don't give results? Or perhaps there isn't enough historical data onhand to give the proper results? if so, how do you load up more historical data that goes back even further?

                        Comment


                          #13
                          Hello outstretchedarm,
                          The SMA word work fine. Since you are using a very high number 5K+ to calculate the SMA, you must load more than 5K+ bars to get the correct SMA value.
                          • Right click on the chart
                          • In the context menu click on Data Series..
                          • In the Data Series dialog set and appropriate value for the option "Days to load"


                          Please note, your connectivity provider/broker must provide historical data for such periods.

                          To assist you further may I know what periodicity chart you are applying the SMA indicator and how many days of data you are loading.
                          Attached Files
                          JoydeepNinjaTrader Customer Service

                          Comment


                            #14
                            wow, you guys are awesome. Increasing the days to load did the trick.

                            Regarding the other posters comment:

                            this test code works one the price panel, but what do i change to get it to print on panel 2 if i put an indicator there?
                            I think he raises a good question. Let's say I wanted to have, in the lower graph, where I chart ADX, for example; could I have the lower graph background turn green when ADX is above 60?

                            This would be a nice development, in fact, if this were possible, I would eventually code everything such that once the background on each of my indicators turned green (because it met my conditions) I would simply pull the trigger on a trade.

                            Thanks in advance for any help.

                            Comment


                              #15
                              Hello outstretchedarm,
                              You cannot do it via an Indicator. However if you have have added the ADX via a strategy then you can use the below code to set the background color of the ADX.

                              Code:
                              //In Initialze 
                              Add(ADX(14));
                              
                              //in OnBarUpdate
                              if (ADX(14)[0] > 60)
                              {
                              	ADX(14).BackColor = Color.Green;
                              }
                              JoydeepNinjaTrader Customer Service

                              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