Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Last market position
Collapse
X
-
Last market position
So I have a strategy that I want to alternate from long to short positions or vice versa and the strategy may generate multiple buys or sells in a row but I only want to take the first one and then wait for the market to reverse. Is there a condition I could add like last market position = short for the buys and reverse for the sells?Tags: None
-
Hello burton82,
Thank you for the question.
One way would be to check your current position and then apply your logic for your buy or sell depending what your position is.
Code:if (Position.MarketPosition == MarketPosition.Flat) { //do something like place an order } else if(Position.MarketPosition == MarketPosition.Long) { //logic to determine when the market reverses exit the position that was entered when flat } else if(Position.MarketPosition == MarketPosition.Short) { //logic to determine when the market reverses exit the position that was entered when flat }
Here is the help guide reference on using Position.MarketPosition:
Please let me know if I may be of additional assistance.
-
Thanks that not exactly what I am looking for though, so say lets say the day opens and my strategy enters a long entry and then exits based on the signal generated, then another long entry signal comes, and that is what I do not want to happen until a short signal comes. So basically I do not what to have consecutive long or short signals.
Comment
-
Hello burton82,
Thank you for clarifying that for me. I have an Idea of some logic that you could use, although not knowing the full complexity of your code this may or may not work for you in your perticular situation.
The following logic would be in addition to your conditions for entering and exiting that you already have in place.
To start You could create two integer variables that are set to 0 at start.
So maybe something like this:
Now starting with the long entry, you would need to check if the longOrder == 0 and if so continue.Code:#region Variables int longOrder = 0; int shortOrder = 0; #endregion
if the longOrder == 0 after you place your order you would set it to a 1 so the original condition can not be met. This prevents multiple orders in the same direction.
So let’s say you entered long and exited and the condition for your short order is met, after the short orders entry you could reset the longOrder back to 0 so the next time the long orders condition is met it can place an order again.
This is essentially a toggle, if an order is placed, don't place more of the same and wait for the opposite, once the opposite happens don't place any more of the same and again wait for the opposite.
Here is an example of the logic I described
Please let me know if I may be of additional assistanceCode:if(longOrder == 0) // if a long order has not been placed, continue. { EnterLong(); //submit the entry longOrder = 1; //add 1 to longOrder so this condition does not happen again until it is 0 again shortOrder = 0; //change shortOrder to 0 so if the short order condition is met a short order can be placed } if(shortOrder == 0) { EnterShort(); shortOrder = 1; //add 1 to shortOrder so this condition does not happen again until it is 0 again longOrder = 0; //change longOrder to 0 so if the longOrder condition is met and a longOrder can be placed again }
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
77 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
45 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
27 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
32 views
0 likes
|
Last Post
|
||
|
Started by Mindset, 02-28-2026, 06:16 AM
|
0 responses
63 views
0 likes
|
Last Post
by Mindset
02-28-2026, 06:16 AM
|

Comment