NinjaTrader Support Forum  

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 08-11-2010, 11:40 AM   #1
gregschr
Member
 
Join Date: Mar 2010
Location: Rogers, Arkansas
Posts: 84
Thanks: 0
Thanked 0 times in 0 posts
Send a message via Skype™ to gregschr
Default Unexpected OCO Behavior

When I initially get an order fill in a strategy, the stop and targets generated by the code below do not have OCO values. If I reload the strategy with an active position, the stop and target orders are assigned OCO values. The desired behavior I want is to not have OCO values on the orders. Any idea what is going on?

PHP Code:
#region OnExecution
  
protected override void OnExecution(IExecution execution)
  {
   
// Only used in Print statements
   
string TimeStamp Time[0].ToString()+" "+Instrument.MasterInstrument.Name+" "+BarsPeriod.Value.ToString();
   Print(
TimeStamp+" In OnExecution, Entry Order="+entryOrder.OrderId+", Entry Order="+execution.Order.OrderId+", Current Order="+execution.Order.ToString());
   
#region Entry Order Filled
   
if (entryOrder != null &&
    
entryOrder.OrderId == execution.Order.OrderId && 
    
execution.Order.OrderState == OrderState.Filled)
   {
    
double fillPrice entryOrder.AvgFillPrice;
    
int fillQty entryOrder.Quantity;
    if (
bEntryLong == true)
    {
     Print(
TimeStamp+"\tLong Entry Fill at "+fillPrice);
     if (
Stop0Ticks != 0)
     {
      
exitS0Order ExitLongStop(0trueEntryQtyfillPrice+Stop0Ticks*TickSize"Stop0""Long");
      Print(
TimeStamp+"\tStop S0 "+EntryQty+" Set at "+(fillPrice+Stop0Ticks*TickSize));
     }
     if (
Target1Ticks != 0)
     {
      
exitT1Order ExitLongLimit(0trueTarget1QtyfillPrice+Target1Ticks*TickSize"Target1""Long");
      Print(
TimeStamp+"\tTarget T1 "+Target1Qty+" Set at "+(fillPrice+Target1Ticks*TickSize));
         }
     if (
Target2Ticks != 0)
     {
      
exitT2Order ExitLongLimit(0trueTarget2QtyfillPrice+Target2Ticks*TickSize"Target2""Long");
      Print(
TimeStamp+"\tTarget T2 "+Target2Qty+" Set at "+(fillPrice+Target2Ticks*TickSize));
     }
     if (
Target3Ticks != 0)
     {
      
exitT3Order ExitLongLimit(0trueTarget3QtyfillPrice+Target3Ticks*TickSize"Target3""Long");
      Print(
TimeStamp+"\tTarget T3 "+Target3Qty+" Set at "+(fillPrice+Target3Ticks*TickSize));
     }
     if (
Target4Ticks != 0)
     {
      
exitT4Order ExitLongLimit(0trueTarget4QtyfillPrice+Target4Ticks*TickSize"Target4""Long");
      Print(
TimeStamp+"\tTarget T4 "+Target4Qty+" Set at "+(fillPrice+Target4Ticks*TickSize));
     }
    }
    if (
bEntryShort == true)
    {
     Print(
TimeStamp+"\tShort Entry Fill at "+fillPrice);
     if (
Stop0Ticks != 0)
     {
      
exitS0Order ExitShortStop(0trueEntryQtyfillPrice-Stop0Ticks*TickSize"Stop0""Short");
      Print(
TimeStamp+"\tStop S0 "+EntryQty+" Set at "+(fillPrice-Stop0Ticks*TickSize));
     }
     if (
Target1Ticks != 0)
     {
      
exitT1Order ExitShortLimit(0trueTarget1QtyfillPrice-Target1Ticks*TickSize"Target1""Short");
      Print(
TimeStamp+"\tTarget T1 "+Target1Qty+" Set at "+(fillPrice-Target1Ticks*TickSize));
     }
     if (
Target2Ticks != 0)
     {
      
exitT2Order ExitShortLimit(0trueTarget2QtyfillPrice-Target2Ticks*TickSize"Target2""Short");
      Print(
TimeStamp+"\tTarget T2 "+Target2Qty+" Set at "+(fillPrice-Target2Ticks*TickSize));
     }
     if (
Target3Ticks != 0)
     {
      
exitT3Order ExitShortLimit(0trueTarget3QtyfillPrice-Target3Ticks*TickSize"Target3""Short");
      Print(
TimeStamp+"\tTarget T3 "+Target3Qty+" Set at "+(fillPrice-Target3Ticks*TickSize));
     }
     if (
Target4Ticks != 0)
     {
      
exitT4Order ExitShortLimit(0trueTarget4QtyfillPrice-Target4Ticks*TickSize"Target4""Short");
      Print(
TimeStamp+"\tTarget T4 "+Target4Qty+" Set at "+(fillPrice-Target4Ticks*TickSize));
     }
    }
   }
   
#endregion
  
}
  
