使用非传统图表的pine脚本中基于百分比的停止

2024-05-29 04:58:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用在传统图表上执行的renko信号,用pine脚本创建了一个好的、简单的工作策略。为了限制提款,我试图增加一个基于百分比的止损点。在阅读了一篇关于kodify的文章后,我试图添加代码,但没有成功。在第24/25行和第33/35行“long/shortLossPerc”和“long/shortStopPrice”上获取未声明的标识符错误。要么我的代码中有错误,要么发生了其他事情。可能是因为我读的文章引用的是版本3而不是版本4?可能是语法错误,也可能是因为我引用了来自renko图表的信号的价格数据,但以实时传统图表价格执行。感谢您的反馈

//@version=4
strategy("DC", overlay=true) //, resolution="", default_quantity_value=1)
//startDate = input(title="Start Date", type=input.integer, defval=1, minval=1, maxval=31)
//startMonth = input(title="Start Month", type=input.integer, defval=1, minval=1, maxval=12)
//startYear = input(title="Start Year", type=input.integer, defval=2019, minval=2000, maxval=2100)
//endDate = input(title="End Date", type=input.integer, defval=1, minval=1, maxval=31)
//endMonth = input(title="End Month", type=input.integer, defval=1, minval=1, maxval=12)
//endYear = input(title="End Year", type=input.integer, defval=2020, minval=2000, maxval=2100)
//t = tickerid(syminfo.prefix, syminfo.ticker)
//realC = security(t, timeframe.period, close)
//plot(realC)
length = input(1, minval=1)
lower = lowest(length)
upper = highest(length)
basis = avg(upper, lower)
//plot(basis, "Basis", color=#FF6A00)
//u = plot(upper, "Upper", color=#0094FF)
//l = plot(lower, "Lower", color=#0094FF)
//fill(u, l, color=#0094FF, transp=95, title="Background")
longLossPerc = input(title="Long Stop Loss (%)", 
     type=float, minval=0.0, step=0.1, defval=1) * 0.01
shortLossPerc = input(title="Short Stop Loss (%)", 
     type=float, minval=0.0, step=0.1, defval=1) * 0.01
longStopPrice = strategy.position_avg_price * (1- longLossPerc)
shortStopPrice = strategy.position_avg_price * (1 + shortLossPerc)
longCondition = security(renko(syminfo.tickerid, "Traditional", 200), timeframe.period, rising(basis, 2))
shortCondition = security(renko(syminfo.tickerid, "Traditional", 200), timeframe.period, falling(basis, 2))
if (longCondition)
    strategy.entry("Long", strategy.long)
if (shortCondition)
    strategy.entry("Short", strategy.short)
if (strategy.position_size > 0)
    strategy.exit(id="LS", stop=longStopPrice)
if (strategy.position_size < 0)
    strategy.exit(id="SS", stop=shortStopPrice)

Tags: inputifplottitletypepositionbasisinteger
1条回答
网友
1楼 · 发布于 2024-05-29 04:58:57

将代码/语法更改为

stopPer = input(1.0, title="Stop Loss %", type=input.float) / 100
longStop = strategy.position_avg_price * (1 - stopPer)
shortStop = strategy.position_avg_price * (1 + stopPer)
if strategy.position_size > 0 
    strategy.exit(id="Close Long", stop=longStop)
if strategy.position_size < 0
    strategy.exit(id="Close Short", stop=shortStop)

效果很好。也许在我不知道的版本3和版本4之间的编码有一些差异。这段代码比我最初尝试的要简单得多。此外,您还可以通过添加

process_orders_on_close=true

在你的“战略”路线中。这样,一旦你进入一个位置,止损就会立即增加,而不是在你进入的蜡烛关闭时。希望这些信息有帮助

相关问题 更多 >

    热门问题