首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在我的脚本中添加add条件()函数?

如何在我的脚本中添加add条件()函数?
EN

Stack Overflow用户
提问于 2019-09-13 23:54:08
回答 1查看 563关注 0票数 1

我是TradingView新手,目前正在测试pine脚本。我从ChartArt取了一个,这是一个双指标策略: RSI和随机数。

我想知道如何添加提醒,但我想不通。这里的策略是:

代码语言:javascript
复制
//@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参数,它们在good if语句中变为true。例如,alertcondition()中的条件变为SHORT == true。通过这样做,我得到了以下错误:“添加到图表操作失败,原因:脚本必须至少有一个输出函数调用(如plot,barcolor等)。原因: AST为空”。

代码语言:javascript
复制
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') 

有关于如何实现这些警报的想法吗?

EN

回答 1

Stack Overflow用户

发布于 2019-09-14 01:25:45

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

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57926839

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档