trader_stddev 不起作用。这也是 trader_bbands 不起作用的原因。
function standard_deviation(array $a, $sample = false)
{
$n = count($a);
if ($n === 0) {
trigger_error("数组中没有元素", E_USER_WARNING);
return false;
}
if ($sample && $n === 1) {
trigger_error("数组中只有一个元素", 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;
}