if (Close[0] > emaHigh) { // Exit any short positions if we get a close above candle high 34ema if (Position.MarketPosition == MarketPosition.Short) { var exitPosition = ExitShort(); exitPosition.Name = "EMA Above"; Print(DateTime.Now + ": " + "Exited shorts due to EMA Above"); } if (longsAllowed && longsOn && Position.MarketPosition == MarketPosition.Flat) // Checks if longsOn flag and longsallowed true or not { // Enter trade for (int i = 0; i < EntriesPerTrade; i++) { // Enter long position with a unique name and set the take profit and stop loss levels EnterLong(1, "Long" + i); SetProfitTarget("Long" + i, CalculationMode.Ticks, TakeProfitLevels[i]); SetStopLoss("Long" + i, CalculationMode.Ticks, InitialStopLoss, isSimulatedStop); Print(DateTime.Now + ": " + "onBarUpdate Entered Long" + i + " at price: " + Position.AveragePrice); positionNames.Add("Long" + i); // Add entry name to list } stopLossesModified = false; } // Do not allow anymore longs until the criteria for short has been met first longsAllowed = false; shortsAllowed = true; }
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
Timing sequence of functions
Collapse
X
-
Timing sequence of functions
I have this block of code which is supposed to exit a position, and then enter an opposing position on the same criteria. Originally it was causing a double entry which i guessed it was because the code was being run at basically the same time, causing an exit then the entry of the opposing entry was resulting in a "Close position" as well. I added a check to make sure market position is flat, but now the entry is not being triggered which i guess is due to the same reason. What is the best way to address this?
Code:Tags: None
-
Hello sofortune,
If your entry is not being summited you would need to use prints to see which part of the condition is not becoming true and modify it.
Submitting an exit in the same event as an entry will cause a reversal and an entry which will double the position in the opposite direction.
One other note is that you should always call the Set methods before the entry method:
SetProfitTarget("Long" + i, CalculationMode.Ticks, TakeProfitLevels[i]);
SetStopLoss("Long" + i, CalculationMode.Ticks, InitialStopLoss, isSimulatedStop);
EnterLong(1, "Long" + i);JesseNinjaTrader Customer Service
-
Originally posted by NinjaTrader_Jesse View PostHello sofortune,
If your entry is not being summited you would need to use prints to see which part of the condition is not becoming true and modify it.
Submitting an exit in the same event as an entry will cause a reversal and an entry which will double the position in the opposite direction.
One other note is that you should always call the Set methods before the entry method:
SetProfitTarget("Long" + i, CalculationMode.Ticks, TakeProfitLevels[i]);
SetStopLoss("Long" + i, CalculationMode.Ticks, InitialStopLoss, isSimulatedStop);
EnterLong(1, "Long" + i);
Thanks for the advice for the set methods, just curious what is the reason for that?
Thanks.
Comment
-
Hello sofortune,
You would need to reformat your conditions and make a specific condition for your entry. Right now it looks like you are combining the exit and entry conditions which wont work. You would need to make a seperate condition checking if the position is flat for the entry to ensure that condition is not called when you exit the position. If your entry and exit conditions happen at the same time that will lead to reversals and duplication of positions.
The reason to call the set methods first is to ensure they use the values you supply with the variable. The targets are submitted upon the entry fill and not when you call the Set method so it should have the values you want to use ahead of time. JesseNinjaTrader Customer Service
Comment
-
Hello sofortune,
If the conditions are essentially the same you would need to seperate those parts that into another condition which also has those same conditions.
I would suggest using the strategy builder for this. You can make two sets, one set with your entry conditions and checking for flat and another set with your exit conditions and checking for being in a position. After doing that click View Code and that will show you how the code would be structured for two separate conditions.JesseNinjaTrader Customer Service
Comment
-
Originally posted by NinjaTrader_Jesse View PostHello sofortune,
If the conditions are essentially the same you would need to seperate those parts that into another condition which also has those same conditions.
I would suggest using the strategy builder for this. You can make two sets, one set with your entry conditions and checking for flat and another set with your exit conditions and checking for being in a position. After doing that click View Code and that will show you how the code would be structured for two separate conditions.
Comment
-
I have done this, with two seperate conditions, but it is giving the same thing:
Code:if (Close[0] > emaHigh) { ExitShortPositionsIfAboveEMAHigh(); } // Close Longs and enter Shorts else if (Close[0] < emaLow) { ExitLongPositionsIfBelowEMALow(); } if (Close[0] > emaHigh) { EnterLongPositionsIfAllowed(); } // Close Longs and enter Shorts else if (Close[0] < emaLow) { EnterShortPositionsIfAllowed(); }
Comment
-
Hello sofortune,
OnBarUpdate runs all of the code at once which is why you would need separate conditions for entries and exits. The position won't be updated immediately, it needs time for the order to fill and the position to change. You would only see that update on the following OnBarUpdate event.
If your current condition is not becoming true you would have to use prints to see which part is not true when you run it to get a better idea of what needs changed. The alternative would be to completely separate the logic into two sets so that you can be certain that one part of your logic is not effecting the other part.JesseNinjaTrader Customer Service
Comment
-
Sorry, when you say seperate, do you mean different? Because i've sperated them here.
Essentially the logic is
When criteria for Long is met:
1) exit short
2) check marketposition.flat then enter long
You confirmed what i suspected that all the code is being run at once which caused this. I know which part is not becoming true, it is the marketposition.flat because the all the code is run at once. So it is not a logic issue from what i can tell. Is there a way you can suggest for me to address this problem?
Comment
-
Sorry, maybe there is another approach. The main reason i want to exit the position first, is so i can assign a unique name to that exit. Since ninjatrader will automatically exit positions of the opposing direction when entering a new position, is there anyway to assign that a exit a unique name. Currently it shows as "Close Position" Only.
Comment
-
Hello sofortune,
What you are describing is a reversal, in that case you would just call the opposite entry order. That closes the position and enters into the opposite direction. If you want to use exit orders then you need to make a specific condition to exit when the position is short and then another separate condition to enter long when flat. Once the account becomes flat the entry could work, that won't be in the same OnBarUpdate event.
JesseNinjaTrader Customer Service
Comment
-
Originally posted by NinjaTrader_Jesse View PostHello sofortune,
What you are describing is a reversal, in that case you would just call the opposite entry order. That closes the position and enters into the opposite direction. If you want to use exit orders then you need to make a specific condition to exit when the position is short and then another separate condition to enter long when flat. Once the account becomes flat the entry could work, that won't be in the same OnBarUpdate event.
Comment
-
Originally posted by NinjaTrader_Jesse View PostHello sofortune,
What you are describing is a reversal, in that case you would just call the opposite entry order. That closes the position and enters into the opposite direction. If you want to use exit orders then you need to make a specific condition to exit when the position is short and then another separate condition to enter long when flat. Once the account becomes flat the entry could work, that won't be in the same OnBarUpdate event.
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by AlgoDreamer, Today, 12:39 PM
|
1 response
7 views
0 likes
|
Last Post Today, 12:50 PM | ||
Started by ninza33, Today, 12:31 PM
|
1 response
4 views
0 likes
|
Last Post Today, 12:46 PM | ||
Started by nleitman, Yesterday, 11:46 AM
|
17 responses
45 views
0 likes
|
Last Post
by nleitman
Today, 12:35 PM
|
||
Started by tradingnasdaqprueba, Today, 03:42 AM
|
7 responses
32 views
0 likes
|
Last Post Today, 12:29 PM | ||
Started by kaywai, Today, 11:59 AM
|
1 response
6 views
0 likes
|
Last Post Today, 12:10 PM |
Comment