火币网随机指数(Stochastic) PHP 算法源码

yufei       3 年, 7 月 前       699

不多说了,反正火币家的 KDJ 或着说随机指数 stoch 的算法就不是标准的。

  1. k 值基本就没加权,就是 rsv 的值
  2. d 值也没有啥加权,就是对 rsv 包括当日在内的 n 个数据取平均值

说多了都是泪,直接上 PHP 源码

<?php

class HuobiKdj {

    protected $fastK_Period = 0;
    protected $slowK_Period = 0;
    protected $slowD_Period = 0;

    public function __construct($fastK_Period,$slowK_Period,$slowD_Period)
    {
        $this->fastK_Period = $fastK_Period;
        $this->slowK_Period = $slowK_Period;
        $this->slowD_Period = $slowD_Period;
    }

    protected  function ma($bids, $n) 
    {
        $rs = [];
        for( $i = 0; $i < count($bids); $i++) {
            if ( $i == 0) {
                $rs[$i] = $bids[$i];
            } else {
                $rs[$i] = ( 2 * $bids[$i] + ($n-1)* $rs[$i - 1]) / ($n+1);
            }
        }
        return $rs;
    }

    protected function huobisma($bids, $n) 
    {
        $rs = [];
        for( $i = 0; $i < count($bids); $i++) {
            if ( $i < $n ) {
                $rs[$i] = $bids[$i];
            } else {
                $rs[$i] = array_sum(array_slice($bids,$i-$n+1,$n)) / $n;
            }
        }
        return $rs;
    }

    protected function clackdj($high,$low,$close )  {
        $length = count($high);
        $rsv = [50];

        // 计算 RSV
        for ($i = 1; $i < $length; $i++)
        {
            $m = $i + 1 - $this->fastK_Period;
            if ($m < 0) {
                $m = 0;
            }

            $hight_e = max(array_slice($high,$m, $i+1-$m));
            $low_e   = min(array_slice($low,$m,$i+1-$m));
            $rsv[$i] = ($close[$i] - $low_e ) * 100.0 / ($hight_e - $low_e);
        }

        $k = $this->ma($rsv, $this->slowK_Period);
        $d = $this->huobisma($k, $this->slowD_Period);
        $j = [];
        for ($i = 0; $i < $length; $i++) {
            $j[$i] = 3.0 * $k[$i] - 2.0 * $d[$i];
        }

        return [$k,$d,$j];
    }
}

$highArray = [
    11317.78,
    11310.00,
    11309.07,
    11301.39,
    11330.95,
    11345.12,
    11420,
    11460.78,
    11388.03
];
$lowArray = [
    11256.84,
    11245.33,
    11247.63,
    11243.00,
    11286.44,
    11321.98,
    11334.85,
    11364.52,
    11353.44
];
$closeArray = [
    11265.53,
    11304.96,
    11279.57,
    11298.16,
    11330.83,
    11342.18,
    11399.89,
    11378.83,
    11353.45
];

var_dump((new HuobiKdj(5,1,3))->clackdj($highArray,$lowArray,$closeArray));
目前尚无回复
简单教程 = 简单教程,简单编程
简单教程 是一个关于技术和学习的地方
现在注册
已注册用户请 登入
关于   |   FAQ   |   我们的愿景   |   广告投放   |  博客

  简单教程,简单编程 - IT 入门首选站

Copyright © 2013-2022 简单教程 twle.cn All Rights Reserved.