Download software Tutorial videos
Subscription & data-feed pricing Class schedule


New account application Trading resources
Margin rates Stock & option commissions

Attention: Discussion forums are read-only for extended maintenance until further notice.
Welcome Guest, please sign in to participate in a discussion. Search | Active Topics |

Blockifying the Ulcer Index risk metric Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
kostas_gr
Posted : Friday, September 28, 2007 3:53:32 PM
Registered User
Joined: 2/26/2005
Posts: 115
Ulcer index is in many respects the best risk metric of all times. Can we get the help of a trainer to formulate it properly as BOTH
(1) a study and
(2) a condition (with <,>,xUp, xDwn capabilities)?!

I include three write-ups below that give precise instructions as to how to calculate the UI
Thanks!


1. http://en.wikipedia.org/wiki/Ulcer_Index
The index is based on a given past period of N days. Working from oldest to newest a highest price (highest closing price) seen so-far is maintained, and any close below that is a retracement, expressed as a percentage

For example if the high so far is $5.00 then a price of $4.50 is a retracement of -10%. The first R is always 0, there being no drawdown from a single price. The quadratic mean (or root mean square) of these values is taken, similar to a standard deviation calculation.

The squares mean it doesn't matter if the R values are expressed as positives or negatives, both come out as a positive Ulcer Index.
The calculation is relatively immune to the sampling rate used. It gives similar results when calculated on weekly prices as it does on daily prices. Martin advises against sampling less often than weekly though, since for instance with quarterly prices a fall and recovery could take place entirely within such a period and thereby not appear in the index.
2. http://www.tangotools.com/ui/ui.htm
The algorithm for computing UI is simple, and can be seen in the pseudo-code fragment below:
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)

