Pine script Study live alerts重置初始化的变量,但在历史数据上效果良好

Pine script Study live alerts重置初始化的变量,但在历史数据上效果良好,pine-script,algorithmic-trading,Pine Script,Algorithmic Trading,为了获得警报,只有在研究中才可能,我根据一个策略创建了一个研究,在这个策略中,我故意不使用金字塔。警报设置为每关闭一个酒吧执行一次 因为研究没有金字塔选项,我在研究中编程了一个类似的功能,以避免金字塔,如下所示: 初始化布尔变量Last_is_long和Last_is_short以存储上次移动是长还是短 将布尔值作为完整的longCondition和完整的shortCondition 我根据这些完整的条件(包括布尔值)生成了一个alertcondition(),因此 比如: //@version

为了获得警报,只有在研究中才可能,我根据一个策略创建了一个研究,在这个策略中,我故意不使用金字塔。警报设置为每关闭一个酒吧执行一次

因为研究没有金字塔选项,我在研究中编程了一个类似的功能,以避免金字塔,如下所示:

  • 初始化布尔变量
    Last_is_long
    Last_is_short
    以存储上次移动是长还是短
  • 将布尔值作为完整的
    longCondition
    和完整的
    shortCondition
  • 我根据这些完整的条件(包括布尔值)生成了一个
    alertcondition()
    ,因此
  • 比如:

    //@version=4
    study(title="MyStudy", shorttitle="MyStudy", overlay=true)
    ...
    //VARIABLE INITIALIZATION:
    var bool Last_is_long = false
    var bool Last_is_short = false
    ...
    longCondition = myLongCondition() and not(Last_is_long)
    shortCondition = myShortCondition() and not(Last_is_short)
    ...
    alertcondition(longCondition, title='Long', message='Long now!')
    if longCondition
        label.new(time, close, text = "Long", color=color.lime, xloc = xloc.bar_time)
        Last_is_long := true
        Last_is_short := false
    ...
    alertcondition(shortCondition, title='Short', message='Short now!')
    if shortCondition
        label.new(time, close, text = "Short", color=color.red, xloc = xloc.bar_time)
        Last_is_long := false
        Last_is_short := true
    
    当查看图表时,它工作得非常好。然而,当警报打开时,看起来像是
    Last\u is\u long
    Last\u is\u short
    变量总是被重新初始化(因此为
    false
    ),因为警报出现在每个条中

    解决这个问题的一种方法是重新处理整个基本的
    myLongCondition()
    myShortCondition()
    函数,以避免本质上的金字塔化,但我看不到一种方法可以避免再次陷入不应该重新初始化的布尔函数

    有什么办法克服它吗

    出于好奇:使用警报进行研究是否是一种已知和期望的行为,只是在下一个执行步骤重新初始化变量

    =========================================

    编辑(下面的完整脚本):

    //许可信息:免费。不过,如果能推荐我,我将不胜感激。
    //说明:用于3分钟到1小时的BTCUSD(或XBTUD)。
    //其想法是提供一种工具,用于检测EMA周围死区的突发事件,并检测死区的回发事件。
    //检测基本上是基于多少%,至少,某个蜡烛体打破了死区(可调)。
    //长和短标志以及死区都被放置在图表上。可用于生成ALER。
    //只需稍加修改,就可以将其转换为策略脚本。
    //如果你想玩的话,下面是一些想法。有待改进的地方:
    //-将常数转换为输入,并辅助多样化阈值
    //-与wether玩耍或不使用金字塔(此处金字塔被阻止)
    //-查看序列中的几个条,而不仅仅是当前条
    //©埃斯特里里奥:
    //电视:https://www.tradingview.com/u/esturilio/
    //因此:https://stackoverflow.com/users/12678720/glauco-esturilio?tab=profile
    //GH:esturilio
    //TW:@esturilio
    //@版本=4
    研究(title=“死区交叉”,shorttitle=“DB-X”,overlay=true)
    //===初始变量===(研究需要,因为研究不知道金字塔)
    var bool Last_为_long=false
    var bool Last_是_short=错误
    //为可视化定义EMA曲线
    emaLen=输入(12,最小值=1,title=“EMA长度”)
    emaSrc=输入(打开,title=“EMA的源代码”)
    emaVal=ema(emaSrc,emaLen)
    绘图(emaVal,title=“EMA”,color=color.blue)
    //定义死区:
    var float SigmaVar=input(0.30,minval=0,title=“%deadband”)//EMA周围的频带
    SigmaUp=emaVal*(1+SigmaVar/100)
    SigmaDw=emaVal*(1-SigmaVar/100)
    绘图(SigmaUp,title=“Band+”,color=color.orange)
    绘图(SigmaDw,title=“Band-”,color=color.orange)
    //交叉检测阈值的定义:
    bar_bodypercent_threshold=输入(50,title=“%bar threshold for cross detection”)
    // =============================================================================
    //检测交叉或已交叉的情况:
    n_bar_span=abs(关闭-打开)
    //以下条件将用于检测看涨情况
    bull_cross()=>(关闭>SigmaUp并打开SigmaUp并打开>SigmaUp)
    背负十字()=>(关闭>SigmaDw,打开(关闭SigmaDw)或(关闭(关闭SigmaUp)
    // =============================================================================
    //向上交叉时,计算SigmaUp上方的杆百分比
    百分之_-bar_高于_-SigmaUp=如果牛市交叉()
    (100*((最大(关闭、打开)-SigmaUp)/n_bar_跨度)
    其他的
    0
    //向下交叉时,计算SigmaUp下方的杆百分比
    百分之_-bar_-below_-SigmaUp=如果牛背_-cross()
    (100*((SigmaUp-min(关闭、打开))/n_bar_span)
    其他的
    0
    //向下交叉时,计算SigmaDw下方的杆百分比
    百分之_-bar_低于_-SigmaDw=如果承受_交叉()
    (100*((SigmaDw-最小(关闭、打开))/n_bar_span)
    其他的
    0
    //交叉时,计算SigmaDw上方的杆百分比
    百分之_条_高于_SigmaDw=如果背负_交叉()
    (100*((最大(关闭、打开)-SigmaDw)/n_bar_跨度)
    其他的
    0
    // =============================================================================
    //最终触发:
    //长期状况:公牛或熊的背部
    longCondition=((关闭>SigmaUp)和(高于SigmaUp>=棒体百分比\U阈值的百分比)或((关闭=棒体百分比\U阈值的百分比))
    //缺点:要么是熊,要么是牛
    短条件=((闭合=bar\U BODYPERT\U阈值)或((闭合>SigmaDw)和(小于等于bar\U SigmaDw>=bar\U BODYPERT\U阈值的bar\U))
    longConditionFull=longCondition且Last_为_long==false
    alertcondition(longConditionFull,title='LongTrigger',message='LongTrigger')
    如果已满
    //如果转换为策略,您可以取消行下的注释以激活订单:
    //strategy.entry(“Long”,strategy.Long)
    //绘制绿色箭头
    label.new(time,close,text=“L”,style=label.style\u labelup,yloc=yloc.belowbar,color=color.lime,xloc=xloc.bar\u time)
    //确保不允许使用金字塔
    Last_is_long:=真
    Last_is_short:=false
    短裙
    
     // Licensing information: free as bird. A referral to me would be much appreciated, though.
    // Description: meant to be used with BTCUSD (or XBTUSD) on 3 mins to 1h candles charts.
    // Idea is to provide a tool to detect break-out's from a dead band around an EMA, and to detect back-in's to the dead band.
    // Detection is fundamentally based on how much %, at least, a certain candle body has ruptured the dead band (adjustable).
    // Long and short flags are placed on the chart, as well as the deadband. Can be used to generate alers.
    // With minimal modifications, can be convert to a Strategy script.
    
    // Following are ideas to play around if you want. Room for improvements:
    //  - convert constants into inputs, and diversify thresholds assimetrically
    //  - play around with wether or not using pyramiding (here pyramiding is blocked)
    //  - look at several bars in a sequence, not only current
    
    // © esturilio:
    //  TV  : https://www.tradingview.com/u/esturilio/
    //  SO  : https://stackoverflow.com/users/12678720/glauco-esturilio?tab=profile
    //  GH  : esturilio
    //  TW  : @esturilio
    
    //@version=4
    study(title="Deadband cross", shorttitle="DB-X", overlay=true)
    
    // === INIT VARIABLES === (needed on Study because Studies don't know about pyramiding)
    var bool Last_is_long = false
    var bool Last_is_short = false
    
    // Defining the EMA curve for visualization
    emaLen = input(12, minval=1, title="EMA Length")
    emaSrc = input(open, title="Source for EMA")
    emaVal = ema(emaSrc, emaLen)
    plot(emaVal, title="EMA", color=color.blue)
    
    // Defining the deadbands:
    var float SigmaVar = input(0.30, minval=0, title="% deadband") // band around EMA
    SigmaUp = emaVal * (1 + SigmaVar/100)
    SigmaDw = emaVal * (1 - SigmaVar/100)
    plot(SigmaUp, title="Band+", color=color.orange)
    plot(SigmaDw, title="Band-", color=color.orange)
    
    // Definition of the threshold for cross detection:
    bar_bodypercent_threshold = input(50,title="%bar threshold for cross detection")
    
    // =============================================================================
    // detect cross or already crossed situations:
    n_bar_span = abs(close - open)
    
    // following conditions will be used to detect a bullish situation
    bull_cross() => (close > SigmaUp and open < SigmaUp) or (close > SigmaUp and open > SigmaUp)
    bear_back_cross() => (close > SigmaDw and open < SigmaDw)
    
    // following conditions will be used to detect a bearish situation
    bear_cross() => (close < SigmaDw and open > SigmaDw) or (close < SigmaDw and open < SigmaDw)
    bull_back_cross() => (close < SigmaUp and open > SigmaUp)
    
    
    // =============================================================================
    // Calculate bar% above SigmaUp when crossing upwards
    percent_bar_above_SigmaUp = if bull_cross()
        (100*(( max(close,open) - SigmaUp)/n_bar_span))
    else
        0
    
    // Calculate bar% below SigmaUp when crossing back down
    percent_bar_below_SigmaUp = if bull_back_cross()
        (100*(( SigmaUp - min(close,open))/n_bar_span))
    else
        0
    
    // Calculate bar% below SigmaDw when crossing downwards    
    percent_bar_below_SigmaDw = if bear_cross()
        (100*(( SigmaDw - min(close,open))/n_bar_span))
    else
        0
    
    // Calculate bar% above SigmaDw when crossing back up
    percent_bar_above_SigmaDw = if bear_back_cross()
        (100*((max(close,open) - SigmaDw)/n_bar_span))
    else
        0
    
    // =============================================================================
    // FINAL TRIGGERNIG:
    
     //longCondition: either bull or back from bear
    longCondition  = ((close > SigmaUp) and (percent_bar_above_SigmaUp >= bar_bodypercent_threshold)) or ((close < SigmaUp) and (percent_bar_above_SigmaDw >= bar_bodypercent_threshold))
    
     //shortCondition: either bear or back from bull 
    shortCondition = ((close < SigmaDw) and (percent_bar_below_SigmaDw >= bar_bodypercent_threshold)) or ((close > SigmaDw) and (percent_bar_below_SigmaUp >= bar_bodypercent_threshold))
    
    
    longConditionFull = longCondition and Last_is_long == false
    alertcondition(longConditionFull, title='LongTrigger', message='LongTrigger')
    
    if  longConditionFull
        //if converting to strategy, you can uncomment below line for activating orders:
        //strategy.entry("Long",strategy.long)
    
        //plot green arrow
        label.new(time, close, text = "L", style=label.style_labelup, yloc=yloc.belowbar, color=color.lime, xloc = xloc.bar_time)
    
        //make sure pyramiding is not allowed 
        Last_is_long := true
        Last_is_short := false
    
    
    shortConditionFull = shortCondition and Last_is_short == false
    alertcondition(shortConditionFull, title='ShortTrigger', message='ShortTrigger')
    
    if shortConditionFull
        //if converting to strategy, you can uncomment below line for activating orders:
        //strategy.entry("Short",strategy.short)
    
        //plot red arrow
        label.new(time, close, text = "S", style=label.style_labeldown, yloc=yloc.abovebar, color=color.red, xloc = xloc.bar_time)
    
        //make sure pyramiding is not allowed 
        Last_is_long := false
        Last_is_short := true
    
    //DEBUG & PLOTTING: 
     // Bull and back from bull
    //plot(percent_bar_above_SigmaUp, title="%above EMA+s",color=color.green)
    //plot(percent_bar_below_SigmaUp, title="%above EMA+s",color=color.olive)
    
     // Bear and back from bear
    //plot(percent_bar_below_SigmaDw, title="%below EMA-s",color=color.red)
    //plot(percent_bar_above_SigmaDw, title="%below EMA-s",color=color.fuchsia)
    
    //@version=4
    study(title="MyStudy", shorttitle="MyStudy", overlay=true)
    
    //VARIABLE INITIALIZATION:
    var bool Last_is_long = false
    var bool Last_is_short = false
    
    barUp = close >= open
    myLongCondition() => barUp and barUp[1]
    myShortCondition() => not barUp and not barUp[1]
    
    longCondition = myLongCondition() and not(Last_is_long)
    shortCondition = myShortCondition() and not(Last_is_short)
    
    alertcondition(longCondition, title='Long', message='Long now!')
    if longCondition
        label.new(time, close, text = "Long", color=color.lime, xloc = xloc.bar_time)
        Last_is_long := true
        Last_is_short := false
    
    alertcondition(shortCondition, title='Short', message='Short now!')
    if shortCondition
        label.new(time, close, text = "Short", color=color.red, xloc = xloc.bar_time)
        Last_is_long := false
        Last_is_short := true
    
    bgcolor(Last_is_long ? color.green : Last_is_short ? color.maroon : na) 
    plotchar(Last_is_long, "Last_is_long", "", location.top)
    plotchar(Last_is_short, "Last_is_short", "", location.top)
    plotchar(longCondition, "longCondition", "▲", location.top)
    plotchar(shortCondition, "shortCondition", "▼", location.bottom)