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
Deleting Unexecuted Stop Orders
Collapse
X
-
to cancel active orders
CancelOrder(orderStop);
or do you mean stuck CancelPending orders that wont dissapear from the sim account?
-
Welcome to the forums mr1997!
As MicroTrends mentions, you can cancel active orders with CancelOrder.
CancelOrder - https://ninjatrader.com/support/help...ancelorder.htm
If you are referring to stuck simulation orders, these would need to be cleared out by resetting the sim account. (Control Center > Accounts tab > Sim101 > Right Click > Edit Account> Reset) Stuck simulation orders can come about when the simulation engine gets a high number of order submissions/changes/fills/cancels all at one. If this is a common problem for your strategy, I suggest looking into how often it is changing orders/submitting new orders/attempting cancels. If these operations are in high succession you could get a stuck simulation order.
We look forward to assisting.JimNinjaTrader Customer Service
Comment
-
MicroTrends I was looking for the actual command to delete stop orders that have not been executed. I wasn't aware of the "stuck pending orders" issue with the sim account. They've made programming for this platform absolutely maddening.
So is there not a script command to flatten all orders???
Comment
-
O.K. I added the flatten command to my script.
It looks like the flatten command is executing but...
My script successfully created a buy stop and sell stop but when the flatten command was fired off in the script it removed the unfilled sell stop but left my market position active.
All orders were closed when I fired the flatten command manually???Last edited by mr1997; 12-09-2020, 02:50 PM.
Comment
-
Originally posted by mr1997 View PostO.K. I added the flatten command to my script.
It looks like the flatten command is executing but...
My script successfully created a buy stop and sell stop but when the flatten command was fired off in the script it removed the unfilled sell stop but left my market position active.
All orders were closed when I fired the flatten command manually???
If you are using a default mode strategy you can use ExitLong or ExitShort
if (Position.MarketPosition==MarketPosition.Long)
ExitLong();
else if (Position.MarketPosition==MarketPosition.Short)
ExitShort();
Comment
-
Hello mr1997,
Are you using Account.Flatten or similar? Please note that this is an Account level AddOn Framework method that would take actions outside of the context of the NinjaScript strategy. It will be similar to using the Close button. I have attached a demonstration video showing this in action as well as the test script so you may do your own tests on your end.
Demo - https://drive.google.com/file/d/1alF...w?usp=drivesdk
To get you on track to a solution, if you are using Unmanaged Approach and want to cancel active orders that the strategy has submitted, I suggest tracking the orders as Order objects so you can cancel them with CancelOrder.
To close a position, you would use SubmitOrderUnmanaged to submit a market order that matches the strategy's Position.Quantity.
The above could be combined in a custom method "CancelAndClose" that cancels all of your orders and sets a bool "WaitingForCancels" to true. Since we set Order objects to null in OnOrderUpdate when they are cancelled, you could check for "WaitingForCancels" to be true in addition to checking if your Order objects are null at the end of OnOrderUpdate.
The result is one method that is called to cancel orders, and then when the cancels come back and the order objects are nulled, the position gets closed. The strategy stays running.
Unmanaged Approach means you are taking control of all aspects of the strategy's order handling, so this would be more complex than the Managed Approach where you can just call ExitLong and ExitShort to cancel orders and close positions.
Please let us know if you have any additional questions.Attached FilesJimNinjaTrader Customer Service
Comment
-
Jim
Thanks for the thorough answer. Not exactly a c programmer but I get the gist of what you're telling me. I'll have a look at your example script when I get home this evening.
Thought I had seen somewhere where you can set an expiration time for a stop or limit order.... Might be an easier way to accomplish my goal at this point.
Comment
-
Hello mr1997,
Order expiration time would be set with TimeInForce.
TimeInForce is set when configuring the strategy. We can use a time In force of DAY or GTC (Good Till Cancelled) GTD (Good Till Day) is not supported for NinjaScript strategies.
If you want more control than what TIF offers, it would be best to implement the canceling behaviors you wish in your strategy logic.
Please let us know if there is anything else we can do to help.JimNinjaTrader Customer Service
Comment
-
NinjaTrader_Jim How do I edit/update your script so I can back test it? Also are profit & stop loss targets in ticks?
I enabled your script and watched it in action but was confused when I viewed the executions tab. It's placing order quantities of 67, 33, 63, 37???
Notes:
1) I am using simulated data feed
2) I am trading micro Nasdaq contact
Last edited by mr1997; 12-11-2020, 09:16 AM.
Comment
-
Hello mr1997,
Yes, you will have to remove the if(State == State.Historical) return; lines to make the strategy backtestable.
Please also note that the strategy uses bools to prevent multiple entries and these bools get reset as orders execute. You will need to ensure that BarsRequiredToTrade is set to 0 in the Strategy Analyzer, or you can incorporate a BarsRequiredToTrade check in the strategy logic to prevent the logic from flipping these bools when order submissions are attempted.
I.E.
Code:if (longEntry == null && shortEntry == null && !entrySubmitted[B] && CurrentBar >BarsRequiredToTrade[/B] && Position.MarketPosition == MarketPosition.Flat)
JimNinjaTrader Customer Service
Comment
-
NinjaTrader_Jim I made the changes you advised above but when I right click the chart and select Strategy/Performance/Historical it pops up quickly with the results but no trades were executed.
...the following error appears in the output:
Strategy 'UnmgdTemplateMOD NT8': Error on calling 'OnBarUpdate' method on bar 0: Object reference not set to an instance of an object.
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by adeelshahzad, Today, 11:49 AM
|
6 responses
23 views
0 likes
|
Last Post
by mcosta72
Today, 04:35 PM
|
||
Started by sidlercom80, 05-05-2020, 07:20 AM
|
4 responses
304 views
0 likes
|
Last Post
by Bidder
Today, 04:35 PM
|
||
Started by andrew56, 11-21-2017, 11:57 AM
|
6 responses
954 views
0 likes
|
Last Post
by Bidder
Today, 04:34 PM
|
||
Indicator categories collapse, CategoryDefaultExpanded/CategoryExpanded has no effect
by Shai Samuel
Started by Shai Samuel, 08-09-2022, 04:41 AM
|
6 responses
184 views
1 like
|
Last Post
by Bidder
Today, 04:32 PM
|
||
Started by TheTradingMantis, 10-31-2017, 03:18 PM
|
5 responses
1,251 views
0 likes
|
Last Post
by TheAngryCube
Today, 04:06 PM
|
Comment