The Ulcer Index is an analytical tool that I would like to use in evaluating trading strategies. I have tried to make a formula file to calculate it and use Exploration output to compare results with the Ulcer Index metric that Amibroker provides with its standard backtest report.
The basic formula from the original developer is:
SumSq = 0
MaxValue = 0
for T = 1 to NumOfPeriods do
if
Value[T] > MaxValue then MaxValue = Value[T]
else
SumSq = SumSq + sqr(100 * ((Value[T] / MaxValue) - 1))
UI = sqrt(SumSq / NumOfPeriods)
Or in common language:
Percent Drawdown = ((Close - NumPeriods Max Close) / Max Close) X 100
Squared Average = (NumPeriods Sum of Percent Drawdown Squared / NumPeriods
Ulcer Index = Square Root of Squared Average
My formula attempt to make the UI calculation is:
GraphLabelDecimals = 2;
SetChartOptions( 0, chartShowDates | chartWrapTitle );
uiPer = Param( "UI Periods", 55, 34, 233 );
//---------------------------------------------------------
// Detect Watchlist
//---------------------------------------------------------
wlnumber = GetOption( "FilterIncludeWatchlist" );
watchlist = GetCategorySymbols( categoryWatchlist, wlnumber );
wlist = CategoryGetName( categoryWatchlist, wlnumber );
//---------------------------------------------------------
// Calculate UI
//---------------------------------------------------------
xPeak = HHV( Close, uiPer );
maxEq = IIf( Close > Ref( xPeak, -1 ), Close, xPeak );
cDD = ( HHV( C, uiPer ) - C ) / HHV( C, uiPer ) * 100; //current drawdown
maxDD = HHV( cDD, uiPer );
sqrDD = cDD ^ 2;
sumDD = Sum( sqrDD, uiPer );
UI = sqrt( sumDD / uiPer );
Plot( UI, "", colorYellow );
//---------------------------------------------------------
// Title Information
//---------------------------------------------------------
Title = Name() + " (" + _SECTION_NAME() + ")" +
EncodeColor( colorDarkYellow ) + " UI Lookback " + UIper +
EncodeColor( colorPaleBlue ) + " - " + Prec( UI, 5 );
//---------------------------------------------------------
// Specify Columns for Exploration
//---------------------------------------------------------
Filter = 1;
SetSortColumns( 2 );
SetOption( "NoDefaultColumns", True );
AddTextColumn( Name(), " " + UIper + " Day", 1.0, -1, -1, 55 );
AddColumn( DateTime(), "Lookback ",
formatDateTime, colorDefault, colorDefault, 110 );
AddColumn( UI, "UI", 1.2, colorCustom1, colorDefault, 50 );type or paste code here
The Exploration output of this formula does not match the Amibroker calculation, but strangely, when the formula is plotted on a chart, the UI value matches.
There is an error in my formula, but after hours of trying, I have not been able to determine what the problem is. I welcome any suggestions from more experienced programmers that would identify necessary corrections so the formula will properly calculate the UI.
Russell