Trades vs Orders With PosTrade Keywords
Each of the Strategy Position Trades keywords refer to trades and not to orders. This article highlights the difference between orders and trades.
Trades versus orders
Since the Strategy Position Trades keywords refer to trades, and not orders, one order can open two different trades.
For example, if you open a position with a buy order for 2 contracts, and close the position with two orders, both with a different name, there are for the entry into the position two trades (but not two entries), that were opened by one order (buy 2 contracts) and closed by two different orders.
So, the PosTradeEntryName() will in that case return values for two trades, both with the same entry name, though you entered the position with one order. In other words, the multiple exit orders will each get matched with an entry order.
Trade example
For example:
In this position there is an entry named “ES MKT” for 1,000 forex units, and two separate exits (an “XS MKT1” for 500 units and an “XS MKT2” for the remaining 500 units). Now, note that this position is opened with one entry order and closed with two exit orders. However, since trades are something different than orders, the position pictured above consists of 2 entry trades and 2 exit trades.
This can be shown by calling the Strategy Position Trades keywords for this position by:
Variables: MP(0), posAgo(0), numOfTrades(0);
MP = MarketPosition;
if MP[0] <> MP[1] and MP[1] <> 0 then begin
Print(Date:0:0, " - ", time_s:0:0, " Position closed:");
pasAgo = 1; // get the data for the latest position
numOfTrades = PosTradeCount(posAgo); // get the number of trades for that position
// Loop through the entry orders
for value1 = 0 to (numOfTrades - 1) begin
Print(Spaces(3), "EntryOrder: ", PosTradeEntryName(posAgo, value1),
" units: ", NumToStr(PosTradeSize(posAgo, value1), 0),
" @ ", NumToStr(PosTradeEntryPrice(posAgo, value1), 5),
" PosTradeCount: ", PosTradeCount(posAgo));
end;
// Loop through the exit trades
for value5 = 0 to (numOfTrades - 1) begin
Print(Spaces(3), "ExitOrder: ", PosTradeExitName(posAgo, value5),
" units: ", NumToStr(PosTradeSize(posAgo, value5), 0),
" @ ", NumToStr(PosTradeExitPrice(posAgo, value5), 5),
" PosTradeCount: ", PosTradeCount(posAgo));
end;
end
Which returns the following.
1110720 - 114600 Position closed:
EntryOrder: ES MKT units: 500 @ 1.41798 PosTradeCount: 2.00
EntryOrder: ES MKT units: 500 @ 1.41798 PosTradeCount: 2.00
ExitOrder: XS MKT1 units: 500 @ 1.42037 PosTradeCount: 2.00
ExitOrder: XS MKT2 units: 500 @ 1.42043 PosTradeCount: 2.00
Here you can see that there are two different exit orders in two trades (one @ 1.42037 and one @ 1.42043), and there are two entry trades but one entry order.