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

No Entry if no Crossover since Exit

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

    No Entry if no Crossover since Exit

    I am stumped on how to write a condition to prevent another entry (long or short) until a crossover happens of any indicator that I choose after an exit has occurred. See the simplified code below. What is happening is the strategy will keep entering positions due to the SMA being greater than the EMA and ADX rising above 25 again and again. What I want is for it to only happen only once due to SMA has not crossed below the EMA again. I know I could use the crossabove method with a look back, but that is not ideal in chop as I would want a big look back for my strategy, which is much more complicated than the latter. Thanks in advance.

    Code:
    //Enter Long    
            if(Position.MarketPosition == MarketPosition.Flat
                && SMA(8)[0] > EMA(14)[0]
                && ADX(8)[0] > 25
                && since exit no crossover of SMA and EMA.
                && (BarsSinceExit() > 1 || BarsSinceExit() == -1))
            {
                EnterLong(DefaultQuantity,"");
            }
            
            //Exit Long
            if(Position.MarketPosition == MarketPosition.Long
                && ADX(8)[0] < 25
                )
            {
                ExitLong("","");
            }

    #2
    Originally posted by Hammerhorn View Post
    I am stumped on how to write a condition to prevent another entry (long or short) until a crossover happens of any indicator that I choose after an exit has occurred. See the simplified code below. What is happening is the strategy will keep entering positions due to the SMA being greater than the EMA and ADX rising above 25 again and again. What I want is for it to only happen only once due to SMA has not crossed below the EMA again. I know I could use the crossabove method with a look back, but that is not ideal in chop as I would want a big look back for my strategy, which is much more complicated than the latter. Thanks in advance.

    Code:
    //Enter Long    
            if(Position.MarketPosition == MarketPosition.Flat
                && SMA(8)[0] > EMA(14)[0]
                && ADX(8)[0] > 25
                && since exit no crossover of SMA and EMA.
                && (BarsSinceExit() > 1 || BarsSinceExit() == -1))
            {
                EnterLong(DefaultQuantity,"");
            }
     
            //Exit Long
            if(Position.MarketPosition == MarketPosition.Long
                && ADX(8)[0] < 25
                )
            {
                ExitLong("","");
            }
    Methinks I gave out the code for that in this thread: http://www.ninjatrader.com/support/f...27704#poststop

    This may be the exact post with the code: http://www.ninjatrader.com/support/f...906#post327906

    To put it in the simplest of terms, you need a bool flag to determine when to trade and only trade if the flag is enabled.
    Last edited by koganam; 05-26-2013, 11:12 AM.

    Comment


      #3
      Thanks again, I will look the links over and I will delve into creating a bool flag. I did perform a search, but failed to find any relevant threads so thanks again for the links.

      Comment


        #4
        I am trying to modify your example to meet my simplistic example (which I just tested and it is horrific so I modified a little), but I am getting stuck on how to enter my entry conditions. I have never used double in that manner.

        Code:
            if (CrossAbove(SMA(8), EMA(16), 1))
                    {
                    this.TradeEnabledLong = true; //we can trade long only after we cross above the EMA.
                    }
                    
                    // Entry Handling
                    double entryPriceLong = Instrument.MasterInstrument.Round2TickSize(EMA(SlowPeriod)[0]) + (this.ReferenceRange * this.EntriesMultiplier + 1) * TickSize;
        Code:
        //Enter Long, ORIGINAL
                if(Position.MarketPosition == MarketPosition.Flat
                    && SMA(8)[0] > EMA(16)[0]
                    && ADX(8)[0] > 25
                    && SMA(8)[0] > SMA(8)[2]
                    && (BarsSinceExit() > 1 || BarsSinceExit() == -1))
                {
                    EnterLong(DefaultQuantity,"");
                }
                
                //Exit Long, ORIGINAL
                if(Position.MarketPosition == MarketPosition.Long
                    && CrossBelow(ADX(8), 35, 1)
                    )
                {
                    ExitLong("","");
                }
        What is confusing me is the thread you linked has a lot of irrelevant parameters that I don't know how to filter perfectly too.

        Comment


          #5
          Originally posted by koganam View Post
          Methinks I gave out the code for that in this thread: http://www.ninjatrader.com/support/f...27704#poststop

          This may be the exact post with the code: http://www.ninjatrader.com/support/f...906#post327906

          To put it in the simplest of terms, you need a bool flag to determine when to trade and only trade if the flag is enabled.
          With the help of, http://www.ninjatrader.com/support/f...ad.php?t=42193

          EDIT: (removed text) I was able to get further in my type of coding, but despite that I thought it was working it is not.

          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>
              /// Enter the description of your strategy here
              /// </summary>
              [Description("v4 =  Changed ADX(6) > 25 to ADX(5) > 25 and DM Exit to 6 from 8")]
              public class TEST : Strategy
              {
                  #region Variables
                  
                  private bool TradeEnabledLong = false;
                  
                  private int stop = 16;
                  #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()
                  {
                      CalculateOnBarClose = true;
                      EntriesPerDirection = 1;
                      TraceOrders = true;
                      
                      SetStopLoss("", CalculationMode.Ticks, stop, false);
                  }
          
                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
                  if(BarsSinceExit() > 1 || BarsSinceExit() == -1)
                  
                  if(CrossAbove(SMA(8), EMA(16), 1))
                      { 
                      this.TradeEnabledLong = true; //can now trade long
                      }
                  
                  if (Position.MarketPosition == MarketPosition.Long) this.TradeEnabledLong = false;
                      
                      
                  //Enter Long    
                  if(Position.MarketPosition == MarketPosition.Flat
                      && SMA(8)[0] > EMA(16)[0]
                      && ADX(8)[0] > 25
                      && SMA(8)[0] > SMA(8)[2]
                      && (BarsSinceExit() > 1 || BarsSinceExit() == -1)
                      && this.TradeEnabledLong == true)
                  {
                      EnterLong(DefaultQuantity,"");
                  }
                  
                  //Exit Long
                  if(Position.MarketPosition == MarketPosition.Long
                      && CrossBelow(ADX(8), 35, 1)
                      )
                  {
                      ExitLong("","");
                  }
                          
                  }
          
                  #region Properties
                     [Description("")]
                  [GridCategory("Parameters")]
                  public int Stop    
                  {
                      get { return stop; }
                      set { stop = Math.Max(1, value); }
                  }
                  #endregion
              }
          }
          Last edited by Hammerhorn; 05-27-2013, 01:20 AM.

          Comment


            #6
            Originally posted by Hammerhorn View Post
            With the help of, http://www.ninjatrader.com/support/f...ad.php?t=42193

            EDIT: (removed text) I was able to get further in my type of coding, but despite that I thought it was working it is not.
            What does the text in red mean? "It is not working" is not a statement of a problem that is actionable: what are you expecting, and what is happening that is not what you expect?

            Comment


              #7
              Originally posted by koganam View Post
              What does the text in red mean? "It is not working" is not a statement of a problem that is actionable: what are you expecting, and what is happening that is not what you expect?
              Sorry for my sloppy grammar, staying up too late to avoid distractions. I meant to say that the simple strategy is entering long more than once after a crossover occurs, despite my attempt to allow only one trade. I am also certain that this is occurring due to there not being a statement to return the bool flag to false after an exit, correct?
              Last edited by Hammerhorn; 05-27-2013, 03:24 PM.

              Comment


                #8
                Originally posted by Hammerhorn View Post
                ... I am also certain that this is occurring due to there not being a statement to return the bool flag to false after an exit, correct?
                If I may offer some advice, if you first wrote down what you want to do (called a Technical Specification), then translated it to pseudocode before jumping into the code proper, you will find a considerable shortening of your development time.


                Here is how I understand what you are trying to do.
                1. Create a flag to gate my entries. This flag must survive each pass, so it should be a class variable.
                2. Test if I am flat and reset any Stops or Targets that use Set statements. (Edit: Corrected this).
                3. Test my entry conditions, and if the flag allows me to enter, do so
                4. Test if I am in a position, and disable the ability to enter (set the flag correctly to prevent further entries).
                Last edited by koganam; 05-28-2013, 01:45 AM. Reason: Corrected spec.

                Comment


                  #9
                  Originally posted by koganam View Post
                  If I may offer some advice, if you first wrote down what you want to do (called a Technical Specification), then translated it to pseudocode before jumping into the code proper, you will find a considerable shortening of your development time.

                  Here is how I understand what you are trying to do.
                  1. Create a flag to gate my entries. This flag must survive each pass, so it should be a class variable.
                  2. Test if I am flat and set a flag to allow me to enter the market
                  3. Test my entry conditions, and if the flag allows me to enter, do so
                  4. Test if I am in a position, and disable the ability to enter (set the flag correctly to prevent further entries).
                  I was the kid who preferred to not do outlines for essay's so you are telling me nothing new and vital too. I used your Technical Specification and the code below does cut the trades in half using a tight stop loss, but for some reason allows up to two trades, not the one.

                  Code:
                   protected override void OnBarUpdate()
                          {
                          if(BarsSinceExit() > 1 || BarsSinceExit() == -1)
                          
                          if(CrossAbove(SMA(8), EMA(16), 1))
                              { 
                              this.TradeEnabledLong = true; //can now trade long
                              }
                          
                          if (Position.MarketPosition == MarketPosition.Long) this.TradeEnabledLong = false;
                              
                              
                          //Enter Long    
                          if(Position.MarketPosition == MarketPosition.Flat
                              && SMA(8)[0] > EMA(16)[0]
                              && ADX(8)[0] > 25
                              && SMA(8)[0] > SMA(8)[1]
                              && (BarsSinceExit() > 1 || BarsSinceExit() == -1)
                              && this.TradeEnabledLong == true)
                          {
                              EnterLong(DefaultQuantity,"");
                          }
                          
                          //Exit Long
                          if(Position.MarketPosition == MarketPosition.Long
                              && CrossBelow(ADX(8), 35, 1)
                              )
                          {
                              ExitLong("","");
                              //this.TradeEnabledLong = false;
                          }
                                  
                          }

                  1. Create a flag to gate my entries. This flag must survive each pass, so it should be a class variable.

                  if(CrossAbove(SMA(8), EMA(16), 1))
                  {
                  this.TradeEnabledLong = true; //can now trade long
                  }


                  2. Test if I am flat and set a flag to allow me to enter the market


                  I did this part wrong.



                  3. Test my entry conditions, and if the flag allows me to enter, do so


                  //Enter Long
                  if(Position.MarketPosition == MarketPosition.Flat
                  && SMA(8)[0] > EMA(16)[0]
                  && ADX(8)[0] > 25
                  && SMA(8)[0] > SMA(8)[1]
                  && (BarsSinceExit() > 1 || BarsSinceExit() == -1)
                  && this.TradeEnabledLong == true)


                  4. Test if I am in a position, and disable the ability to enter (set the flag correctly to prevent further entries).


                  if (Position.MarketPosition == MarketPosition.Long) this.TradeEnabledLong = false;


                  Am I right?

                  Comment


                    #10
                    Originally posted by Hammerhorn View Post
                    I was the kid who preferred to not do outlines for essay's so you are telling me nothing new and vital too. I used your Technical Specification and the code below does cut the trades in half using a tight stop loss, but for some reason allows up to two trades, not the one.

                    Code:
                     protected override void OnBarUpdate()
                            {
                            if(BarsSinceExit() > 1 || BarsSinceExit() == -1)
                     
                            if(CrossAbove(SMA(8), EMA(16), 1))
                                { 
                                this.TradeEnabledLong = true; //can now trade long
                                }
                     
                            if (Position.MarketPosition == MarketPosition.Long) this.TradeEnabledLong = false;
                     
                     
                            //Enter Long    
                            if(Position.MarketPosition == MarketPosition.Flat
                                && SMA(8)[0] > EMA(16)[0]
                                && ADX(8)[0] > 25
                                && SMA(8)[0] > SMA(8)[1]
                                && (BarsSinceExit() > 1 || BarsSinceExit() == -1)
                                && this.TradeEnabledLong == true)
                            {
                                EnterLong(DefaultQuantity,"");
                            }
                     
                            //Exit Long
                            if(Position.MarketPosition == MarketPosition.Long
                                && CrossBelow(ADX(8), 35, 1)
                                )
                            {
                                ExitLong("","");
                                //this.TradeEnabledLong = false;
                            }
                     
                            }

                    1. Create a flag to gate my entries. This flag must survive each pass, so it should be a class variable.

                    if(CrossAbove(SMA(8), EMA(16), 1))
                    {
                    this.TradeEnabledLong = true; //can now trade long
                    }


                    2. Test if I am flat and set a flag to allow me to enter the market


                    I did this part wrong.



                    3. Test my entry conditions, and if the flag allows me to enter, do so


                    //Enter Long
                    if(Position.MarketPosition == MarketPosition.Flat
                    && SMA(8)[0] > EMA(16)[0]
                    && ADX(8)[0] > 25
                    && SMA(8)[0] > SMA(8)[1]
                    && (BarsSinceExit() > 1 || BarsSinceExit() == -1)
                    && this.TradeEnabledLong == true)


                    4. Test if I am in a position, and disable the ability to enter (set the flag correctly to prevent further entries).


                    if (Position.MarketPosition == MarketPosition.Long) this.TradeEnabledLong = false;


                    Am I right?
                    I misled you somewhat. #2 was actually to reset any Stops/Targets, so, as you are using explicit exit orders, that is not a necessary part of the spec. I have corrected the original post.

                    The other statements that you have are correct, as you have them in the post to which I am responding.

                    Comment


                      #11
                      Originally posted by koganam View Post
                      I misled you somewhat. #2 was actually to reset any Stops/Targets, so, as you are using explicit exit orders, that is not a necessary part of the spec. I have corrected the original post.

                      The other statements that you have are correct, as you have them in the post to which I am responding.

                      Why is the current configuration allowing up to two trades after a crossover? It will not allow any additional, but only two.

                      Comment


                        #12
                        Originally posted by Hammerhorn View Post
                        Why is the current configuration allowing up to two trades after a crossover? It will not allow any additional, but only two.
                        Show an anotated picture of a chart that is showing your errant trade, if you will.

                        Comment


                          #13
                          Originally posted by Hammerhorn View Post
                          Why is the current configuration allowing up to two trades after a crossover? It will not allow any additional, but only two.
                          Hope this helps and thanks in advance, again.
                          Attached Files

                          Comment


                            #14
                            Originally posted by Hammerhorn View Post
                            Hope this helps and thanks in advance, again.
                            So where is the problem again? Each of those entries is followed by a StopLoss on the same bar. In other words, you were never Long at the time that the test was made to turn off the entry gate, so the gate remained enabled.

                            Comment


                              #15
                              Originally posted by koganam View Post
                              So where is the problem again? Each of those entries is followed by a StopLoss on the same bar. In other words, you were never Long at the time that the test was made to turn off the entry gate, so the gate remained enabled.
                              I was almost certain that this would turn the entry gate to off until the next crossover event. Ah, I think I just got it, it is only turning the entry gate off during a long trade. How do I convert that to if a long trade occurred it is off until a crossover?
                              Code:
                              if (Position.MarketPosition == MarketPosition.Long) this.TradeEnabledLong = false;

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by burtoninlondon, Today, 12:38 AM
                              0 responses
                              9 views
                              0 likes
                              Last Post burtoninlondon  
                              Started by AaronKoRn, Yesterday, 09:49 PM
                              0 responses
                              14 views
                              0 likes
                              Last Post AaronKoRn  
                              Started by carnitron, Yesterday, 08:42 PM
                              0 responses
                              11 views
                              0 likes
                              Last Post carnitron  
                              Started by strategist007, Yesterday, 07:51 PM
                              0 responses
                              13 views
                              0 likes
                              Last Post strategist007  
                              Started by StockTrader88, 03-06-2021, 08:58 AM
                              44 responses
                              3,983 views
                              3 likes
                              Last Post jhudas88  
                              Working...
                              X