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

Looking for simple EMA's crossover strategy

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

    Looking for simple EMA's crossover strategy

    Hi guys, i'm looking for simple strategy of EMA 5 crossing EMA 34. When its crossing from the bottom, the strategy enters long and the opposite. I am sure that have been done before, but I could not find it in the files. Thank you very much in advance.

    #2
    Hello meowflying,

    Thank-you for your post.

    This is a simple strategy that you can easily create in the strategy wizard. If you are not familiar with the process this would be an excellent strategy to start with.

    Here is a link to the helpguide on the strategy wizard in setting up a simple MA cross over: http://www.ninjatrader.com/support/h..._cross_ove.htm

    Please let me know if I can be of further assistance.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thank you Paul, it was straight forward, however the strategy is not working as it should. For example it does not set targets(20 ticks) or stop loss(15 ticks) and not enter on every crossover between the EMA's. Also I wanted green arrows up or down showing the place of the entries, does not show them as well. Please advise.

      #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>
      /// EMA's crossover
      /// </summary>
      [Description("EMA's crossover")]
      public class EMAS : Strategy
      {
      #region Variables
      // Wizard generated variables
      private int fast = 5; // Default setting for Fast
      private int slow = 34; // Default setting for Slow
      // 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(5));
      Add(EMA(34));
      Add(EMA(5));
      Add(EMA(34));
      SetProfitTarget("20", CalculationMode.Ticks, 0);
      SetStopLoss("15", CalculationMode.Ticks, 0, false);

      CalculateOnBarClose = false;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // Condition set 1
      if (CrossAbove(EMA(5), EMA(34), 1))
      {
      EnterLongLimit(DefaultQuantity, 0, "long");
      DrawArrowUp("My up arrow" + CurrentBar, false, 0, 0, Color.Green);
      }

      // Condition set 2
      if (CrossBelow(EMA(5), EMA(34), 1))
      {
      EnterShortLimit(DefaultQuantity, 0, "short");
      DrawArrowDown("My down arrow" + CurrentBar, false, 0, 0, Color.Red);
      }
      }

      #region Properties
      [Description("Fast EMA")]
      [GridCategory("Parameters")]
      public int Fast
      {
      get { return fast; }
      set { fast = Math.Max(1, value); }
      }

      [Description("Slow EMA")]
      [GridCategory("Parameters")]
      public int Slow
      {
      get { return slow; }
      set { slow = Math.Max(1, value); }
      }
      #endregion
      }

      Originally posted by NinjaTrader_Paul View Post
      Hello meowflying,

      Thank-you for your post.

      This is a simple strategy that you can easily create in the strategy wizard. If you are not familiar with the process this would be an excellent strategy to start with.

      Here is a link to the helpguide on the strategy wizard in setting up a simple MA cross over: http://www.ninjatrader.com/support/h..._cross_ove.htm

      Please let me know if I can be of further assistance.

      Comment


        #4
        Hello meowflying,

        Thanks for posting your code.

        When using a limit order to enter, either long or short, you have to define the entry price. In your code the entry price is zero. Here is a reference to the help guide on the EnterLongLimit: http://www.ninjatrader.com/support/h...rlonglimit.htm Note the use of "GetCurrentBid()" being used for the entry price for LongLimit.

        Here is the help guide on EnterShortLimit: http://www.ninjatrader.com/support/h...shortlimit.htm In this case note the use of "GetCurrentAsk()" for entry price.

        In the wizard where price is displayed as "0', click on the zero to open up "value" window and select, in the case of longLimit, "Bid", click okay and note that strategy action window now shows GetCurrentBid(). I have attached a couple of picture showing this. You would want to do the same thing for the EnterShortLimit except use the "Ask".


        The arrows are being drawn at a level of zero. If you scroll down on your chart to the zero level you will likely find all the arrows You will need to change the Y axis location in the wizard. In the case of the crossAbove condition you probably want to show the arrows below pointing up, so in this case you would want to use the Low[0] of the price and subtract say 4 ticks from that so that the arrow is below the low of the bar. I have attached a couple of pictures showing this.

        You would want to do the same thing but on the opposite side for the cross below and in that case, you can use the High[0] + 4 ticks.

        Please let me know if I can be of further assistance.
        Attached Files
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          RE

          Thank you Paul, i changed and improved the code. Now it does enter long or short and has attached target and stop loss. 2 problems remains:

          1. Strategy does plot few arrows up or down instead of one.
          2. Strategy enters short or long before exiting the previous position when the conditions are right. How to prevent it from happening?


          /// <summary>
          /// EMAS crossover
          /// </summary>
          [Description("EMAS crossover")]
          public class EMA2 : Strategy
          {
          #region Variables
          // Wizard generated variables
          private int fast = 5; // Default setting for Fast
          private int slow = 34; // Default setting for Slow
          // 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(5));
          Add(EMA(34));
          Add(EMA(5));
          Add(EMA(34));
          SetProfitTarget("long", CalculationMode.Ticks, 20);
          SetStopLoss("long", CalculationMode.Ticks, 15, false);
          SetProfitTarget("short", CalculationMode.Ticks, 20);
          SetStopLoss("short", CalculationMode.Ticks, 15, false);

          CalculateOnBarClose = true;
          }

          /// <summary>
          /// Called on each bar update event (incoming tick)
          /// </summary>
          protected override void OnBarUpdate()
          {
          // Condition set 1
          if (CrossAbove(EMA(5), EMA(34), 3))
          {
          EnterLong(DefaultQuantity, "long");
          DrawArrowUp("My up arrow" + CurrentBar, false, 0, High[0] + 4 * TickSize, Color.Lime);
          }

          // Condition set 2
          if (CrossBelow(EMA(5), EMA(34), 3))
          {
          EnterShort(DefaultQuantity, "short");
          DrawArrowDown("My down arrow" + CurrentBar, false, 0, High[0] + -4 * TickSize, Color.Red);
          }
          }

          #region Properties
          [Description(" Fast Ema Period")]
          [GridCategory("Parameters")]
          public int Fast
          {
          get { return fast; }
          set { fast = Math.Max(1, value); }
          }

          [Description("Slow Ema Period")]
          [GridCategory("Parameters")]
          public int Slow
          {
          get { return slow; }
          set { slow = Math.Max(1, value); }
          }
          #endregion
          }
          }

          Comment


            #6
            Hello meowflying,

            Thanks for your reply, glad that you are making progress in your strategy.

            Regarding the arrows, it is possible that in some instances that the lines being evalulated could be crossing back and forth in a short period due to price action and/or the lookback period of 3 is allowing to many evaluations of that condition. Try using a lookback of 1.

            As you said the entries are happening when the conditions are right, so you need to add another condition to evaluate if you are not already in the market. In the strategy wizard condition builder this would be under stategy and you would want to select "Current market position" on the left, "==" in the middle and again the strategy section on the right select "Flat". Code wise it would look like: if (Position.MarketPosition == MarketPosition.Flat). You would want to add that to both of your entry sets.

            Please let me know if I can be of futher assistance.
            Paul H.NinjaTrader Customer Service

            Comment


              #7
              RE

              Paul, the arrows and market position have been fixed. Thank you. However the strategy got 2 more issues:

              1. In case we have position already, we got an opposite side signal when conditions are met, the strategy wont enter till the stop is executed, however just after the stop is executed it does process previous signal with higher/lower price. How to prevent the strategy to execute the "delayed" signals?
              2. I was running the strategy on market replay TF with prices around 1176. Suddenly the strategy tried to enter long with much higher price like 2000. Needless to say when it tried to insert stop loss it did triggered rejection and strategy crashed. Please advise.


              7/18/2014 11:15:30 AM|1|32|Order='c2cf16bc77c34f21bcab591e50a4841d/Replay101' Name='long' New state=PendingSubmit Instrument='TF 06-14' Action=Buy
              Limit price=0 Stop price=0 Quantity=1
              Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
              7/18/2014 11:15:30 AM|1|32|Order='c2cf16bc77c34f21bcab591e50a4841d/Replay101' Name='long' New state=Accepted Instrument='TF 06-14' Action=Buy
              Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
              7/18/2014 11:15:30 AM|1|32|Order='c2cf16bc77c34f21bcab591e50a4841d/Replay101' Name='long' New state=Working Instrument='TF 06-14' Action=Buy
              Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
              7/18/2014 11:15:30 AM|1|32|Order='c2cf16bc77c34f21bcab591e50a4841d/Replay101' Name='long' New state=Filled Instrument='TF 06-14' Action=Buy
              Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=1 Fill price=2000 Error=NoError Native error=''
              7/18/2014 11:15:30 AM|1|16|Execution='15300060a47c4df8b2b30f69434c9d1 f' Instrument='TF 06-14' Account='Replay101' Exchange=Default Price=2000
              Quantity=1 Market position=Long Operation=Insert Order='c2cf16bc77c34f21bcab591e50a4841d' Time='3/26/2014 2:00:00 AM'
              7/18/2014 11:15:30 AM|1|64|Instrument='TF 06-14' Account='Replay101' Avg price=2000 Quantity=1 Market position=Long Operation=Insert Currency=Unknown
              7/18/2014 11:15:30 AM|1|32|Order='ba01bc68fbbc4367bc6cffa8b6e6ad37/Replay101' Name='Stop loss' New state=Rejected Instrument='TF 06-14'
              Action=Sell Limit price=0 Stop price=1998.5 Quantity=1 Type=Stop Filled=0 Fill price=0 Error=OrderRejected Native error='Sell stop or sell stop
              limit orders can't be placed above the market.'
              7/18/2014 11:15:30 AM|0|32|Market Replay Connection, Sell stop or sell stop limit orders can't be placed above the market. affected Order:
              Sell 1 Stop @ 1998.5
              7/18/2014 11:15:30 AM|0|128|Strategy 'EMA2/2a6ee19dd99f4ef8a0233290c2c75afb' submitted an order that generated the following error 'OrderRejected'.
              Strategy has sent cancel requests, attempted to close the position and terminated itself.
              7/18/2014 11:15:30 AM|1|32|Order='59cdd89c53f549de896de7da090794e5/Replay101' Name='Close' New state=PendingSubmit Instrument='TF 06-14' Action=Sell
              Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
              7/18/2014 11:15:30 AM|1|32|Order='59cdd89c53f549de896de7da090794e5/Replay101' Name='Close' New state=Accepted Instrument='TF 06-14' Action=Sell
              Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
              7/18/2014 11:15:30 AM|1|32|Order='59cdd89c53f549de896de7da090794e5/Replay101' Name='Close' New state=Working Instrument='TF 06-14' Action=Sell
              Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=0 Fill price=0 Error=NoError Native error=''
              7/18/2014 11:15:30 AM|1|32|Order='59cdd89c53f549de896de7da090794e5/Replay101' Name='Close' New state=Filled Instrument='TF 06-14' Action=Sell
              Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=1 Fill price=1077 Error=NoError Native error=''
              7/18/2014 11:15:30 AM|1|16|Execution='907d8b6be8b24d97914f60113ab432f 9' Instrument='TF 06-14' Account='Replay101' Exchange=Default Price=1077
              Quantity=1 Market position=Short Operation=Insert Order='59cdd89c53f549de896de7da090794e5' Time='3/26/2014 2:00:00 AM'
              7/18/2014 11:15:30 AM|1|64|Instrument='TF 06-14' Account='Replay101' Avg price=1077 Quantity=0 Market position=Long Operation=Remove Currency=Unknown
              7/18/2014 11:15:30 AM|1|128|Disabling NinjaScript strategy 'EMA2/2a6ee19dd99f4ef8a0233290c2c75afb'


              /// <summary>
              /// EMAS crossover
              /// </summary>
              [Description("EMAS crossover")]
              public class EMA2 : Strategy
              {
              #region Variables
              // Wizard generated variables
              private int fast = 5; // Default setting for Fast
              private int slow = 34; // Default setting for Slow
              // 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(5));
              Add(EMA(34));
              Add(EMA(5));
              Add(EMA(34));
              SetProfitTarget("long", CalculationMode.Ticks, 20);
              SetStopLoss("long", CalculationMode.Ticks, 15, false);
              SetProfitTarget("short", CalculationMode.Ticks, 20);
              SetStopLoss("short", CalculationMode.Ticks, 15, false);

              CalculateOnBarClose = false;
              }

              /// <summary>
              /// Called on each bar update event (incoming tick)
              /// </summary>
              protected override void OnBarUpdate()
              {
              // Condition set 1
              if (CrossAbove(EMA(5), EMA(34), 3)
              && Position.MarketPosition == MarketPosition.Flat)
              {
              EnterLong(DefaultQuantity, "long");
              DrawArrowUp("My up arrow" + CurrentBar, false, 0, High[0] + 4 * TickSize, Color.Lime);
              }

              // Condition set 2
              if (CrossBelow(EMA(5), EMA(34), 3)
              && Position.MarketPosition == MarketPosition.Flat)
              {
              EnterShort(DefaultQuantity, "short");
              DrawArrowDown("My down arrow" + CurrentBar, false, 0, High[0] + -4 * TickSize, Color.Red);
              }
              }

              #region Properties
              [Description(" Fast Ema Period")]
              [GridCategory("Parameters")]
              public int Fast
              {
              get { return fast; }
              set { fast = Math.Max(1, value); }
              }

              [Description("Slow Ema Period")]
              [GridCategory("Parameters")]
              public int Slow
              {
              get { return slow; }
              set { slow = Math.Max(1, value); }
              }
              #endregion
              }


              Originally posted by NinjaTrader_Paul View Post
              Hello meowflying,

              Thanks for your reply, glad that you are making progress in your strategy.

              Regarding the arrows, it is possible that in some instances that the lines being evalulated could be crossing back and forth in a short period due to price action and/or the lookback period of 3 is allowing to many evaluations of that condition. Try using a lookback of 1.

              As you said the entries are happening when the conditions are right, so you need to add another condition to evaluate if you are not already in the market. In the strategy wizard condition builder this would be under stategy and you would want to select &quot;Current market position&quot; on the left, &quot;==&quot; in the middle and again the strategy section on the right select &quot;Flat&quot;. Code wise it would look like: if (Position.MarketPosition == MarketPosition.Flat). You would want to add that to both of your entry sets.

              Please let me know if I can be of futher assistance.

              Comment


                #8
                Hello meowflying,

                Thanks for your reply and appreciate the full information.

                In order to assist you further I will need your log and trace files from when you ran the market replay with the strategy crash.

                You can do this by going to the Control Center-> Help-> Mail to Support.

                Please reference the following ticket number in the body of the email: 1113652 , reference to Paul and copy a link to this post.

                I look forward to reviewing the files.
                Paul H.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by naanku, Today, 07:25 PM
                0 responses
                1 view
                0 likes
                Last Post naanku
                by naanku
                 
                Started by milfocs, Today, 07:23 PM
                0 responses
                1 view
                0 likes
                Last Post milfocs
                by milfocs
                 
                Started by PaulMohn, Today, 06:59 PM
                0 responses
                4 views
                0 likes
                Last Post PaulMohn  
                Started by bortz, 11-06-2023, 08:04 AM
                48 responses
                1,748 views
                0 likes
                Last Post carnitron  
                Started by Jonker, 04-27-2024, 01:19 PM
                3 responses
                23 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Working...
                X