Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

EMA Strategy Question

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

    EMA Strategy Question

    Currently, I am trying to build a strategy that calls for the following:

    However, every time the colors paint, under the angles defined, which are deleted right now, it enters a long or short position ever time it paints. Can I create a condition where it EntersLong, but only on during the first instance of the .EMAup, and EnterShort at the .Emadown. All input would be much appreciated

    Thanks,


    // Condition set 1
    if (EMA_Colors(, , ).EMAup[0] != Close[0])
    {
    EnterLong(DefaultQuantity,
    "EnterLong");
    }
    // Condition set 2
    if (EMA_Colors(, , ).EMAdown[0] != Close[0])
    {
    EnterShort(DefaultQuantity,
    "EnterShort");
    Last edited by Bharrell; 01-11-2010, 03:20 PM. Reason: Quick Change in Copy

    #2
    Bharrell,

    You could try something like adding a position check.

    if (Position.MarketPosition == MarketPosition.Flat && EMA_Color...)
    EnterLong();
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Hi Josh,

      Thanks for the help. I'm new to building strats, and I am very limited to using the strategy wizard. I would need to unlock the code to enter a position check, correct.

      Thanks in advance for the help.

      -Ben

      Comment


        #4
        Not necessarily. You can use the Condition Builder and select the Strategy position on the left hand side and compare it to long, short, or flat on the right.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Josh,

          This is what I would have with your input? Thoughts? Ah, such a beginner.

          // This namespace holds all strategies and is required. Do not change it.
          namespace NinjaTrader.Strategy
          {
          /// <summary>
          /// Linear regression is used to analyze the degree of an angle in combination with a EMA () period, at an angle of .
          /// </summary>
          [Description("Linear regression is used to analyze the degree of an angle in combination with a EMA () period, at an angle of . ")]
          public class Angles : Strategy
          {
          #region Variables
          // Wizard generated variables
          private int myInput0 = 1; // Default setting for MyInput0
          // 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_Colors(, , ));
          Add(EMA_Colors(, , ));
          SetProfitTarget("EnterShort", CalculationMode.Ticks, 10);
          SetProfitTarget("EnterLong", CalculationMode.Ticks, 10);
          SetStopLoss("EnterLong", CalculationMode.Ticks, 10, true);
          SetStopLoss("EnterShort", CalculationMode.Ticks, 10, true);

          CalculateOnBarClose = true;
          }

          /// <summary>
          /// Called on each bar update event (incoming tick)
          /// </summary>
          protected override void OnBarUpdate()
          {
          // Condition set 1
          if (Position.MarketPosition == MarketPosition.Flat && EMA_Colors(45, 90, 50).EMAup[0] != Close[0])
          {
          EnterLong(DefaultQuantity, "EnterLong");
          }

          // Condition set 2
          if (Position.MarketPosition == MarketPosition.Flat && EMA_Colors(45, 90, 50).EMAdown[0] != Close[0])
          {
          EnterShort(DefaultQuantity, "EnterShort");
          }

          }

          #region Properties
          [Description("")]
          [Category("Parameters")]
          public int MyInput0
          {
          get { return myInput0; }
          set { myInput0 = Math.Max(1, value); }
          }
          #endregion
          }
          }
          Last edited by Bharrell; 01-11-2010, 04:47 PM.

          Comment


            #6
            When analyzing the strategy it still takes every trade where there is a painted EMA color, ugh. With the code listed below.

            Any other ideas?

            Comment


              #7
              Looks good on review. Have you actually loaded up a new instance of your strategy?
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                Yes, I did. I will try again and report back. Thanks for the attention.

                Comment


                  #9
                  World's leading screen capture + recorder from Snagit + Screencast by Techsmith. Capture, edit and share professional-quality content seamlessly.


                  As you can see, it takes every instance of the colored EMA line. I want the first occurred instance. Here it is again...


                  // This namespace holds all strategies and is required. Do not change it.
                  namespace NinjaTrader.Strategy
                  {
                  /// <summary>
                  /// Linear regression is used to analyze the degree of an angle in combination with a EMA (50) period, at an angle of 45.
                  /// </summary>
                  [Description("Linear regression is used to analyze the degree of an angle in combination with a EMA (50) period, at an angle of 45. ")]
                  public class Angles : Strategy
                  {
                  #region Variables
                  // Wizard generated variables
                  private int myInput0 = 1; // Default setting for MyInput0
                  // 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_Colors(45, 90, 50));
                  Add(EMA_Colors(45, 90, 50));
                  SetProfitTarget("EnterShort", CalculationMode.Ticks, 10);
                  SetProfitTarget("EnterLong", CalculationMode.Ticks, 10);
                  SetStopLoss("EnterLong", CalculationMode.Ticks, 10, true);
                  SetStopLoss("EnterShort", CalculationMode.Ticks, 10, true);

                  CalculateOnBarClose = true;
                  }

                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
                  // Condition set 1
                  if (Position.MarketPosition == MarketPosition.Flat && EMA_Colors(45, 90, 50).EMAup[0] != Close[0])
                  {
                  EnterLong(DefaultQuantity, "EnterLong");
                  }

                  // Condition set 2
                  if (Position.MarketPosition == MarketPosition.Flat && EMA_Colors(45, 90, 50).EMAdown[0] != Close[0])
                  {
                  EnterShort(DefaultQuantity, "EnterShort");
                  }

                  }

                  #region Properties
                  [Description("")]
                  [Category("Parameters")]
                  public int MyInput0
                  {
                  get { return myInput0; }
                  set { myInput0 = Math.Max(1, value); }
                  }
                  #endregion
                  }
                  }

                  Comment


                    #10
                    You would need to work with UserDefined variables in the wizard to not take another long trade for example after the direction has changed in your trigger EMA.

                    The concept is shown here -



                    Basically you set / reset those 'flag' variables as you execute long / short trades and then include them being at a certain value in your entry conditions, this would filter consecutive trades out since the 'flags' would not have the needed value to trigger the trades then.

                    Comment


                      #11
                      I think my lack of knowledge in this area is still tripping me up, I have downloaded the provided link and sample strategy, but can seem to understand how that would apply to my working strategy.

                      Comment


                        #12
                        Please take a look at the attached strategy, did this some time ago for demonstrating a similiar concept. Hope it helps in using user variables to achieve what you seek.
                        Attached Files

                        Comment


                          #13
                          i understand how to program user variables now, thanks. Still do not know how to omit further paintings of my vertical line, isolate the first painting. You mentioned a change of direction in the LongShort strategy, but that was based on cross overs. The strat I'm trying to achieve deals with the first occasion. Any other ideas. I am making progress, I promise.

                          World's leading screen capture + recorder from Snagit + Screencast by Techsmith. Capture, edit and share professional-quality content seamlessly.

                          Comment


                            #14
                            Bharrell, you may want to start working directly with the code. As you create your strategy in the wizard, you can click 'View Code' to see the changes being made. Then, you can create a strategy like you want it and unlock it, but keep a wizard copy so you can revert if it stops working.

                            Another option would be to edit the indicator so it returns the desired value only when you want it to - in this case, when the color switches.
                            AustinNinjaTrader Customer Service

                            Comment


                              #15
                              That is a great idea as far as changing the actual indicator. However, I can't figure that out either. Here is the code. Any suggestions on how to make only the first instance of the color appear?

                              // 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 EMACOLORS2 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 EMACOLORS2 applies more weight to recent prices than the SMA.")]
                              public class EMACOLORS2 : Indicator
                              {
                              #region Variables
                              private int period = 14;
                              private int ema34slope = 0;
                              private double radToDegrees = 180/Math.PI; // to convert Radians to Degrees for slope calc
                              private int angle1 = 30;
                              private int angle2 = 60;
                              #endregion

                              /// <summary>
                              /// This method is used to configure the indicator and is called once before any bar data is loaded.
                              /// </summary>
                              protected override void Initialize()
                              {
                              Add(new Plot(Color.Green, "EMAup"));
                              Add(new Plot(Color.Maroon, "EMAdown"));
                              Add(new Plot(Color.Red, "EMAslopedown"));
                              Add(new Plot(Color.LimeGreen, "EMAslopeup"));
                              Add(new Plot(Color.Gray, "EMAflat"));
                              Plots[0].Pen.Width = 2;
                              Plots[1].Pen.Width = 2;
                              Plots[2].Pen.Width = 2;
                              Plots[3].Pen.Width = 2;
                              Plots[4].Pen.Width = 2;
                              Plots[0].Pen.DashStyle = DashStyle.Dash;
                              Plots[1].Pen.DashStyle = DashStyle.Dash;
                              Plots[2].Pen.DashStyle = DashStyle.Dash;
                              Plots[3].Pen.DashStyle = DashStyle.Dash;
                              Plots[4].Pen.DashStyle = DashStyle.Dash;
                              Overlay = true;
                              PriceTypeSupported = true;
                              }

                              /// <summary>
                              /// Called on each bar update event (incoming tick)
                              /// </summary>
                              protected override void OnBarUpdate()
                              {
                              if(CurrentBar<Period) return;
                              ema34slope = (int)(radToDegrees*(Math.Atan((EMA(34)[0]-(EMA(34)[1]+EMA(34)[2])/2)/1.5/TickSize)));
                              if(Rising(EMA(Period)))
                              {
                              if(ema34slope>=angle2)
                              {
                              EMAslopeup.Set(1,EMA(Period)[1]);
                              EMAslopeup.Set(EMA(Period)[0]);
                              }
                              else
                              if (ema34slope>=angle1)
                              {
                              EMAup.Set(1,EMA(Period)[1]);
                              EMAup.Set(EMA(Period)[0]);
                              }
                              else
                              { EMAflat.Set(1,EMA(Period)[1]);
                              EMAflat.Set(EMA(Period)[0]);
                              }
                              }
                              else
                              { if(ema34slope<=-angle2)
                              {
                              EMAslopedown.Set(1,EMA(Period)[1]);
                              EMAslopedown.Set(EMA(Period)[0]);
                              }
                              else
                              if (ema34slope<=-angle1)
                              {
                              EMAdown.Set(1,EMA(Period)[1]);
                              EMAdown.Set(EMA(Period)[0]);
                              }
                              else
                              { EMAflat.Set(1,EMA(Period)[1]);
                              EMAflat.Set(EMA(Period)[0]);
                              }
                              }
                              }

                              Comment

                              Latest Posts

                              Collapse

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