【问题标题】:How to program the strategy to ignore certain signals?如何编程策略以忽略某些信号?
【发布时间】:2022-06-28 17:55:03
【问题描述】:

//@version=5
strategy("My script")

uptrend = request.security(syminfo.tickerid, "60", (ta.ema(close, 9) > ta.ema(close, 21)))
downtrend = request.security(syminfo.tickerid, "60", (ta.ema(close, 9) < ta.ema(close, 21)))

buy = request.security(syminfo.tickerid, "5", ta.crossover(ta.ema(close, 9), ta.ema(close, 21)))
sell = request.security(syminfo.tickerid, "5", ta.crossunder(ta.ema(close, 9), ta.ema(close, 21)))

if uptrend
    if buy
        strategy.entry("buy", strategy.long)

if sell
    strategy.close("buy")

if downtrend
    if sell
        strategy.entry("sell", strategy.short)

if buy
    strategy.close("sell")

如图所示,在第一个信号出现后,如何执行脚本以忽略其余信号?

【问题讨论】:

    标签: pine-script pinescript-v5


    【解决方案1】:
    //@version=5
    strategy("My script")
    
    var tradeplaced = 0
    uptrend = request.security(syminfo.tickerid, "60", (ta.ema(close, 9) > ta.ema(close, 21)))
    downtrend = request.security(syminfo.tickerid, "60", (ta.ema(close, 9) < ta.ema(close, 21)))
    
    buy = request.security(syminfo.tickerid, "5", ta.crossover(ta.ema(close, 9), ta.ema(close, 21)))
    sell = request.security(syminfo.tickerid, "5", ta.crossunder(ta.ema(close, 9), ta.ema(close, 21)))
    
    if uptrend and uptrend[1]==false
        tradeplaced:=0
    if downtrend and downtrend[1]==false
        tradeplaced:=0
    
    if uptrend
        if buy and tradeplaced==0
            strategy.entry("buy", strategy.long)
            tradeplaced:=1
    if sell
        strategy.close("buy")
    
    if downtrend
        if sell and tradeplaced==0
            strategy.entry("sell", strategy.short)
            tradeplaced:=1
    if buy
        strategy.close("sell")
    

    Before After

    【讨论】:

      猜你喜欢
      • 2013-09-04
      • 2016-11-04
      • 1970-01-01
      • 2016-02-25
      • 2018-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多