首页 > 解决方案 > 计算移动平均线的角度

问题描述

我正在尝试计算最后 2 根蜡烛之间的移动平均线 (n) 的角度。

我一直在使用这个公式,它给了我移动平均线的 2 根蜡烛之间的正确角度 (2) 但如果我将 MA 的值更改为 eg.20,它会给我 20MA 的角度,20 根蜡烛分开。

angle(_src) =>
    rad2degree=180/3.14159265359  //pi 
    ang=rad2degree*atan((_src[0] - _src[1])/atr(14)) 
ma=ema(src,input(2))
ma_slope=angle(ma)

如何制定代码以给出最后 2 根蜡烛之间的 20MA 角度?

谢谢

标签: pine-scriptmoving-averagetrading

解决方案


study("ma angles - JD")
src = input(ohlc4, title="source")
th = input(2, minval=1, title="threshold for -no trade zones- in degrees")
color_bars = input(false, title="color bars?")
no_trade = input(false, title="black out bars in no trade zones?")

// definition of "Jurik Moving Average", by Everget
jma(_src, _length, _phase, _power) =>
    phaseRatio = _phase < -100 ? 0.5 : _phase > 100 ? 2.5 : _phase / 100 + 1.5
    beta = 0.45 * (_length - 1) / (0.45 * (_length - 1) + 2)
    alpha = pow(beta, _power)
    jma = 0.0
    e0 = 0.0
    e0 := (1 - alpha) * _src + alpha * nz(e0[1])
e1 = 0.0
e1 := (_src - e0) * (1 - beta) + beta * nz(e1[1])
e2 = 0.0
e2 := (e0 + phaseRatio * e1 - nz(jma[1])) * pow(1 - alpha, 2) + 
   pow(alpha, 2) * nz(e2[1])
jma := e2 + nz(jma[1])
jma

//// //// Determine Angle by KyJ //// //// 
angle(_src) =>
    rad2degree = 180 / 3.14159265359  //pi 
    ang = rad2degree * atan((_src[0] - _src[1]) / atr(14))
    ang

    jma_line = jma(src, 10, 50, 1)
jma_line_fast = jma(src, 10, 50, 2)
ma27 = ema(src, 27)
ma83 = ema(src, 83)
ma278 = ema(src, 278)
jma_slope = angle(jma_line)
jma_fast_slope = angle(jma_line_fast)
ma27_slope = angle(ma27)
ma83_slope = angle(ma83)
ma278_slope = angle(ma278)

hline(0)
rising_1 = rising(ma27, 1)
color_1 = color.new(color.green, 75)
falling_1 = falling(ma27, 1)

plot(jma_slope, title="jma slope", style=plot.style_area, color=jma_slope >= 0 ? 
     rising_1 ? color.green : color_1 : falling_1 ? color.red : color.maroon)
plot(jma_fast_slope, title="jma slope", style=plot.style_line, color=jma_fast_slope >= 
     0 ? color.green : color.red, transp=0)
plot(ma27_slope, title="ma27 slope filter", style=plot.style_area, 
     color=abs(ma27_slope) > th ? na : color.yellow)
plot(ma83_slope, title="ma83 slope filter", style=plot.style_area, 
     color=abs(ma83_slope) > th ? na : color.yellow)
plot(ma278_slope, title="ma278 slope filter", style=plot.style_area, 
     color=abs(ma278_slope) > th ? na : color.yellow)
plot(ma27_slope, title="ma27 slope", style=plot.style_line, linewidth=2, 
     color=ma27_slope >= 0 ? color.lime : color.fuchsia)

color_2 = color.new(color.green, 0)
color_3 = color.new(color.red, 0)

plot(ma83_slope, title="ma83 slope", style=plot.style_line, color=ma83_slope >= 0 ? 
     color_2 : color_3)
plot(ma278_slope, title="ma278 slope", style=plot.style_line, color=ma278_slope >= 0 ? 
     color.green : color.red)

plotshape(ma27_slope >= 0 ? ma27 : na, style=shape.triangleup, 
          location=location.bottom, color=color.green)
plotshape(ma27_slope < 0 ? ma27 : na, style=shape.triangledown, location=location.top, 
          color=color.red)
plotshape(ma27_slope >= 0 and not(ma27_slope[1] >= 0) ? ma27 : na, 
          style=shape.triangleup, location=location.bottom, size=size.tiny, 
          color=color.green)
plotshape(ma27_slope < 0 and not(ma27_slope[1] < 0) ? ma27 : na, 
          style=shape.triangledown, location=location.top, size=size.tiny, 
          color=color.red)

rising_2 = rising(ma27, 1)
falling_2 = falling(ma27, 1)

barcolor(color_bars ? no_trade and abs(ma27_slope) <= th ? color.white : jma_slope >= 
         0 ? rising_2 ? color.lime : color.green : falling_2 ? color.fuchsia : 
         color.red : na)

推荐阅读