#endregion 
gregschr is offline  
Reply With Quote
Old 08-11-2010, 11:53 AM   #2
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

Hello gregschr,

Can you clarify where you are checking OCO values for these orders? These order methods don't allow you to specify OCO values, although you may see this behavior as part of internal order handling rules.

http://www.ninjatrader-support.com/H...verview36.html
NinjaTrader_RyanM is offline  
Reply With Quote
Old 08-11-2010, 12:03 PM   #3
gregschr
Member
 
Join Date: Mar 2010
Location: Rogers, Arkansas
Posts: 84
Thanks: 0
Thanked 0 times in 0 posts
Send a message via Skype™ to gregschr
Default

I am seeing the OCO values on the Orders tab of the Control Panel and also in IB Trader Workstation.

I'm not trying to set or use the OCO values. Everything functions fine unless I reload the strategy with an active position. After I reload the script, T2, T3 and T4 get cancelled after T1 is hit. I don't want this behavior

Thanks.
gregschr is offline  
Reply With Quote
Old 08-11-2010, 01:22 PM   #4
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

gregschr,

Make sure you have Stop and Target handling here set to PerEntryExecution.
NinjaTrader_RyanM is offline  
Reply With Quote
Old 08-11-2010, 02:08 PM   #5
gregschr
Member
 
Join Date: Mar 2010
Location: Rogers, Arkansas
Posts: 84
Thanks: 0
Thanked 0 times in 0 posts
Send a message via Skype™ to gregschr
Default

Ryan, I checked and handling is set to PerEntryExecution. But I am not sure it is applicable since I am not using SetStopLoss(), SetTrailStop() or SetProfitTarget(). What does this setting do with orders such as ExitLongStop and ExitLongLimit?
gregschr is offline  
Reply With Quote
Old 08-11-2010, 02:33 PM   #6
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

This property will apply to Set statements or the Exit methods. Can you let us know which version you're using?
NinjaTrader_RyanM is offline  
Reply With Quote
Old 08-11-2010, 02:41 PM   #7
gregschr
Member
 
Join Date: Mar 2010
Location: Rogers, Arkansas
Posts: 84
Thanks: 0
Thanked 0 times in 0 posts
Send a message via Skype™ to gregschr
Default

I using Version 7 Beta 18
gregschr is offline  
Reply With Quote
Old 08-11-2010, 03:10 PM   #8
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

Can you attach the complete strategy file you're using or send to support@ninjatrader.com

To confirm the steps needed to see this:
1) strategy enters position
2) Reload strategy

Issue is that targets at other levels are canceled when the first is hit?
NinjaTrader_RyanM is offline  
Reply With Quote
Old 08-11-2010, 04:45 PM   #9
gregschr
Member
 
Join Date: Mar 2010
Location: Rogers, Arkansas
Posts: 84
Thanks: 0
Thanked 0 times in 0 posts
Send a message via Skype™ to gregschr
Default

In the process of trying to understand what was going on, I modified my test code. I now can't reproduce the problem I was having, but get another, even more bizarre problem. When a target fills, OnExecution and OnOrderUpdate do not appear to get called.

I've attached the strategy file that is doing this.

I'll keep playing with this to see if I can also reproduce the orginal problem I was having.
Attached Files
File Type: cs SimpleEntryTest.cs (27.7 KB, 1 views)
gregschr is offline  
Reply With Quote
Old 08-11-2010, 05:51 PM   #10
gregschr
Member
 
Join Date: Mar 2010
Location: Rogers, Arkansas
Posts: 84
Thanks: 0
Thanked 0 times in 0 posts
Send a message via Skype™ to gregschr
Default

I've been able to duplicate the original problem.

In Control Center Options/Strategies tab/NinjaScript tab, select the "Immediately submit live working historical orders" radio button and uncheck "Cancel exit orders when a strategy is disabled".

I also re-enabled the historical process in the prior strategy I posted. The revised strategy with historical processing enabled is attached to this post.

You don't need to wait for an exit fill. Once you have an entry fill and the targets and stop orders are placed, reload the strategy and the target and stop orders are then all linked by the same OCO group.

Also if one of the orders fills and then you try to reload the strategy again. All the orders are cancelled by the broker because the OCO value matches the value of a group that was already filled.

I'm using IB.
Attached Files
File Type: cs SimpleEntryTest.cs (27.7 KB, 7 views)
gregschr is offline  
Reply With Quote
Old 08-11-2010, 07:52 PM   #11
gregschr
Member
 
Join Date: Mar 2010
Location: Rogers, Arkansas
Posts: 84
Thanks: 0
Thanked 0 times in 0 posts
Send a message via Skype™ to gregschr
Default On Execution Problem

Back to the OnExecution Problem. I have attached 2 stategies.

SimpleEntryTest65 works correctly in both 6.5 and 7.0. Calls to OnExecution happen as expected.

SimpleEntryTest only runs on 7.0 because of GridCategory used in properties. Calls to OnExecution do not happen in this version.

I believe the only diff is the use of GridCategory.
Attached Files
File Type: cs SimpleEntryTest65.cs (27.7 KB, 2 views)
File Type: cs SimpleEntryTest.cs (27.8 KB, 3 views)
gregschr is offline  
Reply With Quote
Old 08-12-2010, 08:44 AM   #12
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

Thanks - I'll take a look at these items and get back to you later today.
NinjaTrader_RyanM is offline  
Reply With Quote
Old 08-12-2010, 09:46 AM   #13
gregschr
Member
 
Join Date: Mar 2010
Location: Rogers, Arkansas
Posts: 84
Thanks: 0
Thanked 0 times in 0 posts
Send a message via Skype™ to gregschr
Default

This is really bizarre.... I saved the file that was not calling OnExecution with a different name (all the code is the same) and the new one works correctly.
gregschr is offline  
Reply With Quote
Old 08-12-2010, 10:30 AM   #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

Thanks for the update. I'm using the original name SimpleEntryTest and am seeing calls to OnExecution() here.
NinjaTrader_RyanM is offline  
Reply With Quote
Old 08-12-2010, 11:17 AM   #15
gregschr
Member
 
Join Date: Mar 2010
Location: Rogers, Arkansas
Posts: 84
Thanks: 0
Thanked 0 times in 0 posts
Send a message via Skype™ to gregschr
Default

Before saving the file under a new name, I installed Beta 19 over 18 and had the same problem. I also tried deleting the NinjaTrader.Custom.dll and NinjaTrader.Custom.xml files, and recompling.

I don't know if it makes any difference, but are you running the SimpleEntryTest with IB?

It almost seems like part of the strategy is cached somewhere and the cached portion is being called. This may be one of those unresolved problems that goes away and hopefully never comes up again.
gregschr 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
Entries being chased (unexpected) lookOutBelow Automated Trading 3 07-21-2010 09:49 AM
Unexpected Order Submission drkempus Automated Trading 3 07-18-2010 11:44 AM
DataSeries.ContainsValue unexpected behavior GaryAlbers Version 7 Beta General Questions & Bug Reports 6 06-22-2010 03:11 AM
Behaviour unexpected joanNT Version 7 Beta General Questions & Bug Reports 1 03-16-2010 10:07 AM
Workspace restore, unexpected behavior Wessel Historical NinjaTrader 6.5 Beta Threads 2 03-12-2008 11:10 AM


All times are GMT -6. The time now is 03:26 PM.