我该如何将2种颜色的移动平均线组合成一条改变颜色的线



这里是新程序员!得益于强大的堆栈溢出社区,我们能够创建一个具有多种颜色移动平均值的指标。

现在我想更进一步。

我希望两条线都变绿,如果它们的斜率为正

我希望两条如果它们的斜率都为负,则它们变红

如果其中一个斜率为正,另一个为负,我希望两条线都保持银色

虽然我知道我的代码没有那么优雅,因为我还没有使用"&";,当我得到未声明的标识符错误时,我很惊讶。

最初的代码是:

outA = ta.sma(close, 15)
positiveSlopeA = outA > outA[1]
colorA = positiveSlopeA ? color.green : color.red //positive slope -> green, else red
plot(outA, color=colorA, title="SMA(15)")
outB = ta.sma(close, 20)
positiveSlopeB = outB > outB[1]
colorB = positiveSlopeB ? color.green : color.red //positive slope -> green, else red
plot(outB, color=colorB, title="SMA(20)")

当我试图自己做的时候,我得到了:

outA = ta.sma(close, 15)
positiveSlopeA = outA > outA[1]
negativeSlopeA = outA < outA[1]

outB = ta.sma(close, 30)
positiveSlopeB = outB > outB[1]
negativeSlopeB = outB < outB[1]

if positiveSlopeA and positiveSlopeB
colorA = color.green
else if negativeSlopeA and negativeSlopeB
colorA = color.red
else 
colorA = color.silver
//@version=5
indicator(shorttitle='MA_2X', title='2 Moving Average Color Direction Detection ', overlay=true, timeframe = "")
// === INPUTS
ma_1 = input(true, title='1-MA')
ma_type = input.string(defval='SMA', title='1-MA Type: ', options=['SMA', 'EMA', 'WMA', 'VWMA', 'SMMA', 'DEMA', 'TEMA', 'HullMA', 'ZEMA', 'TMA', 'SSMA'])
ma_len = input.int(defval=20, title='1-MA Lenght', minval=1)
ma_src = input(close, title='1-MA Source')
reaction_ma_1 = input.int(defval=3, title='1-MA Reaction', minval=1)
ma_2 = input(true, title='2-MA')
ma_type_b = input.string(defval='SMA', title='2-MA Type: ', options=['SMA', 'EMA', 'WMA', 'VWMA', 'SMMA', 'DEMA', 'TEMA', 'HullMA', 'ZEMA', 'TMA', 'SSMA'])
ma_len_b = input.int(defval=8, title='2-MA Lenght', minval=1)
ma_src_b = input(close, title='2-MA Source')
reaction_ma_2 = input.int(defval=3, title='2-MA Reaction', minval=1)
filling = input(false, title='FILLING')
// SuperSmoother filter
// © 2013  John F. Ehlers
variant_supersmoother(src, len) =>
a1 = math.exp(-1.414 * 3.14159 / len)
b1 = 2 * a1 * math.cos(1.414 * 3.14159 / len)
c2 = b1
c3 = -a1 * a1
c1 = 1 - c2 - c3
v9 = 0.0
v9 := c1 * (src + nz(src[1])) / 2 + c2 * nz(v9[1]) + c3 * nz(v9[2])
v9
variant_smoothed(src, len) =>
v5 = 0.0
sma_1 = ta.sma(src, len)
v5 := na(v5[1]) ? sma_1 : (v5[1] * (len - 1) + src) / len
v5
variant_zerolagema(src, len) =>
ema1 = ta.ema(src, len)
ema2 = ta.ema(ema1, len)
v10 = ema1 + ema1 - ema2
v10
variant_doubleema(src, len) =>
v2 = ta.ema(src, len)
v6 = 2 * v2 - ta.ema(v2, len)
v6
variant_tripleema(src, len) =>
v2 = ta.ema(src, len)
v7 = 3 * (v2 - ta.ema(v2, len)) + ta.ema(ta.ema(v2, len), len)
v7
variant(type, src, len) =>
ema_1 = ta.ema(src, len)
wma_1 = ta.wma(src, len)
vwma_1 = ta.vwma(src, len)
variant_smoothed__1 = variant_smoothed(src, len)
variant_doubleema__1 = variant_doubleema(src, len)
variant_tripleema__1 = variant_tripleema(src, len)
wma_2 = ta.wma(src, len / 2)
wma_3 = ta.wma(src, len)
wma_4 = ta.wma(2 * wma_2 - wma_3, math.round(math.sqrt(len)))
variant_supersmoother__1 = variant_supersmoother(src, len)
variant_zerolagema__1 = variant_zerolagema(src, len)
sma_1 = ta.sma(src, len)
sma_2 = ta.sma(sma_1, len)
sma_3 = ta.sma(src, len)
type == 'EMA' ? ema_1 : type == 'WMA' ? wma_1 : type == 'VWMA' ? vwma_1 : type == 'SMMA' ? variant_smoothed__1 : type == 'DEMA' ? variant_doubleema__1 : type == 'TEMA' ? variant_tripleema__1 : type == 'HullMA' ? wma_4 : type == 'SSMA' ? variant_supersmoother__1 : type == 'ZEMA' ? variant_zerolagema__1 : type == 'TMA' ? sma_2 : sma_3

