应该如何获得stochrsi的K、D值?
谢谢
我在网上找了很多公式
如
- LC := REF(CLOSE,1);
- RSI:=SMA(MAX(CLOSE-LC,0),N,1)/SMA(ABS(CLOSE-LC),N,1) *100;
- %K:MA(RSI-LLV(RSI,M),P1)/MA(HHV(RSI,M)-LLV(RSI,M),P1)*100;
- %D:MA(%K,P2);
现在我要自己写PHP有点看不懂
尝试过这样写
- StochRSI($arr['close'],14,9,3,3);
- RSI 14
- stoch 9
- K 3
- D 3
- 取得9个RSI,计算最大最小
- 分别存为数组
- 3组计算的值 用来计算一次K值
- 3个K值 再计算一个D值
- public function StochRSI($data,$p1 = 14,$p2 = 14 ,$n = 3,$m = 3) {
- //计算RSI
- $rsi = array();
- if (count($data) >= $p1) {
- $rsi = trader_rsi($data,$p1);
- }
-
- $rsi = array_values($rsi);//重新以0为递增索引
- //print_r($rsi);
- //%K: MA(RSI-LLV(RSI,14),3)/MA(HHV(RSI,14)-LLV(RSI,14),3) * 100
- //%D: MA(%K,3)
- //print_r($rsi);
- //循环计算stochrsi
-
- $rsi_count = count($rsi);
- if ($rsi_count >= ($p2 + $n + 1)) {
- $icount = $rsi_count - ($p2 + $n +1)
- for ($i=$icount; $i < $rsi_count; $i++) {
- $tmp[] = $rsi[$i];
- if (count($tmp)>=$p2) {
- $min = min($tmp);
- $max = max($tmp);
- $ma1[] = $rsi[$i] - $min;//$rsi[$i] = last rsi
- $ma2[] = $max - $min;
- if (count($ma1)>=$n) {
- $ma3 = end(trader_ma($ma1,$n));
- $ma4 = end(trader_ma($ma2,$n));
- $k = round($ma3 / $ma4 *100,6);
- $stochrsi[] = $k;
- //echo "K:".$k."\n";
- //print_r($stochrsi);
- array_shift($ma1);//保持相应的记录数量
- array_shift($ma2);//保持相应的记录数量
- if (count($stochrsi)>=$m) {
- $d = end(trader_ma($stochrsi,$m));
- //echo "D:".$d."\n";
- return [$k,$d];
- }
- }
- array_shift($tmp);//保持相应的记录数量
- }
- }
- }
-
- }
计算出RSI
谁能帮忙解释一下?
谢谢
或者有没有适用PHP的金融指标插件函数?感谢