View Full Version : how to programatically close all orders in position
music_p13
05-24-2010, 11:42 PM
If I have a position with some OCO orders, some not OCO, some pending - how do I properly programatically close position from within ninjatrader code? Let's say - I got indicator condition that tells me to close all orders I might have.
Also, not sure, but I've heard that I need to close oldest order first and newest last in order to be in synch with current forex regulations.
Note: I am using managed position. But it would be good to know how to close for both - managed and unmanaged.
Thanks in advance...
NinjaTrader_Bertrand
05-25-2010, 04:30 AM
music_p13, were those positions you intend to close programmatically opend from within a NinjaScript strategy?
music_p13
05-26-2010, 12:44 PM
Yes, from the strategy...
In Metatrader, for example, you could get all orders and write your code to close each of them. In NinjaTrader I would assume I also can get all pending and active orders and close all of them using iOrder object. I would assume that it shouldn't matter whether it is manual or automatic - I should be able to select every order for specific pair. Is it correct statement?
Forgot to mention, I am referencing NinjaTrader 7.
NinjaTrader_Josh
05-26-2010, 01:48 PM
music_p13,
If you want to just close all positions you can just submit ExitLong() if you are long and ExitShort() if you are short. This will close all positions generated from that strategy.
music_p13
05-26-2010, 11:19 PM
Thanks, I saw sample of it, but I have few questions on this:
1st - will exitlong() exit all longs there are for a strategy (let's assume that I scaled in with few longs)?
2nd - will it cancel pending orders in case there are (if for example if I had some buylimit orders somewhere)
3rd - if I want to use unsafe/unmanaged approach - can I find all orders that are open/pending for specific pair (let's assume I didn't keep track of them) and close them one by one?
NinjaTrader_Bertrand
05-27-2010, 05:14 AM
music_p13,
1. Yes, if you do not specify any signal name to exit from specifically.
2. No, you would need to keep track of pending orders and then CancelOrder as needed. Another approach is just to let orders expire at each bar's end (default behavior in NT) and then just don't resubmit at your time x for things to stop.
3. You would need to keep track of the IOrders issued, so you have a reference to cancel these orders then with.
music_p13
05-27-2010, 08:59 AM
For point #3 - is there way to find all active iOrders if I didn't keep track of them, for both scenarios - iOrders opened by my strategy and iOrders opened if trading manually; and then to check the trading pair of each iOrder in the list?
NinjaTrader_Bertrand
05-27-2010, 09:09 AM
If you're working in the new unmanaged order submission approach in NT7, you would need keep references yourself in the code. Unfortunately you can't access orders placed / working manually outside of your NinjaScript strategy.
music_p13
05-27-2010, 09:15 AM
If I had a power outage, or if NinjaTrader has suddenly crashed - then I would loose all references for iOrder objects. Is there way to find those iOrder objects in NinjaTrader programatically - or will I have to just manually continue manage this strategy?
NinjaTrader_Bertrand
05-27-2010, 09:19 AM
The strategy would recalculate on next restart then in this case and try to match off what is possible, it could be you need to sync manually depending on how long the outage was if the missed data produced any position changes that would need to be worked in.
music_p13
05-27-2010, 09:31 AM
Can I keep track of all Tokens - and maybe even write them to txt file, and then if NinjaTrader crashed and then restarted - get those Tokens from txt file and verify if iOrder with that specific Token exists, and then to go from there? Or will old Tokens loose all relevance after NT restart?
NinjaTrader_Bertrand
05-27-2010, 09:43 AM
Unfortunately as the tokens would be recalculated if you restart the strategy, there's no persistence in NT 65.
If you're working with NT 7 beta, we've added strategy persistence features and you can compare then the IOrders directly without the need for the token ID.
music_p13
05-27-2010, 09:53 AM
I am talking about NT7 beta only. Where can I read more about strategy persistence features? How and where do I store iOrders objects for the crash event? Can I still find iOrders by Tokens? I don't think I can store object to txt file - I would guess I need to store some object's property in order to find object later on by that property.
NinjaTrader_Bertrand
05-27-2010, 10:23 AM
In NT7 you can just work directly with the IOrders to compare for equality, if you're planning to use the unmanaged approach to submit / manage orders free of the order handling rules, please see this overview page for info on this approach and it's 'caveats' applying -
http://www.ninjatrader.com/support/helpGuides/nt7/unmanaged_approach.htm
music_p13
05-27-2010, 01:57 PM
However, main question remains - if NinjaTrader was shut-down while strategy was running and then started again - I will loose all iOrder objects I was keeping track of. So how can I find/select those iOrder objects back? Can I find them by token that I could store in .txt file or is there any other way to find them after NinjaTrader restart?
NinjaTrader_Josh
05-27-2010, 03:06 PM
When you shutdown NT and start up again there is no "finding" old orders. NT6.5 begins the strategy completely over again from scratch with no knowledge of any prior orders. There is no way to map orders back.
In NT7 if you have sync account position set to true it will automatically try and map back existing orders on your account to your strategy. Only exact matches will be mapped back. Orders that cannot be mapped back will be canceled.
music_p13
05-27-2010, 07:51 PM
Thanks, good to know about that feature. What is the exact syntax in the code to sync account in Initialize method?
So basically, it is safe to assume that in NT7 there will be no orphaned orders related to specific strategy once NinjaTrader starts? And I can check each of the previously saved iOrder objects to verify that they are not NULL? If NULL - that would mean that this order no longer active... Is it correct statement?
NinjaTrader_Bertrand
05-28-2010, 08:47 AM
You can enabled the autoSync on startup of the strategy via SyncAccountPosition = true; in the strategy Initialize().
If you restart the strategy, it would recalculate and try to remap - however there's no guarantee it can do so, therefore you would need to add checks and potentially account for this in your code if needed. You will need to nullify your IOrder objects as needed, for example reset the targetOrder one to null once the target reached filled / cancelled state etc.