Search code examples
mathchartstradingtechnical-indicator

How to program a convoluted Indicator calculations, that requires the previous period value in its calculation?


If we take the Accumulation/Distribution indicator for example.

Investopedia lists the steps as:

  1. Money Flow Multiplier = [(close - low) - (high - close)] /(high - low)

  2. Money Flow Volume = Money Flow Multiplier x volume for the period

  3. Accumulation/Distribution= previous Accumulation/Distribution + current period's Money Flow

The third step is what confuses me, how can I calculate A/D when I don't have the previous A/D, which requires the previous A/D and so on...

There are other indicators that are similar where the indicator requires the indicator itself to be calculated. So how is it done?


Solution

  • There is a common software approach to this:

    Your case has a trivial convolution, with a depth of just 1. There are some deeper convolution indicators, going in this very same principle just a bit deeper.

    Solution:

    find a so called "neutral" value, that the calculus is dependent on and inject it in the "missing" time-steps.

    • for a PriceDOMAIN Indicator, like a moving average, this can be a Close price of the first available bars, that gets replicated "period"-times back
    • for multiplicative and exponentiating Indicator, that can be 1
    • for additive Indicator, that can be 0

    All the toys get aligned to fair values within the next "period" time-steps if the neutral values were algebraically correct, or in a little bit longer time, when substituted values were "just approximations". Both the former and the latter case, is typically well back in time to skew the future indicator values and to somehow bother the actual flow of current decisions, based on the recent indicator outputs.