如何在我的脚本中添加alertcondition()函数?

时间:2019-09-13 15:54:08

标签: pine-script

我是TradingView的新手,目前正在测试pine脚本。我从ChartArt那里选了一个,它是一种双重指标策略:RSI和随机指标。

我想知道如何添加警报,但无法解决。 这里的策略:

//@version=2
strategy("Stochastic + RSI, Double Strategy (by ChartArt)", shorttitle="CA_-_RSI_Stoch_Strat", overlay=true)

// ChartArt's Stochastic Slow + Relative Strength Index, Double Strategy
//
// Version 1.0
// Idea by ChartArt on October 23, 2015.
//
// This strategy combines the classic RSI
// strategy to sell when the RSI increases
// over 70 (or to buy when it falls below 30),
// with the classic Stochastic Slow strategy
// to sell when the Stochastic oscillator
// exceeds the value of 80 (and to buy when
// this value is below 20).
//
// This simple strategy only triggers when
// both the RSI and the Stochastic are together
// in overbought or oversold conditions.
//
// List of my work: 
// https://www.tradingview.com/u/ChartArt/


///////////// Stochastic Slow
Stochlength = input(14, minval=1, title="lookback length of Stochastic")
StochOverBought = input(80, title="Stochastic overbought condition")
StochOverSold = input(20, title="Stochastic oversold condition")
smoothK = input(3, title="smoothing of Stochastic %K ")
smoothD = input(3, title="moving average of Stochastic %K")
k = sma(stoch(close, high, low, Stochlength), smoothK)
d = sma(k, smoothD)


///////////// RSI 
RSIlength = input( 14, minval=1 , title="lookback length of RSI")
RSIOverBought = input( 70  , title="RSI overbought condition")
RSIOverSold = input( 30  , title="RSI oversold condition")
RSIprice = close
vrsi = rsi(RSIprice, RSIlength)


///////////// Double strategy: RSI strategy + Stochastic strategy

if (not na(k) and not na(d))

    if (crossover(k,d) and k < StochOverSold)
        if (not na(vrsi)) and (crossover(vrsi, RSIOverSold))
            strategy.entry("LONG", strategy.long, comment="LONG")



if (crossunder(k,d) and k > StochOverBought)
    if (crossunder(vrsi, RSIOverBought))
        strategy.entry("SHORT", strategy.short, comment="SHORT")

为此,我们可以使用以下函数:alertcondition()。我试图将其直接放入条件中,但范围不正确。如果我将其放在脚本的处并取回if语句的条件,则在测试策略时我不会收到数据。同样,我也无法获得想要的自定义提醒。

我尝试的另一件事是通过创建两个bool参数,当在良好的if语句中时这些参数变为true。例如,alertcondition()中的条件变为SHORT == true。这样做,然后我得到以下错误:“添加到图表操作失败,原因:脚本必须至少调用一个输出函数(例如,绘图,条形等)。原因:AST为空”。

if (not na(k) and not na(d))

    if (crossover(k,d) and k < StochOverSold)
        if (not na(vrsi)) and (crossover(vrsi, RSIOverSold))
            strategy.entry("LONG", strategy.long, comment="LONG")
            SHORT = false
            LONG = true


if (crossunder(k,d) and k > StochOverBought)
    if (crossunder(vrsi, RSIOverBought))
        strategy.entry("SHORT", strategy.short, comment="SHORT")
        SHORT = true
        LONG  = false


alertcondition(SHORT == true, title='SHORT', message='SHORT message') 
alertcondition(LONG == true, title='LONG', message='LONG message') 

关于如何实施这些提示的任何想法吗?

1 个答案:

答案 0 :(得分:0)

无法为strategy创建警报。因此,您需要将策略转化为指标。

请注意,调用该功能将设置警报。您需要手动执行此操作。

这里是alertcondition中的documentation。它通过示例告诉您所有您需要了解的内容。