0
votes

because I cant find any way to test my Pine Script strategy on multiple symbols, I created a way to loop through my whole Script.

In This I made 10 variables for 10 different Symbols like this:

ersteTicker = "AAPL"
zweiteTicker = "MSFT"
dritterTicker = "..."

Than I loopedfrom 1 to 10 and made 10 If-querys, which give me in every loop the right symbol like this:

a = 1
    for i = 0 to 10
        if a == 1
            tickerID = ersteTicker
        if a == 2
            tickerID = .....

Now I thougt everything should be all right, but now the console gives back an error message called:

line 75: Can't call 'security' inside: 'if', 'for'

Does anybody know how to bypass this problem??

best regards Christian

P.S.: I already tested a small other script and in this script the console doesn't give me back this error message, even if I also made a for loop with a security function in it..

(looks like this)

//@version=3
strategy("Meine Strategie", overlay=true)
tickerID = "ADS"
vergleichstimeframe = "D"
TaesRSLPeriode = 200
a = 1

myEma() => ema(close, TaesRSLPeriode)
for i = 0 to 10 
    if ( a == 1)
        Daily_ema = security(tickerID, vergleichstimeframe, myEma())
        //plot(Daily_ema*TagesRSLGrenzwert)

        longCondition = crossover(sma(close, 14), sma(close, 28))
        if (longCondition)
            strategy.entry("My Long Entry Id", strategy.long)

        shortCondition = crossunder(sma(close, 14), sma(close, 28))
        if (shortCondition)
            strategy.entry("My Short Entry Id", strategy.short)
1
Why do you even need here security to be called inside of the loop/if? Have you tried put it outside the statements for/if and indide for/if just refer to the security's value? - Michel_T.
Hello @Michel_T., I neet to use the security-function to get the specific Price of a specific Symbol and on a specific timeframe. In My Code I use the Daily and the hourly Timeframe. This is why I need this security-function. And if I want to get the price of different symbols (like f.e, AAPL, MSFT, GOOG,...) than I also need this function. Or do you know a other way to get this data? - chrissi2909
I don't mind using the secutiy, but why is it inside the for? Make the security global and the code will work. - Michel_T.
In the for loop, there is my whole scipt. In This script I want to compare the hourly price and the daily price. So I need the security function in this for loop... Or do you mean I should make a function outside the for loop and than just call the function in the for loop? Does this wok? - chrissi2909
I've provided below some code as an example how'd I implement that feature. - Michel_T.

1 Answers

0
votes

Here's an example of global security. The security must not be inside of neither for nor if statements. If you need more symbols - use more securities. But bear in mind, that you can't choose a symbol from a set of symbols and call security with that symbol (because it'll be mutable variable and you cannot use them with security):

//@version=3
strategy("Meine Strategie", overlay=true)
tickerID = "ADS"
vergleichstimeframe = "D"
TaesRSLPeriode = 200
a = 1

myEma() => ema(close, TaesRSLPeriode)

// this always must stay global 
Daily_ema = security(tickerID, vergleichstimeframe, myEma())
// here you could put more secureties:
//Daily_ema1 = security(tickerID1, vergleichstimeframe, myEma())
//Daily_ema2 = security(tickerID2, vergleichstimeframe, myEma())
//Daily_ema3 = security(tickerID3, vergleichstimeframe, myEma())
// ...

for i = 0 to 10 
    if a == 1
        if Daily_ema > Daily_ema[i] // actual using of the security's result
            strategy.entry("My Long Entry Id", strategy.long)