Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Drawing SMA in panel 2 and changing color

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

    Drawing SMA in panel 2 and changing color

    Using a custom (only changed the color) sma indicator I would like to draw it in panel 2 or 3 and would like the color Green when the sma 220 SLOPE is positive or > 0 and Color Red when the sma 220 SLPOE is negative or < 0.

    Any help with this would greatly Help...

    Attached is the indicator I have changed the color on.

    Thanks...

    Ken
    Attached Files

    #2
    Hello kelliot,

    Thank you for your post.

    You can use PlotColors for this idea, for example:
    Code:
    			if (CurrentBar == 0)
    				Value.Set(Input[0]);
    			else
    			{
    				double last = Value[1] * Math.Min(CurrentBar, Period);
    
    				if (CurrentBar >= Period)
    					Value.Set((last + Input[0] - Input[Period]) / Math.Min(CurrentBar, Period));
    				else
    					Value.Set((last + Input[0]) / (Math.Min(CurrentBar, Period) + 1));
    				if (Value[0] > 0)
    					PlotColors[0][0] = Color.Green;
    				else if (Value[0] < 0)
    					PlotColors[0][0] = Color.Red;
    			}
    For information on PlotColors please visit the following link: http://www.ninjatrader.com/support/h...plotcolors.htm

    Comment


      #3
      I put this code in place of the other code containing the same info and all I get is green, when the vale is less than 0 its still green.

      Can we make the line width 3 ??

      How do I add (draw) this to my strategy in panel 2 or panel 3 ???

      I can add it to the strategy but I would like it in its own panel and to be drawn by the strategy, not manually adding it to the chart every time.

      Thanks
      Ken
      Last edited by kelliot; 11-09-2014, 09:10 AM.

      Comment


        #4
        Hello Ken,

        Thank you for your response.

        Can you provide the entire code used in your OnBarUpdate() method of the indicator?

        For the width you can use Plots[0].Pen.Width = 3;.

        You cannot draw from a strategy, you can add the indicators via Add() and ensure the indicators are set to Overlay = false in the Intialize() method.

        Comment


          #5
          Where do I put thepen width??

          I am calling the indicator from the add at the beginning of the code, can't get it to work from just adding it to a chart.

          Here is a copy of the indicator code..


          //
          // Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>.
          // NinjaTrader reserves the right to modify or overwrite this NinjaScript component with each release.
          //

          #region Using declarations
          using System;
          using System.Diagnostics;
          using System.Drawing;
          using System.Drawing.Drawing2D;
          using System.ComponentModel;
          using System.Xml.Serialization;
          using NinjaTrader.Data;
          using NinjaTrader.Gui.Chart;
          #endregion

          // This namespace holds all indicators and is required. Do not change it.
          namespace NinjaTrader.Indicator
          {
          /// <summary>
          /// The aSMA (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time.
          /// </summary>
          [Description("The aSMA (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time.")]
          public class aSMA : Indicator
          {
          #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.Blue, "aSMA"));

          Overlay = true;
          }

          /// <summary>
          /// Called on each bar update event (incoming tick).
          /// </summary>
          protected override void OnBarUpdate()
          {
          //NEW***********
          if (CurrentBar == 0)
          Value.Set(Input[0]);
          else
          {
          double last = Value[1] * Math.Min(CurrentBar, Period);

          if (CurrentBar >= Period)
          Value.Set((last + Input[0] - Input[Period]) / Math.Min(CurrentBar, Period));
          else
          Value.Set((last + Input[0]) / (Math.Min(CurrentBar, Period) + 1));
          if (Value[0] > 0)
          PlotColors[0][0] = Color.Green;
          else if (Value[0] < 0)
          PlotColors[0][0] = Color.Red;
          }




          //NEW***********
          /* if (CurrentBar == 0)
          Value.Set(Input[0]);
          else
          {
          double last = Value[1] * Math.Min(CurrentBar, Period);

          if (CurrentBar >= Period)
          Value.Set((last + Input[0] - Input[Period]) / Math.Min(CurrentBar, Period));
          else
          Value.Set((last + Input[0]) / (Math.Min(CurrentBar, Period) + 1));
          }
          */
          }

          #region Properties
          /// <summary>
          /// </summary>
          [Description("Numbers of bars used for calculations")]
          [GridCategory("Parameters")]
          public int Period
          {
          get { return period; }
          set { period = Math.Max(1, value); }
          }
          #endregion
          }
          }

          #region NinjaScript generated code. Neither change nor remove.
          // This namespace holds all indicators and is required. Do not change it.
          namespace NinjaTrader.Indicator
          {
          public partial class Indicator : IndicatorBase
          {
          private aSMA[] cacheaSMA = null;

          private static aSMA checkaSMA = new aSMA();

          /// <summary>
          /// The aSMA (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time.
          /// </summary>
          /// <returns></returns>
          public aSMA aSMA(int period)
          {
          return aSMA(Input, period);
          }

          /// <summary>
          /// The aSMA (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time.
          /// </summary>
          /// <returns></returns>
          public aSMA aSMA(Data.IDataSeries input, int period)
          {
          if (cacheaSMA != null)
          for (int idx = 0; idx < cacheaSMA.Length; idx++)
          if (cacheaSMA[idx].Period == period && cacheaSMA[idx].EqualsInput(input))
          return cacheaSMA[idx];

          lock (checkaSMA)
          {
          checkaSMA.Period = period;
          period = checkaSMA.Period;

          if (cacheaSMA != null)
          for (int idx = 0; idx < cacheaSMA.Length; idx++)
          if (cacheaSMA[idx].Period == period && cacheaSMA[idx].EqualsInput(input))
          return cacheaSMA[idx];

          aSMA indicator = new aSMA();
          indicator.BarsRequired = BarsRequired;
          indicator.CalculateOnBarClose = CalculateOnBarClose;
          #if NT7
          indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
          indicator.MaximumBarsLookBack = MaximumBarsLookBack;
          #endif
          indicator.Input = input;
          indicator.Period = period;
          Indicators.Add(indicator);
          indicator.SetUp();

          aSMA[] tmp = new aSMA[cacheaSMA == null ? 1 : cacheaSMA.Length + 1];
          if (cacheaSMA != null)
          cacheaSMA.CopyTo(tmp, 0);
          tmp[tmp.Length - 1] = indicator;
          cacheaSMA = tmp;
          return indicator;
          }
          }
          }
          }

          // This namespace holds all market analyzer column definitions and is required. Do not change it.
          namespace NinjaTrader.MarketAnalyzer
          {
          public partial class Column : ColumnBase
          {
          /// <summary>
          /// The aSMA (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time.
          /// </summary>
          /// <returns></returns>
          [Gui.Design.WizardCondition("Indicator")]
          public Indicator.aSMA aSMA(int period)
          {
          return _indicator.aSMA(Input, period);
          }

          /// <summary>
          /// The aSMA (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time.
          /// </summary>
          /// <returns></returns>
          public Indicator.aSMA aSMA(Data.IDataSeries input, int period)
          {
          return _indicator.aSMA(input, period);
          }
          }
          }

          // This namespace holds all strategies and is required. Do not change it.
          namespace NinjaTrader.Strategy
          {
          public partial class Strategy : StrategyBase
          {
          /// <summary>
          /// The aSMA (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time.
          /// </summary>
          /// <returns></returns>
          [Gui.Design.WizardCondition("Indicator")]
          public Indicator.aSMA aSMA(int period)
          {
          return _indicator.aSMA(Input, period);
          }

          /// <summary>
          /// The aSMA (Simple Moving Average) is an indicator that shows the average value of a security's price over a period of time.
          /// </summary>
          /// <returns></returns>
          public Indicator.aSMA aSMA(Data.IDataSeries input, int period)
          {
          if (InInitialize && input == null)
          throw new ArgumentException("You only can access an indicator with the default input/bar series from within the 'Initialize()' method");

          return _indicator.aSMA(input, period);
          }
          }
          }
          #endregion

          Comment


            #6
            Kelliot,

            You would place the line of code that Patrick supplied anywhere in the OnBarUpdate() or if you are only changing it once, you can set this in the Initialize() method
            Cal H.NinjaTrader Customer Service

            Comment


              #7
              Not sure shere to ad it every I have tried it won't compile.

              Any luck with the color change?

              It stays green no matter if value > 0

              Comment


                #8
                kelliot,

                Can you please attach the .CS file so that I can get a better idea of what you are doing?

                Click Reply -> Go Advanced -> Paperclip icon on the top tool bar

                The file can be found in (My) Documents -> NinjaTrader 7 -> bin -> Custom -> Indicators
                Cal H.NinjaTrader Customer Service

                Comment


                  #9
                  Its attached to the first post, thanks Ken

                  Comment


                    #10
                    Ken,

                    I would like your updated script to what you have added in.
                    Cal H.NinjaTrader Customer Service

                    Comment


                      #11
                      Its Attached
                      Attached Files

                      Comment


                        #12
                        Ken,

                        You will want to use the Slope() method for the your Value to compare against 0

                        if(Slope(Value, 1, 0 ) > 0)
                        PlotColors[0][0] = Color.Green;

                        Additionally, you will want to put Plots[0].Pen.Width = 3; in the Initialize() method right after your call for new Plot

                        I have attached the indicator back for review
                        Attached Files
                        Cal H.NinjaTrader Customer Service

                        Comment


                          #13
                          Cal,

                          Works GREAT.... Thanks, I thought we needed a slope statement in there somewhere...

                          ?? How do I get it to draw in panel 2 or 3 ??

                          For example I want to call this from a strategy, I add the statement to the strategy and it draw fine. But I would like it to be draw in panel 2 or panel 3 instead of being added to the chart in panel 1. I can manually add it to panel 2 in the chart view, but want it to be added in the strategy so I don't have to recreate the chart indicator everytime I open a new chart.

                          Comment


                            #14
                            Ken,

                            Ensure that Overlay is set to false in the Initialize().

                            This will have the indicator open in a new panel when used.
                            Cal H.NinjaTrader Customer Service

                            Comment


                              #15
                              Cal, All is working Great, thanks for the help.

                              On a different note, I am working on a strategy with a friend and we get different results. It seems that the tick size is different on the 2 computers. Why is this? I have verified that the 2 pc have the exact time. The difference is enough that one computer shows a loss for a month and the other shows a good profit. Some trades you can see the difference in the tick size and it sells or buys early, some one computer buys and the other does not and visa versa.

                              Is there a setting somewhere to make sure both computer are at the same tick level????

                              Thanks again
                              Ken

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              648 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              369 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              108 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              572 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              574 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X