Loading Strategy Trades Into an Array


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);


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;




PriorTotalTrades = TotalTrades;



The BarStatus(1) = 2 bracket 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);


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.



Initial 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:


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

The code may be visualized here:

LoadTradeArray Function



Bookmark and Share