PDA

View Full Version : Partial Exit signal


mix.net
07-14-2010, 10:24 AM
Hello. Can I close one entry signal by using two different exit signals?

Such as:

// Open LONG position
EnterLong(10)

// Close first 5
ExitLong(5)

// After a few minutes close the other
ExitLong(5)

When I use similar commands in My Strategy, the second ExitLong is Ignored, for the reason "There already is a matching, filled exit order in place", despite the fact that the position is still 5L.

Thank you in advance

NinjaTrader_Austin
07-14-2010, 10:30 AM
mix.net, are you specifying entry and exit signal names? If not, please try that because that would force NinjaTrader to look at the specific matching signals. Could you post the complete code so that we can see if anything else looks out of place?

mix.net
07-14-2010, 12:06 PM
Thank you for your reply.

The strategy looks for some data in my own SQL database and based on that data it calls Enter or Exit functions. The signal names don't collide.

I suppose posting complete code wouldn't be helpful, so I post just extracts.

// this is done for mQuantity = 5
IOrder mOrder1 = EnterLong(mQuantity, "aaa");

After some period of time the strategy makes the first exit call:

// this is done for mQuantity = 3
IOrder mOrder2 = ExitLong(mQuantity, "bbb1", "aaa");

And after another period of time the strategy makes the second exit call:

// this is done for mQuantity = 2
IOrder mOrder2 = ExitLong(mQuantity, "bbb2", "aaa");

When I specify names like in the sample above, the result in the order trace looks like this:

14.7.2010 19:49:15 Entered internal PlaceOrder() method at 14.7.2010 19:49:15: Action=Buy OrderType=Market Quantity=5 LimitPrice=0 StopPrice=0 SignalName='aaa' FromEntrySignal=''

14.7.2010 19:49:25 Entered internal PlaceOrder() method at 14.7.2010 19:49:25: Action=Sell OrderType=Market Quantity=3 LimitPrice=0 StopPrice=0 SignalName='bbb1' FromEntrySignal='aaa'

14.7.2010 19:49:35 Entered internal PlaceOrder() method at 14.7.2010 19:49:35: Action=Sell OrderType=Market Quantity=2 LimitPrice=0 StopPrice=0 SignalName='bbb2' FromEntrySignal='aaa'

14.7.2010 19:49:35 Ignored PlaceOrder() method at 14.7.2010 19:49:35: Action=Sell OrderType=Market Quantity=-1 LimitPrice=0 StopPrice=0 SignalName='bbb2' FromEntrySignal='aaa' Reason='There is no remaining quantity to exit'

When I don't specify the signal names, the result is similar:

14.7.2010 20:08:00 Entered internal PlaceOrder() method at 14.7.2010 20:08:00: Action=Buy OrderType=Market Quantity=5 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''

14.7.2010 20:08:10 Entered internal PlaceOrder() method at 14.7.2010 20:08:10: Action=Sell OrderType=Market Quantity=3 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''

14.7.2010 20:08:30 Entered internal PlaceOrder() method at 14.7.2010 20:08:30: Action=Sell OrderType=Market Quantity=2 LimitPrice=0 StopPrice=0 SignalName='' FromEntrySignal=''

14.7.2010 20:08:30 Ignored PlaceOrder() method: Action=Sell OrderType=Market Quantity=2 LimitPrice=0 StopPrice=0 SignalName=Sell' FromEntrySignal='' Reason='There already is a matching, filled exit order in place'

NinjaTrader_Austin
07-14-2010, 01:56 PM
mix.net, in the first set of order traces, it lists a quantity of -1 for the bbb2 exit. Could this be part of the problem?

I created a simple script to test this out on my end and everything worked as expected:

protected override void OnBarUpdate()
{
if (CurrentBar == 100)
{
IOrder entryOrder = EnterLong(10, "long entry");
}
else if (CurrentBar == 110)
{
IOrder exitOrder1 = ExitLong(5, "exit 1", "long entry");
}
else if (CurrentBar == 120)
{
IOrder exitOrder2 = ExitLong(5, "exit 2", "long entry");
}
}
The output is as follows:

7/13/2010 8:41:00 AM Entered internal PlaceOrder() method at 7/13/2010 8:41:00 AM: Action=Buy OrderType=Market Quantity=10 LimitPrice=0 StopPrice=0 SignalName='long entry' FromEntrySignal=''
7/13/2010 8:51:00 AM Entered internal PlaceOrder() method at 7/13/2010 8:51:00 AM: Action=Sell OrderType=Market Quantity=5 LimitPrice=0 StopPrice=0 SignalName='exit 1' FromEntrySignal='long entry'
7/13/2010 9:01:00 AM Entered internal PlaceOrder() method at 7/13/2010 9:01:00 AM: Action=Sell OrderType=Market Quantity=5 LimitPrice=0 StopPrice=0 SignalName='exit 2' FromEntrySignal='long entry'
If you could strip down your strategy to a more basic version that doesn't require the SQL connection but still exhibits this behavior, I'd like to test it because at this point, it appears to be a coding issue.

mix.net
07-15-2010, 11:05 AM
I replaced the DB queries by incrementing and checking a counter for the strategy to became more simple