// === Moving Average
ma_series = variant(ma_type, ma_src, ma_len)
ma_series_b = variant(ma_type_b, ma_src_b, ma_len_b)
direction = 0
falling_1 = ta.falling(ma_series, reaction_ma_1)
direction := ta.rising(ma_series, reaction_ma_1) ? 1 : falling_1 ? -1 : nz(direction[1])
change_direction = ta.change(direction, 1)
direction_b = 0
falling_2 = ta.falling(ma_series_b, reaction_ma_2)
direction_b := ta.rising(ma_series_b, reaction_ma_2) ? 1 : falling_2 ? -1 : nz(direction_b[1])
change_direction_b = ta.change(direction_b, 1)
// Plot MA series and color it according too direction
pcol = direction > 0 ? color.lime : direction < 0 ? color.red : na
a = plot(ma_1 ? ma_series : na, color=pcol, style=plot.style_line, join=true, linewidth=3, title='1-MA Plot', transp=10)
pcol_b = direction_b > 0 ? color.lime : direction_b < 0 ? color.red : na
b = plot(ma_2 ? ma_series_b : na, color=pcol_b, style=plot.style_line, join=true, linewidth=2, title='2-MA Plot', transp=10)
fill(a, b, color=direction == 1 and direction_b == 1 and filling == true ? color.lime : direction == -1 and direction_b == -1 and filling == true ? color.red : direction == 1 and direction_b == -1 and filling == true ? color.purple : direction == -1 and direction_b == 1 and filling == true ? color.purple : color.white, transp=80)
/////// Alerts ///////
alertcondition(change_direction, title='Change Direction 1-MA', message='Change Direction 1-MA')
alertcondition(change_direction_b, title='Change Direction 2-MA', message='Change Direction 2-MA')
alertcondition(ta.crossunder(ma_series_b, ma_series), title='Cross Under 2-Ma/1-Ma', message='Cross Under 2-Ma/1-Ma')
alertcondition(ta.crossunder(ma_series, ma_series_b), title='Cross Under 1-Ma/2-Ma', message='Cross Under 1-Ma/2-Ma')
alertcondition(ta.crossover(ma_series_b, ma_series), title='Cross Over 2-Ma/1-Ma', message='Cross Over 2-Ma/1-Ma')
alertcondition(ta.crossover(ma_series, ma_series_b), title='Cross Over 1-Ma/2-Ma', message='Cross Over 1-Ma/2-Ma')
alertcondition(ta.cross(ma_series_b, ma_series), title='Cross 1-Ma/2-Ma', message='Cross 1-Ma/2-Ma')
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © yashchotrani
//@version=5
indicator("MA COLOR CODED")
ma(source, length, type) =>
type == "SMA" ? ta.sma(source, length) :
type == "EMA" ? ta.ema(source, length) :
type == "SMMA (RMA)" ? ta.rma(source, length) :
type == "WMA" ? ta.wma(source, length) :
type == "VWMA" ? ta.vwma(source, length) :
type == "VwWMA" ? ta.ema(source*volume, length)/ta.ema(source, length) :
na

ma_type   = input.string(defval="VWMA", title="1-MA Type: ", options=["SMA", "EMA", "WMA",  "VWMA", "SMMA(RMA)"])

mas  = input(close, "MA Source")
ma1l = input(13, "MA 1 Length") 
ma2l = input(21, "MA 2 Length")
ma1 = ma(mas, ma1l, ma_type)
ma2 = ma(mas, ma2l, ma_type)
ma1p = ma1 > ma1[1]
ma1n = ma1 < ma1[1]
ma2p = ma2 > ma2[1]
ma2n = ma2 < ma2[1]
colorMA = ma1p and ma2p ? color.green : ma1n and ma2n ? color.red : color.black
plot(ma1, color = colorMA, linewidth = 2)
plot(ma2, color = colorMA, linewidth = 2)

最新更新