如何实施超级趋势跨界

发布于 2025-01-29 03:27:06 字数 1800 浏览 4 评论 0原文

我是Pine脚本的新手。我正在尝试以低于条件的方式创建策略。

  • 如果当前价格大于超趋势(5,1),并且当前价格大于20 EMA,则“购买”
  • 如果当前价格小于超趋势(5,1),并且当前价格小于20 EMA,则“卖出”。

为了避免连续买卖信号,我在链接我如何停止多个购买/出售信号连续打印? (Pine,Pine,Script,Trading,View,Pinescript,TradingView)

现在信号无法正常工作。一旦购买信号逆转,我就不会收到卖出信号(反之亦然)。下面是我的代码,

//@version=5

indicator('EMA 1 min', shorttitle='EMA 1 min', overlay=true)
ema = ta.ema(close, 20)
[supertrend,direction] = ta.supertrend(5,1)
// Base condition
longentry = (close > supertrend) and (close > ema)
shortentry = (close < supertrend) and (close < ema)


// we create a variable that "saves" and doesn't calc on each bar 
var pos = 0

// we save it to a new number when long happens. Long can be 1 
if longentry and pos <= 0
    pos := 1
// we save it again when short happens. Short can be -1 
if shortentry and pos >= 0 
    pos := -1

// here we check if we have a newly detected change from another number to our pos number this bar
// Is pos equal to 1 and was it not equal to 1 one bar ago
longsignal  = pos ==  1 and (pos !=  1)[1]
shortsignal = pos == -1 and (pos != -1)[1]


plot(ema)

plotshape(longsignal, style=shape.triangleup, color=color.new(color.green, 0), text='BUY', editable=false, location=location.belowbar, size=size.small)
plotshape(shortsignal, style=shape.triangledown, color=color.new(color.black, 0), text='SELL', editable=false, location=location.abovebar, size=size.small)

请参见下面的图像,该图像没有逆转。

I am new to pine script. I am trying to create strategy with below condition.

  • if current price is greater than supertrend(5,1) and current price is greater than 20 EMA then "BUY"
  • if current price is less than supertrend(5,1) and current price is less than 20 EMA then "SELL".

In order to avoid the continuous buy sell signal I have included the code from the link How do I stop multiple BUY/SELL signals from printing in a row? (Pine, Script, Trading, View, PineScript, TradingView)

Now the signal is not working correctly. once the buy signal is reversed I am not getting sell signal (vice versa). Below is my code

//@version=5

indicator('EMA 1 min', shorttitle='EMA 1 min', overlay=true)
ema = ta.ema(close, 20)
[supertrend,direction] = ta.supertrend(5,1)
// Base condition
longentry = (close > supertrend) and (close > ema)
shortentry = (close < supertrend) and (close < ema)


// we create a variable that "saves" and doesn't calc on each bar 
var pos = 0

// we save it to a new number when long happens. Long can be 1 
if longentry and pos <= 0
    pos := 1
// we save it again when short happens. Short can be -1 
if shortentry and pos >= 0 
    pos := -1

// here we check if we have a newly detected change from another number to our pos number this bar
// Is pos equal to 1 and was it not equal to 1 one bar ago
longsignal  = pos ==  1 and (pos !=  1)[1]
shortsignal = pos == -1 and (pos != -1)[1]


plot(ema)

plotshape(longsignal, style=shape.triangleup, color=color.new(color.green, 0), text='BUY', editable=false, location=location.belowbar, size=size.small)
plotshape(shortsignal, style=shape.triangledown, color=color.new(color.black, 0), text='SELL', editable=false, location=location.abovebar, size=size.small)

See the below image where the signal is not reversing.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文