You will need to self code it. If you want it automatic you need to use Set(). If you want control you need to use Exit() and self code the logic.
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
Best practice to OCO-Orders in code ...
Collapse
X
-
Hallo,
I am trying to set a stop and reverse within OnBarUpdate, once I am long or short, in the way I pasted here below; unfortunately it does not work and I do not understand why.
In the first turnaround the Enter order is executed, but the Stop is not activated. In the following bars I aways have only the activation of the Stop order, but the EnterLimit orders never get to a working status.
What I want to achieve is to get the Stop to be accepted and the EnterLimit order to get working at the same time. Once one of the 2 is hit, if I understood correctly, the other pending order should be cancelled as the "if" condition is not met anymore. Could you please help me to understand where the mistake is?
Thank you in advance
// Condition set 1
if (Position.MarketPosition == MarketPosition.Flat
&& CrossAbove(SMA(MM2), SMA(MM1), 1) == true)
{
EnterShortLimit(DefaultQuantity, High[1]);
}
// Condition set 2
if (Position.MarketPosition == MarketPosition.Flat
&& CrossAbove(SMA(MM1), SMA(MM2), 1) == true)
{
EnterLongLimit(DefaultQuantity, Low[1]);
}
// Condition set 3
if (Position.MarketPosition == MarketPosition.Short)
{
EnterLongLimit(DefaultQuantity, Low[1]);
SetStopLoss(CalculationMode.Ticks, StopLoss);
}
// Condition set 4
if (Position.MarketPosition == MarketPosition.Long)
{
EnterShortLimit(DefaultQuantity, High[1]);
SetStopLoss(CalculationMode.Ticks, StopLoss);
}
Comment
-
marticora,
Please note that your entry condition when you are flat means you are submitting a limit order that is only active for 1 bar. You will need to keep resubmitting the order should you want to keep it alive.
For your reversals, you will need to call your Set() methods before you call your entry method. Otherwise it will still be using your old Set() instead of your new one at submission time.Josh P.NinjaTrader Customer Service
Comment
-
The entry condition is working fine. As soon as I have a cross it is activated.
What does not work at all is the reverse. The Stop is activated, but the EnterLimit does not work at all. I tried to reverse the sequence of the 2 orders, but without any success.
CalculateOnBarClose is set to False.
Any other possible mistake?
Comment
-
You will need to use TraceOrders = true to understand what is going on from there. Please add that line to Initialize() and then open the Output Window to see the trace output. It will tell you the reason your orders are ignored/rejected/etc. if that is what is happening.Josh P.NinjaTrader Customer Service
Comment
-
I followed your suggestion and I get the confirmation that the order is entered, but just is not getting working.
I tried to cancel the SetStopLoss lines: the EnterLimit orders are working perfectly in that case.
What happens therefore is that the SetStopLoss inhibits the EnterLimit order to get working. How can I fix that?
Comment
-
Hi, please find here below an extract of what appears on the output window.
In fact the messages confirm the SetStopTarget() method while the EnterLongLimit, what would reverse the position, does not appear at all if am not wrong.
12/04/2010 23.56.54 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Ticks Value=8 Currency=0 Simulated=False
12/04/2010 23.56.54 Entered internal PlaceOrder() method at 12/04/2010 23.56.54: Action=SellShort OrderType=Limit Quantity=1 LimitPrice=1156,75 StopPrice=0 SignalName='' FromEntrySignal=''
12/04/2010 23.56.54 Entered internal SetStopTarget() method: Type=Stop FromEntrySignal='' Mode=Ticks Value=8 Currency=0 Simulated=False
12/04/2010 23.56.54 Entered internal PlaceOrder() method at 12/04/2010 23.56.54: Action=SellShort OrderType=Limit Quantity=1 LimitPrice=1156,75 StopPrice=0 SignalName='' FromEntrySignal=''
12/04/2010 23.56.54 Ignored PlaceOrder() method: Action=SellShort OrderType=Limit Quantity=1 LimitPrice=1156,75 StopPrice=0 SignalName=Sell short' FromEntrySignal='' Reason='There already is a matching order with same prices and quantity'
Comment
-
That is exactly the issue: the EnterLimit orders are not passed when the SetStopLoss is activated. Can it be related to the default quantity? If I take the same code and only cancel the SetStopLoss llines, the EnterLongLimit and EnterShortLimit orders are working perfectly
Comment
-
marticora,
Please add Print() inside your if-statements to see if your code ever reaches there. If an order is ignored it will be stated in the TraceOrders. It will not be ignored with no message. No message generally means your code never reaching that point.Josh P.NinjaTrader Customer Service
Comment
-
Josh,
I think I found the issue: the manual explicitly says that what I am trying to do is not possible with NT.
After an EnterLong() I want to protect my order with a SetStopLoss() and at the same time input an EnterShortLimit() to take profit and reverse, if this last order is hit before the StopLoss.
The manual says:
"
Methods that generate orders (excluding market orders) to enter a position will be ignored if:
[...]- A position is open and an order submitted by a set method (SetStopLoss() for example) is active and the order is used to open a position in the opposite direction
Comment
-
marticora,
If you are actually running into an Internal Order Handling Rule you would still get TraceOrders = true output signifying such to be the case. Perhaps you have copied an irrelevant part of the TraceOrders output which is why we are not seeing it.
There is no way to "bypass" Internal Order Handling Rules. If you want control over this aspect you can try using Exit() methods in place of the Set(). Then you will need to be mindful of the other rules with having working Exit() in place and trying to go Enter() in the opposite direction. In those situations, you simply need to cancel the old order you do not want any more and then send in your new one.Josh P.NinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by cre8able, Today, 01:16 PM
|
1 response
4 views
0 likes
|
Last Post Today, 01:30 PM | ||
Started by chbruno, 04-24-2024, 04:10 PM
|
3 responses
48 views
0 likes
|
Last Post Today, 01:29 PM | ||
Started by samish18, Today, 01:01 PM
|
1 response
6 views
0 likes
|
Last Post Today, 01:25 PM | ||
Started by WHICKED, Today, 12:56 PM
|
1 response
8 views
0 likes
|
Last Post Today, 01:17 PM | ||
Started by WHICKED, Today, 12:45 PM
|
1 response
11 views
0 likes
|
Last Post Today, 01:08 PM |
Comment