Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
2 entry orders help!
Collapse
X
-
Hello PaulMohn,
Your inquiry has been about modifying orders on a keypress, is this not what you are trying to do?
The code itself where the orders are modified is in the handler method for the keypress, is this also not correct?
Is this not where you are currently investigating?
The code in Account_OrderUpdate is for submitting the stop and limit and adding these orders to a list, correct?
At the top of the Account_OrderUpdate method to print the order to know information about the order updating:
Print(string.Format("{0} | Account_OrderUpdate | order.ToString(): {1}", orderUpdateArgs.Time, orderUpdateArgs.Order.ToString());
In the keypress where are you investigating modify the orders (ChartControl_PreviewKeyDown). To Print the orders information:
for (int index = 0; index < submitTargetsOrdersList.Count; index++)
{
Print(string.Format("{0} | index: {1}, order.OrderState: {2}, order.LimitPrice: {3}, currentPTPrice", submitTargetsOrdersList[index].Time, index, submitTargetsOrdersList[index].OrderState, submitTargetsOrdersList[index].LimitPrice, currentPTPrice));
}
Unrelated, TriggerCustomEvent() is used to synchronize a Series from a non-data-driven event. If the code is not using Series information, this does not need to be called.Last edited by NinjaTrader_ChelseaB; 06-28-2022, 02:03 PM.
- Likes 1
-
New output txt attached (without the OnbarUpdate prints)
New Demo
I printed the order states
PHP Code:entryBuyMar****rder Snippet Buy entryBuyMar****rder : orderId='652bbdb8c0e841da8e211bcbcc086078' account='Sim101' name='Entry' orderState=Submitted instrument='CL 08-22' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1 tif=Day oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=2147 time='2022-06-28 21:28:38' gtd='2099-12-01' statementDate='2022-06-28' multiOrders.Count : 2 Account_OrderUpdate SNIPPETS submitTargetsOrdersList.Count : 1 profitTargetOrder.OrderState == OrderState.Filled : False profitTargetOrder.OrderState == OrderState.Cancelled: False profitTargetOrder.OrderState == OrderState.Rejected: False Account_OrderUpdate SNIPPETS submitTargetsOrdersList.Count : 2 profitTargetOrder.OrderState == OrderState.Filled : False profitTargetOrder.OrderState == OrderState.Cancelled: False profitTargetOrder.OrderState == OrderState.Rejected: False ChartControl_PreviewKeyDown MOVE UP Target SNIPPETS submitTargetsOrdersList.Count : 2 profitTargetOrder.OrderState == OrderState.Filled : False profitTargetOrder.OrderState == OrderState.Cancelled: False profitTargetOrder.OrderState == OrderState.Rejected: False ChartControl_PreviewKeyDown MOVE UP Target SNIPPETS submitTargetsOrdersList.Count : 2 profitTargetOrder.OrderState == OrderState.Filled : False profitTargetOrder.OrderState == OrderState.Cancelled: False profitTargetOrder.OrderState == OrderState.Rejected: False ChartControl_PreviewKeyDown MOVE UP Target SNIPPETS submitTargetsOrdersList.Count : 2 profitTargetOrder.OrderState == OrderState.Filled : False profitTargetOrder.OrderState == OrderState.Cancelled: False profitTargetOrder.OrderState == OrderState.Rejected: False entryBuyMar****rder Snippet Buy entryBuyMar****rder : orderId='6a965a84f8c34947bf2adec8fc5384be' account='Sim101' name='Entry' orderState=Submitted instrument='CL 08-22' orderAction=Buy orderType='Market' limitPrice=0 stopPrice=0 quantity=1 tif=Day oco='' filled=0 averageFillPrice=0 onBehalfOf='' id=2154 time='2022-06-28 21:28:54' gtd='2099-12-01' statementDate='2022-06-28' multiOrders.Count : 5 Account_OrderUpdate SNIPPETS submitTargetsOrdersList.Count : 3 profitTargetOrder.OrderState == OrderState.Filled : False profitTargetOrder.OrderState == OrderState.Cancelled: False profitTargetOrder.OrderState == OrderState.Rejected: False Account_OrderUpdate SNIPPETS submitTargetsOrdersList.Count : 4 profitTargetOrder.OrderState == OrderState.Filled : False profitTargetOrder.OrderState == OrderState.Cancelled: False profitTargetOrder.OrderState == OrderState.Rejected: False Account_OrderUpdate SNIPPETS submitTargetsOrdersList.Count : 5 profitTargetOrder.OrderState == OrderState.Filled : False profitTargetOrder.OrderState == OrderState.Cancelled: False profitTargetOrder.OrderState == OrderState.Rejected: False ChartControl_PreviewKeyDown MOVE UP Target SNIPPETS submitTargetsOrdersList.Count : 5 profitTargetOrder.OrderState == OrderState.Filled : False profitTargetOrder.OrderState == OrderState.Cancelled: False profitTargetOrder.OrderState == OrderState.Rejected: False ChartControl_PreviewKeyDown MOVE UP Target SNIPPETS submitTargetsOrdersList.Count : 5 profitTargetOrder.OrderState == OrderState.Filled : False profitTargetOrder.OrderState == OrderState.Cancelled: False profitTargetOrder.OrderState == OrderState.Rejected: False ChartControl_PreviewKeyDown MOVE UP Target SNIPPETS submitTargetsOrdersList.Count : 5 profitTargetOrder.OrderState == OrderState.Filled : False profitTargetOrder.OrderState == OrderState.Cancelled: False profitTargetOrder.OrderState == OrderState.Rejected: False ChartControl_PreviewKeyDown MOVE UP Target SNIPPETS submitTargetsOrdersList.Count : 5 profitTargetOrder.OrderState == OrderState.Filled : False profitTargetOrder.OrderState == OrderState.Cancelled: False profitTargetOrder.OrderState == OrderState.Rejected: False ChartControl_PreviewKeyDown MOVE UP Target SNIPPETS submitTargetsOrdersList.Count : 5 profitTargetOrder.OrderState == OrderState.Filled : False profitTargetOrder.OrderState == OrderState.Cancelled: False profitTargetOrder.OrderState == OrderState.Rejected: False ChartControl_PreviewKeyDown MOVE UP Target SNIPPETS submitTargetsOrdersList.Count : 5 profitTargetOrder.OrderState == OrderState.Filled : False profitTargetOrder.OrderState == OrderState.Cancelled: False profitTargetOrder.OrderState == OrderState.Rejected: False
All false
Can you please inform on what the comparison with price you suggest should be from the demo? Thanks!Attached Files
Leave a comment:
-
It's the OnBarUpdate prints for about 4 minutes.
The demo is loaded.
I'll print the order state now.
What order states you suggest printing?
Print("profitTargetOrder.OrderState == OrderState.Filled : " + (profitTargetOrder.OrderState == OrderState.Filled));
Print("profitTargetOrder.OrderState == OrderState.Cancelled: " + (profitTargetOrder.OrderState == OrderState.Cancelled));
Print("profitTargetOrder.OrderState == OrderState.Rejected: " + (profitTargetOrder.OrderState == OrderState.Rejected));
What new price to compare to do you have in mind? Any sample? Thanks!Last edited by PaulMohn; 06-28-2022, 01:24 PM.
Leave a comment:
-
Hello PaulMohn,
OnBarUpdate SNIPPETS entryBuyMar****rder==null : True entrySellMar****rder==null : True profitTargetOrder==null : True stopLossOrder==null : True multiOrders.Count : 0 submitTargetsOrdersList.Count : 0 submitStopLossesOrdersList.Count : 0
Is this from Account_OrderUpdate() or is this OnBarUpdate()?
Is this from the code where you are pressing the keys an investigating the behavior?
Have you pressed the keys a few thousand times?
Are you printing the orderstate of each order to ensure this is working or accepted and the current price of the order and comparing this to the new price you are submitting the order to?
Then the order object from the OrderUpdate event would show the orders either being updated or not updated to the change price..
Leave a comment:
-
Hello Chelsea,
Here a demo
Attached the indicator with prints and latest modifications
Here a download link for the output txt (2.59mb, with preliminary OnBarUpdate not useful prints) of the demo
As seen in demo, the Target orders of the 1 NumPad7 press (1st entry) do move freely.
But, upon the 2 NumPad7 press (2nd entry), only one set of Target orders move up,
and only on the 1st press (no further move upon subsequent presses).
NumPad7 + Ctrl does move back all orders but also only once
(when multiple moves are expected and working in when only a single entry is the case).
Thank you for your tests and recommendations!Attached FilesLast edited by PaulMohn; 06-28-2022, 12:54 PM.
Leave a comment:
-
Hello PaulMohn,
for (int index = 0; index < quantitySelector.Value; index++)
Using quantitySelector.Value for the max size of the index does not make sense.
The index must be less than the size of the collection. It must be less than submitTargetsOrdersList.Count().
To save output from the output window, right-click the NinjaScript Output window -> select Save As.. Then you can attach the output text file to your posts.
Print the order object for all orders to see when orders are being updated.
To confirm, no orders are being modified, not even the first two in the list? No error is appearing in the NinjaScript Output window or on the Log tab of the Control Center?
You somewhere else are checking the OrderState of the orders to be OrderState.Working or .Accepted to ensure they can be modified?
May we have the output showing the orderstate of the orders you are trying to modify?
Leave a comment:
-
I used those prints but it's not helping
PHP Code:{ currentPTPrice = profitTargetOrder.LimitPrice + (ProfitTargetMove * TickSize); Print("1. currentPTPrice : " + currentPTPrice); for (int index = 0; index < quantitySelector.Value; index++) { submitTargetsOrdersList[index].LimitPriceChanged = currentPTPrice; Print("1. submitTargetsOrdersList.Count : " + submitTargetsOrdersList.Count); Print("2. currentPTPrice : " + currentPTPrice); } Print("2. submitTargetsOrdersList.Count : " + submitTargetsOrdersList.Count); myAccount.Change(submitTargetsOrdersList); Print("3. submitTargetsOrdersList.Count : " + submitTargetsOrdersList.Count); }
Buy
1. currentPTPrice : 110.91
1. submitTargetsOrdersList.Count : 2
2. currentPTPrice : 110.91
1. submitTargetsOrdersList.Count : 2
2. currentPTPrice : 110.91
2. submitTargetsOrdersList.Count : 2
3. submitTargetsOrdersList.Count : 2
Buy
1. currentPTPrice : 110.9
1. submitTargetsOrdersList.Count : 4
2. currentPTPrice : 110.9
1. submitTargetsOrdersList.Count : 4
2. currentPTPrice : 110.9
2. submitTargetsOrdersList.Count : 4
3. submitTargetsOrdersList.Count : 4
The Targets OCO orders moved.What was moved?
myAccount.Change(submitTargetsOrdersList);What line of code does the moving?
Yes (please see prints above).Is this line of being triggered?
PHP Code:Is this being triggered with the expected values?
Yes (please see prints above).
Did you have other prints in mind? Thanks!
Leave a comment:
-
Hello PaulMohn,
This is an array with one Order element.
Account.Submit() requires a collection. This means if you have one order to send, you need to wrap it in a collection like an array.
See the help guide.
https://ninjatrader.com/support/help...nt8/submit.htm
You will need to debug your code.
Previously you were asking about adding orders to a list.
What was moved? What line of code does the moving? Is this line of being triggered? Is this being triggered with the expected values?
Use prints to understand behavior.
You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our NinjaTrader Ecosystem team follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request or provide one on one educational services.Last edited by NinjaTrader_ChelseaB; 06-28-2022, 09:09 AM.
Leave a comment:
-
What's the use of "new[] {}" in
submissionAccount.Submit(new[] { entryBuyMar****rder });
?
Isn't that an empty array? If not what is it? Thanks!
I added the list clearing methods to the OnBarUpdate method
(the idea is the list should be reset only when no open orders remain)
PHP Code:protected override void OnBarUpdate() { if (myAccount != null && myAccount.Positions.Where(o => o.Instrument == Instrument).Count() > 0) accountPosition = myAccount.Positions.Where(o => o.Instrument == Instrument).Last(); else accountPosition = null; if (accountPosition == null) { multiOrders.Clear(); submitTargetsOrdersList.Clear(); submitStopLossesOrdersList.Clear(); }
And prints statement in the NumPad7 Hotkeys Move Target loop method
PHP Code:{ currentPTPrice = profitTargetOrder.LimitPrice + (ProfitTargetMove * TickSize); for (int index = 0; index < quantitySelector.Value; index++) { submitTargetsOrdersList[index].LimitPriceChanged = currentPTPrice; Print("submitTargetsOrdersList.Count : " + submitTargetsOrdersList.Count); } myAccount.Change(submitTargetsOrdersList); }
and it prints the right value
But it's not working. It still only moves 1 order. And now it moves it only once upon multiple NumPad7 key pressesBuy
submitTargetsOrdersList.Count : 2
submitTargetsOrdersList.Count : 2
Buy
submitTargetsOrdersList.Count : 4
submitTargetsOrdersList.Count : 4
(whereas before it moved it as many times as there were key presses, as it was correct that way).
Do you need a demo?
What else is not working? Thanks!Last edited by PaulMohn; 06-28-2022, 08:54 AM.
Leave a comment:
-
Hello PaulMohn,
You were previously creating a new list in the Account_OrderUpdate method.
submitTargetsOrdersList = new List<Order>();
Why are you creating new list here?
If you don't need to reset the list, then don't make a new list or clear it. If you need to reset it, clear it.
When should you clear it? When you want to reset it.
Don't clear it if you don't want to reset it.
"OnDataLoaded" is this your own custom method? Where is this method triggered from?
Is this custom method triggered from OnStateChange when State is State.DataLoaded?
submissionAccount.Submit() requires a collection. entryBuyMar****rder is not a collection. This is a single Order variable. I did not transform any list into any array.
What isn't working?
You have declared a new list and have you added an element to the list and then printed the list count and the element is not there?
You can check the instrument an order is placed to. <Order>.Instrument.
https://ninjatrader.com/support/help.../nt8/order.htmLast edited by NinjaTrader_ChelseaB; 06-28-2022, 07:50 AM.
Leave a comment:
-
Hello Chelsea and thanks for the suggestion.Originally posted by NinjaTrader_ChelseaB View PostHello PaulMohn,
You can use list.Clear() to clear a list.
https://docs.microsoft.com/en-us/dot...r?view=net-6.0
Your code appears to be making a new list object every time entryBuyMar****rder is the order updating.
If you want multiple orders in here, don't make a new list object here. Continue to add to the existing list object.
Move submitTargetsOrdersList = new List<Order>(); to State.DataLoaded.
Why would I need to clear the list? and where?
I've moved the 3 lists to OnDataLoaded action block (and commented them out in the OnPreviewKeyDown() and Account_OrderUpdate() methods)
PHP Code:multiOrders = new List<Order>(); submitTargetsOrdersList = new List<Order>(); submitStopLossesOrdersList = new List<Order>();
But it's not working. Please would you clarify what you wanted me to infer? Thanks!
Also, I noticed you use a kind of array from a list, line 126
submissionAccount.Submit(new[] { entryBuyMar****rder });
in your ProfitChaseStopTrailIndicatorExample script.
Why did you transform the list into an array and didn't just use a list? Are there benefits? What are they if any? Thanks!
Also, how would you restrict simplify and restrict the multi-loops solution to the instrument the indicator is loaded on?
I'd privilege you solution as it seems to be simpler and less resource intensive if I can make it work. Thanks!Last edited by PaulMohn; 06-28-2022, 05:34 AM.
Leave a comment:
-
Thanks Chelsea, I'll test that next.
In the meantime I found this working way
Class level variable
PHP Code:private List<Order> testr;
Code in the NumPad7 Hotkey Action Block
PHP Code:testr = new List <Order>(); foreach(Account acct in Account.All) { if (acct.Positions != null) { foreach(Position pos in acct.Positions) { if (pos.MarketPosition == MarketPosition.Long) { lock(myAccount.Orders) { foreach(Order moveTPOrders in myAccount.Orders) { testr.Add(moveTPOrders); } for (int index = 0; index < testr.Count; index++) { testr[index].LimitPriceChanged = currentPTPrice; } } } } } } myAccount.Change(testr); }
From previous solution
Any tip on simplifying the multiple loops above?
Any tip on how to get the orders by the groups they were entered in? Thanks!Last edited by PaulMohn; 06-27-2022, 05:03 PM.
Leave a comment:
-
Hello PaulMohn,
You can use list.Clear() to clear a list.
https://docs.microsoft.com/en-us/dot...r?view=net-6.0
Your code appears to be making a new list object every time entryBuyMar****rder is the order updating.
If you want multiple orders in here, don't make a new list object here. Continue to add to the existing list object.
Move submitTargetsOrdersList = new List<Order>(); to State.DataLoaded.
- Likes 1
Leave a comment:
-
Thanks Chelsea for the list count vs counter observation.
I think the counter doesn't help as it's the list that defines the targets available to be moved, regardless of the loop upper limit.
So I think I'll abandon the counter use idea.
My problem is that the list RESETS itself when this is true
PHP Code:else if ((profitTargetOrder != null && (profitTargetOrder.OrderState == OrderState.Filled || profitTargetOrder.OrderState == OrderState.Rejected || profitTargetOrder.OrderState == OrderState.Cancelled)) || (stopLossOrder != null && (stopLossOrder.OrderState == OrderState.Filled || stopLossOrder.OrderState == OrderState.Rejected || stopLossOrder.OrderState == OrderState.Cancelled))) { entryBuyMar****rder = null; entrySellMar****rder = null; profitTargetOrder = null; stopLossOrder = null; }
I think because of
profitTargetOrder = null;
stopLossOrder = null;
because profitTargetOrder and stopLossOrder are the orders variables for the submitTargetsOrdersList in the Targets Snippet below
submitTargetsOrdersList.Add(profitTargetOrder);
PHP Code:#region Initial Target & StopLoss Snippets private async void Account_OrderUpdate(object sender, OrderEventArgs orderUpdateArgs) { #region LONG Orders Initial Target and StopLoss OCO orders if (entryBuyMar****rder != null && entryBuyMar****rder == orderUpdateArgs.Order && orderUpdateArgs.Order.OrderState == OrderState.Filled) { string oco = Guid.NewGuid().ToString("N"); submitTargetsOrdersList = new List<Order>(); submitStopLossesOrdersList = new List<Order>(); await ChartControl.Dispatcher.InvokeAsync((Action) (() => { if (UseProfitTarget) { currentPTPrice = orderUpdateArgs.AverageFillPrice + ProfitTargetDistance * TickSize; for (int index = 0; index < quantitySelector.Value; index++) { profitTargetOrder = myAccount.CreateOrder( orderUpdateArgs.Order.Instrument, OrderAction.Sell, OrderType.Limit, OrderEntry.Automated, TimeInForce.Day, orderUpdateArgs.Quantity, currentPTPrice, 0, oco+index, "Profit Target", Core.Globals.MaxDate, null); submitTargetsOrdersList.Add(profitTargetOrder); } }
What I need is a way to get the TOTAL running count of the profitTargetOrder value upon multiple simultaneous orders.
I just tried to reset the profitTargetOrder and stopLossOrder variables in the OnBarUpdate instead as
PHP Code:#region OnBarUpdate Prints Snippets protected override void OnBarUpdate() { if (myAccount != null && myAccount.Positions.Where(o => o.Instrument == Instrument).Count() > 0) accountPosition = myAccount.Positions.Where(o => o.Instrument == Instrument).Last(); else accountPosition = null; if (accountPosition == null) { profitTargetOrder = null; stopLossOrder = null; }
And commenting them out in the previous snippet as
PHP Code:else if ((profitTargetOrder != null && (profitTargetOrder.OrderState == OrderState.Filled || profitTargetOrder.OrderState == OrderState.Rejected || profitTargetOrder.OrderState == OrderState.Cancelled)) || (stopLossOrder != null && (stopLossOrder.OrderState == OrderState.Filled || stopLossOrder.OrderState == OrderState.Rejected || stopLossOrder.OrderState == OrderState.Cancelled))) { entryBuyMar****rder = null; entrySellMar****rder = null; // profitTargetOrder = null; // stopLossOrder = null; }
To only reset the list to zero once the position account is null (not as soon as the profitTargetOrder is Filled or Rejected or Cancelled)
but it's still only printing 2 for the submitTargetsOrdersList.Count value after 2nd order.
PHP Code:accountPosition == null : True accountPosition == null : True Buy 2 initial: submitTargetsOrdersList.Count : 1 initial: submitTargetsOrdersList.Count : 2 Move : submitTargetsOrdersList.Count : 2 Buy 2 initial: submitTargetsOrdersList.Count : 1 initial: submitTargetsOrdersList.Count : 2 Move : submitTargetsOrdersList.Count : 2 Move : submitTargetsOrdersList.Count : 2 Move : submitTargetsOrdersList.Count : 2
How can I get ALL the running profitTargetOrder orders added to the submitTargetsOrdersList list without RESSETING TO ZERO after each new order submission (i.e. upon new numPad7 presses) ?
My end goal it to be able to move ALL profitTargetOrder orders upon multiple simultaneous orders are live,
NOT just the most recent profitTargetOrder orders of the last LONG order sumbitted (what the submitTargetsOrdersList currently does). Thanks!Last edited by PaulMohn; 06-27-2022, 02:57 PM.
Leave a comment:
-
Hello PaulMohn,
submitTargetsOrdersList has a count of 2. Meaning there are only two elements (orders) added to the list. You cannot ask for more elements than are in the list.
Either add 4 elements to the list, or don't ask for more than 2.
PaulMohn an index cannot be larger than the size of the collection.
Why is multiOrdersTargets being added to quantitySelector.Value? This is what is causing your error.
What are you trying to do?
If you are using a counter then why are you using a list?
If you have a list, you don't need counter. To loop through the list, loop from 0 to the list.Count.
Leave a comment:
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by CarlTrading, 05-11-2026, 05:56 AM
|
0 responses
56 views
0 likes
|
Last Post
by CarlTrading
05-11-2026, 05:56 AM
|
||
|
Started by CarlTrading, 05-10-2026, 08:12 PM
|
0 responses
33 views
0 likes
|
Last Post
by CarlTrading
05-10-2026, 08:12 PM
|
||
|
Started by Hwop38, 05-04-2026, 07:02 PM
|
0 responses
195 views
0 likes
|
Last Post
by Hwop38
05-04-2026, 07:02 PM
|
||
|
Started by CaptainJack, 04-24-2026, 11:07 PM
|
0 responses
359 views
0 likes
|
Last Post
by CaptainJack
04-24-2026, 11:07 PM
|
||
|
Started by Mindset, 04-21-2026, 06:46 AM
|
0 responses
280 views
0 likes
|
Last Post
by Mindset
04-21-2026, 06:46 AM
|

Leave a comment: