略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: trader_stddev

2024-11-14

trader_stddev

(PECL trader >= 0.2.0)

trader_stddevStandard Deviation

说明

trader_stddev(array $real, int $timePeriod = ?, float $nbDev = ?): array

参数

real

浮点数数组。

timePeriod

Number of period. Valid range from 2 to 100000.

nbDev

返回值

Returns an array with calculated data or false on failure.

add a noteadd a note

User Contributed Notes 1 note

up
1
nijatasadov at gmail dot com
3 years ago
trader_stddev doesn't work. This is why trader_bbands doesn't work as well.

function standard_deviation(array $a, $sample = false)
    {
        $n = count($a);
        if ($n === 0) {
            trigger_error("The array has zero elements", E_USER_WARNING);

            return false;
        }
        if ($sample && $n === 1) {
            trigger_error("The array has only 1 element", E_USER_WARNING);

            return false;
        }
        $mean = array_sum($a) / $n;
        $carry = 0.0;
        foreach ($a as $val) {
            $d = ((double) $val) - $mean;
            $carry += $d * $d;
        };
        if ($sample) {
            --$n;
        }

        return sqrt($carry / $n);
    }

trader_stddev($data, $period) {
       $stds = [];
        $current = [];

        foreach ($data as $i => $close) {
            $current[] = $close;
            if ($i < $period) {
                $stds[$i] = false;
            } else {
                $stds[$i] = standard_deviation($current);
                array_shift($current);
            }
        }

        return $stds;

}

官方地址:https://www.php.net/manual/en/function.trader-stddev.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3