#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(0, true, EntryQty, fillPrice+Stop0Ticks*TickSize, "Stop0", "Long");
Print(TimeStamp+"\tStop S0 "+EntryQty+" Set at "+(fillPrice+Stop0Ticks*TickSize));
}
if (Target1Ticks != 0)
{
exitT1Order = ExitLongLimit(0, true, Target1Qty, fillPrice+Target1Ticks*TickSize, "Target1", "Long");
Print(TimeStamp+"\tTarget T1 "+Target1Qty+" Set at "+(fillPrice+Target1Ticks*TickSize));
}
if (Target2Ticks != 0)
{
exitT2Order = ExitLongLimit(0, true, Target2Qty, fillPrice+Target2Ticks*TickSize, "Target2", "Long");
Print(TimeStamp+"\tTarget T2 "+Target2Qty+" Set at "+(fillPrice+Target2Ticks*TickSize));
}
if (Target3Ticks != 0)
{
exitT3Order = ExitLongLimit(0, true, Target3Qty, fillPrice+Target3Ticks*TickSize, "Target3", "Long");
Print(TimeStamp+"\tTarget T3 "+Target3Qty+" Set at "+(fillPrice+Target3Ticks*TickSize));
}
if (Target4Ticks != 0)
{
exitT4Order = ExitLongLimit(0, true, Target4Qty, fillPrice+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(0, true, EntryQty, fillPrice-Stop0Ticks*TickSize, "Stop0", "Short");
Print(TimeStamp+"\tStop S0 "+EntryQty+" Set at "+(fillPrice-Stop0Ticks*TickSize));
}
if (Target1Ticks != 0)
{
exitT1Order = ExitShortLimit(0, true, Target1Qty, fillPrice-Target1Ticks*TickSize, "Target1", "Short");
Print(TimeStamp+"\tTarget T1 "+Target1Qty+" Set at "+(fillPrice-Target1Ticks*TickSize));
}
if (Target2Ticks != 0)
{
exitT2Order = ExitShortLimit(0, true, Target2Qty, fillPrice-Target2Ticks*TickSize, "Target2", "Short");
Print(TimeStamp+"\tTarget T2 "+Target2Qty+" Set at "+(fillPrice-Target2Ticks*TickSize));
}
if (Target3Ticks != 0)
{
exitT3Order = ExitShortLimit(0, true, Target3Qty, fillPrice-Target3Ticks*TickSize, "Target3", "Short");
Print(TimeStamp+"\tTarget T3 "+Target3Qty+" Set at "+(fillPrice-Target3Ticks*TickSize));
}
if (Target4Ticks != 0)
{
exitT4Order = ExitShortLimit(0, true, Target4Qty, fillPrice-Target4Ticks*TickSize, "Target4", "Short");
Print(TimeStamp+"\tTarget T4 "+Target4Qty+" Set at "+(fillPrice-Target4Ticks*TickSize));
}
}
}
#endregion
}
#endregion
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
Unexpected OCO Behavior
Collapse
X
-
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:Tags: None
-
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.
Comment
-
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?
Comment
-
Comment
-
Can you attach the complete strategy file you're using or send to [email protected]
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?Ryan M.NinjaTrader Customer Service
Comment
-
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
Comment
-
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
Comment
-
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.
Comment
-
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.
Comment
-
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.
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by ETFVoyageur, Yesterday, 10:13 PM
|
3 responses
25 views
0 likes
|
Last Post
by ETFVoyageur
Today, 09:08 PM
|
||
Started by ETFVoyageur, Yesterday, 12:52 AM
|
3 responses
33 views
0 likes
|
Last Post
by ETFVoyageur
Today, 09:05 PM
|
||
Started by ETFVoyageur, Today, 02:10 AM
|
2 responses
27 views
0 likes
|
Last Post
by ETFVoyageur
Today, 08:55 PM
|
||
Started by ryan_21, Today, 08:46 PM
|
1 response
2 views
0 likes
|
Last Post Today, 08:52 PM | ||
Started by ESHunter, Today, 08:06 PM
|
1 response
8 views
0 likes
|
Last Post Today, 08:22 PM |
Comment