Page 1 of 1

Tying Order ID to Open Positions when Scaling In/Out of Trades

Posted: May 01 2019
by mattbarsotti
Hi,

I am working with scaling in and out of positions and found excellent prior guidance in this post:
viewtopic.php?t=45503

I have recreated the basic functionality of this post. While the example is merely notional, in practice the various entry trades would probably be linked to different criteria (and will in my implementation).

As such, I am interested in evaluating the currently open positions when some scaling out has occurred already to determine which orders they originated from. My basic scenario would be that the overall strategy has scaled in by three positions, and then scaled out of one of those positions. What is the easiest method to interrogate the remaining open market positions to determine which of the three trades they originated with?

StrategyInfo.MarketPosition indicates the net market position (+2 in this case), but it does not say which of the three entry orders is connected to that position and which has been closed.

I have had some luck by looping over all open positions and looking at the name field:

int pos_count = Positions[0].OpenTrades.Count;
for (int i = 0; i < pos_count; i++)
{
Output.WriteLine("Position Number: {0}, Position Name: {1}, Position Size: {2}", i, Positions[0].OpenTrades.EntryOrder.Name, Positions[0].OpenTrades.EntryOrder.Contracts);
}

However, it seems cumbersome to look at open positions to see if one matches the name of a particular entry order. I note that Orders have and ID field, but it isn't clear if this ID is ever passed off to the Position objects. That may be ideal: track a position starting from the Order ID, link it to a Position ID, etc. I'm certain there must be a simple way to tie presently open (or closed) positions (reflected in StrategyInfo.MarketPosition) to the particular order types from which they originated.

Thanks!

Re: Tying Order ID to Open Positions when Scaling In/Out of Trades

Posted: May 06 2019
by fbertram
It seems easier to me to manually save some info about the number of shares per position while placing the opening orders.

Cheers!

Re: Tying Order ID to Open Positions when Scaling In/Out of Trades

Posted: May 09 2019
by Henry MultiŠ”harts
Hello mattbarsotti,

You may find useful the code sample provided in this thread:
viewtopic.php?f=19&t=45335

Re: Tying Order ID to Open Positions when Scaling In/Out of Trades

Posted: May 20 2019
by mattbarsotti
Thanks Henry & fbertram, I appreciate both sets of feedback.