Stability of Indicators
Author: thodder
Creation Date: 9/9/2009 3:23 PM
profile picture

thodder

#1
I found Cone's article on the WL4 site interesting (http://wl4.wealth-lab.com/cgi-bin/WealthLab.DLL/kbase?id=118). I was wondering if you were considering adding this to the WLP 5 Wiki site.

I converted the WL4 version to WLP 5.4. Since we don't have a data window in WLP 5.4 (that I know of), I used the debug window to spit out the last value for the indicator. This is helpful when comparing runs as the debug window holds the results through multiple runs. I did use an undocumented object, Renderer, so it may break in the future.

I was hoping to access all the indicators on the chart, but this only includes indicators that are plotted by the script. It will not pick up indicators added to the chart through the toolbar. Do you know if there is a way to access those indicators? I wanted to have a script that would print out the value by just adding indicators on the chart so users that didn't know how to code could test their favorite indicator before including it in a rule based script.
CODE:
Please log in to see this code.
profile picture

thodder

#2
The following might be safer code to print the indicator values as Bars.Cache is a documented object; however, I still do not pick up the indicators dropped on the chart.
CODE:
Please log in to see this code.
profile picture

Eugene

#3
QUOTE:
I was wondering if you were considering adding this to the WLP 5 Wiki site.

Please see the WealthScript Programming Guide, it's already included there - Indicators > Stability of Indicators.
QUOTE:
The following might be safer code to print the indicator values as Bars.Cache is a documented object;

You need to traverse through Bars.Cache.Keys - this is how Analysis Series picks up all the data series internally - including those drag-and-dropped on one's chart of a rule-based strategy.
profile picture

thodder

#4
I read the Programming Guide but I missed the Stability of Indicators section. Thanks for pointing it out.

QUOTE:
You need to traverse through Bars.Cache.Keys


Bars.Cache is a Dictionary according to intellisense, so Keys is a string and Values contains the DataSeries. On my second post I traverse through Bars.Cache.Values and I still do not get the DataSeries objects that may be dropped on the chart. Perhaps the chart indicators are added after my script runs so I won't be able to see them.

Thanks.

profile picture

Eugene

#5
Tim,

It should work because it's what I did when re-coding the tool for WL5. :)

First you need to loop through all dataseries names:
CODE:
Please log in to see this code.


Next, follow the QuickRef example for Bars.Cache:

1. Create a data series
2. If the key is found (Bars.Cache.ContainsKey), assign the result to the series just created (don't forget to cast it to DataSeries as per the example).
profile picture

thodder

#6
Eugene,

I tried what you suggested, but it still doesn't provide the indicators dropped on the chart. Here is what I have:
CODE:
Please log in to see this code.


If I run a test, I see the following:

RESULTS FOR DATA RANGE OF 21 BARS:
EMA(Close,20,Modern): 12.5005714285714
MACD(Close): -0.104579016959468
RSI(Close,20): 50.8090614886731
CCI(20): 58.8463781885043
SMA(Close,20): 12.473

That is correct. However, if I go to the chart and add 'ADX' and rerun the script, I still see the same results -- no ADX in the list.

I can get the same results as above with the code segment:

CODE:
Please log in to see this code.


Perhaps I missed something.
profile picture

Eugene

#7
Well, you're right: the Bars.Cache.Keys.Count is 5 even after dragging and dropping ADX on the chart. However, if you look on the Analysis Series tab, you'll notice that the series dropdown list has an ADX entry for the manually added indicator.

Since this is neither a use case nor a supported method, I think there is a very good reason why the developers made it work for Visualizers and not work in Strategies.
profile picture

Cone

#8
Strategies run and execute before drag and drop indicators are added to the chart, so that's why the cache isn't updated at that point in time. The architect determined that Strategies would never rely on dragged indicators.
profile picture

thodder

#9
Cone confirmed what I suspected after all my tries to view the indicator values.
QUOTE:
9/9/2009 4:35 PM Post: "Perhaps the chart indicators are added after my script runs so I won't be able to see them."


No problem. Thanks for all your help. I can work with this.
profile picture

thodder

#10
I developed this script to be used in debug mode (i.e. using PrintDebug) to review an entire library for stable indicators. I thought this would be helpful for reviewing the Community.Indicators library as it changes fairly frequently. I developed it for my own use, but I thought I'd post it here in case it is helpful to others.

This script will call the "Series" method of each indicator in the library using the default values specified in its IndicatorHelper class. The script needs to run twice with different range settings: 1) Build first set of results; 2) Build second set of results and generate a report in the debug window of the comparison of the results. The comparison goes on the assumption that the last bar should have the same value no matter what the range is. If the last bar value is not the same, the script will flag the indicator as Stable = FALSE.

