Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Test to see if one EMA > other EMA

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

    Test to see if one EMA > other EMA

    Im using the statement below to test one EMA to see if its greater then the other but am getting errors,


    if(EMA(Closes[1], fEMA) > EMA(Closes[1], sEMA))

    Error "Operator > cannot be applied to operands of type ...."

    How should this be done correctly?

    #2
    GKonheiser, you would need to actually pick a value from your EMA series objects to compare, this is usually done by indexing via [ ] brackets.

    if(EMA(Closes[1], fEMA)[0] > EMA(Closes[1], sEMA)[1])

    Comment


      #3
      OK thats working now. In the Initialize section I have :-

      EMA(Closes[1], fEMA).Plots[0].Pen.Color = Color.Black;
      EMA(Closes[1], sEMA).Plots[0].Pen.Color = Color.Red;

      But I'm not seeing them plotted on the chart?

      Comment


        #4
        You see any log errors as this happens? I would not expect that to work since the BarsObject could not be accessed in Initialize() yet.

        Comment


          #5
          Nothing in the logs. So is there any point of creating them in Initialize?

          Comment


            #6
            The option is there to add an indicator to display, you could not pass a custom bars object series to it though. An alternative would be doing the MultiSeries calcs in the indicator itself and then adding this complete indicator from the strategy script.

            Comment


              #7
              Originally posted by GKonheiser View Post
              OK thats working now. In the Initialize section I have :-

              EMA(Closes[1], fEMA).Plots[0].Pen.Color = Color.Black;
              EMA(Closes[1], sEMA).Plots[0].Pen.Color = Color.Red;

              But I'm not seeing them plotted on the chart?
              That is setting the colors of some indicators. Where are you assigning them to a Plot? Merely setting the colors will not make them plot.

              Comment


                #8
                So its not posible to plot an indicator based on a second bar index without writing a seperate indicator?

                Comment


                  #9
                  That is unfortunately correct, another approach would be visualization via the Strategy Plot samples concepts - http://www.ninjatrader.com/support/f...ead.php?t=6651

                  Comment


                    #10
                    OK so im going to write a diferent indicaor for this. Ive taken the EMA code and used that as a starting point. Where in the Value.Set to I pass the Index locating 1 to indicate using the 15 Min bars?
                    Thanks for the help. Here is the code I have so far:-

                    //
                    // 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>
                    /// Exponential Moving Average. The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
                    /// </summary>
                    [Description("The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.")]
                    public class EMA15 : Indicator
                    {
                    #region Variables
                    private int fEMA = 7;
                    private int sEMA = 21;
                    #endregion


                    protected override void Initialize()
                    {
                    Add(PeriodType.Minute, 15);
                    Add(new Plot(Color.Orange, "FEMA"));
                    Add(new Plot(Color.Orange, "SEMA"));

                    Overlay = true;
                    }


                    protected override void OnBarUpdate()
                    {
                    Value.Set(CurrentBar == 0 ? Input[0] : Input[0] * (2.0 / (1 + FEMA)) + (1 - (2.0 / (1 + FEMA))) * Value[1]);
                    Value.Set(CurrentBar == 0 ? Input[0] : Input[0] * (2.0 / (1 + SEMA)) + (1 - (2.0 / (1 + SEMA))) * Value[1]);

                    }

                    #region Properties

                    [Description("Numbers of bars used for calculations")]
                    [GridCategory("Parameters")]
                    public int FEMA
                    {
                    get { return fEMA; }
                    set { fEMA = Math.Max(1, value); }
                    }
                    [Description("Numbers of bars used for calculations")]
                    [GridCategory("Parameters")]
                    public int SEMA
                    {
                    get { return sEMA; }
                    set { sEMA = 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 EMA15[] cacheEMA15 = null;

                    private static EMA15 checkEMA15 = new EMA15();

                    /// <summary>
                    /// The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
                    /// </summary>
                    /// <returns></returns>
                    public EMA15 EMA15(int fEMA, int sEMA)
                    {
                    return EMA15(Input, fEMA, sEMA);
                    }

                    /// <summary>
                    /// The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
                    /// </summary>
                    /// <returns></returns>
                    public EMA15 EMA15(Data.IDataSeries input, int fEMA, int sEMA)
                    {
                    if (cacheEMA15 != null)
                    for (int idx = 0; idx < cacheEMA15.Length; idx++)
                    if (cacheEMA15[idx].FEMA == fEMA && cacheEMA15[idx].SEMA == sEMA && cacheEMA15[idx].EqualsInput(input))
                    return cacheEMA15[idx];

                    lock (checkEMA15)
                    {
                    checkEMA15.FEMA = fEMA;
                    fEMA = checkEMA15.FEMA;
                    checkEMA15.SEMA = sEMA;
                    sEMA = checkEMA15.SEMA;

                    if (cacheEMA15 != null)
                    for (int idx = 0; idx < cacheEMA15.Length; idx++)
                    if (cacheEMA15[idx].FEMA == fEMA && cacheEMA15[idx].SEMA == sEMA && cacheEMA15[idx].EqualsInput(input))
                    return cacheEMA15[idx];

                    EMA15 indicator = new EMA15();
                    indicator.BarsRequired = BarsRequired;
                    indicator.CalculateOnBarClose = CalculateOnBarClose;
                    #if NT7
                    indicator.ForceMaximumBarsLookBack256 = ForceMaximumBarsLookBack256;
                    indicator.MaximumBarsLookBack = MaximumBarsLookBack;
                    #endif
                    indicator.Input = input;
                    indicator.FEMA = fEMA;
                    indicator.SEMA = sEMA;
                    Indicators.Add(indicator);
                    indicator.SetUp();

                    EMA15[] tmp = new EMA15[cacheEMA15 == null ? 1 : cacheEMA15.Length + 1];
                    if (cacheEMA15 != null)
                    cacheEMA15.CopyTo(tmp, 0);
                    tmp[tmp.Length - 1] = indicator;
                    cacheEMA15 = 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 Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
                    /// </summary>
                    /// <returns></returns>
                    [Gui.Design.WizardCondition("Indicator")]
                    public Indicator.EMA15 EMA15(int fEMA, int sEMA)
                    {
                    return _indicator.EMA15(Input, fEMA, sEMA);
                    }

                    /// <summary>
                    /// The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
                    /// </summary>
                    /// <returns></returns>
                    public Indicator.EMA15 EMA15(Data.IDataSeries input, int fEMA, int sEMA)
                    {
                    return _indicator.EMA15(input, fEMA, sEMA);
                    }
                    }
                    }

                    // This namespace holds all strategies and is required. Do not change it.
                    namespace NinjaTrader.Strategy
                    {
                    public partial class Strategy : StrategyBase
                    {
                    /// <summary>
                    /// The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
                    /// </summary>
                    /// <returns></returns>
                    [Gui.Design.WizardCondition("Indicator")]
                    public Indicator.EMA15 EMA15(int fEMA, int sEMA)
                    {
                    return _indicator.EMA15(Input, fEMA, sEMA);
                    }

                    /// <summary>
                    /// The Exponential Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average. The EMA applies more weight to recent prices than the SMA.
                    /// </summary>
                    /// <returns></returns>
                    public Indicator.EMA15 EMA15(Data.IDataSeries input, int fEMA, int sEMA)
                    {
                    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.EMA15(input, fEMA, sEMA);
                    }
                    }
                    }
                    #endregion

                    Comment


                      #11
                      GKonheiser, would suggest to use this approach attached instead.
                      Attached Files

                      Comment


                        #12
                        Thats very kind thanks.

                        Comment


                          #13
                          How do I then call the indicator in the strategy to overlay on the chart?

                          Comment


                            #14
                            You mean to add for charting? Just via the usual Add() call in your Initialize(), i.e.

                            Add(EMA15(5, 20));

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            571 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            331 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
                            549 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by RFrosty, 01-28-2026, 06:49 PM
                            0 responses
                            549 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X