NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 02-15-2012, 09:32 AM   #1
zeller4
Senior Member
 
Join Date: Sep 2008
Posts: 185
Thanks: 20
Thanked 2 times in 2 posts
Default Cancelling Target Orders when Stops are filled/Overfill/OCO handling

Hello,

I'm receiving this message in the log/output window
http://screencast.com/t/MxKpUFnVM

Is it preferable to allow the Internal Order Handling to cancel this target order as shown once the corresponding stop is hit? Or should I make a specific section of code to cancel these orders before the IOrder sequence gets to this point?

The reason I'm asking is that I'll be attempting to code Overfill error avoidance in my strat. Any help is appreciated since I'll need to know where it is best to cancel or read the orderstate: i.e. OnOrderUpdate or OnExecution.

Thanks,
Kirk
zeller4 is offline  
Reply With Quote
Old 02-15-2012, 09:37 AM   #2
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
Default

zeller4,

I am happy to assist you.

Quote:
Is it preferable to allow the Internal Order Handling to cancel this target order as shown once the corresponding stop is hit? Or should I make a specific section of code to cancel these orders before the IOrder sequence gets to this point?
This is a personal preference. It really depends on what you are willing to accept, and your conditions for the orders. In other words, maybe you have some condition where you would like to cancel the order before hand. Internal order handling is coded to basically try to prevent overfills, etc. The unmanaged approach offers you more control, but then you need to build in your own safeties as it suites you.

Quote:
The reason I'm asking is that I'll be attempting to code Overfill error avoidance in my strat. Any help is appreciated since I'll need to know where it is best to cancel or read the orderstate: i.e. OnOrderUpdate or OnExecution.
OnOrderUpdate() updates when any order is submitted, pending, etc. OnExecution only occurs when an order is filled.

Please let me know if I may assist further.
NinjaTrader_AdamP is offline  
Reply With Quote
The following user says thank you to NinjaTrader_AdamP for this post:
Old 02-15-2012, 10:38 AM   #3
mcorbett
Junior Member
 
Join Date: Jan 2012
Posts: 26
Thanks: 0
Thanked 0 times in 0 posts
Default

Hello,

If I have a long position on and I have a target price and a stop price in. How can I then cancel the stop price if my target price is filled ?

Put another way can a stop be placed in conjuction with OCO ? And how do I use the OCO function? I am not using a script, these are manually entered.

Thanks
mcorbett is offline  
Reply With Quote
Old 02-15-2012, 11:02 AM   #4
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
Default

Quote:
Originally Posted by mcorbett View Post
Hello,

If I have a long position on and I have a target price and a stop price in. How can I then cancel the stop price if my target price is filled ?

Put another way can a stop be placed in conjuction with OCO ? And how do I use the OCO function? I am not using a script, these are manually entered.

Thanks
You can use ATM strategies for this. Are these manual orders?

http://www.youtube.com/watch?v=kIHBYC15luc
NinjaTrader_AdamP is offline  
Reply With Quote
Old 02-15-2012, 11:07 AM   #5
mcorbett
Junior Member
 
Join Date: Jan 2012
Posts: 26
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_AdamP View Post
You can use ATM strategies for this. Are these manual orders?

http://www.youtube.com/watch?v=kIHBYC15luc
Yes, I would have bought and then put in stop and target, if either stop or target get fills I want to cancel one or otehr depending which one gets filled fist...

thanks
mcorbett is offline  
Reply With Quote
Old 02-15-2012, 11:14 AM   #6
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
Default

mcorbett,

Thanks for your note.

If you right click in the Chart trader or Dom before you place your stop and target, you can enable OCO, then place your orders, then right click again and uncheck OCO.

Otherwise, please watch the video I have provided as it will allow you to create a ATM strategy that does this as well, however automated.

Please let me know if I may assist further.
NinjaTrader_AdamP is offline  
Reply With Quote
Old 02-15-2012, 11:18 AM   #7
mcorbett
Junior Member
 
Join Date: Jan 2012
Posts: 26
Thanks: 0
Thanked 0 times in 0 posts
Default

Hi,

Is it possible to just use the field in the control center labeled OCO?

Thanks
mcorbett is offline  
Reply With Quote
Old 02-15-2012, 11:21 AM   #8
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
Default

mcorbett,

Here is our OCO documentation :

http://www.ninjatrader.com/support/h...ng_orders3.htm

You can use OCO in chart trader and the superdom.
NinjaTrader_AdamP is offline  
Reply With Quote
Old 02-15-2012, 11:52 AM   #9
mcorbett
Junior Member
 
Join Date: Jan 2012
Posts: 26
Thanks: 0
Thanked 0 times in 0 posts
Default

Thank you.

I read it.

However why is there an OCO field in the control center window?

I was thinking there is an easy manula way to link orders, the OCO function as it stands is really just for intraday trades...
mcorbett is offline  
Reply With Quote
Old 02-15-2012, 11:54 AM   #10
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
Default

mcorbett,

