//------------------------------------------------------------------------
// 简称: DailyAMAArrow
// 名称: 日线AMA突破箭头(真实交易日)
// 类别: 技术指标
//------------------------------------------------------------------------
Params
Numeric N(10);
Numeric Fast(2);
Numeric Slow(30);
Vars
Series<Numeric> sClose; // 存储每个交易日的收盘价
Series<Numeric> sAMA;
Series<Numeric> sState;
Numeric curDate;
Numeric prevDate;
Numeric dailyClose;
Numeric ama;
Numeric fastestSC;
Numeric slowestSC;
Numeric ER;
Numeric SC;
Numeric sumDir;
Numeric sumVol;
Integer i;
Bool isNewDay;
Bool upBreak;
Bool dnBreak;
Begin
fastestSC = 2.0 / (Fast + 1);
slowestSC = 2.0 / (Slow + 1);
curDate = TradingDate[0];
prevDate = TradingDate[1];
isNewDay = (curDate != prevDate);
// 关键:在每个交易日的最后一根K线(即夜盘收盘)记录收盘价
// 由于我们无法直接判断最后一根K线,改用以下逻辑:
// 当新交易日到来时,前一根K线就是上一个交易日的最后一根K线,
// 因此我们可以在新交易日的第一根K线上,获取前一根K线的收盘价作为上一个交易日的收盘价。
If (isNewDay And CurrentBar > 0)
{
// 上一个交易日的收盘价 = 前一根K线的收盘价(因为前一根K线属于上一个交易日)
dailyClose = Close[1];
sClose = dailyClose;
}
Else
{
// 非新交易日首根K线,保持当前交易日的收盘价不变(使用之前记录的值)
If (CurrentBar > 0)
sClose = sClose[1];
Else
sClose = Close;
dailyClose = sClose;
}
// 计算日线AMA(基于 dailyClose 序列)
If (CurrentBar == 0)
{
sAMA = dailyClose;
}
Else
{
// 效率系数 ER
sumDir = Abs(dailyClose - sClose[Min(N, CurrentBar)]);
sumVol = 0;
For i = 1 To Min(N, CurrentBar)
{
sumVol = sumVol + Abs(sClose[i] - sClose[i-1]);
}
If (sumVol != 0)
ER = sumDir / sumVol;
Else
ER = 0;
SC = ER * (fastestSC - slowestSC) + slowestSC;
SC = SC * SC;
sAMA = sAMA[1] + SC * (dailyClose - sAMA[1]);
}
ama = sAMA;
// 状态判断
If (dailyClose > ama)
sState = 1;
Else If (dailyClose < ama)
sState = -1;
Else
sState = 0;
// 在新交易日的第一根K线上,根据前一个交易日状态变化画箭头
If (isNewDay And CurrentBar > 0)
{
upBreak = (sState[1] != 1) And (sState == 1);
dnBreak = (sState[1] != -1) And (sState == -1);
If (upBreak)
PlotNumeric("UpArrow", Low - (High - Low) * 0.2, Green);
If (dnBreak)
PlotNumeric("DnArrow", High + (High - Low) * 0.2, Red);
}
End