Pine script 如何在脚本中添加alertcondition()函数?

Pine script 如何在脚本中添加alertcondition()函数?,pine-script,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

我是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")
/@version=2
策略(“随机+RSI,双策略(ChartArt)”,shorttitle=“CA_-RSI_Stoch_Strat”,overlay=true)
//沙塔特随机慢+相对强度指数,双策略
//
//版本1.0
//ChartArt于2015年10月23日提出的想法。
//
//这种策略结合了经典的RSI
//RSI增加时的销售策略
//70岁以上(或30岁以下购买),
//采用经典随机慢策略
//当随机振荡器
//超过价值80(并在
//该值低于20)。
//
//这个简单的策略只有在
//RSI和随机指数都在一起
//处于超买或超卖状态。
//
//我的工作清单:
// https://www.tradingview.com/u/ChartArt/
/////////////随机慢
Stochlength=输入(14,minval=1,title=“随机变量的回溯长度”)
StochoverBuw=输入(80,title=“随机超买条件”)
StochOverSold=输入(20,title=“随机超卖条件”)
smoothK=输入(3,title=“随机%K的平滑”)
smoothD=输入(3,title=“随机%K的移动平均值”)
k=sma(stoch(闭合、高、低、Stochlength)、smoothK)
d=sma(k,平滑d)
/////////////RSI
RSIlength=输入(14,minval=1,title=“RSI的回望长度”)
RSI超买=输入(70,title=“RSI超买条件”)
RSI超卖=输入(30,title=“RSI超卖条件”)
价格=收盘价
vrsi=rsi(rsi价格,rsi长度)
/////////////双策略:RSI策略+随机策略
if(非na(k)和非na(d))
if(交叉(k,d)和kstochoverbunk下交叉)
if(交叉低于(vrsi、RSI超买))
strategy.entry(“SHORT”,strategy.SHORT,comment=“SHORT”)
为此,我们可以使用函数:alertcondition()。我试图把它直接放在我的条件中,但范围不正确。如果我把它放在脚本的末尾并收回If语句的条件,那么在测试策略时我不会收到数据。此外,我无法获得所需的自定义警报

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

if(非na(k)和非na(d))
if(交叉(k,d)和kstochoverbunk下交叉)
if(交叉低于(vrsi、RSI超买))
strategy.entry(“SHORT”,strategy.SHORT,comment=“SHORT”)
短=真
长=假
alertcondition(SHORT==true,title='SHORT',message='SHORT message')
alertcondition(LONG==true,title='LONG',message='LONG message')

关于如何实施这些警报有什么想法吗?

无法为
策略创建警报。因此,你需要将你的战略转化为一个指标

请注意,调用该函数不会设置警报。您需要手动执行此操作


以下是
alertcondition
的示例。它通过示例告诉您需要了解的所有信息。

无法为
策略创建警报。因此,你需要将你的战略转化为一个指标

请注意,调用该函数不会设置警报。您需要手动执行此操作

以下是
alertcondition
的示例。它告诉你所有你需要知道的例子

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