NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 03-05-2009, 08:50 AM   #1
heech
Senior Member
 
Join Date: Nov 2008
Posts: 576
Thanks: 0
Thanked 1 time in 1 post
Default AllTrades set in OnPositionUpdate?

Hi,

Can I get confirmation from someone on when "Performance.AllTrades" is set...? If I've just had a position flattened, is that trade already written into by the time I go into OnPositionUpdate?

Here's the problem. I need to know the price I exited at... and I've been using Performance.AllTrades to figure out that price (scanning through looking for matching instrument name.) I'm doing this in OnPositionUpdate instead of OnExecution/OnOrderUpdate because of reports those might be wrong on IB.

This has worked really well in the past, but just threw me a weird exception suggesting Performance.AllTrades was empty at the time when it executed. I'm printing line numbers, so I know it happened on this line:

Quote:
// First, we have to find info about the most recent trade...
Trade latestTrade = null;
for (int index = Performance.AllTrades.Count-1; index >= 0; index++)
{
latestTrade = Performance.AllTrades[index];
if (latestTrade.Entry.Instrument.FullName == Instrument.FullName)
break;
}
Exception looks like this:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.Collections.CollectionBase.System.Collectio ns.IList.get_Item(Int32 index)
at NinjaTrader.Strategy.TradeCollection.get_Item(Int3 2 index)
at NinjaTrader.Strategy.ProtectDownMulti.OnPositionUp date(IPosition position) in c:\Documents and Settings\Chon\My Documents\NinjaTrader 6.5\bin\Custom\Strategy\ProtectDownMulti.cs:line 942
System.Collections.IList.get_Item
Line: 0
Column: 0
heech is offline  
Reply With Quote
Old 03-05-2009, 09:33 AM   #2
heech
Senior Member
 
Join Date: Nov 2008
Posts: 576
Thanks: 0
Thanked 1 time in 1 post
Default

I might try to get away from using Performance.AllTrades entirely.

So, a related question is... inside OnPositionUpdate, is my IOrder object (for the latest order) already filled out with updated details on the exit execution? Can I pull price information from there?

Just want to avoid a possible race condition.
heech is offline  
Reply With Quote
Old 03-05-2009, 09:55 AM   #3
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

heech,

OnPositionUpdate() will be the last one in the sequence meaning you can use Performance or IOrder and they will be up to date at that moment in time. As far as your exception goes. Please check your for-statement again. You can easily go in with a -1 index as the first run through on that statement.
NinjaTrader_Josh is offline  
Reply With Quote
Old 03-05-2009, 10:18 AM   #4
heech
Senior Member
 
Join Date: Nov 2008
Posts: 576
Thanks: 0
Thanked 1 time in 1 post
Default

Quote:
Originally Posted by NinjaTrader_Josh View Post
heech,

OnPositionUpdate() will be the last one in the sequence meaning you can use Performance or IOrder and they will be up to date at that moment in time. As far as your exception goes. Please check your for-statement again. You can easily go in with a -1 index as the first run through on that statement.
Don't think it can be -1 index... this code doesn't execute unless there was a previous long order (which is now flat), so there *should* be something in AllTrades, so count should = 1.

I'll modify the code to use IOrder just in case.
heech is offline  
Reply With Quote
Old 03-05-2009, 10:24 AM   #5
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

heech,

You have no checks there to prevent you from running the for-loop when Performance.AllTrades.Count is 0. This is why you are getting the index out of range.
NinjaTrader_Josh is offline  
Reply With Quote
Old 03-05-2009, 10:29 AM   #6
heech
Senior Member
 
Join Date: Nov 2008
Posts: 576
Thanks: 0
Thanked 1 time in 1 post
Default

Quote:
Originally Posted by NinjaTrader_Josh View Post
heech,

You have no checks there to prevent you from running the for-loop when Performance.AllTrades.Count is 0. This is why you are getting the index out of range.
My point is there's no reason AllTrades.Count can be 0, because this code is only getting turned on if there was a prior long order (which is now going flat). So, that's why I asked about the sequence of Performance versus OnOrderUpdate.
heech is offline  
Reply With Quote
Old 03-05-2009, 10:38 AM   #7
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

Aside from .Count then please also reconsider your index++ logic. You should be going index--.
NinjaTrader_Josh is offline  
Reply With Quote
Old 03-05-2009, 10:41 AM   #8
heech
Senior Member
 
Join Date: Nov 2008
Posts: 576
Thanks: 0
Thanked 1 time in 1 post
Default

Quote:
Originally Posted by NinjaTrader_Josh View Post
Aside from .Count then please also reconsider your index++ logic. You should be going index--.
DUH. Haha, thanks.
heech is offline  
Reply With Quote
Old 03-05-2009, 11:51 AM   #9
mgbloomfield
Senior Member
 
Join Date: Dec 2006
Location: Seattle, Washington, USA
Posts: 150
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_Josh View Post
OnPositionUpdate() will be the last one in the sequence
Where in the Help guide can I find the full list of OnXXX events that are called and the sequence order they are called?

This would be great to know and understand.
mgbloomfield is offline  
Reply With Quote
Old 03-05-2009, 12:05 PM   #10
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

You can find the list in the Index page of the Help Guide. Just scroll down to the O section. Unfortunately there is no article about the sequence. Generally its OnOrderUpdate->OnExecution->OnPosition.
NinjaTrader_Josh is offline  
Reply With Quote
Old 03-17-2009, 12:44 PM   #11
heech
Senior Member
 
Join Date: Nov 2008
Posts: 576
Thanks: 0
Thanked 1 time in 1 post
Default

Quote:
Originally Posted by NinjaTrader_Josh View Post
heech,

OnPositionUpdate() will be the last one in the sequence meaning you can use Performance or IOrder and they will be up to date at that moment in time. As far as your exception goes. Please check your for-statement again. You can easily go in with a -1 index as the first run through on that statement.
Josh,

Can you tell me the sequence order between OnExecution, and either Performance or IOrder...? (Is Performance updated by the time OnExecution is called..?) I'm changing my logic around slightly again, and I find it necessary to deal with the above.

Thanks.
heech is offline  
Reply With Quote
Old 03-17-2009, 12:48 PM   #12
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

Yes it will be updated.
NinjaTrader_Josh is offline  
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is trade available within OnPositionUpdate? heech Strategy Development 2 12-04-2008 08:01 AM
OnPositionUpdate problem kekkis Strategy Development 5 11-21-2008 07:02 AM
Performance.AllTrades index ? michaelbb Strategy Development 1 06-17-2008 01:58 AM
Bug in Performance.AllTrades? trader_rick Automated Trading 4 05-16-2008 07:09 PM
OnPositionUpdate Questions OnePutt General Programming 1 11-30-2007 01:09 PM


All times are GMT -6. The time now is 07:16 AM.