Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
moving SL orders created using an ATM Strategy
Collapse
X
-
moving SL orders created using an ATM Strategy
I am writing a strategy that opens order using an ATM strategy (i.e. using AtmStrategyCreate). This creates the orders, I then have code to detect Stop and Target orders and move the stop per strategy logic.. When i do this, i get error saying "Unhandled exception: Collection was modified; enumeration operation may not execute".. I am using AtmStrategyChangeStopTarget() method for each stop order.. Is this not the right way to do this? How can I move the stops without breaking any "enumeration operations"?Tags: None
-
I tried both below approaches but somehow both are giving me runtime exception.. What am i missing..
Code:private void moveStopUp2() { int orderQuantity = GetAtmStrategyPositionQuantity(atmStrategyId); if (stopOrders?.Count != orderQuantity) { _print($"Not Moving stop. Stop count not the same as entry count"); return; } bool allStopOrdersAccepted = stopOrders.Values.All(order => order.OrderState == OrderState.Accepted || order.OrderState == OrderState.Working); if (!allStopOrdersAccepted) { _print($"Not moving stop. Not all Stop orders are in Accepted or Working state"); } double currentPrice = getCurrentPrice(); if (allStopOrdersAccepted) { var ordersToChange = new List<Order>(); foreach (var kvp in stopOrders) { Order o = kvp.Value; double currentStopPrice = o.StopPrice; double newStopPrice = currentStopPrice + 4 * TickSize; if (newStopPrice < currentPrice - 2 * TickSize || newStopPrice > currentPrice + 2 * TickSize) { o.StopPriceChanged = newStopPrice; ordersToChange.Append(o); } } account.Change(ordersToChange); } } private void moveStopUp() { int orderQuantity = GetAtmStrategyPositionQuantity(atmStrategyId); if (stopOrders?.Count != orderQuantity) { _print($"Not Moving stop. Stop count not the same as entry count"); return; } bool allStopOrdersAccepted = stopOrders.Values.All(order => order.OrderState == OrderState.Accepted || order.OrderState == OrderState.Working); if (!allStopOrdersAccepted) { _print($"Not moving stop. Not all Stop orders are in Accepted or Working state"); } double currentPrice = getCurrentPrice(); if (allStopOrdersAccepted) { foreach (var kvp in stopOrders) { double currentStopPrice = kvp.Value.StopPrice; double newStopPrice = currentStopPrice - 1; if (newStopPrice < currentPrice - 2 * TickSize || newStopPrice > currentPrice + 2 * TickSize ) { _print("Moving stop up"); try { AtmStrategyChangeStopTarget(0, newStopPrice, kvp.Value.Name, atmStrategyId); } catch (Exception ex) { _print(ex.ToString()); } } } } }
-
i am also at loss to understand why only some of the orders get closed and others fail to close when below code gets called. I have tried to run this code as button click handler and i have tried ot run this code as a onbarupdate method.. in both cases it doesn't close some of stop orders or some of target orders..
Code:private void ExitATMOrders(string reason) { string methodName = "ExitATMOrders() >>"; if (State != State.Realtime) { _print($"{methodName} Not in realtime state"); return; } if (!isAtmStrategyCreated) { _print($"{methodName} Orders may not have been created by an ATM Strategy. Will flatten variables only."); //return; } if (atmStrategyId.IsNullOrEmpty() || atmStrategyOrderId.IsNullOrEmpty()) { _print($"{methodName} No valid atmStrategyId or atmStrategyOrderId found. Cannot close ATM orders EOD. Something went wrong"); return; } string[] atmEntryOrder = GetAtmStrategyEntryOrderStatus(atmStrategyOrderId); if (atmEntryOrder.Length > 0 && atmEntryOrder[2] == "Working") { _print($"{methodName} Cancelling ATM Entry Order : {atmStrategyOrderId}"); _print($"{methodName} Cancel Reason : {reason}"); AtmStrategyCancelEntryOrder(atmStrategyOrderId); } else { _print($"{methodName} Exiting ATM Strategy Orders : {atmStrategyId}"); _print($"{methodName} Exiting Reason : {reason}"); AtmStrategyClose(atmStrategyId); } flattenATMVariables(); }
Comment
-
Hello jpkulkarni,
The error you are seeing is likely due to the list you made stopOrders, if that is modified while the list is looping you would get that message. You may need to simplify the code to see which specific part is having a problem.
Regarding closing the orders, which specific part of the logic are you referring to in the code you provided? Are you saying that AtmStrategyClose is not working?
Comment
-
Yeah.. AtmStrategyClose is not closing all the entry, stop and target orders.. Its leaving zombie stop orders (e.g. 1 of 3 stop orders) or target orders. What event is this recommended to run on? I tried to it as button click handler as well as onbarupdate (which is set to onpricechange)..
Thanks for the suggestion on stopOrders.. since i am updating the stop orders, the order update events are getting fired and that is i think modifying the stop orders asynchronusly.. this may be the issue.. I will try to clone stop orders and see if that works.
Comment
-
Hello jpkulkarni,
You can execute that from a button event or OnBarUpdate, if you use a button event you should use TriggerCustomEvent :
I would suggest making a copy of the SampleMACrossOver and modify it to use AtmStrategyClose from a condition in OnBarUpdate. See if that also has the same problem when using the same ATM template, if so attach the .cs file so I can see what was added and compare that against the sample. I can also test that to see the issue and see if there is a way to work around that. Code:TriggerCustomEvent(o => { // call your code here }, null);
Comment
-
Okay.. so cloning stopOrders before modifying them in a loop worked.. Still having issues with AtmClose.. I get 'Close operation failed. Operation timed out. Manually close your position'.. I get this when I am on Sim..
Comment
-
Hello jpkulkarni,
Did you try making a copy of the sampleATMStrategy which can recreate the issue? If so I would need to see if that also happens on my end if that sample recreates the problem. If that does not recreate the problem that would point to something being done in your custom script.
Comment
-
Attached is the full strategy code.. Its using supertrend as a signal.. but it can be changed easily for ema cross.. Eitherway, I am not sure if this is a event issue or something else.. The move stop code works on button click event and I am calling the flatten code exactly like I am calling move stop code.. Stumped.Last edited by jpkulkarni; 01-31-2025, 11:42 AM.
Comment
-
Hello jpkulkarni,
Please try making a copy of the SampleATMCStategy that comes with NinjaTrader and see if that still has the issue, that will help to understand if what you coded is somehow at fault. While I cannot debug the full script you provided we can use the sample which is known to work to test with. That submits a single ATM which you can modify to use AtmStrategyClose to test if that still happens or not.
Comment
-
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by NullPointStrategies, Today, 05:17 AM
|
0 responses
50 views
0 likes
|
Last Post
|
||
|
Started by argusthome, 03-08-2026, 10:06 AM
|
0 responses
126 views
0 likes
|
Last Post
by argusthome
03-08-2026, 10:06 AM
|
||
|
Started by NabilKhattabi, 03-06-2026, 11:18 AM
|
0 responses
69 views
0 likes
|
Last Post
|
||
|
Started by Deep42, 03-06-2026, 12:28 AM
|
0 responses
42 views
0 likes
|
Last Post
by Deep42
03-06-2026, 12:28 AM
|
||
|
Started by TheRealMorford, 03-05-2026, 06:15 PM
|
0 responses
46 views
0 likes
|
Last Post
|

Comment