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 |

Exponential moving averages of formulas in pcfs Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
gsn
Posted : Thursday, March 17, 2005 3:04:41 PM
Registered User
Joined: 11/13/2004
Posts: 121
I have a custom indicator:

V*(C-C1)

that I display in a centered zero line format with an exponential moving average of 3.

How would I write that as a PCF to get the 3-day xavg number of
V*(C-C1) so I can see if it is above zero or under zero?
j2d2
Posted : Sunday, March 20, 2005 5:17:41 AM

Registered User
Joined: 1/14/2005
Posts: 21
Hi qsn: Why don't you just sortby with the custom indicator sort? Then you can scroll down until you come to the dividing line between the positive and negative charts? The custom sorts seem to run a lot faster now with the latest verson of TeleChart. Just a friendly suggestion.
Regards,
j2d2
Craig_S
Posted : Sunday, March 20, 2005 7:23:16 AM


Worden Trainer

Joined: 10/1/2004
Posts: 18,819
Visual sorting will be your best bet. Right-click on your indicator and choose SORT BY. When it finishes your WatchList will be sorted by your smoothed indicator. Values above 50 (50% of the window) are above 0 and values below 50 are less than 0. Here is a video to check out.

Sort by Visual Value to find new highs and lows in price and indicators

- Craig
Here to Help!
dhoward
Posted : Sunday, March 20, 2005 12:38:14 PM
Registered User
Joined: 1/2/2005
Posts: 132
Even though our best bet would be to perform a visual sort, is a PCF as requested above still out of the question??? I'd like to see it's code too if possible.

Thanks!
Craig_S
Posted : Tuesday, March 22, 2005 3:40:23 PM


Worden Trainer

Joined: 10/1/2004
Posts: 18,819
I personally do not know a way to code this function as a smoothed exponential average.

- Craig
Here to Help!
bustermu
Posted : Wednesday, March 23, 2005 7:42:04 AM
Registered User
Joined: 1/1/2005
Posts: 2,645
A PCF for EMA(V*(C-C1),3) is:

0.5
*(V0*(C0-C1)+0.5
*(V1*(C1-C2)+0.5
*(V2*(C2-C3)+0.5
*(V3*(C3-C4)+0.5
*(V4*(C4-C5)+0.5
*(V5*(C5-C6)+0.5
*(V6*(C6-C7)+0.5
*(V7*(C7-C8)+0.5
*(V8*(C8-C9)+0.5
*(V9*(C9-C10)+0.5
*(V10*(C10-C11)+0.5
*(V11*(C11-C12)+0.5
*(V12*(C12-C13)+0.5
*(V13*(C13-C14)+0.5
*(V14*(C14-C15)+0.5
*(2*V15*(C15-C16)))))))))))))))))

Always try to stack parentheses on the right. If you have too few, you will be told; if you have too many, they will be ignored.

I strongly suggest that you place the PCF in the Top Window as a Custom Indicator. Also, place:

V*(C-C1)

in the Top Window as a Custom Indicator with an Exponential Moving Average of period 3 applied. Double punch the "." and read the values of the two. They must agree (almost).

Thanks,
Jim Murphy

Tanstaafl
Posted : Wednesday, March 23, 2005 9:01:46 AM
Registered User
Joined: 10/7/2004
Posts: 799
Location: Duluth, GA
Hi, QSN:

Here is another version of the solution for you. It is accurate to within 0.01%, and will calculate faster than a comparably-accurate nested-paren model, since TC does not have to deal with all the paren's:

.001*( 500.0305*(V0*(C0-C1)) +250.0153*(V1*(C1-C2)) +125.0076*(V2*(C2-C3)) +62.5038*(V3*(C3-C4)) +31.2519*(V4*(C4-C5)) +15.626*(V5*(C5-C6)) +7.813*(V6*(C6-C7)) +3.9065*(V7*(C7-C8)) +1.9532*(V8*(C8-C9)) +.9766*(V9*(C9-C10)) +.4883*(V10*(C10-C11)) +.2442*(V11*(C11-C12)) +.1221*(V12*(C12-C13)) +.061*(V13*(C13-C14)) )

Jim Dean

