繁体   English   中英

Pine v5 脚本 - 为什么这个百分比转换不起作用?

[英]Pine v5 script - why isn't this percentage conversion working?

我有一个 TradingView Pine v5 策略脚本,如果价格收盘价低于设定的 EMA 水平一定百分比时买入,如果价格收盘价高于相同 EMA 水平一定百分比时卖出。

这是它的工作代码:

// EMA Length
emaLength = input.int(title="EMA Length", defval=200, minval=0)

// EMA Price Percentages
emaPercBuy = input.float(title="Buy % Below EMA", defval=1.006, minval=0, step=0.001)      // 1.006 means 0.6%
emaPercSell = input.float(title="Sell % Above EMA", defval=1.006, minval=0, step=0.001)    // 1.006 means 0.6%

// EMA Line Function
emaLine = ta.ema(close, emaLength)

// EMA Plot line
plot(emaLine, color=close[1] > emaLine and close > emaLine ? colorGreen : colorRed, linewidth=2)

// Buy and Sell
longCond = close < emaLine/emaPercBuy
shortCond = close > emaLine*emaPercSell

defval=1.006是小数的 0.6%,行longCond = close < emaLine/emaPercBuy将 EMA 除以该小数(又名“emaPercBuy”),将 EMA 水平降低 -0.6%。 一切正常,但输入1.006看起来很麻烦,我希望能够在输入中输入0.6

这意味着让代码进行计算,所以如果我执行0.6 +100 /100将其转换回代码需要使用的值1.006

考虑到这一点,我修改了代码以允许输入看起来正常的百分比,但这不起作用 - 它没有在图表上显示任何交易:

// EMA Length
emaLength = input.int(title="EMA Length", defval=200, minval=0)

// EMA Price Percentages
emaPercBuy = input.float(title="Buy % Below EMA", defval=0.6, minval=0, step=0.1)
emaPercSell = input.float(title="Sell % Above EMA", defval=0.6, minval=0, step=0.1)

// EMA Line Function
emaLine = ta.ema(close, emaLength)

// EMA Plot line
plot(emaLine, color=close[1] > emaLine and close > emaLine ? colorGreen : colorRed, linewidth=2)

// Buy and Sell
longCond = close < emaLine/emaPercBuy+100/100
shortCond = close > emaLine*emaPercSell+100/100

线close < emaLine/emaPercBuy+100/100应该是正确的计算,但它不起作用 - 图表上没有出现交易,这与上面的第一段代码不同,它确实有效 - 但看起来很乱,1.006 意味着 0.6%。

我不知道为什么做+100/100没有解决它,因为这是正确的计算,我不知道我还能尝试什么。 似乎即使代码可以编译,我也忽略了一些简单的事情吗?

提前感谢任何可以回答这个问题的 Pine 编码员。

使用longCond = close < emaLine/emaPercBuy+100/100 ,您只需将1添加到emaLine/emaPercBuy的结果。

该表达式将被评估为(emaLine/emaPercBuy) + (100/100)

我会以另一种方式做到这一点。 我会像这样获得百分比值:

emaPercBuy = input.float(title="Buy % Below EMA", defval=0.6, minval=0, step=0.001) * 0.01

然后在不修改emaPercBuy的情况下计算值,如下所示:

longCond = (emaLine * (1 - emaPercBuy))

否则,每次使用时都需要将emaPercBuy转换为百分比。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM