需要帮助将Pinescript V2转换为版本5

发布于 2025-01-20 18:13:40 字数 945 浏览 3 评论 0原文

我通过图表艺术找到了这段代码。我正在制定自己的策略,我会喜欢我自己的这个功能。它为我的入场信号提供了更好的确认。我正在尝试自己学习 pinescript,但我认为如果我学习 pinescript v2 和 v5 将花费大量时间和精力,所以我请求一些帮助将其转换为 V5,然后我可以继续改进代码。我真的很感激任何帮助。预先感谢大家。

//@version=2

threshold = input(title="Price Difference Threshold", type=float, defval=0, step=0.001)

getDiff() =>
    yesterday=security(tickerid, 'D', close[1])
    today=security(tickerid, 'D', close)
    delta=today-yesterday
    percentage=delta/yesterday
    
closeDiff = getDiff()
 
buying = closeDiff > threshold ? true : closeDiff < -threshold ? false : buying[1]

hline(0, title="zero line")

bgcolor(buying ? #3399FF : #FFFFFF , transp=1)
plot(closeDiff, color=silver, style=area, transp=75)
plot(closeDiff, color=aqua, title="prediction")

longCondition = buying
if (longCondition)
    strategy.entry("Long", strategy.long)
    
shortCondition = buying != true
if (shortCondition)
    strategy.entry("Short", strategy.short)

I found this code by chart art. I am building my own strategy and I would love this feature in mine. It provides better confirmation for my entry signals. Im trying to learn pinescript myself but I think it would take a lot of time and effort if I learn pinescript v2 and v5, so I am asking some help to convert this to V5 then I can continue improve the code. I would really appreciate any help. Thank you all in advance.

//@version=2

threshold = input(title="Price Difference Threshold", type=float, defval=0, step=0.001)

getDiff() =>
    yesterday=security(tickerid, 'D', close[1])
    today=security(tickerid, 'D', close)
    delta=today-yesterday
    percentage=delta/yesterday
    
closeDiff = getDiff()
 
buying = closeDiff > threshold ? true : closeDiff < -threshold ? false : buying[1]

hline(0, title="zero line")

bgcolor(buying ? #3399FF : #FFFFFF , transp=1)
plot(closeDiff, color=silver, style=area, transp=75)
plot(closeDiff, color=aqua, title="prediction")

longCondition = buying
if (longCondition)
    strategy.entry("Long", strategy.long)
    
shortCondition = buying != true
if (shortCondition)
    strategy.entry("Short", strategy.short)

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷清清 2025-01-27 18:13:40

直接v2 - &gt; V5转换看起来像这样:

//@version=5
strategy("")
threshold = input.float(title="Price Difference Threshold", defval=0, step=0.001)

getDiff() =>
    yesterday=request.security(syminfo.tickerid, 'D', close[1], lookahead=barmerge.lookahead_on)
    today=request.security(syminfo.tickerid, 'D', close, lookahead=barmerge.lookahead_on)
    delta=today-yesterday
    percentage=delta/yesterday
    
closeDiff = getDiff()

bool buying = na 
buying := closeDiff > threshold ? true : closeDiff < -threshold ? false : buying[1]

hline(0, title="zero line")

bgcolor(buying ? #3399FF : #FFFFFF , transp=1)
plot(closeDiff, color=color.new(color.silver, 75), style=plot.style_area)
plot(closeDiff, color=color.aqua, title="prediction")

longCondition = buying
if (longCondition)
    strategy.entry("Long", strategy.long)
    
shortCondition = buying != true
if (shortCondition)
    strategy.entry("Short", strategy.short)

请注意,脚本使用security()函数从“ D”时间范围请求数据,这允许它可以展望未来并在实时图表上进行栏的值(更多信息在这里)。这是在新脚本中皱眉的。为避免这种情况,应使用Barmerge.loohakead_off(默认情况下,在V3 -V5中默认打开)。在下面的脚本中,我更改了它,因此结果将与原始脚本有所不同,但是它是最好的,因为原始脚本运行的数据应该无法访问:

//@version=5
strategy("")
threshold = input.float(title="Price Difference Threshold", defval=0, step=0.001)

getDiff() =>
    [today, yesterday] = request.security(syminfo.tickerid, 'D', [close, close[1]], lookahead=barmerge.lookahead_off)
    delta = today - yesterday
    percentage = delta / yesterday
    
closeDiff = getDiff()

bool buying = na 
buying := closeDiff > threshold ? true : closeDiff < -threshold ? false : buying[1]

hline(0, title="zero line")

bgcolor(buying ? color.new(#3399FF, 1) : color.new(#FFFFFF, 1))
plot(closeDiff, color=color.new(color.silver, 75), style=plot.style_area)
plot(closeDiff, color=color.aqua, title="prediction")

longCondition = buying
if (longCondition)
    strategy.entry("Long", strategy.long)
    
shortCondition = buying != true
if (shortCondition)
    strategy.entry("Short", strategy.short)

The direct v2 -> v5 conversion would look like this:

//@version=5
strategy("")
threshold = input.float(title="Price Difference Threshold", defval=0, step=0.001)

getDiff() =>
    yesterday=request.security(syminfo.tickerid, 'D', close[1], lookahead=barmerge.lookahead_on)
    today=request.security(syminfo.tickerid, 'D', close, lookahead=barmerge.lookahead_on)
    delta=today-yesterday
    percentage=delta/yesterday
    
closeDiff = getDiff()

bool buying = na 
buying := closeDiff > threshold ? true : closeDiff < -threshold ? false : buying[1]

hline(0, title="zero line")

bgcolor(buying ? #3399FF : #FFFFFF , transp=1)
plot(closeDiff, color=color.new(color.silver, 75), style=plot.style_area)
plot(closeDiff, color=color.aqua, title="prediction")

longCondition = buying
if (longCondition)
    strategy.entry("Long", strategy.long)
    
shortCondition = buying != true
if (shortCondition)
    strategy.entry("Short", strategy.short)

Note that the script uses the security() function to request data from the "D" timeframe, which allows it to look ahead into the future and get values of bars before it would be possible to do so on real-time charts (more info here). This is frowned upon in new scripts. To avoid this, barmerge.loohakead_off should be used (it is turned on by default in v3 - v5). In the script below, I changed it, so the results will differ from the original script, but it is for the best, because the original script operates data that it should not be able to access:

//@version=5
strategy("")
threshold = input.float(title="Price Difference Threshold", defval=0, step=0.001)

getDiff() =>
    [today, yesterday] = request.security(syminfo.tickerid, 'D', [close, close[1]], lookahead=barmerge.lookahead_off)
    delta = today - yesterday
    percentage = delta / yesterday
    
closeDiff = getDiff()

bool buying = na 
buying := closeDiff > threshold ? true : closeDiff < -threshold ? false : buying[1]

hline(0, title="zero line")

bgcolor(buying ? color.new(#3399FF, 1) : color.new(#FFFFFF, 1))
plot(closeDiff, color=color.new(color.silver, 75), style=plot.style_area)
plot(closeDiff, color=color.aqua, title="prediction")

longCondition = buying
if (longCondition)
    strategy.entry("Long", strategy.long)
    
shortCondition = buying != true
if (shortCondition)
    strategy.entry("Short", strategy.short)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文