//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("",""); }
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!
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
NinjaTrader
No Entry if no Crossover since Exit
Collapse
X
-
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:Tags: None
-
Originally posted by Hammerhorn View PostI 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("",""); }
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.
-
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
-
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("",""); }
Comment
-
Originally posted by koganam View PostMethinks 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.
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
-
Originally posted by Hammerhorn View PostWith 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.
Comment
-
Originally posted by koganam View PostWhat 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?Last edited by Hammerhorn; 05-27-2013, 03:24 PM.
Comment
-
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?
Here is how I understand what you are trying to do.- Create a flag to gate my entries. This flag must survive each pass, so it should be a class variable.
- Test if I am flat and reset any Stops or Targets that use Set statements. (Edit: Corrected this).
- Test my entry conditions, and if the flag allows me to enter, do so
- Test if I am in a position, and disable the ability to enter (set the flag correctly to prevent further entries).
Comment
-
Originally posted by koganam View PostIf 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.- Create a flag to gate my entries. This flag must survive each pass, so it should be a class variable.
- Test if I am flat and set a flag to allow me to enter the market
- Test my entry conditions, and if the flag allows me to enter, do so
- Test if I am in a position, and disable the ability to enter (set the flag correctly to prevent further entries).
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
-
Originally posted by Hammerhorn View PostI 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?
The other statements that you have are correct, as you have them in the post to which I am responding.
Comment
-
Originally posted by koganam View PostI 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
-
-
Originally posted by Hammerhorn View PostHope this helps and thanks in advance, again.
Comment
-
Originally posted by koganam View PostSo 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.
Code:if (Position.MarketPosition == MarketPosition.Long) this.TradeEnabledLong = false;
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by sdauteuil, 09-23-2021, 10:16 AM
|
4 responses
1,208 views
0 likes
|
Last Post Today, 11:41 PM | ||
Started by agclub, 04-21-2024, 08:57 PM
|
5 responses
34 views
0 likes
|
Last Post
by agclub
Today, 09:47 PM
|
||
Started by ESHunter, Today, 08:06 PM
|
2 responses
18 views
0 likes
|
Last Post
by ESHunter
Today, 09:27 PM
|
||
Started by ETFVoyageur, 05-07-2024, 07:05 PM
|
19 responses
150 views
0 likes
|
Last Post
by ETFVoyageur
Today, 09:14 PM
|
||
Started by ETFVoyageur, Yesterday, 10:13 PM
|
3 responses
26 views
0 likes
|
Last Post
by ETFVoyageur
Today, 09:08 PM
|
Comment