Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
How to make the strategy enter only once
Collapse
X
-
How to make the strategy enter only once
A strategy I’m trying to develop involves price crossing a the swing indicator, and entering after that. The problem arises where it might cross 5 times, and if it hits my stop loss it’ll just keep entering. I’ll get 2-4 losses sometimes instead of just one because of this. Is there a way to have it only enter once per new swing? Even a time limit/ candle limit between trades might help -
Hello BackToTheFutures,
Thanks for your post.
You could use a bool variable to control the number of times a trade is entered.
You could create a bool variable name OkToTade (initially set to false), you could create a condition that checks if you are in a Flat position using if (Position.MarketPosition == MarketPosition.Flat) and set that bool to true. Then, you could create your order entry conditions and also check if OkToTrade is true and call your order entry, and set your bool back to false.
By doing this, your strategy will only place an order when the OkToTrade bool is true. And the OkToTrade bool will only become true when you are in a flat position.
For example, see below.
Let us know if we may assist further.Code:private bool OkToTrade; if (State == State.SetDefaults) { OkToTrade = false; } protected override void OnBarUpdate() { if (Position.MarketPosition == MarketPosition.Flat) OkToTrade = true; if (Close[0] > Open[0] && OkToTrade) { EnterLong() OkToTrade = false; } }<span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>
-
Brandon,Originally posted by NinjaTrader_BrandonH View PostHello BackToTheFutures,
Thanks for your post.
You could use a bool variable to control the number of times a trade is entered.
You could create a bool variable name OkToTade (initially set to false), you could create a condition that checks if you are in a Flat position using if (Position.MarketPosition == MarketPosition.Flat) and set that bool to true. Then, you could create your order entry conditions and also check if OkToTrade is true and call your order entry, and set your bool back to false.
By doing this, your strategy will only place an order when the OkToTrade bool is true. And the OkToTrade bool will only become true when you are in a flat position.
For example, see below.
Let us know if we may assist further.Code:private bool OkToTrade; if (State == State.SetDefaults) { OkToTrade = false; } protected override void OnBarUpdate() { if (Position.MarketPosition == MarketPosition.Flat) OkToTrade = true; if (Close[0] > Open[0] && OkToTrade) { EnterLong() OkToTrade = false; } }
Thank you for the above. To piggyback off this post, How would one write a condition for if a trade was executed within the last 2 bars to set the OkToTrade bool to false?
Comment
-
It’s not an issue while I’m currently in a trade, but say it ranges around that swing high, it’ll go up, get the entry, go back and hit my stop loss, and repeat. To be fair it does that with winners on the occasion as well, but I’m going to get it to only take one trade until a new swing high or low is formed. There might be something there if I were to set a condition comparing the swing indicator to itself, but I’m not sure how that works
Comment
-
Hello LesterC01 and BackToTheFutures,
Thanks for your notes.
LesterC01:
If you are wanting to check if a certain number of bars have passed since an entry execution or exit execution, you could use the BarsSinceEntryExecution() method or BarsSinceExitExecution() method. For example, see below.
if (BarsSinceEntryExecution() == 2) || BarsSinceEntryExecution() == -1) { //Do something }
if (BarsSinceExitExecution() == 2) || BarsSinceExitExecution() == -1) { //Do someting }
See the help guide pages below for more information.
BarsSinceEntryExecution(): https://ninjatrader.com/support/help...yexecution.htm
BarsSinceExitExecution(): https://ninjatrader.com/support/help...texecution.htm
BackToTheFutures:
I understand that you are currently detecting when a swing occurs in your script for your order entry condition. You could still use a bool to control when trades are placed by setting a bool, such as OkToTrade, to true when the swing condition you are detecting occurs. Then, you would set the bool to false after you call your entry order method.
Let us know if we may assist further.<span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>
Comment
-
Is it possible to recreate this using the Strategy Builder? I created a OkToTrade bool variable, but I'm admittedly unclear what to enter under Conditions & Actions.Originally posted by NinjaTrader_BrandonH View PostHello BackToTheFutures,
Thanks for your post.
You could use a bool variable to control the number of times a trade is entered.
You could create a bool variable name OkToTade (initially set to false), you could create a condition that checks if you are in a Flat position using if (Position.MarketPosition == MarketPosition.Flat) and set that bool to true. Then, you could create your order entry conditions and also check if OkToTrade is true and call your order entry, and set your bool back to false.
By doing this, your strategy will only place an order when the OkToTrade bool is true. And the OkToTrade bool will only become true when you are in a flat position.
For example, see below.
Let us know if we may assist further.Code:private bool OkToTrade; if (State == State.SetDefaults) { OkToTrade = false; } protected override void OnBarUpdate() { if (Position.MarketPosition == MarketPosition.Flat) OkToTrade = true; if (Close[0] > Open[0] && OkToTrade) { EnterLong() OkToTrade = false; } }
Any insight would be greatly appreciated!
Comment
-
Hello gravityflyer,
Thanks for your post.
Yes, it would be possible to use a bool in the Strategy Builder to control when trades are placed by the strategy.
See the attached example script demonstrating how this could be accomplished in the Strategy Builder.
See this help guide for more information about working with the Strategy Builder: https://ninjatrader.com/support/help...gy_builder.htm
Let us know if we may assist further.Attached Files<span class="name">Brandon H.</span><span class="title">NinjaTrader Customer Service</span><iframe name="sig" id="sigFrame" src="/support/forum/core/clientscript/Signature/signature.php" frameborder="0" border="0" cellspacing="0" style="border-style: none;width: 100%; height: 120px;"></iframe>
Comment
-
Hi Brandon, I am having a similar problem as LesterC01 in that I'd like to enter a stop limit order only once. I'm using the Swing indicator, and what I'd like to achieve is that when a new Swing high is confirmed, I would like to place a stop limit buy order at that swing high. I can place the order successfully, but once the price goes back below the swing-high limit price, it will create another stop limit order. So I'd like only one stop limit order placed per swing high. Thank you!
Code:if (Swing[Strength].SwingHigh[0]>-1); { TradeTriggered = true; } if (TradeTriggered == true) { EnterLongStopLimit(Convert.ToInt32(DefaultQuantity ), Swing[Strength].SwingHigh[0], Swing[Strength].SwingHigh[0], @"myLongEntry"); LastEntry = Position.AveragePrice; TradeTriggered = false; }
Comment
-
Hello,
To ensure that only one stop limit order is placed per confirmed swing high using the Swing indicator in NinjaTrader, you need to manage the state of your orders more effectively. The key is to prevent the re-triggering of the condition that places the order each time the condition for a swing high is met again before the order is executed or canceled. Here are some strategies and code modifications to help you accomplish this:
1. Track Last Swing High: Maintain a variable to track the last swing high for which an order was placed. This prevents re-entering an order if the price revisits the same swing high.
2. Use a Boolean Flag: A boolean flag (orderPlaced) can be used to check whether an order has already been placed at the current swing high, ensuring that subsequent orders are not placed unless a new swing high is detected. 3. Reset Order Flag
The flag should be reset when an order is executed or canceled, allowing for new orders to be placed when a new swing high is confirmed.
Here is how you could modify your code:
How this works:Code:private double lastSwingHigh = 0; private bool orderPlaced = false; protected override void OnBarUpdate() { // Check for a new swing high if (Swing(Strength).SwingHigh[0] > -1 && Swing(Strength).SwingHigh[0] != lastSwingHigh) { lastSwingHigh = Swing(Strength).SwingHigh[0]; // Update last swing high orderPlaced = false; // Allow a new order to be placed } // Place order if not already placed if (!orderPlaced && lastSwingHigh > 0) { EnterLongStopLimit(Convert.ToInt32(DefaultQuantity), lastSwingHigh, lastSwingHigh, "myLongEntry"); orderPlaced = true; // Set flag to true to block further orders } } protected override void OnOrderUpdate(Order order) { if (order.Name == "myLongEntry" && (order.OrderState == OrderState.Filled || order.OrderState == OrderState.Cancelled)) { orderPlaced = false; // Reset flag on order fill or cancellation } } - Tracking Swing Highs: The lastSwingHigh variable stores the price of the last confirmed swing high. It updates only when a new, distinct swing high is detected.
- Order Placement Control: The orderPlaced flag ensures that orders are only placed once per swing high. It's set to true when an order is placed and reset to false when the order is filled or canceled.
- Order Update Handling: The OnOrderUpdate method checks the state of orders. If the order named "myLongEntry" is either filled or canceled, it resets orderPlaced to allow for a new order at the next swing high.
This approach ensures that your strategy places only one stop limit order per swing high, avoiding multiple orders at the same price point as the market revisits that level. If you have multiple order types or conditions, make sure to adjust the logic to handle each scenario appropriately.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Today, 05:17 AM
|
0 responses
24 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
120 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
63 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
41 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
45 views
0 likes
|
Last Post
|

Comment