dhoward
Posted : Wednesday, March 23, 2005 9:48:13 AM
Registered User
Joined: 1/2/2005
Posts: 132
Perfect!! Thanks a lot to Jim Dean and Jim Murphy for their great PCF submissions. You've made my day.
bustermu
Posted : Wednesday, March 23, 2005 11:17:26 AM
Registered User
Joined: 1/1/2005
Posts: 2,645
QUOTE (Tanstaafl)
It is accurate to within 0.01%, ...


Jim D.,

To within 0.01% of what? That is not meaningful for an oscillator. Anyway, the accuracy is beyond reproach for both.

I notice you initialized with zero which is better than me initializing with the next term, V15*(C15-C16), for an oscillator.

Thanks,
Jim Murphy
Tanstaafl
Posted : Wednesday, March 23, 2005 11:27:52 AM
Registered User
Joined: 10/7/2004
Posts: 799
Location: Duluth, GA
The 0.01% error is versus that of a fully randomized infinite series. The coefficients I use have been adjusted for the effect of truncation as well.

And, btw ... those calc's are "actually" a bit MORE precise than the truncation method that TC uses internally ... but under normal circumstances the error in their calcs is WAY below the horizon ... you need to use BRKA or DJ-30 to see the significant figure differences show up.

I've found that 0.01% is *essential* for MACDH logic, but actually 0.1% is usually OK for most EMA applications (even tho it might be a penny off here and there on higher-priced stocks). For most bounded oscillator applications, 0.5% is usually adequate.

All done automatically in Excel, of course. I just type in the error cutoff I want.

Jim Dean

Tanstaafl
Posted : Wednesday, March 23, 2005 11:29:55 AM
Registered User
Joined: 10/7/2004
Posts: 799
Location: Duluth, GA
Further clarification ... the %error I mention has nothing to do with the accuracy of the root datum, be it a price or an oscillator or whatever ... it is simply a statement of the accuracy of the series as a model of a the "true" infinite-EMA calc.

bustermu
Posted : Wednesday, March 23, 2005 5:02:05 PM
Registered User
Joined: 1/1/2005
Posts: 2,645
QUOTE (Tanstaafl)
The 0.01% error is versus that of a fully randomized infinite series.


Jim D.,

The 0.01% error must be 0.01% of something. Let's see if I understand. By "fully randomized infinite series", I am going to take to mean a White Noise sequence with variance vin^2 and, consequently, standard deviation vin. Now suppose the same White Noise is the input to a perfect EMA3 with output y and also the input to the filter you presented with output z. The error is then e = z-y. Suppose the variance of the error is verr^2. Then, its standard deviation is verr. By 0.01% error, I am under the impression that you mean that the error standard deviation is 0.01% of the input standard deviation, i.e.,

verr = 0.0001*vin.

Thus, I have concluded that the standard deviation of the error is 0.01% of the standard deviation of the input (when the input is White Noise). Did I guess correctly?

Now let's see if I have guessed correctly on your filter structure. If P is the Period of the EMA, define a to be

a = (P-1)/(P+1)

then your filter coefficients are

a('k') = c*a^k, k = 0,1,2,..., K-1
a('k') = 0, k = K,K+1,K+2,...

The constant c is chosen so that the sum of the coefficients is unity. Am I correct before roundoff? The K is chosen to give you the desired percent error.

If I understand your filter correctly and your percent error correctly, I just calculated the percent error to be only 0.005%. That does not include roundoff of the filter coefficients. Does that agree with any of your work. I did the calculation fast. If I find an error, I will post an errata.

I have never seen this before, but it makes perfectly good sense and it is quite simple and easy to do analytically. More importantly, it stops the weighting of the last term from being greater than that of the previous term. My hat is now off to you. Congradulations.

Notice that the PCF I gave will agree with your filter structure if we zero the initialization term, V*(C15-C16), and adjust the 0.5 factor out front.

Thanks,
Jim Murphy

PS I wrote a('k') because if I leave the apostrophes out, I get a. Would you believe that?
bustermu
Posted : Wednesday, March 23, 2005 6:55:17 PM
Registered User
Joined: 1/1/2005
Posts: 2,645
Suppose we wish to use Jim Dean's approximation to an EMA of period P. Let K be the number of terms used in the approximation. Define:

a = (P-1)/(P+1)

c = (1-a)/(1-a^K)

The filter PCF on a data sequence X can be written:

c
*(X0+a
*(X1+a
*(X2+a
*(X3+a
...
*(X(K-2)+a
*(X(K-1))))))...)

The percent error as described above is:

%error = 100*(a^K)*SQR(2*(1-a)/((1+a)*(1-a^K)))

For P = 3 and K = 14,

a = 0.5

c = 0.500030519

%error = 0.00498

approximately.

Thanks,
Jim Murphy







Tanstaafl
Posted : Wednesday, March 23, 2005 11:54:52 PM
Registered User
Joined: 10/7/2004
Posts: 799
Location: Duluth, GA
Hi, Jim:

As usual, your precise and formal way of evaluating and describing the math behind these things contains some stuff that I only follow in a general fashion.

My procedure is pretty simple actually. First, re the fully randomized thing, I mentioned that only to indicate that the error calc I use is independent of the data series values.

Here's how it works ... I calc all the actual ema coef's in a series that is about 10x the periods ... much longer than I really need. Then I do a running sum (most recent to oldest) in a parallel column of the coeff's. I chop the series off at the bar for which the difference between the running sum and unity is represents about half the percent-error cutoff that I've specified in my input param's. That becomes the last bar of the series.

Then, as you surmised, I simply multiply all the coef's for the selected bars by 1/(sum of truncated series coefs), thus resulting in a collection of coeff's that equals unity.

I don't have any references or theorems to prove all this ... it just seems a natural and correct way to do it. I did, once upon a time, do some REALLY MASSIVE checks against actual data series with in excess of 20,000 sequential bars ... which led me ultimately to this simple approach. You might remember some of my long-ago postings of that adventure on the Yahoo TC2000AuxiliaryUsers board.

Anyhoo, the method works quite nicely. I have applied it to nested ema's, macd's, histo's, ema's of histo's, wilder series, etc ... all with excellent results, and a fast calculation since paren nesting is minimized. Even tho W has provided the XAvg func, it is still a valuable tool since complex expressions cannot be used in XAvg.

Re the precise accuracy calc related to the series I posted above, I stated it is <0.01% since that was my limiting input. After actual truncation and adjustments, the spreadsheet tells me that the true error limit is 0.00610%

Jim Dean

bustermu
Posted : Thursday, March 24, 2005 7:45:11 AM
Registered User
Joined: 1/1/2005
Posts: 2,645
Jim D.,

You seem to have used a computer to sum the first K coefficients of an EMA of period P. Define:

a = (P-1)/(P+1)

The coefficients of the EMA are:

a('k') = (1-a)*a^k, k=0,1,2,...

The sum of the first K coefficients is:

Sum(a('k'),0,K-1) = 1-a^K

I would think it would be easier and lead to a better understanding to use the right-hand side than to use a computer to calculate the left-hand side.

You still should relate how the output of your filter compares to the output of the EMA. I have done that for the case of a White Noise input. No matter how you compare the outputs, you are safe if you get a^K small enough. Anyway, if we use enough terms, say 5*P, the error is so small it does not matter. The 5*P turns out to be safe because:

a^(5*P) < EXP(-5*2) = 0.0000454

approximately.

Thanks,
Jim Murphy

Tanstaafl
Posted : Thursday, March 24, 2005 7:56:43 AM
Registered User
Joined: 10/7/2004
Posts: 799
Location: Duluth, GA
The computer does a WHOLE lot more than just a paltry coeff calc. The excel file in all its glory has grown to be 37 MEG in size. It's primary purpose is to automatically BUILD PCF's ... an ancilliary aspect of that is to calculate some EMA coeff's. The EMA calc is done in a way that fits the PCF-string generation process.

It works just fine. It ain't broke. AFAIK, there ain't nuthin else like it out there.

Nuff said.

Jim

Craig_S
Posted : Thursday, March 24, 2005 3:13:40 PM


Worden Trainer

Joined: 10/1/2004
Posts: 18,819
Tanstaafl,

StockGuy and Doug R. shared the emails you sent. We appreciate the kind words. Thanks for taking the time to write and send them.

- Craig
Here to Help!
Tanstaafl
Posted : Thursday, March 24, 2005 3:17:06 PM
Registered User
Joined: 10/7/2004
Posts: 799
Location: Duluth, GA
Y'all deserve it! You are doing a great job, during a particularly stressful time. Thanks very much ... I am sure that many people feel that way!

Jim Dean

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.