There were a lot of Stable = FALSE on my first test with the values differing by an extremely small amount. I determined that I wanted to set a tolerance so values that were off by a very small amount would not be flagged as FALSE. The parameter "Tolerance Exp" can be used to adjust this amount. A -4 will set a tolerance value of 0.0001 -- which will be treated as 0.01% difference. If the difference between two backtests with different ranges is less the 0.0001 then the values will be considered equal. A tolerance of -5 will be 0.00001 (smaller difference allowed) which may result in more Stable = FALSE.

Obviously some indicators will always fail this test, but could still be stable. Any indicator that returns a bar value will almost always fail this test as the bar index will change for each range.

Sometimes the default period for an indicator is large. This may cause an error running the Series method with a period larger than the data range specified for the test. An error will cause the indicator to be dropped from the comparison. I've found that I need at least 60 bars for "WealthLab.Indicators" and 255 bars for "Community.Indicators" or some indicators will be dropped.

The report in the debug window is difficult to read. It is designed to copy all the contents and paste into a spreadsheet (I use Excel). This makes it easier to read the results and filter it as you wish.

CODE:
Please log in to see this code.


For my test, the script is set to scan the "WealthLab.Indicators" library for review. My test was done with 60 and 70 bars.

There were a few indicators that appeared to be unstable which were not specified in help: AccumDist, ADXR, KST, Parabolic. Should these be considered unstable or are there reasons for the difference?
profile picture

Eugene

#11
Hi Tim,

What a great effort. Thank you.

btw, Community.Indicators is somewhat different from WealthLab.Indicators. Unlike WealthLab.Indicators which considers the indicator's [largest] lookback period as its first valid value, the FirstValidValue of an unstable indicator in Community.Indicators is typically set at three times its [largest] lookback period.

A couple of remarks.

Re: IsDoubleEqual - this (or a similar) function is already a member of Community.Components: AlmostEquals
Re: copying to Excel - Community.Components has a static method that will make copying to clipboard and pasting in Excel a breeze
profile picture

thodder

#12
Hi Eugene,

I appreciate your good words.

Do you know the answer to my question below?

QUOTE:
There were a few indicators that appeared to be unstable which were not specified in help: AccumDist, ADXR, KST, Parabolic. Should these be considered unstable or are there reasons for the difference?


I can log this in support if needed.
profile picture

Eugene

#13
KST uses EMA so it's unstable by definition.
ADXR is a derivative of ADX - known as unstable indicator - for its use of WilderMA

I think it makes sense to add them to the list. Thanks for the heads-up.

AccumDist, Parabolic - no idea; however, the latter has again a Wilder connection so...
profile picture

Cone

#14
AccumDist is unstable for a different reason - accumulation is simply based on some starting point.

KST wasn't a Standard Indicator in v4, so that's what it wasn't in the list. But it uses EMAs.

ADXR wasn't a standard indicator when the article was written.

Parabolic certainly qualifies on both counts - initialization at a specific starting point as well as progressive calculations.


I can add these to the WL5 Programming Guide topic. My comments above refer to the KB article at wl4.com. Thanks.
profile picture

thodder

#15
Hi Eugene,

I reviewed Community.Components.AlmostEquals. It is not the same is my IsDoubleEquals.

Method IsDoubleEquals in my script looks at a difference as a percent whereas AlmostEquals is just looking at the difference between the two values.

CODE:
Please log in to see this code.


Simplified version of my IsDoubleEqual would be:

CODE:
Please log in to see this code.


I need the "if (d1 == d2)" to remove any potential zero denominator situations.

Tim
profile picture

Eugene

#16
Hi Tim,

I think the script would be an excellent addition to WL 5.5 if you decide to upload it on the site > Published Strategies
profile picture

thodder

#17
Strategy is uploaded.
profile picture

Eugene

#18
Thank you.
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).