{
Function:           _SystemQualityNr

Description:        Van K. Tharp's System Quality Number calculation, used to evaluate trading
                    performance.

                    Reference: Van K. Tharp: Definitive Guide To Position Sizing (August 2008)

Author:             MarkSanDiego

Updated:            09/14/09    original version
                    09/17/09    original version inadvertantly saved as Series Function.  Recompiled
                                as Simple Function now.  DoCalc variable eliminated. 


Usage when called from strategy:
    
    arrays:
        TradeArray[3000](0);            { TradeArray may be loaded with function _LoadTradeArray }


    if LastBarOnChart then begin
    
        value1 = _SystemQualityNr(TradeArray)

    end;            
}


inputs:
    TradeArray[nn](NumericArrayRef);    { array holding profit/loss for all trades }

var: 
    NTrades(0),                         { number of trades in TradeArray }
    p(0),                               { position profit }
    Sum(0),                             { sum of profits }
    SumSq(0),                           { sum of square of profits }
    NWins(0),                           { number of winning trades }
    MeanWin(0),                         { average profit of winning trades }
    MeanSqWin(0),                       { average profit of the square of winning trades }
    StdDevWin(0),                       { standard deviation of winning trade profits }
    
    j(0),                               { miscellaneous looping counter }
    k(0),                               { miscellaneous looping counter }
    x(0);                               { temporary result holding variable }
  
        

    NTrades = TradeArray[0];            { zero element of TradeArray used to store number of trades in array }

    for j = 1 to NTrades begin
        p = TradeArray[j];
        if p > 0 then begin
            NWins = NWins + 1;
            Sum = Sum + p; 
            SumSq = SumSq + p * p;
        end;
    end;
            
        if NWins >= 2 then begin
            MeanWin = Sum / NWins;      { minimum of 2 trades needed to calculate standard deviation }
            MeanSqWin = SumSq / NWins;
            x = MeanSqWin - MeanWin * MeanWin;
        end;

        if x > 0 then begin
            StdDevWin = SquareRoot(x);
            _SystemQualityNr = SquareRoot(NTrades) * MeanWin / StdDevWin;
        end else 
            _SystemQualityNr = 0;
    








