Hướng dẫn moving average in php

There seems to be a error in sma calculation in the code below... can someone point out where..

/** * Simple Moving Average (sma) * *

A Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average, a mathematical analysis of the security's average value over a predetermined time period is made. As the security's price changes,its average price moves up or down.

* *

A simple, or arithmetic, moving average is calculated by adding the closing price of the security for a number of time periods (e.g., 12 days) and then dividing this total by the number of time periods. The result is the average price of the security over the time period. Simple moving averages give equal weight to each daily price.

* *

Formula:

* * - SUM(Closes of n periods)/n */

sma[ $i + $range - 1 ] = $temp_sum / $range;
        $i++;
    }
    return $this->sma;
}
}

$mysma = new sma();
$mysma->get($data,5); $sma = $mysma->get();
echo mysma;

?>

Also sma calculation in other code seems to be easier.. a few example is here.. if someone has done it in php similarly..??

(defn moving-average
[coll n]
(cond
(< n 1) nil
(= n 1) coll
:else   (let [sums (reductions + 0 coll)]
          (map #(/ (- %1 %2) n) (drop n sums) sums))))

(time (doall (moving-average coll n)))
# "Elapsed time: 9.184 msecs"

Also this..

double[] MovingAverage(int period, double[] source)
{
    var ma = new double[source.Length];

    double sum = 0;
    for (int bar = 0; bar < period; bar++)
        sum += source[bar];

    ma[period - 1] = sum/period;

    for (int bar = period; bar < source.Length; bar++)
        ma[bar] = ma[bar - 1] + source[bar]/period
                              - source[bar - period]/period;

    return ma;
}

Moving Average

Calculate Moving Averages.

This package can help when you need moving averages in your PHP project.

For example, regular measurements of temperature or weight are not continuous; to have an idea of direction over time, a moving average is what you need.

To make numbers visual, I generated some example graphs for this doc.

You can also have a look at the tests for some usage examples.

Big or small data: Arrays and Generators

Statistics can have large data sets, and then Generators can help.

MovingAverage supports both Arrays and Generators for both input and output.


$array = $movingAverage->getCalculatedFromArray($sourceArray);

$generator = $movingAverage->generateFromArray($sourceArray);

$array = $movingAverage->getCalculatedFromGenerator($sourceGenerator);

$generator = $movingAverage->generateFromGenerator($sourceGenerator);

There are tests for all 4 variants. Use them to get started!

Average periods, delays, and weighted average.

The graphs below use this set of values:

 $values = [0, 2, 4, 6, 8, 4, 6, 8, 12, 10, 6, 8, 10, 14, 8, 10];

1. No average

Calculate the "average" over a period of 1, you will get the exact same set of values:


$movingAverage = new MovingAverage();
$movingAverage->setPeriod(1);

$data = $movingAverage->getCalculatedFromArray($values);

Result values are equal to input values, because of period=1.

Hướng dẫn moving average in php

2. Average over the last n values

Calculate average over the current value and 3 previous values.


$movingAverage = new MovingAverage();
$movingAverage->setPeriod(4);

Hướng dẫn moving average in php

3. Weighted average over the last n values

Calculate average over the current value and 3 previous values, with different importance. In the example, current value is least important (w=1), previous value most (w=5).


$movingAverage = new MovingAverage(MovingAverage::WEIGHTED_ARITHMETIC);
$movingAverage->setPeriod(4)
    ->setWeights([2, 3, 5, 1]);

Hướng dẫn moving average in php

4. Average over previous n, current, and next n values.

Calculate average over 5 values: previous 2, current, and next 2.


$movingAverage = new MovingAverage();
$movingAverage->setPeriod(5)
    ->setDelay(2);

Hướng dẫn moving average in php

5. Weighted average over previous n, current, and next n values.

Calculate average over 5 values: previous 2, current, and next 2, with different importance. In the example, the current value is most important (w=5), past less (4, 2), future least (3, 1).


$movingAverage = new MovingAverage(MovingAverage::WEIGHTED_ARITHMETIC);
$movingAverage->setPeriod(5)
    ->setDelay(2)
    ->setWeights([2, 4, 5, 3, 1]);

Hướng dẫn moving average in php


Graphs in this doc have been generated using JPGraph