Gamestudio Links
Zorro Links
Newest Posts
M1 Oversampling
by Petra. 04/24/24 10:34
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Data from CSV not parsed correctly
by EternallyCurious. 04/20/24 21:39
Scripts not found
by juergen_wue. 04/20/24 18:51
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
folder management functions
by VoroneTZ. 04/17/24 06:52
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
5 registered members (AndrewAMD, Ayumi, PeWi, Quad, VoroneTZ), 513 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Mega_Rod, EternallyCurious, howardR, 11honza11, ccorrea
19048 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
loop function as used in workshop 6 #459276
05/04/16 16:35
05/04/16 16:35
Joined: Sep 2014
Posts: 5
Colombia
E
eyal Offline OP
Newbie
eyal  Offline OP
Newbie
E

Joined: Sep 2014
Posts: 5
Colombia
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.

Last edited by eyal; 05/04/16 16:36.
Re: loop function as used in workshop 6 [Re: eyal] #459286
05/05/16 04:56
05/05/16 04:56
Joined: Nov 2013
Posts: 123
Mithrandir77 Offline
Member
Mithrandir77  Offline
Member

Joined: Nov 2013
Posts: 123
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!

Re: loop function as used in workshop 6 [Re: Mithrandir77] #459289
05/05/16 13:30
05/05/16 13:30
Joined: Jun 2013
Posts: 1,609
D
DdlV Offline
Serious User
DdlV  Offline
Serious User
D

Joined: Jun 2013
Posts: 1,609
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.

Re: loop function as used in workshop 6 [Re: DdlV] #459290
05/05/16 15:55
05/05/16 15:55
Joined: Sep 2014
Posts: 5
Colombia
E
eyal Offline OP
Newbie
eyal  Offline OP
Newbie
E

Joined: Sep 2014
Posts: 5
Colombia
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

Re: loop function as used in workshop 6 [Re: eyal] #459297
05/05/16 18:14
05/05/16 18:14
Joined: Nov 2013
Posts: 123
Mithrandir77 Offline
Member
Mithrandir77  Offline
Member

Joined: Nov 2013
Posts: 123
You are welcome eyal! (I am blind or did you change your username? tongue )

Last edited by Mithrandir77; 05/05/16 18:15.
Re: loop function as used in workshop 6 [Re: Mithrandir77] #459311
05/06/16 14:15
05/06/16 14:15
Joined: Sep 2014
Posts: 5
Colombia
E
eyal Offline OP
Newbie
eyal  Offline OP
Newbie
E

Joined: Sep 2014
Posts: 5
Colombia
hi,

cool! no, did not change my username... laugh


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1