再舉一實例,修改 快樂股市 中的 AMA 最佳移動平均
原公式:
{Adaptive Moving Average , AMA , 最佳移動平均}
Direction:=CLOSE - REF( CLOSE , N ) ;
XX:=ABS( CLOSE - REF( CLOSE , 1 ) ) ;
Volatility:=SUM( XX , N ) ;
ER:=ABS( Direction / Volatility ) ;
FastC:= 2 / ( p + 1 ) ;
SlowC:= 2 / ( q + 1 ) ;
SSC:=ER * ( FastC - SlowC ) + SlowC ;
Constant :=SSC * SSC , Linethick0;
YY:=REF( CLOSE , 1 ) + Constant * ( CLOSE - REF( CLOSE , 1 ) ) ;
AA:=IF( SUM( 1 , 0 )= N + 1 , YY , 0 ) ;
BB:=BarsLast( AA>0 ) ;
DD:=REF( C , BB ) ;
收盤價格:CLOSE , Linethick0 , Precision2;
CC:=CLOSE , Linethick0 ;
<%
NN= FFL.VarData("N")
VD= FFL.VarData("DD")
VC= FFL.VarData("CC")
Vonstant= FFL.VarData("Constant")
LT = UBound( VC )
for m=NN + 1 to LT
VD( m )=VD( m - 1 ) + Vonstant( m ) * ( VC( m ) - VD( m - 1 ) )
next
FFL.VarData("DD")=VD
%>
AMA:DD , Linethick0;
FFilter:=( K / 100 ) * Std( AMA - REF( AMA , 1 ) , N ) ;
XA:=IF( AMA<REF( AMA , 1 ) , AMA , 0 ) ;
XN:=BarsLast( XA>0 ) ;
AMALow :=REF( AMA , XN ) ;
YA:=IF( AMA>REF( AMA , 1 ) , AMA , 0 ) ;
YN:=BarsLast( YA>0 ) ;
AMAHigh :=REF( AMA , YN ) ;
RR:=IF( AMAHigh - AMA > FFilter , -1 , 0 ) ;
TT:=IF( AMA - AMALow > FFilter , 1 , RR ) ;
PartLine(TT=1,AMA) , ColorRed , Linethick2;
PartLine(TT= -1,AMA) , ColorGreen , Linethick2;
PartLine(TT= 0,AMA) , ColorWhite , Linethick2;
修改後的公式:
{Adaptive Moving Average , AMA , 最佳移動平均}
CC:=CLOSE , Linethick0 ;
<%
CC = FFL.VarData("CC")
MAXKBAR = FFL.VarData("MAXKBAR")
MaxSet = ubound(CC)-MAXKBAR
MinKBar= FFL.VarStartIndex("CC")
if MaxSet < MinKBar then MaxSet = MinKBar
FFL.VarData("CC") = CC
if MAXKBAR > 0 THEN FFL.VarStartIndex("CC")=MaxSet
%>
Direction:=CC - REF( CC , N ) ;
XX:=ABS( CC - REF( CC , 1 ) ) ;
Volatility:=SUM( XX , N ) ;
ER:=ABS( Direction / Volatility ) ;
FastC:= 2 / ( p + 1 ) ;
SlowC:= 2 / ( q + 1 ) ;
SSC:=ER * ( FastC - SlowC ) + SlowC ;
Constant :=SSC * SSC , Linethick0;
YY:=REF( CC , 1 ) + Constant * ( CC - REF( CC , 1 ) ) ;
AA:=IF( SUM( 1 , 0 )= N + 1 , YY , 0 ) ;
BB:=BarsLast( AA>0 ) ;
DD:=REF( CC , BB ) ;
收盤價格:CC , Linethick0 , Precision2;
<%
NN= FFL.VarData("N")
VD= FFL.VarData("DD")
VC= FFL.VarData("CC")
Vonstant= FFL.VarData("Constant")
MAXKBAR = FFL.VarData("MAXKBAR")
MaxSet = ubound(VD)-MAXKBAR
MinKBar= FFL.VarStartIndex("DD")
if MaxSet < MinKBar then MaxSet = MinKBar
if MAXKBAR > 0 then NN = MaxSet
LT = UBound( VC )
for m=NN + 1 to LT
VD( m )=VD( m - 1 ) + Vonstant( m ) * ( VC( m ) - VD( m - 1 ) )
next
FFL.VarData("DD")=VD
if MAXKBAR > 0 THEN FFL.VarStartIndex("DD")=MaxSet
%>
AMA:DD , Linethick0;
FFilter:=( K / 100 ) * Std( AMA - REF( AMA , 1 ) , N ) ;
XA:=IF( AMA<REF( AMA , 1 ) , AMA , 0 ) ;
XN:=BarsLast( XA>0 ) ;
AMALow :=REF( AMA , XN ) ;
YA:=IF( AMA>REF( AMA , 1 ) , AMA , 0 ) ;
YN:=BarsLast( YA>0 ) ;
AMAHigh :=REF( AMA , YN ) ;
RR:=IF( AMAHigh - AMA > FFilter , -1 , 0 ) ;
TT:=IF( AMA - AMALow > FFilter , 1 , RR ) ;
PartLine(TT=1,AMA) , ColorRed , Linethick2;
PartLine(TT= -1,AMA) , ColorGreen , Linethick2;
PartLine(TT= 0,AMA) , ColorWhite , Linethick2;
註:if MAXKBAR > 0 then NN = MaxSet 因NN也是K線的起算點,所以也要跟著變動,必須加入此行
附件: TEST_AMA.ALG
此文件已被下載 1431 次。
|