{     
Function:       _LoadTradeArray

Description:    Loads profit/loss for all trades into an array.

                If CapitalBase is specified (non-zero), then it is added to the profit/loss

                This function should be called from strategy only once per bar.

                If the last trade of the stategy occurs on LastBarOnChart and this
                bar has not yet closed, this last trade will not be loaded into 
                TradeArray[NTrades].

                _LoadTradeArray is a simple function.  Therefore, when placed within a conditional loop
                in a strategy, it will not be called unless the loop conditions are satisfied.  Had it been
                programmed as a series function, then it would be called with each strategy pass regardless 
                of whether conditional loop conditions were satisfied.

Note:           PositionProfit, MaxPositionProfit, MaxPositionLoss all record only the most recent 10 trades.
                Therefore, they cannot be used to load TradeArray when LastBarOnChart is reached.

Author:         MarkSanDiego

Updated:        09/15/09    original version
                07/04/10    conversion from series to simple function.


Usage when called from strategy:

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


Note:           The BarStatus(1) = 2 condition is also retained within the function 
                because this condition is essential to ensure trades are not double 
                counted when "enable intrabar order generation and calculation" is
                being used.  Less experienced coders may inadvertantly omit this
                required condition when calling the function, as recommended in 
                the Usage example above.  
}


inputs:
    TradeArray[nn](NumericArrayRef);

vars:
    n(0),               { counter for number of trades }
    j(0),
    PriorTotalTrades(0),        
    NewTrades(0);

    { to avoid double counting of trades if "Enable intrabar order generation and calculation" }
    { is in use, only process trades when bar completes ( BarStatus(1) = 2) }
        
    if BarStatus(1) = 2 then begin                  
        

        { NewTrades will be > 1 if more than 1 trade is closed on same bar }
        NewTrades = TotalTrades - PriorTotalTrades;       
    
        if NewTrades > 0 then begin
        
            for j = 1 to NewTrades begin
                n = n + 1;
                TradeArray[n] = PositionProfit(j); 
            end;
         
            { store number of trades in unused element 0 of array }  
            TradeArray[0] = n;                      
                
        end;
    
        PriorTotalTrades = TotalTrades;
                
    end;

    _LoadTradeArray = 0;  