private int tCount = 0;
private System.Windows.Forms.Timer mTimer;
protected override void OnStart()
{
mTimer = new System.Windows.Forms.Timer();
mTimer.Interval = 1000;
mTimer.Tick += new EventHandler(TimerEvent);
mTimer.Enabled = true;
mTimer.Start();
}
private void TimerEvent(Object myObject, EventArgs myEventArgs)
{
TriggerCustomEvent(mTick, 0, null);
}
private void mTick(object state)
{
if(tCount == 0)
EnterLong(10, "long entry");
elseif(tCount == 1)
ExitLong(5, "exit1", "long entry");
elseif(tCount == 2)
ExitLong(5, "exit2", "long entry");

tCount++;
}

mix.net
07-15-2010, 11:06 AM
(continuation of the previous post)
However, the problem remained. The first EXIT signal is entered OK, but the second EXIT is ignored:


15.7.2010 19:04:23 Entered internal PlaceOrder() method at 15.7.2010 19:04:23: Action=Buy OrderType=Market Quantity=10 LimitPrice=0 StopPrice=0 SignalName='long entry' FromEntrySignal=''

15.7.2010 19:04:24 Entered internal PlaceOrder() method at 15.7.2010 19:04:24: Action=Sell OrderType=Market Quantity=5 LimitPrice=0 StopPrice=0 SignalName='exit 1' FromEntrySignal='long entry'

15.7.2010 19:04:25 Entered internal PlaceOrder() method at 15.7.2010 19:04:25: Action=Sell OrderType=Market Quantity=5 LimitPrice=0 StopPrice=0 SignalName='exit 2' FromEntrySignal='long entry'

15.7.2010 19:04:25 Ignored PlaceOrder() method at 15.7.2010 19:04:25: Action=Sell OrderType=Market Quantity=0 LimitPrice=0 StopPrice=0 SignalName='exit 2' FromEntrySignal='long entry' Reason='There is no remaining quantity to exit'


I'm using NinjaTrader 7 beta 18. Your simple test script worked OK for me.

Thank you for your help

NinjaTrader_Bertrand
07-15-2010, 11:50 AM
mix.net, would you mind trying to start your Time up in OnStartUp() instead? Also: Do you see the same issue happen when working with the IOrder objects as Austin's test did?

mix.net
07-15-2010, 12:05 PM
When I initialize and start the Timer in the OnStartUp method, the problem remains, nothing changes.

I tried Austin's test (using OnBarUpdate method) in my NinjaTrader and it worked OK.

When working with IOrder objects in my sample (using timer ticks) the problem remains the same, the second ExitLong method returns null instead of an IOrder object.

That means when I call the Exit methods in the OnBarUpdate method, it works OK, but when I call them similarly in the Timer tick method, the second Exit order is ignored.

NinjaTrader_Josh
07-15-2010, 12:48 PM
mix.net,

In your code you use OnStart(). This is not the OnStartUp() method that is required. Please amend the code to use OnStartUp().

mix.net
07-15-2010, 01:17 PM
I'm not sure that I understand you correctly.

I tried initializing and starting the timer in OnStartUp() method, but the problem remained. Should there be a difference?

NinjaTrader_Josh
07-15-2010, 02:13 PM
In your code you created a Forms.Timer object but you have not added one of the required using declarations.

using System.Windows.Forms;

mix.net
07-16-2010, 08:26 AM
I tried it, but adding the using declaration didn't make any change.

The timer is working correctly, but the second EXIT order (in the third tick of the timer) is ignored with reason "There is no remaining quantity to exit", although when putting the same code in OnBarUpdate method instead of timer tick method, both exit orders are processed with no problem.

Therefore I suppose there may be some issue with threading, that causes bad processing of the second EXIT call. The second ExitLong function call returns null, instead of an IOrder object.

Thank you for your patiance

NinjaTrader_Josh
07-16-2010, 08:38 AM
mix.net,

To scale-out, you will need to scale-in first. Please split your entry order into two separate orders with different signal names. Then call the exit orders on each signal name individually.

mix.net
07-16-2010, 09:25 AM
OK, thank you.

Just for me to be informed: why does it work, when I call the methods from OnBarUpdate method, while it doesn't work when called from timer tick methods?

NinjaTrader_Bertrand
07-16-2010, 09:58 AM
mix.net, you're likely hitting a race condition here with the timers - do you see the same if you increase the timing to produce a later second Exit call?

mix.net
07-16-2010, 11:10 AM
Yes, the issue comes independently on the timing - I've tried different timer periods (1sec, 2sec, 10sec, 1min, ...). I've also tried calling the second exit at different times (at 3rd timer tick, 4th tick, 10th tick...).

NinjaTrader_Austin
07-16-2010, 11:52 AM
mix.net, are you using Timers to call orders in the strategy of yours that originally exhibited this behavior?

If so, is there any other way you could restructure your strategy to not use Timers?

mix.net
07-16-2010, 12:35 PM
Yes Austin, I will try to find other solution, not using timers. Thank you very much for your help.

Is there any way to report this problem to developers of the NT? It seems to be a bug, or at least an unexpected behaviour, because in the the strategy everything seems to be allright and there is probably no reason for such behaviour.

NinjaTrader_Austin
07-16-2010, 01:08 PM
Hello, I will forward this thread to development and see what they have to say.