PDA

View Full Version : ExitShort ExitLong behaviour


stormze
05-27-2009, 02:36 PM
Hello, Im seeing some kind of problem with a strategy executed in real time on the simulated NT account...
This is the code:

if(/*some short condition*/)
{
if(Position.Quantity > 0)
ExitLong(1, 1, "TO_ExitLong", "TO_EnterLong");
if((ToTime(Time[0]) > 103000 && ToTime(Time[0]) < 163000))
EnterShort(1, 1, "TO_EnterShort");
}
elseif(/*some long condition*/)
{
if(Position.Quantity > 0)
ExitShort(1, 1, "TO_ExitShort", "TO_EnterShort");
if((ToTime(Time[0]) > 103000 && ToTime(Time[0]) < 163000))
EnterLong(1, 1, "TO_EnterLong");
}

The problem is that the long condition is executing 2 orders of buy and the short part just closes that 2 positions... (the script never go short)
ExitShort is not acting well, why is that ?

stormze
05-27-2009, 02:38 PM
Note: this started happening since I manually closed one of the orders that the script submitted

NinjaTrader_Josh
05-27-2009, 02:42 PM
stormze,

If you want to reverse there is no need to place ExitShort then EnterLong orders like that. Just call EnterLong and just call EnterShort. If you were in an opposing position it will automatically close that position first and then get you into your long/short.

stormze
05-27-2009, 02:52 PM
I know but I only want to reverse on some circustances and just close the actual position on others.
Apparently when Im long 1 contract and I do a close, instead of selling that contract, is buying another one

NinjaTrader_Josh
05-27-2009, 03:06 PM
If you were long and you called ExitLong() it will close the position. If in addition you called EnterShort() you will for sure get the additional short. You will need to debug your orders to see which are being placed with TraceOrders = true.

http://www.ninjatrader-support2.com/vb/showthread.php?t=3627

stormze
05-27-2009, 03:30 PM
Ok I will try to collect some logs with TraceOrders = true
But may be there is a bug here, I think what happened is the following:

- Script executed EnterShort of 1 contract
- I manually closed that position and went to flat
- Script executed an ExitShort of 1 contract but it didnt realized that it was already flat and bought 1 contract
- Script executed an EnterLong of 1 contract and bought another contract being long 2 contracts... and so on

NinjaTrader_Josh
05-27-2009, 03:37 PM
Could happen since your code is never checking for a flat. Plus inflight executions can occur. If the execution has not been reported back yet and you still submit a new order it will go through.

stormze
05-27-2009, 04:05 PM
Ah interesting...
I tryed to check for a flat on my code with

if(Position.Quantity > 0)
close actual position

Is that wrong or some other better way to do it ? Note that all orders are submitted to only one ticker but its not the main ticker

NinjaTrader_Bertrand
05-28-2009, 05:17 AM
You can use Position.MarketPosition for checking if you're flat or not - http://www.ninjatrader-support.com/HelpGuideV6/MarketPosition.html

stormze
05-28-2009, 10:03 AM
Ok I still have the problem.
EnterLong of 1 contract is buiyng me 2 contracts being flat, and I can't change that behaviour :(
Is acting like if it were short 1 contract.
I tryed reseting the Sim101 db restarting NT but nothing.
What can I do

NinjaTrader_Josh
05-28-2009, 10:13 AM
stormze,

You need to use TraceOrders = true to see what is going on. If you really are flat it will never buy 2 if the order was submitted as 1. Changes you make to the code need to be compiled by pressing F5 in the compiler and changes are only reflected AFTER you start a new instance of the strategy.

stormze
05-28-2009, 10:33 AM
I dont see anything unusual with TraceOrders = true, prior EnterLong is baing executed I see a "Close position" order generated automatically, I want to prevent this or reset the counter it has. How NT knows that it need to close a position ???
Yea I also reload the strategy for every change...

5/28/2009 1:22:01 PM,Position,Instrument='ES 06-09' Account='Sim101' Avg price=895.5 Quantity=2 Market position=Long Operation=Update Currency=Unknown,
5/28/2009 1:22:01 PM,Execution,Execution='a399113f8e2041e18abb6b5828 e69295' Instrument='ES 06-09' Account='Sim101' Exchange=Default Price=895.5 Quantity=1 Market position=Long Operation=Insert Order='bb5f8897c86b42a19f78711279b930b1' Time='5/28/2009 1:22:01 PM',
5/28/2009 1:22:01 PM,Order,Order='bb5f8897c86b42a19f78711279b930b1/Sim101' Name='TO_EnterLong' New State=Filled Instrument='ES 06-09' Action=Buy Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=1 Fill price=895.5 Error=NoError Native error='',
5/28/2009 1:22:01 PM,Order,Order='bb5f8897c86b42a19f78711279b930b1/Sim101' Name='TO_EnterLong' New State=Working Instrument='ES 06-09' Action=Buy Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=0 Fill price=0 Error=NoError Native error='',
5/28/2009 1:22:01 PM,Order,Order='bb5f8897c86b42a19f78711279b930b1/Sim101' Name='TO_EnterLong' New State=Accepted Instrument='ES 06-09' Action=Buy Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=0 Fill price=0 Error=NoError Native error='',
5/28/2009 1:22:01 PM,Position,Instrument='ES 06-09' Account='Sim101' Avg price=895.5 Quantity=1 Market position=Long Operation=Insert Currency=Unknown,
5/28/2009 1:22:01 PM,Execution,Execution='a12a22d9449a4f3ca052efb15f 3a92df' Instrument='ES 06-09' Account='Sim101' Exchange=Default Price=895.5 Quantity=1 Market position=Long Operation=Insert Order='f242023ab619414aa912c98857ffc9d4' Time='5/28/2009 1:22:01 PM',
5/28/2009 1:22:01 PM,Order,Order='f242023ab619414aa912c98857ffc9d4/Sim101' Name='Close position' New State=Filled Instrument='ES 06-09' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=1 Fill price=895.5 Error=NoError Native error='',
5/28/2009 1:22:01 PM,Order,Order='f242023ab619414aa912c98857ffc9d4/Sim101' Name='Close position' New State=Working Instrument='ES 06-09' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=0 Fill price=0 Error=NoError Native error='',
5/28/2009 1:22:01 PM,Order,Order='f242023ab619414aa912c98857ffc9d4/Sim101' Name='Close position' New State=Accepted Instrument='ES 06-09' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=0 Fill price=0 Error=NoError Native error='',
5/28/2009 1:22:00 PM,Order,Order='bb5f8897c86b42a19f78711279b930b1/Sim101' Name='TO_EnterLong' New State=PendingSubmit Instrument='ES 06-09' Action=Buy Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=0 Fill price=0 Error=NoError Native error='',
5/28/2009 1:22:00 PM,Order,Order='f242023ab619414aa912c98857ffc9d4/Sim101' Name='Close position' New State=PendingSubmit Instrument='ES 06-09' Action=BuyToCover Limit price=0 Stop price=0 Quantity=1 Type=Market Filled=0 Fill price=0 Error=NoError Native error='',

NinjaTrader_Josh
05-28-2009, 10:38 AM
stormze,

It is evident your signal tracking has gone awry. You will need to debug your strategy and find exactly where it happens. You have a lot of Enter/Exits mixing and matching everywhere. You will need to check them by hand.