急求!哪位懂股票的IT大神能用php代码写出RSI的算法,最好带有注解 谢谢 20
最好是知道RSI的相关知识,网上也有公式但是不是很理解,需要精通这两个领域的人帮忙做个整合,谢谢...
最好是知道RSI的相关知识,网上也有公式但是不是很理解,需要精通这两个领域的人帮忙做个整合,谢谢
展开
2个回答
展开全部
php RSI:
class RSI{
static function lag($period = 14){
return $period;
}
static function run($data, $period = 14){
$change_array = array();
//loop data
foreach($data as $key => $row){
//need 2 points to get change
if($key >= 1){
$change = $data[$key]['close'] - $data[$key - 1]['close'];
//add to front
array_unshift($change_array, $change);
//pop back if too long
if(count($change_array) > $period)
array_pop($change_array);
}
//have enough data to calc rsi
if($key > $period){
//reduce change array getting sum loss and sum gains
$res = array_reduce($change_array, function($result, $item) {
if($item >= 0)
$result['sum_gain'] += $item;
if($item < 0)
$result['sum_loss'] += abs($item);
return $result;
}, array('sum_gain' => 0, 'sum_loss' => 0));
$avg_gain = $res['sum_gain'] / $period;
$avg_loss = $res['sum_loss'] / $period;
//check divide by zero
if($avg_loss == 0){
$rsi = 100;
} else {
//calc and normalize
$rs = $avg_gain / $avg_loss;
$rsi = 100 - (100 / ( 1 + $rs));
}
//save
$data[$key]['val'] = $rsi;
}
}
return $data;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询