简体   繁体   English

pine-script linreg function 是如何工作的

[英]how does the pine-script linreg function work

i want to transfer a pine-script linreg function to php, and i need some help which values are exactly passed to the linreg function.我想将一个 pine-script linreg function 转移到 php,我需要一些帮助,这些值恰好传递给了 linreg function。

ie my linreg function in pine-script looks like this:即我在 pine-script 中的 linreg function 看起来像这样:

linreg(close, 20, 0)

for calculating the linear regression in php, i have the following function:为了计算 php 中的线性回归,我有以下 function:

public static function linear_regression($x, $y) {

        // calculate number points
        $n = count($x);

        // ensure both arrays of points are the same size
        if ($n != count($y)) {

          trigger_error("linear_regression(): Number of elements in coordinate arrays do not match.", E_USER_ERROR);

        }

        // calculate sums
        $x_sum = array_sum($x);
        $y_sum = array_sum($y);

        $xx_sum = 0;
        $xy_sum = 0;

        for($i = 0; $i < $n; $i++) {

          $xy_sum+=($x[$i]*$y[$i]);
          $xx_sum+=($x[$i]*$x[$i]);

        }

        // calculate slope
        $m = (($n * $xy_sum) - ($x_sum * $y_sum)) / (($n * $xx_sum) - ($x_sum * $x_sum));

        // calculate intercept
        $b = ($y_sum - ($m * $x_sum)) / $n;

        // return result
        return array("m"=>$m, "b"=>$b);
    }

my question now is what data i have to pass to my php function to get the same result as in the pine-script.我现在的问题是我必须将哪些数据传递给我的 php function 以获得与 pine-script 中相同的结果。

According to the reference manual, the result of the linreg function is calculated as linreg = intercept + slope * (length - 1 - offset), where intercept and slope are the values calculated with the least squares method on source series.根据参考手册,linreg function 的计算结果为 linreg = 截距 + 斜率 *(长度 - 1 - 偏移),其中截距和斜率是在source序列上使用最小二乘法计算的值。

This question has also been asked here: Converting linreg function from pinescript to Python?这里也有人问过这个问题: Converting linreg function from pinescript to Python? . . Have you tried the solution proposed there?您是否尝试过那里提出的解决方案?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM