loop function as used in workshop 6

Posted By: eyal

loop function as used in workshop 6 - 05/04/16 16:35

In workshop 6, the use of the loop function is demonstrated through portfolio strategy:

while(asset(loop("EUR/USD","USD/JPY")))
while(algo(loop("TRND","CNTR")))
{
if(Algo == "TRND")
tradeTrend();
else if(Algo == "CNTR")
tradeCounterTrend();
}

on my screen ,in the tutorial, it appears that the second loop is not indented but since it won't work without indentation since the loops won't be nested I assume it is just a problem on my screen for some reason...

My real question comes after reading the loop function help file that states that the pointers/strings are returned in sequence on each successive call to the loop function and 0 when there are no arguments to loop upon. So, after the inner loop has done for "EUR/USD" and I move to "USD/JPY", why the inner loop function don't return 0? Why has it undergone a "reset" and returning the pointers in sequence again?
I understand it works but I don't understand why - since this function clearly retains its state between function calls I don't understand what makes it "rewind"?

Hopefully, someone can shed some light on this point.
Posted By: Mithrandir77

Re: loop function as used in workshop 6 - 05/05/16 04:56

Hi eval, it is correct what you say concerning the behaviour of the asset loop but also remember the behaviour of the run() function: it is called every bar. So, it is correct that the asset loop eventually return zero but it is called again at the next run() call to start with "EUR/USD" again, it is like a c program with main() that is restarted. Concerning the algo loop, it is restarted for each iteration of the outer loop, like in this c script

Code:
for (int i = 0; i < 3; i++)
    for (int j = 0; j < 2; j++)
        printf("%i\n",i+j);



it prints 0 1 1 2 2 3 (0+0 0+1 1+0 1+1 2+0 2+1)

This is what happens in the first two bars:

First bar:

"EUR/USD" with "TRND"
then "EUR/USD" with "CNTR"
then the inner loop exits
then "USD/JPY" with "TRND"
then "USD/JPY"with "CNTR"
then the inner loop exits
then the outer loop exits

Then comes the second bar:

"EUR/USD" with "TRND"
then "EUR/USD" with "CNTR"
then the inner loop exits
then "USD/JPY" with "TRND"
then "USD/JPY"with "CNTR"
then the inner loop exits
then the outer loop exits

Feel free to ask if you have any doubt!
Posted By: DdlV

Re: loop function as used in workshop 6 - 05/05/16 13:30

Hi eyal,

A few other things to realize to perhaps help clarify:

a) Indentation and other formatting is for us humans to help understand code. The computer doesn't care about indentation. Don't rely on it for understanding - it will sometimes mislead you!

b) In this code, the while(asset is the outer loop and the while(algo is the inner loop.

c) The outer while(asset loop contains only one statement - the while(algo. For this reason it doesn't need the {}.

d) As Mithrandir77 shows, the outer while(asset loop is first set to EUR/USD and then the inner while(algo loop executes - TRND, CNTR, 0. Then it's done, control returns to the outer while(asset, USD/JPY is set, the inner while(algo executes all over again as before, control again returns to the outer while(asset, 0 is set, and the looping is done.

HTH.
Posted By: eyal

Re: loop function as used in workshop 6 - 05/05/16 15:55

hi DdlV and Mithrandir77,

Thanks a lot guys - it really helped.

I am going to write some test scripts to see that I fully understand its use.

Eyal
Posted By: Mithrandir77

Re: loop function as used in workshop 6 - 05/05/16 18:14

You are welcome eyal! (I am blind or did you change your username? tongue )
Posted By: eyal

Re: loop function as used in workshop 6 - 05/06/16 14:15

hi,

cool! no, did not change my username... laugh
© 2024 lite-C Forums