Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Is there already script somewhere here for 2 EMA crossovers?

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

    Is there already script somewhere here for 2 EMA crossovers?

    Hopefully there already exists on the forum the code for 2 EMAs crossing.
    Even better if it plots a dot or a diamond above the high if the fast crosses below the slower EMA or below the low if the fast crosses up.

    I know there is a sample strategy for simple ma crosses but i wanted an indicator for EMA's and hopefully with a dot above/below.

    #2
    Not sure if it already exists. You can try checking the file sharing section.

    Code:
    if (CrossAbove(EMA(10), EMA(20), 1))
         DrawDiamond("crossabove" + CurrentBar, true, 0, High[0] + 2 * TickSize, Color.Blue);
    else if (CrossBelow(EMA(10), EMA(20), 1))
         DrawDiamond("crossbelow" + CurrentBar, true, 0, Low[0] - 2 * TickSize, Color.Red);
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      [quote=NinjaTrader_Josh;109775]Not sure if it already exists. You can try checking the file sharing section.

      I was just now able to combine the strategy sample for 2 SMAs into my 2 EMA indicator, I think. I havent tried your diamonds yet since it isnt finished.
      I want one EMA to be based on the close and the other EMA on the open prices. This only has one price option based on the closes of each EMA.
      HTML 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.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.Strategy;
      #endregion
      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Indicator
      {
      /// <summary>
      /// exponential moving average crossovers
      /// </summary>
      [Description("Simple moving average cross over strategy.")]
      public class EMACrossOver :Indicator
      {
      #region Variables
      private int fast = 5;
      private int slow = 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()
      {
      EMA(Fast).Plots[0].Pen.Color = Color.Orange;
      EMA(Slow).Plots[0].Pen.Color = Color.Green;
       
      Overlay = true;
      PriceTypeSupported = true;
      CalculateOnBarClose = false;
      }
      /// <summary>
      /// Called on each bar update event (incoming tick).
      /// </summary>
      protected override void OnBarUpdate()
      {
      // if (CrossAbove(EMA(Fast), SMA(Slow), 1))
      // EnterLong();
      // else if (CrossBelow(EMA(Fast), SMA(Slow), 1))
      // EnterShort();
      Value.Set(CurrentBar == 0 ? Input[0] : Input[0] * (2.0 / (1 + fast)) + (1 - (2.0 / (1 + fast))) * Value[1]);
      Value.Set(CurrentBar == 0 ? Input[0] : Input[0] * (2.0 / (1 + slow)) + (1 - (2.0 / (1 + slow))) * Value[1]);
      }
      #region Properties
      /// <summary>
      /// </summary>
      [Description("Period for fast MA")]
      [Category("Parameters")]
      public int Fast
      {
      get { return fast; }
      set { fast = Math.Max(1, value); }
      }
      /// <summary>
      /// </summary>
      [Description("Period for slow MA")]
      [Category("Parameters")]
      public int Slow
      {
      get { return slow; }
      set { slow = 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 EMACrossOver[] cacheEMACrossOver = null;
      private static EMACrossOver checkEMACrossOver = new EMACrossOver();
      /// <summary>
      /// Simple moving average cross over strategy.
      /// </summary>
      /// <returns></returns>
      public EMACrossOver EMACrossOver(int fast, int slow)
      {
      return EMACrossOver(Input, fast, slow);
      }
      /// <summary>
      /// Simple moving average cross over strategy.
      /// </summary>
      /// <returns></returns>
      public EMACrossOver EMACrossOver(Data.IDataSeries input, int fast, int slow)
      {
      checkEMACrossOver.Fast = fast;
      fast = checkEMACrossOver.Fast;
      checkEMACrossOver.Slow = slow;
      slow = checkEMACrossOver.Slow;
      if (cacheEMACrossOver != null)
      for (int idx = 0; idx < cacheEMACrossOver.Length; idx++)
      if (cacheEMACrossOver[idx].Fast == fast && cacheEMACrossOver[idx].Slow == slow && cacheEMACrossOver[idx].EqualsInput(input))
      return cacheEMACrossOver[idx];
      EMACrossOver indicator = new EMACrossOver();
      indicator.BarsRequired = BarsRequired;
      indicator.CalculateOnBarClose = CalculateOnBarClose;
      indicator.Input = input;
      indicator.Fast = fast;
      indicator.Slow = slow;
      indicator.SetUp();
      EMACrossOver[] tmp = new EMACrossOver[cacheEMACrossOver == null ? 1 : cacheEMACrossOver.Length + 1];
      if (cacheEMACrossOver != null)
      cacheEMACrossOver.CopyTo(tmp, 0);
      tmp[tmp.Length - 1] = indicator;
      cacheEMACrossOver = 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>
      /// Simple moving average cross over strategy.
      /// </summary>
      /// <returns></returns>
      [Gui.Design.WizardCondition("Indicator")]
      public Indicator.EMACrossOver EMACrossOver(int fast, int slow)
      {
      return _indicator.EMACrossOver(Input, fast, slow);
      }
      /// <summary>
      /// Simple moving average cross over strategy.
      /// </summary>
      /// <returns></returns>
      public Indicator.EMACrossOver EMACrossOver(Data.IDataSeries input, int fast, int slow)
      {
      return _indicator.EMACrossOver(input, fast, slow);
      }
      }
      }
      // This namespace holds all strategies and is required. Do not change it.
      namespace NinjaTrader.Strategy
      {
      public partial class Strategy : StrategyBase
      {
      /// <summary>
      /// Simple moving average cross over strategy.
      /// </summary>
      /// <returns></returns>
      [Gui.Design.WizardCondition("Indicator")]
      public Indicator.EMACrossOver EMACrossOver(int fast, int slow)
      {
      return _indicator.EMACrossOver(Input, fast, slow);
      }
      /// <summary>
      /// Simple moving average cross over strategy.
      /// </summary>
      /// <returns></returns>
      public Indicator.EMACrossOver EMACrossOver(Data.IDataSeries input, int fast, int slow)
      {
      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.EMACrossOver(input, fast, slow);
      }
      }
      }
      #endregion

      Comment


        #4
        Actually I just tried overlaying a single EMA to test my 2 EMA indicator and only the slow 6 EMA of the pair printed on my chart but it printed orange which was listed as the fast color.

        Comment


          #5
          simpletrades, I'm not sure I follow, if you want to specify running the EMA's on another dataseries as the Close, please use for example this overload -

          Code:
           
          double myEmaOnOpenPrices = EMA(Open, 20)[0];
          BertrandNinjaTrader Customer Service

          Comment


            #6
            Originally posted by NinjaTrader_Bertrand View Post
            simpletrades, I'm not sure I follow, if you want to specify running the EMA's on another dataseries as the Close, please use for example this overload -

            Code:
             
            double myEmaOnOpenPrices = EMA(Open, 20)[0];
            Not sure what overload means. And I couldnt see in my script where it even picked which pricing to go by (I thought it would be part of the selection I make for each chart).
            What I want is the one EMA to be the close and the other based on the open prices.
            Right now, though, as I said in my last post, I only see the slow line at the fast color.

            Comment


              #7
              For example for the EMA you have two different ways to call this method in your scripts - one with a data series input and one without - http://www.ninjatrader-support.com/H...verageEMA.html

              'Overloading' just means to call a method with different inputs....

              In your script you use Input which refers to the one you select when running the indicastor ( PriceTypeSupported = true )...

              To reflect different prices, just replace Input[0] with Open[0] or Close[0] as you calculate your two EMA values.
              BertrandNinjaTrader Customer Service

              Comment


                #8
                Originally posted by NinjaTrader_Bertrand View Post
                In your script you use Input which refers to the one you select when running the indicastor ( PriceTypeSupported = true )...

                To reflect different prices, just replace Input[0] with Open[0] or Close[0] as you calculate your two EMA values.
                Like this? And will this make the second line draw on my chart now?

                Value.Set(CurrentBar == 0 ? close[0] : close[0] * (2.0 / (1 + fast)) + (1 - (2.0 / (1 + fast))) * Value[1]);
                Value.Set(CurrentBar == 0 ? open[0] : open[0] * (2.0 / (1 + slow)) + (1 - (2.0 / (1 + slow))) * Value[1]);

                Comment


                  #9
                  Yes, it should, as now both are based on different input data and not the same (Input[0]) so you saw only one line.....
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    Originally posted by NinjaTrader_Bertrand View Post
                    Yes, it should, as now both are based on different input data and not the same (Input[0]) so you saw only one line.....
                    Didnt work right.
                    The parameters box when I open the indicator reads:
                    Fast 5
                    Price type Close
                    Slow 6

                    and just the slow ema prints on my chart using 'open' as it should but its the orange fast color

                    Comment


                      #11
                      If you have two separate plots you can't use Value.Set for both. You have to call each individual plot by its name and set it accordingly. Please take a look at the MACD indicator for an example. Pay attention to what is in the Properties region and how they call .Set for the various plots.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by NinjaTrader_Josh View Post
                        If you have two separate plots you can't use Value.Set for both. You have to call each individual plot by its name and set it accordingly. Please take a look at the MACD indicator for an example. Pay attention to what is in the Properties region and how they call .Set for the various plots.
                        I just added this to variables like in MACD:
                        private DataSeries fastEma;
                        private DataSeries slowEma;

                        I changed Value.Set(CurrentBar == 0 ?....
                        Value.Set(CurrentBar == 0 ? ....


                        to read fastEMA.set(CurrentBar......
                        slowEMA.set(CurrentBar......
                        and the eror reads the names fastEMA and slowEMA do not exist in the current context.

                        In properties this was there slightly different 3 times but I dont know what it means:
                        [Browsable(false)]
                        [XmlIgnore()]
                        public DataSeries Avg
                        {
                        get { return Values[1];

                        Comment


                          #13
                          fastEMA and slowEMA are not your plot names. The plot names are what is set in Properties.

                          [Browsable(false)]
                          [XmlIgnore()]
                          public DataSeries Avg


                          Avg is your plot name.
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            Originally posted by NinjaTrader_Josh View Post
                            fastEMA and slowEMA are not your plot names. The plot names are what is set in Properties.

                            [Browsable(false)]
                            [XmlIgnore()]
                            public DataSeries Avg


                            Avg is your plot name.
                            In MACD each of the 3 read either default, Avg or Diff

                            For the EMAs I would use fast and slow or default and slow?
                            (ignoring capitals above)

                            Comment


                              #15
                              So title yours how you want them to be in Properties. What ever you name it in the line I have highlighted in Properties is what you have to call for the .Set.
                              Josh P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by funk10101, Today, 08:14 AM
                              3 responses
                              5 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by frankthearm, Today, 09:08 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post frankthearm  
                              Started by samish18, Yesterday, 08:57 AM
                              11 responses
                              28 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by yertle, Today, 08:38 AM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Started by love2code2trade, Yesterday, 01:45 PM
                              3 responses
                              22 views
                              0 likes
                              Last Post NinjaTrader_BrandonH  
                              Working...
                              X