php实现macd(移动平均值)

		//数据格式
        $data = [
			[
			 	"min" => "3.00"
			    "count" => 320
			    "max" => "93.00"
			    "avg" => "48.00"
			    "day" => "03-13"
			],
			[
			 	"min" => "16.00"
			    "count" => 989
			    "max" => "78.00"
			    "avg" => "47.00"
			    "day" => "03-14"
			],
			....
			
		];
        $mc = 5;//计算每五天的移动平均值,这个数可以改的
        $data = array_values($data);
        //计算移动平均值
        foreach ($data as $k =>&$v){
            $k = $k;
            if($k < $mc){
                $v['macd'] = 0;
            }else{

                $v['macd'] = sprintf("%.2f",($sum/$mc));
                $sum -= $data[$k-$mc]['avg'] ?? 0;
            }
            $sum += $v['avg'];
        }
        return data;

你可能感兴趣的:(php)