仅当先前的一系列条件无效时才绘制信号

时间:2018-10-23 18:17:26

标签: tradingview-api pine-script

我正在尝试过滤掉条形条件,并按期权仅显示条件系列的第一个条形。 See my Image我被困住了,找不到解决办法。希望得到帮助。谢谢

    //inputs
    srcClose = close
    resCustom = input(title="Timeframe in Minutes", type=string, defval="343")
    len1 = input(7, title="MA7")
    len2 = input(77, title="MA77")
    len3 = input(231, title="MA231")
    useCurrentRes = input(false, title="Use Current Chart Resolution?")
    res = useCurrentRes ? period : resCustom
    colbar = input(true, title="Color Bars")
    signals = input(true, title="Buy and Sell Signals")
    colgaps = input(true, title="Colored Gaps")

    //EMA calculation
    ema7Calc = ema(srcClose, len1)
    ema77Calc = ema(srcClose, len2)
    ema231Calc = ema(srcClose, len3)

    output1 = security(tickerid, res, ema7Calc)
    output2 = security(tickerid, res, ema77Calc)
    output3 = security(tickerid, res, ema231Calc)

    //Conditions
    emacrossbuy = (close > output2 and crossover(close,output1))
    emacrosssell = (close < output2 and crossunder(close,output1))
    emacrsstrng = (crossover(close,output3) and output1 >= output2) or (crossover(close,output2) and output1 >= output3)
    emacrsstrngsl = (crossunder(close,output2) and output1 <= output2)
    underwater = (close < output3 and output1 < output2 or output3)
    overwater = (close > output3 and output1 > output3)

    //Ploting Barcolors
    barcolor((signals and emacrossbuy or emacrsstrng) ? lime : na, title="Long Signal Bars ", editable=true)
    barcolor((signals and emacrosssell or emacrsstrngsl) ? red : na, title="Short Signal Bars ", editable=true)
    barcolor((colbar and overwater) ? white : na, title="Overwater", editable=true)
    barcolor((colbar and underwater) ? #444444 : na, title="Underwater", editable=true)`enter code here`

1 个答案:

答案 0 :(得分:0)

这并不难,但是需要反复试验。 我这样解决了它,但可能可以对其进行更多优化。

使用类似这样的内容:

buy = false // defines variable, mandatory in v3
sell = false // defines variable, mandatory in v3
buy := buy[1] // sets last bar value as current
sell := sell[1] // sets last bar value as current

if (cond 1) or (cond 2) or (cond 3) and (buy = false) // if it meets conditions and buy value is false, then set "buy" flag and close "sell" flag
buy := true
sell := false

if (cond 4) or (cond 5) or (cond 6) and (sell = false) // if it meets conditions and sell value is false, then set "sell" flag and close "buy" flag
sell := true
buy := false

希望有帮助。