研究实时警报会重置初始化的变量,但在历史数据上效果很好

问题描述 投票:0回答:1

[为了获得警报,只有在研究中才有可能,我从策略中创建了研究,但我故意不使用金字塔。警报设置为“每栏关闭一次”。

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

  1. 初始化布尔变量Last_is_longLast_is_short以存储最后一步是长线还是短线
  2. 将布尔值作为完整的longCondition的一部分,并将其作为完整的shortCondition的一部分
  3. 我基于这样的完整条件(包括布尔值,因此得出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_is_longLast_is_short变量总是被重新初始化(因此被初始化为false),因为警报出现在每个小节中。

解决它的一种方法是重新构建整个基本的myLongCondition()myShortCondition()函数,以免内在地发生金字塔式叠加,但是我看不到一种不应该再次陷入不应该重新初始化的布尔值的方法。

有什么想法可以克服吗?

出于好奇:带有警报的研究是否在每个下一个执行步骤中都简单地重新初始化变量,这是一种已知的期望行为吗?

=========================================>

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

 // 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)

[为了获得警报,只有在研究中才有可能,我从策略中创建了研究,但我故意不使用金字塔。警报设置为“每条关闭一次”。因为研究没有...

pine-script algorithmic-trading
1个回答
0
投票

您的代码逻辑听起来不错,并且警报在这里正常工作。也许您可以尝试使用调试代码(如此处包括的代码)在最后验证状态:

© www.soinside.com 2019 - 2024. All rights reserved.