如何实施超级趋势跨界
我是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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论