Do we need two forms for each TA function?

There are 2 forms could be defined for each TA function, such as:

1. Var emaVar(Var var, Opt period)
2. float ema(int idx, Var var, Opt period)

The first one will return a series of variable which have been calculated;
The second one just return one value in the series that is exactly at point idx.

Now, let's apply the two forms to the MACD indicator:

public class MACDIndicator extends AbstractContIndicator {

    Opt periodS = new DefaultOpt("Period Short", 12.0);
    Opt periodL = new DefaultOpt("Period Long",  26.0);
    Opt periodD = new DefaultOpt("Period Diff",  9.0 );
    
    Var

In the MACD example, there came out calculating dependency order issue if we apply 1st form function no properly. That is, the 'dea' actually should be calculated after the 'dif'. And it's difficult to insert the emaVar() into the proper place (should be in the 'for' clause, exactly after the point 'i' of 'dif' is calculated), because emaVar() is not aware of 'i'

To avoid these confusion, we should only provide the second function:

float ema(int idx, Var var, Opt period)
and force the developer to write the code as:

public class MACDIndicator extends AbstractContIndicator {
    
    Opt periodS = new DefaultOpt("Period Short", 12.0);
    Opt periodL = new DefaultOpt("Period Long",  26.0);
    Opt periodD = new DefaultOpt("Period Diff",  9.0 );
    
    Var

This version makes the calculating dependency order clearly, no more confusion.

Comments

1. Matt Gundersen -- 2006-05-05 16:00

Consider that if this is going to be used in realtime, the above is not performance friendly. If you plan on intraday traders using this, you'll want the algorithm(s) to be a faster and smarter so they don't have to recompute everytime a tick occurs. There can be numerous ticks per second.

If someone puts a 12/26/9 MACD on the chart and one tick comes in, does that mean you'll have to rebild the 12 and 26 MA for the most recent tick, then rebuild the 9 smoother?

Or is it smarter than that to only rebuild the minimal amount necessary?

You should review TA-Lib and see how they do some of this stuff. Some of the C functions are smart about this...

2. Caoyuan -- 2006-05-05 16:00

The current design of indicator will smartly compute from the minimal amount. And I've finished the design of funtions that may be called simultaneously by different indicators to compute smartly too.

3. Caoyuan -- 2006-05-05 16:00

That is, all the intermediate status of each function will be remembered. And the indicator is aware of the time that should re-compute from, then call the functions to compute the necessary part only.

Add New Comment