Pine TradingView-警报脚本未触发ema穿越

时间:2020-03-11 21:06:44

标签: stock pine-script

我遇到了tradingview,试图创建一个脚本来触发对冲策略的警报。基本上,它按预期运行,但仅适用于过去的值。 (我认为它与EMA150计算有关,它仅适用于150天以上的值,但对此不作任何保证)

基本思路:

第一警报:如果股价低于EMA150和EMA5 始终买入信号

这是我第一次使用此脚本功能,所以如果我做错了,请纠正我!

//
study(title="Ema cross alert", overlay=true)

// var long and short to check if the last signal was long or short
var long = 1
var short = 1
resCustom = input(title="EMA Timeframe", type=input.resolution, defval="1440")
source = close

// input for ema
len2 = input(10, minval=1, title="EMA1")
len3 = input(5, minval=1, title="EMA2")
len4 = input(150 , minval=1, title="EMA3")

// calculate ema
ema2 = ema(source, len2)
ema3 = ema(source, len3)
ema4 = ema(source, len4)

// define cond.. for alert and last.. for timestamp

condlong = int(na)
condshort = int(na)
lastlong = int(na)
lastshort = int(na)

// check if close is under ema150, than activate long and short alerts with ema10 and ema5 crossings

if (close < ema(source, len4))
    if crossover(ema3, ema2)
        condlong = false
        condshort = false

        if (long == 1) 
            lastlong := time
            short := 1
            long := 0
            condlong = true
    else


        condlong = false
        condshort = false
        if crossunder(ema3, ema2)

            if (short == 1)

                lastshort := time
                long := 1
                short := 0
                condshort = true

else

    condlong = false
    condshort = false

// check if close is over ema150 if yes --> always long signal

if (short == 0 and long == 1 and (close > ema(source, len4)))
    condlong = true
    lastlong := time
    short := 1
    long := 0

// Trigger alerts

alertcondition(condlong, "Long / Buy")
alertcondition(condshort, "Short / Sell")

// Plot alerts

plotshape(lastlong, title="Long", color=color.green, style=shape.diamond, size=size.small, offset=0)
plotshape(lastshort, title="Short", color=color.red, style=shape.diamond, size=size.small, offset=0)

// plot ema

plot(ema2, "Ema 2", style=plot.style_line, linewidth=2, color=color.red)
plot(ema3, "Ema 3", style=plot.style_line, linewidth=2, color=color.blue)
plot(ema4, "Ema 4", style=plot.style_line, linewidth=2, color=color.green)```



0 个答案:

没有答案
相关问题