3. http://www.tangotools.com/ui/UlcerIndex.xls for an good excel spreadsheet displaying the index’s mechanics
kostas_gr
Posted : Friday, September 28, 2007 4:07:10 PM
Registered User
Joined: 2/26/2005
Posts: 115
The text of the #1 reference I provided below (http://en.wikipedia.org/wiki/Ulcer_Index) does not display the math formula, which can be found at the linked page
Bruce_L
Posted : Tuesday, October 2, 2007 11:39:04 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
Please start by creating the following Code Block as Line and Int to Line (please keep in mind I'm not a programmer):

Ulcer Index:

Replace everything in the Code Block with (Firefox seems to work for copy and paste, IE does not):

<WBIGuid("19581e0c-f265-4b59-9fb5-235d896a343e"),FriendlyName("Ulcer Index")> _
Public Class Ulcer_Index
inherits BaseTemplateDLStoDLSPeriod
Public Overrides Sub calculate()
If InputCount >= ParameterValue
Dim SumSq As Single = 0
Dim MaxValue As Single = 0
For bar As Integer = ParameterValue-1 To InputCount-1
SumSq = 0
MaxValue = 0
For bar1 As Integer = bar-ParameterValue To bar
If InputValue(bar1) >= MaxValue Then
MaxValue = InputValue(bar1)
Else
SumSq = SumSq+(100*(InputValue(bar1)/MaxValue-1))^2
End If
Next
AddToOutput(InputDate(bar),SumSq^.5/ParameterValue)
Next
End If
End Sub
End Class

You may wish to review the following for help with creating Custom Code Blocks:

VB.Net (code block) to color down Monday red
VB.net exercise to access Quickedit from your codeblock
Hello World! Writing Code Blocks in Visual Basic .Net

Once you have created the Custom Code Block, you should be able to use the attached files in Blocks.

Please save the attached .pane file to:

My Documents\Blocks Files\Tool Parts\Chart

You can add it to any chart using the ADD STUDY button and then selecting Add Study | My Computer.

Please download the attached .scond files to:

\My Documents\Blocks Files\Tool Parts\Strategy Conditions

Once downloaded, you should be able to select Add Condition | My Computer when adding a Strategy Condition to access them.

You can use QuickEdit to adjust the settings or view the Block Diagrams.

Attachments:
Ulcer Index.pane - 5 KB, downloaded 553 time(s).
Ulcer Index.scond - 5 KB, downloaded 564 time(s).
Ulcer Index vs MA.scond - 7 KB, downloaded 505 time(s).
Ulcer Index vs Ulcer Index.scond - 6 KB, downloaded 535 time(s).



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
kostas_gr
Posted : Wednesday, October 3, 2007 1:09:58 PM
Registered User
Joined: 2/26/2005
Posts: 115
Hey Bruce, I have encountered two problems:
1.
I seemed to have created the code block that compiled well but when I try to add the UI study to my chart a window opens with the legends "Bar Chart.Ulcer Index.2" and next "Moving Average.5 Date_Number" and
with a myriad of choices listed below that presumably refers to all the charts I have open within my workspace. Which one am I suppose to choose? And at the bottom there are three buttons: Prev, Next, and Skip.

I chose the first Price History.2 and pressed Next only to see a line plotted that was lebeled "MovAvg50" and not Ulcer Index. Its block diagram has nothing but an expo ma of 50. What is wrong here?

2. I opened the block diagram of the Ulcer Index as a condition and its diagram seems broken in two disconnected pieces A and B:

Piece A goes from Strategy Time frame to Time Frame Switch (1 day) to Close 1 day (fed from Test Symbol) and then it goes nowhere...

Completely disconnected from the previous portion is piece B that has Bar Offset (0) to Less Than Value (1) to Recent True (1,1) to T/F Condition.

I surmise that between the two portions I needed to introduce the Ulcer Block so I connected the Close 1 day block of piece A to the Ulcer Block I found in the library, and then I connected the Ulcer block to Bar Offest (0) block of the piece B.

Is this right?!

Thanks!
Bruce_L
Posted : Wednesday, October 3, 2007 2:10:18 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
When I add the Ulcer Index to my charts it only requests one connection. This connection is the input to the Ulcer Index Block in the Ulcer Index Plot and should normally be Price History.

When you create the Ulcer Index Custom Code Block, did you replace everything in the Code Block with the code posted in this topic?

The Block Diagrams for Pane and Strategy Conditions that were attached will only work correctly if the Custom Code Block on your computer has the same WBIGuid as the Custom Code Block on my computer (which was used when the Block Diagrams were created).

You may want to re-create your Custom Code Block if the WBIGuids do not match. It will make things a lot easier.

The Ulcer Index Plot in the Ulcer Index Pane has the Price History Plot going into the Ulcer Index Block going into the Numeric Plot Block.

The MovAvg Plot in the Ulcer Index Pane has the Ulcer Index Plot going into the Moving Average Exponential Block going into the Numeric Plot Block.

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
kostas_gr
Posted : Wednesday, October 3, 2007 5:56:24 PM
Registered User
Joined: 2/26/2005
Posts: 115
Terrific, Bruce. I made the mistake after reading the manual of not copying your WBIGuid number and letting my computer create its own. I corrected that and all seems to work well. Two further issues, I would appreciate your help on:

1. On the same topic, how can now create a column (last value or minichart) from the Ulcer Index pane you designed? It would be great if I could have the minicharts and/or UI values in a column for all my watchlist items.

2. Finally, is there a way to measure a strategy's UI? I gues I can modify the UI block diagram so that the UI block feeds off the equity line for a given strategy? Is this right??

Thanks!
Bruce_L
Posted : Wednesday, October 3, 2007 9:36:38 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
You should be able to just left-click and drag the Ulcer Index to your Watchlist to create a Column (it worked for me).

You can choose any Plot when adding the Ulcer Index Pane (including the Strategy Equity Line).

You may wish to review the following:

NEW FEATURE Drag and Drop sorting with Mini-Charts

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
kostas_gr
Posted : Tuesday, October 16, 2007 2:07:25 PM
Registered User
Joined: 2/26/2005
Posts: 115
Hey Bruce, 

On the work you did on the Ulcer Index, I am trying to add the next layer--the Ulcer Index Performance Ratio. The formula is 

[Security Return (over a period) - Risk Free Rate (usually the 90 day treasury) ]/ Ulcer Index (over the given period)

Can you give it a try? I thought of using Rate of Change for the Return but I have no idea if we can link anything that updates a short-term treasury rate for the Risk Free.

Thx
Bruce_L
Posted : Thursday, October 18, 2007 1:39:37 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
Please save the attached .pane file to:

My Documents\Blocks Files\Tool Parts\Chart

You can add it to any chart using the ADD STUDY button and then selecting Add Study | My Computer.

You can use QuickEdit to adjust the settings or view the Block Diagrams.

I'm not sure if it is right or not. It uses the actual return over 13-Weeks. It is possible these values should be annualized instead.

Attachments:
Ulcer Index Performance Ratio.pane - 10 KB, downloaded 520 time(s).



-Bruce
Personal Criteria Formulas
TC2000 Support Articles
Bruce_L
Posted : Monday, August 18, 2008 5:26:34 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
The following should Plot the Ulcer Index as a RealCode Indicator:

'Ulcer Index
'# Period = UserInput.Integer = 10
Dim SumSq As Single = 0
Dim MaxValue As Single = 0
For bar1 As Integer = Period To 0 Step -1
    If Price.Last(bar1) >= MaxValue Then
        MaxValue = Price.Last(bar1)
    Else
        SumSq = SumSq+(100*(Price.Last(bar1)/MaxValue-1))^2
    End If
Next
If CurrentIndex >= Period-1 Then
    Plot = SumSq^.5/Period
Else
    Plot = Single.NaN
End If

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
Users browsing this topic
Guest-1

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.