【问题标题】:Pine script: Switch trendline based on if statementPine 脚本:根据 if 语句切换趋势线
【发布时间】:2021-07-08 08:32:58
【问题描述】:

如何根据 if 语句设置趋势线颜色和绘图,即如果股票数据 > 1 年,则使用趋势线 1,否则使用趋势线方法 2:

trend_1 = sma(close, 15)
trend_2 = sma(close, 30)

bearish = true

for i = 0 to trendConfirmationLength - 1
    bearish := bearish and (close[i] < trend[i])

bullish = not bearish

// Set the color of the plot based on whether we are bullish (green) or not (red)
c = bullish ? color.green : color.red

// Plot the trend line
trend = 
if (len(close) > 252)
   trend_1
if (len(close) < 252)
   trend_2

trend_plot = plot(trend, title='Trend', color = c)

谢谢

【问题讨论】:

    标签: if-statement pine-script


    【解决方案1】:
    trend_1 = sma(close, 15)
    trend_2 = sma(close, 30)
    
    float trend = na
    
    if bar_index >= 252
        trend := trend_1
    else
        trend := trend_2
    
    bool foundBearish = false
    bool foundBullish = false
    
    for i = 0 to trendConfirmationLength - 1
        if close[i] < trend[i]
            foundBearish := true
        else if close[i] > trend[i]
            foundBullish := true
    
    bullish = foundBullish and not foundBearish
    bearish = foundBearish and not foundBullish
    neutral = not bullish and not bearish
    
    color c = na
    if bullish
        c := color.green
    else if bearish
        c := color.red
    else if neutral
        c := color.blue
    
    trend_plot = plot(trend, title='Trend', color = c)
    

    如果trendConfirmationLength 内的所有关闭都低于trend,则foundBearish 将为真,foundBullish 将为假(反之亦然)。如果foundBearshfoundBullish 都为真,这意味着我们在trend 的上方和下方都有关闭。

    或者,您可以这样做以保持趋势颜色直到形成新趋势,而不是显示中性颜色。

    color c = na
    if bullish
        c := color.green
    else if bearish
        c := color.red
    
    trend_plot = plot(trend, title='Trend', color = fixnan(c))
    

    【讨论】:

      猜你喜欢
      • 2010-09-11
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多