松树脚本中的买入和卖出信号,条件为移动平均线

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

我希望在移动平均线首次按以下顺序排列时收到买入信号:

sma200>sma100>sma50>sma20

并在以下订单首次建立时获得卖出信号:

sma200

这是我的代码:

study(title="4SMA", shorttitle="4SMA", overlay=true)

SMA1 = input(20, minval=1, title="SMA1"),
SMA2 = input(50, minval=1, title="SMA2")
SMA3 = input(100, minval=1, title="SMA3")
SMA4 = input(200, minval=1, title="SMA4"),

plot(sma(close, SMA1), color=green, linewidth=2)
plot(sma(close, SMA2), color=yellow, linewidth=2)
plot(sma(close, SMA3), color=orange, linewidth=2)
plot(sma(close, SMA4), color=red, linewidth=2)
signals pine-script pine-script-v5 trading
1个回答
0
投票

这是您的 Pine v4

4SMA
研究已转换为 Pine v5 指标,您可以在其上创建买入/卖出警报:

//@version=5
indicator(title = "4SMA", shorttitle = "4SMA", overlay = true, max_labels_count = 500)

// Inputs
int sma1Length = input.int(20, minval = 1, title = "SMA1")
int sma2Length = input.int(50, minval = 1, title = "SMA2")
int sma3Length = input.int(100, minval = 1, title = "SMA3")
int sma4Length = input.int(200, minval = 1, title = "SMA4")

// TA calculations
float sma1 = ta.sma(close, sma1Length)
float sma2 = ta.sma(close, sma2Length)
float sma3 = ta.sma(close, sma3Length)
float sma4 = ta.sma(close, sma4Length)

// Signals

// Every time the SMAs are aligned as sma4 > sma3 > sma2 > sma1 on bar close, consider it as a buy signal (only once every time the EMAs are aligned)
bool buySignal = barstate.isconfirmed and ta.change(sma4 > sma3 and sma3 > sma2 and sma2 > sma1)

// Every time the SMAs are aligned as sma4 < sma3 < sma2 < sma1 on bar close, consider it as a sell signal (only once every time the EMAs are aligned)
bool sellSignal = barstate.isconfirmed and ta.change(sma4 < sma3 and sma3 < sma2 and sma2 < sma1)

// Alerts

// Create alert conditions so they can be used as a "Condition" in the alert creation dialog
alertcondition(buySignal, "Buy signal", "Buy signal message")
alertcondition(sellSignal, "Sell signal", "Sell signal message")

// Plots
plot(sma1, color = color.green, linewidth = 2)
plot(sma2, color = color.yellow, linewidth = 2)
plot(sma3, color = color.orange, linewidth = 2)
plot(sma4, color = color.red, linewidth = 2)

// Labels (for debugging purposes, so that you know where each alert would have been sent in the past)
// Only draw each label once per bar close
if barstate.isconfirmed
    if buySignal
        label.new(bar_index, high, "Buy signal", style = label.style_label_down, color = color.green, textcolor = color.white)

    if sellSignal
        label.new(bar_index, low, "Sell signal", style = label.style_label_up, color = color.red, textcolor = color.white)

创建警报时,选择指示器和所需信号:

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