Function LoadTradeArray accomplishes the task of loading strategy trades into an array for subsequent processing, such as the calculation of optimization objectives like Tharp’s System Quality Number or annualized Expectancy.

Simple code like the following, however, is NOT adequate to accurately capture all strategy trades:

TotTrades = TotalTrades;
if TotTrades <> TotTrades[1] then
       n = n + 1;
TradeArray[n] = PositionProfit(1);
end;

The reason why is that TotalTrades may increment by more than 1 per bar in the situation that 2 separate trades close on the same bar. This could happen, for example, if a prior trade is reversed on a bar and then that new reversal trade is closed before the bar ends. In this case, TotalTrades will have increased by 2, rather than 1.

Therefore, additional logic is needed to ensure all trades are captures into the array. The following code may be used:

if BarStatus(1) = 2 then begin

{ NewTrades will be > 1 if more than 1 trade is closed on same bar }
NewTrades = TotalTrades – PriorTotalTrades;
for j = 1 to NewTrades begin
          n = n + 1;
          TradeArray[n] = PositionProfit(j);

           { store number of trades in unused element 0 of array }
           TradeArray[0] = n;
 end;
 PriorTotalTrades = TotalTrades;

 end;

The BarStatus(1) = 2 brackets within this function is needed to avoid double counting of trades if “enable intrabar order generation and calculation” is in use.

A better practice would be to bracket the call to the function with the BarStatus(1) = 2 requirement in the strategy code to reduce the frequency of calls to this function:

if BarStatus(1) = 2 then begin
       value1 = LoadTradeArray(TradeArray);
end;

If the function is properly called as in the above example, the BarStatus(1) = 2 condition repeated in the function is redundant. However, it is nevertheless being retained in the function because it is an essential condition to avoid double counting of trades when “enable intrabar order generation and calculation” is used, and less experienced coders may inadvertently omit bracketing calls to this function with the required BarStatus(1) = 2 condition.

Downloads

 Initially posted version:  09/16/09

Latest Update:  07/04/10

*.ELD files are compiled for TS 8.6

All ELD and code text files packaged here:

LoadTradeArrray.zip

Users of earlier versions fo TradeStation may compile the code
from the text files included in the above *.zip file.

The code may be visualized here:

LoadTradeArray Function