douketangyouzh5219 2012-08-31 14:10
浏览 51

Welle的Wilder Accumulative Swing Index PHP计算 - 无法返回正确的值

I am trying to get the accumulative swing index for an aapl stock chart. I am using this calculation for reference.

http://www.barchart.com/education/std_studies.php?what=int_swing&hideheader=true#study

This is what I have written so far. This should return 252.09 but I cannot get it to work.

$asi[0] = -78.75
$ht = 584; // High today
$lt = 574.25; // low
$ct = 584.00; // close
$ot = 578; // open

$hy = 574; // High yesterday
$ly = 565.61;
$cy = 569.05;
$oy = 571.67;


$k = max(($hy-$ct),($ly-$ct));

$abc = array(($ht-$cy), ($lt-$cy), ($ht-$lt));
$max = max($abc);
$r = 0;
if($max == $abc[0]){
    $r = ($ht-$cy)-.5*($lt-$cy)+.25*($cy-$oy);
}elseif($max == $abc[1]){
    $r = ($lt-$cy)-.5*($ht-$cy)+.25*($cy-$oy);
}elseif($max == $abc[2]){
    $r = ($ht-$lt)+.25*($cy-$oy);
}else{
    echo "Error in welles accumulative swing index";
    exit;
}

$l = 3 //period;

$val = 50 * (($cy - $ct) + .5 *($cy - $oy) + .25*($ct-$ot)) / $r * $k / $l;

$asi[] = $asi[$i-1] + $val;

Any help would be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • doufeixuan8882 2012-10-11 20:13
    关注

    I have tried to implement this index newly symbol-by-symbol and have get different result (swing: -248.7032967033 ). May be your control value wrong?

    That is my code:

    class Swing
    {
        public function calculate($high_price, $low_price, $close_price, $open_price, $t)
        {
            // (Ct-1 - Ct)
            $summand0 = ($close_price[$t-1] - $close_price[$t]);
            // 0.5(Ct-1 - Ot-1)
            $summand1 = 0.5 * ($close_price[$t-1] - $open_price[$t-1]);
            // 0.25(Ct - Ot)
            $summand2 = 0.25 * ($close_price[$t] - $open_price[$t]);
    
            $limit_move_default = 3.0;
    
            $r = $this->get_r_value($high_price, $low_price, $close_price, $open_price, $t);
            $k = $this->get_k_value($high_price, $low_price, $close_price, $t);
    
            $factor0 = 50.0 * ($summand0 + $summand1 + $summand2) / $r;
            $factor1 = $k / $limit_move_default;
    
            // SWING = 50 * ((Ct-1 - Ct)+ 0.5(Ct-1 - Ot-1)+ 0.25(Ct - Ot))/ R * K / M
            return $factor0 * $factor1;
        }
    
        public function get_k_value($high_price, $low_price, $close_price, $t)
        {
            // K= MAX(| Ht-Ct-1|, | Lt-Ct-1|)
            return max(
                abs($high_price[$t] - $close_price[$t-1]),
                abs($low_price[$t] - $close_price[$t-1]));
        }
    
        public function get_r_value($high_price, $low_price, $close_price, $open_price, $t)
        {
            // A. |Ht-Ct-1|
            $a = abs($high_price[$t] - $close_price[$t-1]);
            // B. |Lt-Ct-1|
            $b = abs($low_price[$t] - $close_price[$t-1]);
            // C. |Ht-Lt|
            $c = abs($high_price[$t] - $low_price[$t]);
    
            $max_value = max($a, $b, $c);
    
            $d = abs($high_price[$t] - $low_price[$t]);
    
            if($a == $max_value)
                // R= (| Ht-Ct-1|)-.5(| Lt-Ct-1|)+.25(| Ct-1-Ot-1|)
                return $a - 0.5 *  $b + 0.25 *  $d;
    
            if($b == $max_value)
                // R= (| Lt-Ct-1|)-.5(| Ht-Ct-1|)+.25(| Ct-1-Ot-1|)
                return  $b - 0.5 * $a + 0.25 *  $d;
    
            if($c == $max_value)
                // R= (| Ht-Lt|)+.25(| Ct-1-Ot-1|)
                return $c + 0.25 *  $d;
        }
    };
    
    $swing = new Swing();
    
    $high_price = array(574.0, 584.0);
    $low_price = array(565.61, 574.25);
    $close_price = array(569.05, 584.0);
    $open_price = array(571.67, 578.0);
    
    $value = $swing->calculate($high_price, $low_price, $close_price, $open_price, 1);
    echo("swing: $value 
    ");
    
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能