# Calculate the KDJ value of an array of numbers. # @(K,D,J) = calcKDJ( # /@closepAry, /@lowpAry, /@highpAry, # $rsv_days, $Kdays, $Jdays, $preDaysCnt) # @closepAry -- Close Price Array sorted in descendent date order # $preDaysCnt -- start calculating from @closepAry[$preDaysCnt] data; # sub calcKDJ { my $debug = $FALSE; my ($closepAryRef, $lowpAryRef, $highpAryRef, $N, $M1, $M2, $preDaysCnt) = @_; myd($debug, scalar(@$closepAryRef), " @$closepAryRef"); myd($debug, scalar(@$lowpAryRef), " @$lowpAryRef"); myd($debug, scalar(@$highpAryRef), " @$highpAryRef"); myd($debug, $N, $M1, $M2, $preDaysCnt); my ($val_rsv, $val_K, $val_D, $val_J) = (); my (@subLowpAry, @subHighpAry) = (); my @preDayVals = (); my $startAryPos = $preDaysCnt; if ($startAryPos > scalar(@$closepAryRef)-1) { $startAryPos = scalar(@$closepAryRef)-1; } my $endAryPos = $preDaysCnt + $N-1; if ($endAryPos > scalar(@$closepAryRef)-1) { $endAryPos = scalar(@$closepAryRef)-1; } myd($debug, "startAryPos: $startAryPos /t endAryPos: $endAryPos"); #Calculate RSV(Raw Stochastic Value) $val_rsv = calcRSV($closepAryRef, $lowpAryRef, $highpAryRef, $N, $preDaysCnt); if ($startAryPos == scalar(@$closepAryRef)-1) { $val_K = $val_D = 50; # $val_K = $val_D = $val_rsv; } else { @preDayVals = calcKDJ($closepAryRef, $lowpAryRef, $highpAryRef, $N, $M1, $M2, $preDaysCnt+1) ; myd($debug, "preDayVals is: @preDayVals"); $val_K = sprintf("%f", 1/$M1*$val_rsv + ($M1-1) / $M1 * $preDayVals[0]); $val_D = sprintf("%f", 1/$M2*$val_K + ($M2-1) / $M2 * $preDayVals[1]); } $val_J = 3*$val_K - 2*$val_D; myd($debug, "val_rsv: $val_rsv val_K: $val_K, val_D: $val_D, val_J: $val_J"); return ($val_K, $val_D, $val_J); } # Calculate the W&R value of an array of numbers. # @(WR1, WR2) = calcWR( # /@closepAry, /@lowpAry, /@highpAry, # $wr_days1, $wr_days2, $preDaysCnt) # @closepAry -- Close Price Array sorted in descendent date order # $preDaysCnt -- start calculating from @closepAry[$preDaysCnt] data; # sub calcWR { my ($closepAryRef, $lowpAryRef, $highpAryRef, $N1, $N2, $preDaysCnt) = @_; my @result = (); push(@result, sprintf("%.2f", 100 - calcRSV($closepAryRef,$lowpAryRef,$highpAryRef,$N1,$preDaysCnt)) ); push(@result, sprintf("%.2f", 100 - calcRSV($closepAryRef,$lowpAryRef,$highpAryRef,$N2,$preDaysCnt)) ); return @result; } # Calculate the RSV(Raw Stochastic Value) value of an array of numbers. sub calcRSV { my ($closepAryRef, $lowpAryRef, $highpAryRef, $N, $preDaysCnt) = @_; my $debug = $FALSE; my $rsvVal = 0; my ($startAryPos, $endAryPos) = (); my (@subLowpAry, @subHighpAry) = (); $startAryPos = $preDaysCnt; if ($startAryPos > scalar(@$closepAryRef)-1) { $startAryPos = scalar(@$closepAryRef)-1; } #calculate RSV value in N days $endAryPos = $preDaysCnt + $N-1; if ($endAryPos > scalar(@$closepAryRef)-1) { $endAryPos = scalar(@$closepAryRef)-1; } myd($debug, "startAryPos: $startAryPos /t endAryPos: $endAryPos"); @subLowpAry = @$lowpAryRef[$startAryPos..$endAryPos]; myd($debug, "subLowpAry is: @subLowpAry"); @subHighpAry = @$highpAryRef[$startAryPos...$endAryPos]; myd($debug, "subHighpAry is: @subHighpAry"); my $minInAry = min(@subLowpAry); my $maxInAry = max(@subHighpAry); if ($minInAry == $maxInAry) { $rsvVal = 0.00; } else { $rsvVal = sprintf("%.2f", ($$closepAryRef[$startAryPos] - $minInAry) / ($maxInAry - $minInAry) * 100); } return $rsvVal; }