在 For 循环中使用 request.security 函数的替代方法

发布于 2025-01-10 10:01:53 字数 1599 浏览 0 评论 0原文

我编写了一些代码来确定 FTSE 相对于 DAX 和 CAC 的相对强弱。

//Define the lookback period

lookback=10

//Determine if there is strength in the FTSE relative to the DAX and the CAC over the specified lookback period

relativestrengthindicator=0
relativeweaknessindicator=0
float FTSEreturn=0
float DAXreturn=0
float CACreturn=0
float relativereturntoDAX=0
float relativereturntoCAC=0


for i=0 to (lookback-1)

    FTSElastclose=request.security("UK100","5",close[i+1])
    FTSEsecondlastclose=request.security("UK100","5",close[i+2])

    FTSEreturn:=((FTSElastclose-FTSEsecondlastclose)/FTSEsecondlastclose)*100
    
    DAXlastclose=request.security("DE40","5",close[i+1])
    DAXsecondlastclose=request.security("DE40","5",close[i+2])

    DAXreturn:=((DAXlastclose-DAXsecondlastclose)/DAXsecondlastclose)*100

    CAClastclose=request.security("FR40","5",close[i+1])
    CACsecondlastclose=request.security("FR40","5",close[i+2])

    CACreturn:=((CAClastclose-CACsecondlastclose)/CACsecondlastclose)*100
    
    relativereturntoDAX:=FTSEreturn-DAXreturn
    relativereturntoCAC:=FTSEreturn-CACreturn
    
    if relativereturntoDAX>0 and relativereturntoCAC>0
        relativestrengthindicator:=relativestrengthindicator+1
        
    else if relativereturntoDAX<0 and relativereturntoCAC<0
        relativeweaknessindicator:=relativeweaknessindicator+1
        


plot(relativestrengthindicator)
plot(relativeweaknessindicator)

然而,产生的错误消息是“无法在‘if’、‘switch’或‘for’内调用‘request.*()’函数”。

如果我无法在 For 循环中使用 request.security 函数,我应该如何提取价格并将其包含在 For 循环中?

谢谢。

I've written some code to determine the relative strength and weakness of the FTSE relative to the DAX and the CAC.

//Define the lookback period

lookback=10

//Determine if there is strength in the FTSE relative to the DAX and the CAC over the specified lookback period

relativestrengthindicator=0
relativeweaknessindicator=0
float FTSEreturn=0
float DAXreturn=0
float CACreturn=0
float relativereturntoDAX=0
float relativereturntoCAC=0


for i=0 to (lookback-1)

    FTSElastclose=request.security("UK100","5",close[i+1])
    FTSEsecondlastclose=request.security("UK100","5",close[i+2])

    FTSEreturn:=((FTSElastclose-FTSEsecondlastclose)/FTSEsecondlastclose)*100
    
    DAXlastclose=request.security("DE40","5",close[i+1])
    DAXsecondlastclose=request.security("DE40","5",close[i+2])

    DAXreturn:=((DAXlastclose-DAXsecondlastclose)/DAXsecondlastclose)*100

    CAClastclose=request.security("FR40","5",close[i+1])
    CACsecondlastclose=request.security("FR40","5",close[i+2])

    CACreturn:=((CAClastclose-CACsecondlastclose)/CACsecondlastclose)*100
    
    relativereturntoDAX:=FTSEreturn-DAXreturn
    relativereturntoCAC:=FTSEreturn-CACreturn
    
    if relativereturntoDAX>0 and relativereturntoCAC>0
        relativestrengthindicator:=relativestrengthindicator+1
        
    else if relativereturntoDAX<0 and relativereturntoCAC<0
        relativeweaknessindicator:=relativeweaknessindicator+1
        


plot(relativestrengthindicator)
plot(relativeweaknessindicator)

However, the error message produced is "Cannot call 'request.*()' function inside 'if', 'switch' or 'for'".

If I am unable to use the request.security function inside a For loop, how should I go about extracting the prices and including it in the For loop?

Thank you.

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

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

发布评论

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

评论(2

在你怀里撒娇 2025-01-17 10:01:53

如前所述,request.security() 函数会创建一个与当前图表并行的图表。
您可以通过创建此并行图表来解决您的问题,然后在 for 循环中调用它。
代码如下所示:

//@version=5
strategy("My strategy", overlay=true, margin_long=100, margin_short=100)
lookback=10

//Determine if there is strength in the FTSE relative to the DAX and the CAC over the specified lookback period

relativestrengthindicator=0
relativeweaknessindicator=0
float FTSEreturn=0
float DAXreturn=0
float CACreturn=0
float relativereturntoDAX=0
float relativereturntoCAC=0

FTSEclose = request.security("UK100","5",close)
DAXclose = request.security("DE40","5",close)
CACclose = request.security("FR40","5",close)

for i=0 to (lookback-1)

    FTSElastclose = FTSEclose[i+1]
    FTSEsecondlastclose = FTSEclose[i+2]

    FTSEreturn:=((FTSElastclose-FTSEsecondlastclose)/FTSEsecondlastclose)*100
    
    DAXlastclose = DAXclose[i+1]
    DAXsecondlastclose = DAXclose[i+2]

    DAXreturn:=((DAXlastclose-DAXsecondlastclose)/DAXsecondlastclose)*100

    CAClastclose = CACclose[i+1]
    CACsecondlastclose = CACclose[i+2]

    CACreturn:=((CAClastclose-CACsecondlastclose)/CACsecondlastclose)*100
    
    relativereturntoDAX:=FTSEreturn-DAXreturn
    relativereturntoCAC:=FTSEreturn-CACreturn
    
    if relativereturntoDAX>0 and relativereturntoCAC>0
        relativestrengthindicator:=relativestrengthindicator+1
        
    else if relativereturntoDAX<0 and relativereturntoCAC<0
        relativeweaknessindicator:=relativeweaknessindicator+1
        


plot(relativestrengthindicator)
plot(relativeweaknessindicator)

As mentioned, the request.security() function creates a parallel chart to the current one.
You can solve your issue by creating this parallel chart, and then call it inside the for loop.
The code will look like this:

//@version=5
strategy("My strategy", overlay=true, margin_long=100, margin_short=100)
lookback=10

//Determine if there is strength in the FTSE relative to the DAX and the CAC over the specified lookback period

relativestrengthindicator=0
relativeweaknessindicator=0
float FTSEreturn=0
float DAXreturn=0
float CACreturn=0
float relativereturntoDAX=0
float relativereturntoCAC=0

FTSEclose = request.security("UK100","5",close)
DAXclose = request.security("DE40","5",close)
CACclose = request.security("FR40","5",close)

for i=0 to (lookback-1)

    FTSElastclose = FTSEclose[i+1]
    FTSEsecondlastclose = FTSEclose[i+2]

    FTSEreturn:=((FTSElastclose-FTSEsecondlastclose)/FTSEsecondlastclose)*100
    
    DAXlastclose = DAXclose[i+1]
    DAXsecondlastclose = DAXclose[i+2]

    DAXreturn:=((DAXlastclose-DAXsecondlastclose)/DAXsecondlastclose)*100

    CAClastclose = CACclose[i+1]
    CACsecondlastclose = CACclose[i+2]

    CACreturn:=((CAClastclose-CACsecondlastclose)/CACsecondlastclose)*100
    
    relativereturntoDAX:=FTSEreturn-DAXreturn
    relativereturntoCAC:=FTSEreturn-CACreturn
    
    if relativereturntoDAX>0 and relativereturntoCAC>0
        relativestrengthindicator:=relativestrengthindicator+1
        
    else if relativereturntoDAX<0 and relativereturntoCAC<0
        relativeweaknessindicator:=relativeweaknessindicator+1
        


plot(relativestrengthindicator)
plot(relativeweaknessindicator)
忆梦 2025-01-17 10:01:53

您不能执行此操作,因为安全性需要保持不变并在运行时已知,因为它会创建与当前图表平行的图表。

You cannot do this as security needs to be constant and known at runtime as it creates a parallel chart to the current one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文