[英]How to automatically change input.timeframe('D') to a period greater than the user's chart. PineScript V5
我是新手。 我有一个 Pine 脚本代码。
function 的问题:
input.timeframe('D')
我需要脚本调用资产公式的时间比实时使用的时间长。
我使用 function:
request.security()
使用可变变量调用字符串时出错:
input.timeframe(VAR)
返回错误:
An argument of 'simple string' type was used but a 'const string' is expected.
调用时间范围与实际用户时间范围不同的资产。
当我使用 HOURS 时间表时。 指标 function 应该调用上层时间,即天数 = 'D'。
或者
当实时为“D”时。 指标 function 应该调用上层时间,即周 = 'W'。
我想要自动时间搜索 function。
并且用户以可选方式手动选择时间范围。
代码EXAMPLE如下:
//@version=5
indicator(title="My Indicator", shorttitle="My Indicator", overlay=true, timeframe="", timeframe_gaps=true)
timeframe_automatic = (timeframe.isintraday ? 'D' : timeframe.isdaily ? 'W' : timeframe.isweekly ? 'M' : na)
timeframe_options = input.timeframe((timeframe_automatic), "Resolution Big", options=['D', 'W', 'M'])
timeframe_called = (request.security(syminfo.tickerid, (timeframe_options), close, gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off))
Pine Script 编译器需要一个用于 input.timeframe('D') 和 'D' 的 'String Const'。
并在使用可变的“简单字符串”进行自动搜索时拒绝:
line xxx: Cannot call 'input.timeframe' with argument 'defval'='timeframe_options'. An argument of 'simple string' type was used but a 'const string' is expected.
我在文档中找不到合适的函数来解决这个问题。
谢谢您的帮助。
你已经拥有了。 在您的安全呼叫中使用timeframe_automatic
。
timeframe_called = (request.security(syminfo.tickerid, timeframe_automatic, close, gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off))
问题是让用户决定是将其置于自动模式还是置于手动模式。
timeframe_automatic = is a mutable variable.
并且input.timeframe(timeframe_automatic)
不接受可变变量,而只接受常量字符串。
但是, request.security()
接受周期可变变量,要求它是常量字符串。
我找到的解决方案是将决策分为 3 个不同的代码。
第一个代码我必须在自动或手动代码之间添加一个手动选择器到 select。
以及用于决策的第二个代码。
最后,第三个代码调用 function。
代码如下所示,示例:
//@version=5
indicator(title="My Indicator", shorttitle="My Indicator", overlay=true, timeframe="", timeframe_gaps=true)
timeframe_selector = input.string(title = "Timeframe Big", defval = "Automatic", options=["Automatic", "Manual"])
timeframe_automatic = (timeframe.isintraday ? 'D' : timeframe.isdaily ? 'W' : timeframe.isweekly ? 'M' : timeframe.ismonthly ? '12M': na)
timeframe_manual = input.timeframe('D', "Resolution Big", options=['D', 'W', 'M', '12M'])
timeframe_decision = (timeframe_selector == "Automatic") ? timeframe_automatic : (timeframe_selector == "Manual") ? timeframe_manual : na
timeframe_called = (request.security(syminfo.tickerid, (timeframe_decision), close, gaps=barmerge.gaps_off, lookahead=barmerge.lookahead_off))
现在可以!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.