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
ExitLong() and ExitShort() doesnt close StopLoss and ProfitTarget() orders
Collapse
X
-
ExitLong() and ExitShort() doesnt close StopLoss and ProfitTarget() orders
I use ExitLong() and ExitShort() to Close positions on end of week. The Close is working fine for the position but it doesn't close out the StopLoss and TakeProfit orders. How to make the ExitLong() and ExitShort() to close out all orders for the position.Tags: None
-
Originally posted by ajsdnyyrr1234DDf View PostI use ExitLong() and ExitShort() to Close positions on end of week. The Close is working fine for the position but it doesn't close out the StopLoss and TakeProfit orders. How to make the ExitLong() and ExitShort() to close out all orders for the position.
-
Could you point me to a CancelOrder() sample script?
Do I have to call two separate Cancel calls, one for the StopLoss and one for TakeProfit?
It looks like these are two separate OrderIDs and linked via a single OCO ID.
How about the CloseStrategy() call instead of ExitLong()/ExitShort()/SetState()
Will CloseStartegy close positions, cancel orders, and strategy properly?
Following is what I have currently:
//Friday Code
if (Time[0].DayOfWeek == DayOfWeek.Friday && ToTime(Time[0]) >= 133000)
{
if (Position.MarketPosition == MarketPosition.Long)
ExitLong(@"LongX");
else if (Position.MarketPosition == MarketPosition.Short)
ExitShort(@"ShortX");
if (State == State.Realtime)
{
SetState(State.Terminated);
Print("Friday Condition - Disabling Strategy" + Time[0].ToString());
return;
}
}
Comment
-
Originally posted by ajsdnyyrr1234DDf View PostCould you point me to a CancelOrder() sample script?
Do I have to call two separate Cancel calls, one for the StopLoss and one for TakeProfit?
It looks like these are two separate OrderIDs and linked via a single OCO ID.
How about the CloseStrategy() call instead of ExitLong()/ExitShort()/SetState()
Will CloseStartegy close positions, cancel orders, and strategy properly?
Following is what I have currently:
//Friday Code
if (Time[0].DayOfWeek == DayOfWeek.Friday && ToTime(Time[0]) >= 133000)
{
if (Position.MarketPosition == MarketPosition.Long)
ExitLong(@"LongX");
else if (Position.MarketPosition == MarketPosition.Short)
ExitShort(@"ShortX");
if (State == State.Realtime)
{
SetState(State.Terminated);
Print("Friday Condition - Disabling Strategy" + Time[0].ToString());
return;
}
}Last edited by RJay; 03-04-2018, 02:25 PM.
Comment
-
I am getting the following error:
The name CancelAllOrders does not exist in the current context
CS0103
Comment
-
Originally posted by ajsdnyyrr1234DDf View PostI am getting the following error:
The name CancelAllOrders does not exist in the current context
CS0103
Comment
-
-
Originally posted by ajsdnyyrr1234DDf View PostI am using NT8
privateAccountmyAccount;
protectedoverridevoidOnStateChange()
{
if(State==State.SetDefaults)
{
// Initialize myAccount
}
}
privatevoidOnExecutionUpdate(objectsender,ExecutionEventArgse)
{
// Cancel all orders if an execution is triggered after 9pm
if(e.Time>newDateTime(now.Year,now.Month,now.Day,21,0,0))
myAccount.CancelAllOrders(e.Execution.Instrument);
}
Comment
-
Here is what I have come up with and seems to be working:
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
lock (Account.All)
myAccount = Account.All.FirstOrDefault(a => a.Name == "Realmoney");
} }
//Friday Code
if (Time[0].DayOfWeek == DayOfWeek.Friday && ToTime(Time[0]) >= 133000)
{
if (Position.MarketPosition == MarketPosition.Long)
{
myAccount.CancelAllOrders(BarsArray[0].Instrument);
ExitLong(@"LongX");
}
else if (Position.MarketPosition == MarketPosition.Short)
{
myAccount.CancelAllOrders(BarsArray[0].Instrument);
ExitShort(@"ShortX");
}
if (State == State.Realtime)
{
SetState(State.Terminated);
Print("Condition not met. Disabling Strategy" + Time[0].ToString());
return;
}
}
Comment
-
Hello ajsdnyyrr1234DDf,
To confirm, you are not disabling the script while a position is open, is this correct?
(If so, the 'Cancel exit orders when a strategy is disabled' option will cancel any working orders when the script is disabled)
Instead, the strategy is staying running, a position is open, then an ExitLong() or ExitShort() call is used to exit the position, the position becomes closed, however, a stop loss and profit target that were generated from the script remain working?
Is this all correct?
If so, I would like to test script to see if I am able to reproduce this behavior on my end.Chelsea B.NinjaTrader Customer Service
Comment
-
Yes correct. Following is the order I want to accomplish
1) Exit position
2) Close all orders
3) Disable strategy for the weekend.
Yes the behavior you have described is correct.
Using ExitLong() and ExitShort() only exit the positions and not any Stop or Limit orders associated with the position for StopLoss or TakeProfit.
I don't see the issue with Historical backtesting. I see the issue with realtime.
Comment
-
Hello ajsdnyyrr1234DDf,
I am not able to reproduce this behavior.
Below is a link to a video.
Attached is the tested script.
The video demonstrates when the position is exited, the stop loss and profit target are automatically cancelled.
Are you using SetProfitTarget and SetStopLoss?Attached FilesChelsea B.NinjaTrader Customer Service
Comment
-
Yes. I am using SetStopLoss() and SetProfitTarget(). I see what I am different that your example, and that is probably why the problem is not there for the historical back test.
Right after the Ext() call I am terminating the Strategy using SetState(State.Terminated) and I am doing this only for Real-time. I am not doing this for historical.
It appears, the limit orders closes out after some time when the position is exited. Since I am not waiting and immediately setting the state to terminated, that may be the reason why the limit orders stay open.
I read a similar behavior in some forum, where the limit orders associated with a position cancels out after a certain timeout but not immediately.
I modified my code as below:
====
if (Time[0].DayOfWeek == DayOfWeek.Friday && ToTime(Time[0]) >= 133000)
{
if (Position.MarketPosition == MarketPosition.Long)
{
myAccount.CancelAllOrders(BarsArray[0].Instrument);
ExitLong(@"LongX");
}
else if (Position.MarketPosition == MarketPosition.Short)
{
myAccount.CancelAllOrders(BarsArray[0].Instrument);
ExitShort(@"ShortX");
}
if (State == State.Realtime)
{
SetState(State.Terminated);
Print("Condition not met. Disabling Strategy" + Time[0].ToString());
return;
}
}
====
I see no difference in historical back tests but will see how it behaves this weekend.
Comment
-
Hello ajsdnyyrr1234DDf,
If you are forcing the strategy to be terminated and you want orders to be automatically exited, you need to wait until the position is flat and all orders are cancelled.
OnPositionUpdate and OnOrderUpdate checking that all positions and orders are cancelled or filled will allow the stop losses and profit targets to be cancelled before the strategy stops processing and can no longer make actions with orders.Chelsea B.NinjaTrader Customer Service
Comment
-
I also have the problem that the following code exits the position but leaves the stoploss and profit targets associated with that position open. How do i identify and cancel the profit target and stoploss orders which are set via setstoploss and setprofittarget when the original position is created? Why are these OCO orders not cancelled when the Exitlong/short is called?
Regards
if (BarsInProgress == 23 && Positions[23].MarketPosition != MarketPosition.Flat && Bars.PercentComplete > 0.999
||BarsInProgress == 11 && Positions[11].MarketPosition != MarketPosition.Flat && Bars.PercentComplete > 0.999)
{
ExitLong(23,tradeSizemnq,"ON NQ Long end of session close","NQ ON LONG LIMIT");
ExitShort(23,tradeSizemnq,"ON NQ Short end of session close","NQ ON SHORT LIMIT");
ExitShort(11,tradeSizemnq,"ON NQ Short end of session close","NQ ON SHORT LIMIT MICRO");
ExitLong(11,tradeSizemnq,"ON NQ Long end of session close","NQ ON LONG LIMIT MICRO");
}
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by judysamnt7, 03-13-2023, 09:11 AM
|
4 responses
57 views
0 likes
|
Last Post
by DynamicTest
Today, 08:25 PM
|
||
Started by ScottWalsh, Today, 06:52 PM
|
4 responses
36 views
0 likes
|
Last Post
by ScottWalsh
Today, 07:40 PM
|
||
Started by olisav57, Today, 07:39 PM
|
0 responses
7 views
0 likes
|
Last Post
by olisav57
Today, 07:39 PM
|
||
Started by trilliantrader, Today, 03:01 PM
|
2 responses
19 views
0 likes
|
Last Post
by helpwanted
Today, 07:25 PM
|
||
Started by cre8able, Today, 07:24 PM
|
0 responses
9 views
0 likes
|
Last Post
by cre8able
Today, 07:24 PM
|
Comment