If you are referring to the orders tab in the control center, this is just giving you the OCO ID so you can see which trades are grouped in OCO.

Please let me know if I may assist further.
NinjaTrader_AdamP is offline  
Reply With Quote
Old 02-15-2012, 11:58 AM   #11
mcorbett
Junior Member
 
Join Date: Jan 2012
Posts: 26
Thanks: 0
Thanked 0 times in 0 posts
Default

I see a blank space on the orders tab named "OCO".

It appears to be a place where I can input an order "ID" or token.

Example, I were to put in a stop I could also include an "ID" here that would link it to the limit order. So if stop is hit limit would cancel..... sort of manual

The tutorial is unclear... no idea what its trying to do
mcorbett is offline  
Reply With Quote
Old 02-15-2012, 12:01 PM   #12
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
Default

mcorbett,

ID's are generated by ninjatrader whenever you place an OCO order, there is no way to set them in the control center.

OCO is enabled or disabled manually in the SuperDOM or ChartTrader. We have a video that covers how to do it.

http://www.youtube.com/watch?v=DBuD2Lps46s
NinjaTrader_AdamP is offline  
Reply With Quote
Old 02-16-2012, 01:46 PM   #13
zeller4
Senior Member
 
Join Date: Sep 2008
Posts: 185
Thanks: 20
Thanked 2 times in 2 posts
Default Code to simulate Overfill

Hello,

I've developed a small script based on SampleCancelOrder so that I could learn how to avoid errors with Overfills.

Here is a screenshot of the error message:

http://screencast.com/t/1bDpZKpo

I placed this code in the OnOrderUpdate to attempt to force an Overfill error.

Code:
if (order.OrderState == OrderState.PendingCancel)
{
	Print("26 - order.OrderState == OrderState.PendingCancel: "+DateTime.Now.ToString());
	//placed here on purpose to force an Overfill condition
	CancelOrder(entryOrder);
	Print("26 ==================================");
}
I'm trying to observe if this produces an overfill every time... I don't think it does yet (still testing).
Am I right in assuming the PendingCancel within OnOrderUpdate is the main area where overfill problems will surface? Or can they surface in OnExecution as well?
Thanks,
kz
Attached Images
File Type: jpg YM 03-12 (15 Seconds) 2_16_2012.jpg (49.2 KB, 6 views)
Attached Files
File Type: cs SampleOverfill_CancelOrder_v01.cs (26.3 KB, 8 views)
Last edited by zeller4; 02-16-2012 at 01:51 PM.
zeller4 is offline  
Reply With Quote
Old 02-16-2012, 03:23 PM   #14
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

Hi zeller4,

There's no specific handler used for generating overfills. Overfill is when an order is marked for cancellation and then filled. It can also happen when the strategy has competing orders sent at the same time, when only one should be filled. The main area where they will occur is with orders resting close near the market and cancel is sent for any reason: One pair of OCO is filled, position set to opened in opposite direction, autoexpirations, or you send CancelOrder().

I don't think you'll be able to reproduce an overfill on simulated account like you're doing. For an overfill the order must be filled in-between pending cancel and cancel. Unfortunately we do not have a strategy to provide you that will produce overfills on demand.
Last edited by NinjaTrader_RyanM; 02-16-2012 at 03:35 PM.
NinjaTrader_RyanM is offline  
Reply With Quote
Old 02-16-2012, 03:47 PM   #15
zeller4
Senior Member
 
Join Date: Sep 2008
Posts: 185
Thanks: 20
Thanked 2 times in 2 posts
Default

Thanks for the explanation. My only reason for "mis-coding" to achieve an overfill was so that I can learn how to prevent them. The sample or code snippet of the method of checking a "resting-near-the-market" order would be beneficial.

Also, what do you mean by
Quote:
competing orders sent at the same time, when only should be filled.
If I understand the OCO part, once the stop is filled, the target is filled because they have the identical naming string from EntrySignal.

My purpose of creating the indi was to get a better understanding from the print statements how the Internal Orders are processed in OnOrderUpdate vs. OnExecution.

Also, is there any validity to the following? Or will it never occur? (I'm trying to understand
why (or if) a fill state would have to be checked for something that is cancelled.)
Code:
if (entryOrder_1a != null && entryOrder_1a == execution.Order)
{
if (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled == 0)
{//print or do something...
}
}
Last edited by zeller4; 02-16-2012 at 03:55 PM.
zeller4 is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Overfill Handling guillembm Miscellaneous Support 4 02-29-2012 01:48 PM
OCO issues cancelling entry orders on partial fills tony98 Strategy Development 3 01-31-2012 01:38 PM
ATM strategy stops on partially filled orders arnonmoscona ATM Strategies (Discretionary Trading) 1 01-10-2012 06:31 AM
Retrieving partially filled OCO orders billitin Strategy Development 10 07-15-2008 10:35 PM
Setting stops and Target orders trader333 ATM Strategies (Discretionary Trading) 1 08-29-2007 07:37 AM


All times are GMT -6. The time now is 10:13 PM.