FC2ブログ

# Ehlers MESA Adaptive Moving Average
# 引数 期間 値 ($Speed, $FastLimit, $SlowLimit, $Price)
# 戻り値 EhlersMESAAdaptiveMovingAverage (@mama)
sub MAMA{
my ($Speed, $FastLimit, $SlowLimit, $Price) = @_;
my @mama = ();
my $count = @$Price - 5;

my @Smooth = (0, 0, 0, 0, 0, 0, 0);
my @Detrender = (0, 0, 0, 0, 0, 0, 0);
my @Q1 = (0, 0, 0, 0, 0, 0, 0);
my @I1 = (0, 0, 0, 0, 0, 0, 0);
my $jI = 0;
my $jQ = 0;
my @Q2 = (0, 0);
my @I2 = (0, 0);
my @Re = (0, 0);
my @Im = (0, 0);
my @Period = (0, 0);
my @SmoothPeriod = (0, 0);
my @Phase = (0, 0);
my $DeltaPhase = 0;
my $alpha = 0;

# $Speed,$FastLimit,$SlowLimit,配列数の確認
if(($Speed <= 0) || (1 <= $Speed) || ($FastLimit <= 0) || (1 <= $FastLimit) || ($SlowLimit <= 0) || (1 <= $SlowLimit) || ($count < 0)){
return 0;
}

# 計算
$mama[$count + 1] = $$Price[$count + 1];
for(my $i = $count; $i >= 0; $i--){
# 配列の要素を1つ後ろに動かす
for(my $j = 6; $j >= 1; $j--){
$Smooth[$j] = $Smooth[$j - 1];
$Detrender[$j] = $Detrender[$j - 1];
$Q1[$j] = $Q1[$j - 1];
$I1[$j] = $I1[$j - 1];
}
$Q1[1] = $Q1[0];
$I1[1] = $I1[0];
$Re[1] = $Re[0];
$Im[1] = $Im[0];
$Period[1] = $Period[0];
$SmoothPeriod[1] = $SmoothPeriod[0];
$Phase[1] = $Phase[0];

$Smooth[0] = (4 * $$Price[$i] + 3 * $$Price[$i + 1] + 2 * $$Price[$i + 2] + 1 * $$Price[$i + 3] + 1 * $$Price[$i + 4]) / 10;
$Detrender[0] = ((0.0962 * $Smooth[0]) + (0.5769 * $Smooth[2]) - (0.5769 * $Smooth[4]) - (0.0962 * $Smooth[6])) * ((0.075 * $Period[1]) + 0.54);
$Q1[0] = ((0.0962 * $Detrender[0]) + (0.5769 * $Detrender[2]) - (0.5769 * $Detrender[4]) - (0.0962 * $Detrender[6])) * ((0.075 * $Period[1]) + 0.54);
$I1[0] = $Detrender[3];
$jI = ((0.0962 * $I1[0]) + (0.5769 * $I1[2]) - (0.5769 * $I1[4]) - (0.0962 * $I1[6])) * ((0.075 * $Period[1]) + 0.54);
$jQ = ((0.0962 * $Q1[0]) + (0.5769 * $Q1[2]) - (0.5769 * $Q1[4]) - (0.0962 * $Q1[6])) * ((0.075 * $Period[1]) + 0.54);
$I2[0] = $I1[0] - $jQ;
$Q2[0] = $Q1[0] + $jI;
$I2[0] = (0.2 * $I2[0]) + (0.8 * $I2[1]);
$Q2[0] = (0.2 * $Q2[0]) + (0.8 * $Q2[1]);
$Re[0]= ($I2[0] * $I2[1]) + ($Q2[0] * $Q2[1]);
$Im[0] = ($I2[0] * $Q2[1]) - ($Q2[0] * $I2[1]);
$Re[0] = (0.2 * $Re[0]) + (0.8 * $Re[1]);
$Im[0] = (0.2 * $Im[0]) + (0.8 * $Im[1]);

# 期間
if(($Im[0] != 0) && ($Re[0] != 0)){
$Period[0] = 360 / atan2($Re[0], $Im[0]);
}
if($Period[0] > (1.5 * $Period[1])){
$Period[0] = 1.5 * $Period[1];
}
if($Period[0] < (0.67 * $Period[1])){
$Period[0] = 0.67 * $Period[1];
}
if($Period[0] < 6){
$Period[0] = 6;
}
if($Period[0] > 50){
$Period[0] = 50;
}
$Period[0] = (0.2 * $Period[0]) + (0.8 * $Period[1]);
$SmoothPeriod[0] = (0.33 * $Period[0]) + (0.67 * $SmoothPeriod[1]);

if($I1[0] != 0){
$Phase[0] = atan2($I1[0], $Q1[0]);
}
$DeltaPhase = $Phase[1] - $Phase[0];
if($DeltaPhase < 1){
$DeltaPhase = 1;
}

$alpha = $Speed / $DeltaPhase;
if($alpha < $SlowLimit){
$alpha = $SlowLimit;
}
if($alpha > $FastLimit){
$alpha = $FastLimit;
}

# Ehlers MESA Adaptive Moving Average
$mama[$i] = ($alpha * $$Price[$i]) + ((1 - $alpha) * $mama[$i + 1]);
}

return @mama;
}


参考URL
MAMA
オンライン コンパイラ/インタプリタ
テクニカル分析
プロフィール

Author:雨宮
Firefoxを使用しているので気づかなかったけど、IE6でソースコードを上手くコピーできない

5/3
携帯用ならIE6でもソースコードをコピーできる
携帯用

検索フォーム


あわせて読みたいブログパーツ
一寸先は闇 RSS