Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

2 EMA crossover need to convert strategy script to an Indicator

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

    2 EMA crossover need to convert strategy script to an Indicator

    I tried writing a 2 EMA cross script before and even with forum help got me totally confused.
    I have been able to convert 2 paintbar strategies to indicators successfully--the most recent was today converting a candle paintbar for O>C or O<C just because I hate the Ninja default candles with the outline I cant eliminate and still see the wicks so I wrote my own which I'll be glad to share if anyone wants it.

    Anyhow, I wrote a strategy for 2 EMA with 2 different pricing types that crossover or under. The values it plots were correct except both lines were orange and I couldnt change it because I wasnt sure where to tell it or how to get the ability to change colors from the chart once its loaded.

    I tried to copy it into a new indicator script window and added some things from previous forum attempts to help me, and it compiles but wont print as an indicator. In the Indicators Window it shows as TwoEMAs(<unknown>,<unknown>)

    Reproducing each below:
    Strategy first which plots correctly but unsure how to set colors as an input:
    HTML 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.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// 2 EMAs Crossing
    /// </summary>
    [Description("2 EMAs Crossing")]
    public class TwoEMAs : Strategy
    {
    #region Variables
    // Wizard generated variables
    private int periodF = 5; // Default setting for PeriodF
    private int periodS = 6; // Default setting for PeriodS
    // User defined variables (add any user defined variables below)
    #endregion
    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    Add(EMA(Close, 5));
    Add(EMA(Open, 6));
    Add(EMA(Close, 5));
    Add(EMA(Open, 6));
    CalculateOnBarClose = false;
    }
    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (CrossAbove(EMA(Close, 5), EMA(Open, 6), 1))
    {
    DrawDiamond("My diamond" + CurrentBar, false, 0, 0, Color.Green);
    }
    // Condition set 2
    if (CrossBelow(EMA(Close, 5), EMA(Open, 6), 1))
    {
    DrawDiamond("My diamond" + CurrentBar, false, 0, 0, Color.Red);
    }
    }
    #region Properties
    [Description("Fast EMA")]
    [Category("Parameters")]
    public int PeriodF
    {
    get { return periodF; }
    set { periodF = Math.Max(1, value); }
    }
    [Description("SlowEMA")]
    [Category("Parameters")]
    public int PeriodS
    {
    get { return periodS; }
    set { periodS = Math.Max(1, value); }
    }
    #endregion
    }
    }
    and the indicator which compiles but wont plot:
    HTML 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.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Indicator
    {
    /// <summary>
    /// 2 EMAs Crossing
    /// </summary>
    [Description("2 EMAs Crossing")]
    public class TwoEMAs : Indicator
    {
    #region Variables
    // Wizard generated variables
    private int periodF = 5; // Default setting for PeriodF
    private int periodS = 6; // Default setting for PeriodS
    // User defined variables (add any user defined variables below)
    #endregion
    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    Add(new Plot(Color.Orange, "Fast"));
    Add(new Plot(Color.Blue, "Slow"));
    double periodF=EMA(Close, 5)[0];
    double periodS=EMA(Open, 6)[0];
    // Add(EMA(Close, 5));
    // Add(EMA(Open, 6));
    Plots[0].Pen.Color = Color.Orange; 
    Plots[1].Pen.Color = Color.Blue; 
     
    Overlay = true;
    CalculateOnBarClose = false;
    PriceTypeSupported = true;
     
    }
    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    // Condition set 1
    if (CrossAbove(EMA(Close, 5), EMA(Open, 6), 1))
    {
    DrawDiamond("My diamond" + CurrentBar, false, 0, 0, Color.Green);
    }
    // Condition set 2
    if (CrossBelow(EMA(Close, 5), EMA(Open, 6), 1))
    {
    DrawDiamond("My diamond" + CurrentBar, false, 0, 0, Color.Red);
    }
     
     
    Value.Set(CurrentBar == 0 ? Close[0] : Close[0] * (2.0 / (1 + periodF)) + (1 - (2.0 / (1 + periodF))) * Value[1]);
    Value.Set(CurrentBar == 0 ? Open[0] : Open[0] * (2.0 / (1 + periodS)) + (1 - (2.0 / (1 + periodS))) * Value[1]);
    }
    #region Properties
    [Description("Fast EMA")]
    [Category("Parameters")]
    public int PeriodF
    {
    get { return periodF; }
    set { periodF = Math.Max(1, value); }
    }
    [Description("SlowEMA")]
    [Category("Parameters")]
    public int PeriodS
    {
    get { return periodS; }
    set { periodS = 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 TwoEMAs[] cacheTwoEMAs = null;
    private static TwoEMAs checkTwoEMAs = new TwoEMAs();
    /// <summary>
    /// 2 EMAs Crossing
    /// </summary>
    /// <returns></returns>
    public TwoEMAs TwoEMAs(int periodF, int periodS)
    {
    return TwoEMAs(Input, periodF, periodS);
    }
    /// <summary>
    /// 2 EMAs Crossing
    /// </summary>
    /// <returns></returns>
    public TwoEMAs TwoEMAs(Data.IDataSeries input, int periodF, int periodS)
    {
    checkTwoEMAs.PeriodF = periodF;
    periodF = checkTwoEMAs.PeriodF;
    checkTwoEMAs.PeriodS = periodS;
    periodS = checkTwoEMAs.PeriodS;
    if (cacheTwoEMAs != null)
    for (int idx = 0; idx < cacheTwoEMAs.Length; idx++)
    if (cacheTwoEMAs[idx].PeriodF == periodF && cacheTwoEMAs[idx].PeriodS == periodS && cacheTwoEMAs[idx].EqualsInput(input))
    return cacheTwoEMAs[idx];
    TwoEMAs indicator = new TwoEMAs();
    indicator.BarsRequired = BarsRequired;
    indicator.CalculateOnBarClose = CalculateOnBarClose;
    indicator.Input = input;
    indicator.PeriodF = periodF;
    indicator.PeriodS = periodS;
    indicator.SetUp();
    TwoEMAs[] tmp = new TwoEMAs[cacheTwoEMAs == null ? 1 : cacheTwoEMAs.Length + 1];
    if (cacheTwoEMAs != null)
    cacheTwoEMAs.CopyTo(tmp, 0);
    tmp[tmp.Length - 1] = indicator;
    cacheTwoEMAs = tmp;
    Indicators.Add(indicator);
    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>
    /// 2 EMAs Crossing
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.TwoEMAs TwoEMAs(int periodF, int periodS)
    {
    return _indicator.TwoEMAs(Input, periodF, periodS);
    }
    /// <summary>
    /// 2 EMAs Crossing
    /// </summary>
    /// <returns></returns>
    public Indicator.TwoEMAs TwoEMAs(Data.IDataSeries input, int periodF, int periodS)
    {
    return _indicator.TwoEMAs(input, periodF, periodS);
    }
    }
    }
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    public partial class Strategy : StrategyBase
    {
    /// <summary>
    /// 2 EMAs Crossing
    /// </summary>
    /// <returns></returns>
    [Gui.Design.WizardCondition("Indicator")]
    public Indicator.TwoEMAs TwoEMAs(int periodF, int periodS)
    {
    return _indicator.TwoEMAs(Input, periodF, periodS);
    }
    /// <summary>
    /// 2 EMAs Crossing
    /// </summary>
    /// <returns></returns>
    public Indicator.TwoEMAs TwoEMAs(Data.IDataSeries input, int periodF, int periodS)
    {
    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.TwoEMAs(input, periodF, periodS);
    }
    }
    }
    #endregion
    Last edited by simpletrades; 08-21-2009, 07:01 PM.

    #2
    Hello,

    For the indicator try adding a current bar check greater than any bar you reference in your code and see if that helps:
    if(CurrentBar < 7)
    {
    return;
    }

    I will post about the other one asap.
    DenNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Ben View Post
      Hello,

      For the indicator try adding a current bar check greater than any bar you reference in your code and see if that helps:
      if(CurrentBar < 7)
      {
      return;
      }

      I will post about the other one asap.
      thanks--where does it get inserted?
      I tried after
      protectedoverridevoid Initialize()
      and
      protectedoverridevoid OnBarUpdate()

      didnt help

      Comment


        #4
        Originally posted by NinjaTrader_Ben View Post
        Hello,


        I will post about the other one asap.
        No need to fix the strategy colors. I just found out by accident that even for strategies, the color choice for those EMAs is thru indicators.
        If my diamond crossover markers work I'll know tomorrow nite when the futures reopen.
        Also, if I need to load it for now as a strategy but not use it as such, can the lookback bars be 1 instead of 20?

        Comment


          #5
          Hello,

          After OnBarUpdate, as it shows in this link:


          If that didn't work I'd start debugging. I like to work backward and check the value I am plotting in the .Set method like this for your example:
          Print(Open[0] * (2.0 / (1 + periodS)) + (1 - (2.0 / (1 + periodS))) * Value[1]));

          If that value does not look like it should, then check the values that make up that value until you work back to the issue.

          Also, first, plot a value you know will plot like .Set(Close[0]); and see if the indicator is even plotting at all or if its your calc's.
          DenNinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_Ben View Post
            Hello,

            For the indicator try adding a current bar check greater than any bar you reference in your code and see if that helps:
            if(CurrentBar < 7)
            {
            return;
            }
            Does the placement of the { } matter?
            In this thread you have it after the if(CurrentBar<7)

            But in the link you offered it is placed before the if statement
            protected override void OnBarUpdate()
            {
            if (CurrentBar < 1)
            return;


            if (Close[0] > Close[1])
            // Do something
            }

            Comment


              #7
              Also, like I mentioned earlier, when I load the TwoEMAs into a chart, in the Indicators Window it shows as TwoEMAs(<unknown>,<unknown>)

              What part of the code tells the unknowns to read close,5, open,6 or whatever? Nevermind, that comes only from variables, but if variables is right, can that read unknown?
              Last edited by simpletrades; 08-23-2009, 07:34 AM.

              Comment


                #8
                I removed some parts to try again to make it look like the single EMA code and now the indicators window reads close,5,6 so I still cant get the separate open close option for each, and the only line it plots is the 6 open, but the data box places its value in the fast line item and slow ema is blank even though slow open is what got plotted and not fast close.

                I did figure out that that lines (now made invisible) that read
                double periodF=EMA(Close, 5)[0];
                double periodS=EMA(Open, 6)[0];
                were causing the unknown readings in the indicators window.

                I also found out that removing properties region only made the indicators window read just TwoEMAs(close) but the chart still plotted the 6 open while the data box still placed it in fast not slow.

                Also, why if I post as code it says it's too long, but as HTML its OK?
                HTML 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.Indicator;
                using NinjaTrader.Gui.Chart;
                using NinjaTrader.Strategy;
                #endregion
                // This namespace holds all strategies and is required. Do not change it.
                namespace NinjaTrader.Indicator
                {
                /// <summary>
                /// 2 EMAs Crossing
                /// </summary>
                [Description("2 EMAs Crossing")]
                public class TwoEMAs : Indicator
                {
                #region Variables
                // Wizard generated variables
                private int periodF = 5; // Default setting for PeriodF
                private int periodS = 6; // Default setting for PeriodS
                #endregion
                /// <summary>
                /// This method is used to configure the strategy and is called once before any strategy method is called.
                /// </summary>
                protected override void Initialize() 
                {
                Add(new Plot(Color.Orange, "Fast"));
                Add(new Plot(Color.Magenta, "Slow"));
                //double periodF=EMA(Close, 5)[0];
                //double periodS=EMA(Open, 6)[0];
                // Add(EMA(Close, 5));
                // Add(EMA(Open, 6));
                //Plots[0].Pen.Color = Color.Orange; taking these 2 out lets ninja plot defaults
                //Plots[1].Pen.Color = Color.Blue; 
                 
                Overlay = true;
                CalculateOnBarClose = false;
                PriceTypeSupported = true;
                 
                }
                /// <summary>
                /// Called on each bar update event (incoming tick)
                /// </summary>
                protected override void OnBarUpdate()
                 
                {
                //// Condition set 1
                //if (CrossAbove(EMA(Close, 5), EMA(Open, 6), 1))
                //{
                //DrawDiamond("My diamond" + CurrentBar, false, 0, 0, Color.Green);
                //}
                //// Condition set 2
                //if (CrossBelow(EMA(Close, 5), EMA(Open, 6), 1))
                //{
                //DrawDiamond("My diamond" + CurrentBar, false, 0, 0, Color.Red);
                //}
                //****************
                {
                if (CurrentBar < 7)
                return;
                }
                 
                //****************************** 
                Value.Set(CurrentBar == 0 ? Close[0] : Close[0] * (2.0 / (1 + periodF)) + (1 - (2.0 / (1 + periodF))) * Value[1]);
                Value.Set(CurrentBar == 0 ? Open[0] : Open[0] * (2.0 / (1 + periodS)) + (1 - (2.0 / (1 + periodS))) * Value[1]);
                }
                #region Properties
                [Description("Fast EMA")]
                [Category("Parameters")]
                public int PeriodF
                {
                get { return periodF; }
                set { periodF = Math.Max(1, value); }
                }
                [Description("SlowEMA")]
                [Category("Parameters")]
                public int PeriodS
                {
                get { return periodS; }
                set { periodS = 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 TwoEMAs[] cacheTwoEMAs = null;
                private static TwoEMAs checkTwoEMAs = new TwoEMAs();
                /// <summary>
                /// 2 EMAs Crossing
                /// </summary>
                /// <returns></returns>
                public TwoEMAs TwoEMAs(int periodF, int periodS)
                {
                return TwoEMAs(Input, periodF, periodS);
                }
                /// <summary>
                /// 2 EMAs Crossing
                /// </summary>
                /// <returns></returns>
                public TwoEMAs TwoEMAs(Data.IDataSeries input, int periodF, int periodS)
                {
                checkTwoEMAs.PeriodF = periodF;
                periodF = checkTwoEMAs.PeriodF;
                checkTwoEMAs.PeriodS = periodS;
                periodS = checkTwoEMAs.PeriodS;
                if (cacheTwoEMAs != null)
                for (int idx = 0; idx < cacheTwoEMAs.Length; idx++)
                if (cacheTwoEMAs[idx].PeriodF == periodF && cacheTwoEMAs[idx].PeriodS == periodS && cacheTwoEMAs[idx].EqualsInput(input))
                return cacheTwoEMAs[idx];
                TwoEMAs indicator = new TwoEMAs();
                indicator.BarsRequired = BarsRequired;
                indicator.CalculateOnBarClose = CalculateOnBarClose;
                indicator.Input = input;
                indicator.PeriodF = periodF;
                indicator.PeriodS = periodS;
                indicator.SetUp();
                TwoEMAs[] tmp = new TwoEMAs[cacheTwoEMAs == null ? 1 : cacheTwoEMAs.Length + 1];
                if (cacheTwoEMAs != null)
                cacheTwoEMAs.CopyTo(tmp, 0);
                tmp[tmp.Length - 1] = indicator;
                cacheTwoEMAs = tmp;
                Indicators.Add(indicator);
                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>
                /// 2 EMAs Crossing
                /// </summary>
                /// <returns></returns>
                [Gui.Design.WizardCondition("Indicator")]
                public Indicator.TwoEMAs TwoEMAs(int periodF, int periodS)
                {
                return _indicator.TwoEMAs(Input, periodF, periodS);
                }
                /// <summary>
                /// 2 EMAs Crossing
                /// </summary>
                /// <returns></returns>
                public Indicator.TwoEMAs TwoEMAs(Data.IDataSeries input, int periodF, int periodS)
                {
                return _indicator.TwoEMAs(input, periodF, periodS);
                }
                }
                }
                // This namespace holds all strategies and is required. Do not change it.
                namespace NinjaTrader.Strategy
                {
                public partial class Strategy : StrategyBase
                {
                /// <summary>
                /// 2 EMAs Crossing
                /// </summary>
                /// <returns></returns>
                [Gui.Design.WizardCondition("Indicator")]
                public Indicator.TwoEMAs TwoEMAs(int periodF, int periodS)
                {
                return _indicator.TwoEMAs(Input, periodF, periodS);
                }
                /// <summary>
                /// 2 EMAs Crossing
                /// </summary>
                /// <returns></returns>
                public Indicator.TwoEMAs TwoEMAs(Data.IDataSeries input, int periodF, int periodS)
                {
                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.TwoEMAs(input, periodF, periodS);
                }
                }
                }
                #endregion
                Last edited by simpletrades; 08-23-2009, 08:32 AM.

                Comment


                  #9
                  Hello,

                  Ok I think I see what the issue is now. It is your use of Value.Set twice. Take a look at the default Stochastics indicator code, which has two plots. Note the use of different .Sets: K.Set and D.Set. Then look in the properties area and model what they are doing with the DataSeries K and DataSeries D parts to get the two plots. I recommend copying and pasting the code then changing it so you get it correct with new variable names etc. Also, take out all of the commented stuff so the code is clean and easy to read...it will make things easier the more organized you are in your coding.
                  DenNinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_Ben View Post
                    Hello,

                    Ok I think I see what the issue is now. It is your use of Value.Set twice. Take a look at the default Stochastics indicator code, which has two plots. Note the use of different .Sets: K.Set and D.Set. Then look in the properties area and model what they are doing with the DataSeries K and DataSeries D parts to get the two plots. I recommend copying and pasting the code then changing it so you get it correct with new variable names etc. Also, take out all of the commented stuff so the code is clean and easy to read...it will make things easier the more organized you are in your coding.
                    I tried playing with that but still couldnt figure it out. I looked at the TSI code too.
                    Here is the value section from stochs, but it has nom.Set and denSet which references PeriodK after the // and den and nom are also private data series in variables so I wasnt really sure what to borrow and what not. Also I thought being private data series in variables makes it public data series in properties. But not in stochs.

                    Code:
                    [FONT=Courier New][SIZE=2][COLOR=#0000ff]
                    [SIZE=2][FONT=Courier New][COLOR=#0000ff][SIZE=2][FONT=Courier New][COLOR=#0000ff]protected[/COLOR][/FONT][/SIZE][/COLOR][/FONT][/SIZE][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]override[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff][FONT=Courier New][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] OnBarUpdate()[/SIZE][/FONT]
                    [SIZE=2][FONT=Courier New]{[/FONT][/SIZE]
                    [SIZE=2][FONT=Courier New]nom.Set(Close[[/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]] - MIN(Low, PeriodK)[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]]);[/SIZE][/FONT]
                    [SIZE=2][FONT=Courier New]den.Set(MAX(High, PeriodK)[[/FONT][/SIZE][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]] - MIN(Low, PeriodK)[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]]); [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// PeriodK = Kperiod[/COLOR][/SIZE][/FONT]
                    [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT]
                    [SIZE=2][FONT=Courier New][SIZE=2][FONT=Courier New]K.Set([/FONT][/SIZE][/FONT][/SIZE][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]100[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] * SUM(nom, Smooth)[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]] / (SUM(den, Smooth)[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]] == [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] ? [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]1.0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2] : SUM(den, Smooth)[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]])); [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// Smooth = SlowKperiod[/COLOR][/SIZE][/FONT]
                    [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]D.Set(SMA(K, PeriodD)[[/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080][FONT=Courier New][SIZE=2][COLOR=#800080]0[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]]); [/SIZE][/FONT][/SIZE][/FONT][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000][FONT=Courier New][SIZE=2][COLOR=#008000]// PeriodD = SlowDperiod[/COLOR][/SIZE][/FONT]
                    [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Courier New][SIZE=2][FONT=Courier New][SIZE=2]}[/SIZE][/FONT]
                    [/SIZE][/FONT]
                    Last edited by simpletrades; 08-23-2009, 06:52 PM.

                    Comment


                      #11
                      Originally posted by NinjaTrader_Ben View Post
                      Hello,

                      Also, take out all of the commented stuff so the code is clean and easy to read...it will make things easier the more organized you are in your coding.
                      Here's a clean copy but without any of your recent suggestions per stoch.

                      HTML Code:
                      //TwoEMAsCleanedUP as of 8-23
                      #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.Indicator;
                      using NinjaTrader.Gui.Chart;
                      using NinjaTrader.Strategy;
                      #endregion
                      // This namespace holds all strategies and is required. Do not change it.
                      namespace NinjaTrader.Indicator
                      {
                      /// <summary>
                      /// 2 EMAs Crossing
                      /// </summary>
                      [Description("2 EMAs Crossing")]
                      public class TwoEMAsCleanedUP : Indicator
                      {
                      #region Variables
                      private int periodF = 5; 
                      private int periodS = 6; 
                      #endregion
                      /// <summary>
                      /// This method is used to configure the strategy and is called once before any strategy method is called.
                      /// </summary>
                      protected override void Initialize() 
                      {
                      Add(new Plot(Color.Orange, "Fast")); 
                      Add(new Plot(Color.Magenta, "Slow"));
                      Overlay = true;
                      CalculateOnBarClose = false;
                      PriceTypeSupported = true;
                      
                      }
                      /// <summary>
                      /// Called on each bar update event (incoming tick)
                      /// </summary>
                      protected override void OnBarUpdate()
                      
                      {
                      {
                      if (CurrentBar < 7)
                      return;
                      }
                      
                      Value.Set(CurrentBar == 0 ? Close[0] : Close[0] * (2.0 / (1 + periodF)) + (1 - (2.0 / (1 + periodF))) * Value[1]);
                      Value.Set(CurrentBar == 0 ? Open[0] : Open[0] * (2.0 / (1 + periodS)) + (1 - (2.0 / (1 + periodS))) * Value[1]);
                      }
                      #region Properties
                      [Description("Fast EMA")]
                      [Category("Parameters")]
                      public int PeriodF
                      {
                      get { return periodF; }
                      set { periodF = Math.Max(1, value); }
                      }
                      [Description("SlowEMA")]
                      [Category("Parameters")]
                      public int PeriodS
                      {
                      get { return periodS; }
                      set { periodS = 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 TwoEMAsCleanedUP[] cacheTwoEMAsCleanedUP = null;
                      private static TwoEMAsCleanedUP checkTwoEMAsCleanedUP = new TwoEMAsCleanedUP();
                      /// <summary>
                      /// 2 EMAs Crossing
                      /// </summary>
                      /// <returns></returns>
                      public TwoEMAsCleanedUP TwoEMAsCleanedUP(int periodF, int periodS)
                      {
                      return TwoEMAsCleanedUP(Input, periodF, periodS);
                      }
                      /// <summary>
                      /// 2 EMAs Crossing
                      /// </summary>
                      /// <returns></returns>
                      public TwoEMAsCleanedUP TwoEMAsCleanedUP(Data.IDataSeries input, int periodF, int periodS)
                      {
                      checkTwoEMAsCleanedUP.PeriodF = periodF;
                      periodF = checkTwoEMAsCleanedUP.PeriodF;
                      checkTwoEMAsCleanedUP.PeriodS = periodS;
                      periodS = checkTwoEMAsCleanedUP.PeriodS;
                      if (cacheTwoEMAsCleanedUP != null)
                      for (int idx = 0; idx < cacheTwoEMAsCleanedUP.Length; idx++)
                      if (cacheTwoEMAsCleanedUP[idx].PeriodF == periodF && cacheTwoEMAsCleanedUP[idx].PeriodS == periodS && cacheTwoEMAsCleanedUP[idx].EqualsInput(input))
                      return cacheTwoEMAsCleanedUP[idx];
                      TwoEMAsCleanedUP indicator = new TwoEMAsCleanedUP();
                      indicator.BarsRequired = BarsRequired;
                      indicator.CalculateOnBarClose = CalculateOnBarClose;
                      indicator.Input = input;
                      indicator.PeriodF = periodF;
                      indicator.PeriodS = periodS;
                      indicator.SetUp();
                      TwoEMAsCleanedUP[] tmp = new TwoEMAsCleanedUP[cacheTwoEMAsCleanedUP == null ? 1 : cacheTwoEMAsCleanedUP.Length + 1];
                      if (cacheTwoEMAsCleanedUP != null)
                      cacheTwoEMAsCleanedUP.CopyTo(tmp, 0);
                      tmp[tmp.Length - 1] = indicator;
                      cacheTwoEMAsCleanedUP = tmp;
                      Indicators.Add(indicator);
                      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>
                      /// 2 EMAs Crossing
                      /// </summary>
                      /// <returns></returns>
                      [Gui.Design.WizardCondition("Indicator")]
                      public Indicator.TwoEMAsCleanedUP TwoEMAsCleanedUP(int periodF, int periodS)
                      {
                      return _indicator.TwoEMAsCleanedUP(Input, periodF, periodS);
                      }
                      /// <summary>
                      /// 2 EMAs Crossing
                      /// </summary>
                      /// <returns></returns>
                      public Indicator.TwoEMAsCleanedUP TwoEMAsCleanedUP(Data.IDataSeries input, int periodF, int periodS)
                      {
                      return _indicator.TwoEMAsCleanedUP(input, periodF, periodS);
                      }
                      }
                      }
                      // This namespace holds all strategies and is required. Do not change it.
                      namespace NinjaTrader.Strategy
                      {
                      public partial class Strategy : StrategyBase
                      {
                      /// <summary>
                      /// 2 EMAs Crossing
                      /// </summary>
                      /// <returns></returns>
                      [Gui.Design.WizardCondition("Indicator")]
                      public Indicator.TwoEMAsCleanedUP TwoEMAsCleanedUP(int periodF, int periodS)
                      {
                      return _indicator.TwoEMAsCleanedUP(Input, periodF, periodS);
                      }
                      /// <summary>
                      /// 2 EMAs Crossing
                      /// </summary>
                      /// <returns></returns>
                      public Indicator.TwoEMAsCleanedUP TwoEMAsCleanedUP(Data.IDataSeries input, int periodF, int periodS)
                      {
                      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.TwoEMAsCleanedUP(input, periodF, periodS);
                      }
                      }
                      }
                      #endregion

                      Comment


                        #12
                        simpletrades, you still use Value.Set twice essentially overwriting the previously set indicator value so you see only one displayed, try adding Plots and then assign a value to each plot - for example like the Stochastics indicator referenced by Ben yesterday.

                        Comment


                          #13
                          Originally posted by NinjaTrader_Bertrand View Post
                          simpletrades, you still use Value.Set twice essentially overwriting the previously set indicator value so you see only one displayed, try adding Plots and then assign a value to each plot - for example like the Stochastics indicator referenced by Ben yesterday.
                          I know the code didnt reflect his changes, I kept getting more errors, so I only took the clean it up suggestion to make it easier to review.

                          Comment


                            #14
                            Ok, please review the mentioned Stochastics indicator and how it handles two plots with the associated dataseries....then work then same concept in your custom indicator.

                            Comment


                              #15
                              Originally posted by NinjaTrader_Bertrand View Post
                              Ok, please review the mentioned Stochastics indicator and how it handles two plots with the associated dataseries....then work then same concept in your custom indicator.
                              I tried using stoch as my example--copying what was done for the 2 data series called den and nom.

                              I added this to variables:
                              private DataSeries fast;
                              private DataSeries slow;

                              added this to protectedoverridevoid Initialize() :
                              fast = new DataSeries(this);
                              slow =
                              new DataSeries(this);

                              changed this in protectedoverridevoid OnBarUpdate():

                              fast.Set(CurrentBar ==
                              0 ? Close[0] : Close[0] * (2.0 / (1 + periodF)) + (1 - (2.0 / (1 + periodF))) * fast[1]);

                              slow.Set(CurrentBar ==
                              0 ? Open[0] : Open[0] * (2.0 / (1 + periodS)) + (1 - (2.0 / (1 + periodS))) * slow[1]);


                              no change to properties.
                              it compiles but doesnt chart. The indicator window reads TwoEMAsCleanedUp(Close,5,6) but the data box has no values for either.

                              I reproduced my changes above because somehow it now says my code is too long to copy in the entire script, but the changes are to what I reproduced in my earlier 8/23 posting.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                              0 responses
                              601 views
                              0 likes
                              Last Post Geovanny Suaza  
                              Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                              0 responses
                              347 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by Mindset, 02-09-2026, 11:44 AM
                              0 responses
                              103 views
                              0 likes
                              Last Post Mindset
                              by Mindset
                               
                              Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                              0 responses
                              559 views
                              1 like
                              Last Post Geovanny Suaza  
                              Started by RFrosty, 01-28-2026, 06:49 PM
                              0 responses
                              558 views
                              1 like
                              Last Post RFrosty
                              by RFrosty
                               
                              Working...
                              X