我正在尝试在给定的时间范围图表上显示不同的时间范围macd。因此在1分钟图表上显示5分钟MACD。
我决定通过将数字5乘以一个整数间隔来实现这一点,然后将其转换为字符串并在绘图中使用它。 由于我不需要这样做,所以效果很好;不必每次将图表的时间范围从1分钟更改为10分钟等时都进行更改,并且它仍会基于倍数显示更长的时间范围macd。
以下代码使用三元运算符?可以正常工作:
//@version = 2
study(title="test")
source = close
fastLength = input(12, minval=1)
slowLength=input(26,minval=1)
signalLength=input(9,minval=1)
// res5 mutiplies the current interval which is an integer by a factor 5 and turns it into a string with the value of "interval*5" or "1D" depending on the value of interval*5
res5= interval*5 < 1440 ? tostring(interval*5) : "1D"
src5=security(tickerid, res5, close)
fastMA5 = ema(src5, fastLength)
slowMA5 = ema(src5, slowLength)
macd5 = fastMA5 - slowMA5
signal5 = sma(macd5, signalLength)
outMacD5 = security(tickerid, res5, macd5)
plot( outMacD5 ? outMacD5 : na, color= red)
但是如果我将其更改为具有如下所示的更多条件,则三元运算符将失败。
//@version = 2
study(title="test")
source = close
fastLength = input(12, minval=1)
slowLength=input(26,minval=1)
signalLength=input(9,minval=1)
// res5 mutiplies the current interval which is an integer by a factor 5 and turns it into a string with the value of "interval*5" or "1D" depending on the value 9of inteval*5
//res5= interval*5 < 1440 ? tostring(interval*5) : "1D"
res5= interval*5 < 1440 ? tostring(interval*5) : interval >= 1440 and interval*5 < 2880 ? "1D":na
src5=security(tickerid, res5, close)
fastMA5 = ema(src5, fastLength)
slowMA5 = ema(src5, slowLength)
macd5 = fastMA5 - slowMA5
signal5 = sma(macd5, signalLength)
outMacD5 = security(tickerid, res5, macd5)
plot( outMacD5 ? outMacD5 : na, color= red)
那会带来错误
Add to Chart operation failed, reason: Error: Cannot call `operator ?:` with arguments (bool, literal__string, na); available overloads ...
使用iff会返回相同的错误,说明参数不正确。
我真的可以在这里使用一些帮助。我对使用这些条件运算符迷失了。
任何提示都是有帮助的。
答案 0 :(得分:0)
使用此:
res5= interval*5 < 1440 ? tostring(interval*5) : interval >= 1440 and interval*5 < 2880 ? "1D": ""
plotchar(res5=="5", "res5 test", "", location=location.top)
通过plotchar()
调用,您可以确认res5
的值。在这里,它正在测试是否为“ 5”,因此您将能够在数据窗口中验证(在指标窗格中不打印任何内容,因此不会干扰刻度),其值为1-> true。在1分钟的图表上。
[编辑2019.08.19 09:02-LucF] 您的问题是三元代码不起作用,上述代码可以解决。在发表评论之后,您还需要一个更完整的函数来计算v2中当前时间范围的倍数。使用这个:
f_MultipleOfRes( _mult) =>
// Convert target timeframe in minutes.
_TargetResInMin = interval * _mult * (
isseconds ? 1. / 60. :
isminutes ? 1. :
isdaily ? 1440. :
isweekly ? 7. * 24. * 60. :
ismonthly ? 30.417 * 24. * 60. : na)
// Find best way to express the TF.
_TargetResInMin <= 0.0417 ? "1S" :
_TargetResInMin <= 0.167 ? "5S" :
_TargetResInMin <= 0.376 ? "15S" :
_TargetResInMin <= 0.751 ? "30S" :
_TargetResInMin <= 1440 ? tostring(round(_TargetResInMin)) :
tostring(round(min(_TargetResInMin / 1440, 365))) + "D"
有关用例,请参见here,但不要使用该功能代码,因为它是v4。