Home > Software design >  Alternatives to using the request.security function within a For loop
Alternatives to using the request.security function within a For loop

Time:02-28

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.

CodePudding user response:

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

CodePudding user response:

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)
  • Related