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

Questions about MultiCharts .NET and user contributed studies.
mattbarsotti
Posts: 3
Joined: 14 Sep 2018
Has thanked: 1 time

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

Postby mattbarsotti » 01 May 2019

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!

User avatar
fbertram
Posts: 166
Joined: 16 Oct 2014
Location: Seattle, USA
Has thanked: 36 times
Been thanked: 76 times
Contact:

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

Postby fbertram » 06 May 2019

It seems easier to me to manually save some info about the number of shares per position while placing the opening orders.

Cheers!

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

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

Postby Henry MultiСharts » 09 May 2019

Hello mattbarsotti,

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

mattbarsotti
Posts: 3
Joined: 14 Sep 2018
Has thanked: 1 time

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

Postby mattbarsotti » 20 May 2019

Thanks Henry & fbertram, I appreciate both sets of feedback.


Return to “MultiCharts .NET”