Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (degenerate_762, Nymphodora), 1,012 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
How to code this for() condition? #478035
08/28/19 10:52
08/28/19 10:52
Joined: Feb 2018
Posts: 68
T
tomaslolo Offline OP
Junior Member
tomaslolo  Offline OP
Junior Member
T

Joined: Feb 2018
Posts: 68
Hi all, I´m bad coding, I try many things and fail, but keep learning. I have a condition I´m triying to code and I ask for help:.

If (priceClose(0)>SMA(200)) during the last 100 bars. How can I code It?

Do I have to write 100 times the condition?

Code
vars priceC=series(priceClose());
vars mySMA200=series(SMA(priceC,200));

if(priceC[0]>mySMA200[0]
and priceC[1]>mySMA200[1]
and priceC[2]>mySMA200[2]
.....

and priceC[100]>mySMA200[100])

enterLong()


Because if I code this;

Code
int i;
for(i=0;i<100;i++)
if (priceC[i]>mySMA200[i])
enterLong();


I guess enters Long if the condition is met any time during last 100 bars, and I want to enterLong only if the whole last 100 priceClose are above SMA(200).

Any clues?

Thank you very much

Re: How to code this for() condition? [Re: tomaslolo] #478037
08/28/19 12:38
08/28/19 12:38
Joined: Feb 2015
Posts: 652
Milano, Italy
M
MatPed Offline
User
MatPed  Offline
User
M

Joined: Feb 2015
Posts: 652
Milano, Italy
int i
bool flag= true;
for(i=0;i<100;i++)
if (priceC[i]<mySMA200[i]) flag= false;
if(flag) enterLong();

double check the sintax. Ciao

Re: How to code this for() condition? [Re: tomaslolo] #478040
08/28/19 21:08
08/28/19 21:08
Joined: Feb 2018
Posts: 68
T
tomaslolo Offline OP
Junior Member
tomaslolo  Offline OP
Junior Member
T

Joined: Feb 2018
Posts: 68
Thank you very much. I understand your logic, I wouldn´t though about it.

But it doesn´t enter any trade, even if I limit int i to 10, even to 3, to make sure it should enterLong.

As I understand:
1.- Every new bar flag is true
2.- If any (the last 100 bars) priceC<mySMA200 flag would be false, so next condition if(flag) would never be true, so no trade is opened.
3. But if priceC>mySMA200 (all last 100 bars), flag would continue being true so it should enterLong, but it doesn´t. Why??

What´s wrong??

Re: How to code this for() condition? [Re: tomaslolo] #478044
08/29/19 08:42
08/29/19 08:42
Joined: Feb 2018
Posts: 68
T
tomaslolo Offline OP
Junior Member
tomaslolo  Offline OP
Junior Member
T

Joined: Feb 2018
Posts: 68
It´s working fine. Had to restart Zorro.

Thank you very much MatPed, I really apreciate.

Vielen Dank!

Re: How to code this for() condition? [Re: tomaslolo] #478050
08/29/19 15:36
08/29/19 15:36
Joined: Jan 2019
Posts: 73
berlin
L
laz Offline
Junior Member
laz  Offline
Junior Member
L

Joined: Jan 2019
Posts: 73
berlin
According to your rules you can also stop searching after 1 false is found:

if(priceC[i]<mySMA200[i]) { flag=false; break; }

Not so important for only 100 loops but maybe you increase that later wink

Re: How to code this for() condition? [Re: tomaslolo] #478053
08/29/19 22:10
08/29/19 22:10
Joined: Feb 2018
Posts: 68
T
tomaslolo Offline OP
Junior Member
tomaslolo  Offline OP
Junior Member
T

Joined: Feb 2018
Posts: 68
Good point! Thank you for noting the break rule.

Vielen Dank laz!!

Re: How to code this for() condition? [Re: tomaslolo] #478054
08/29/19 22:51
08/29/19 22:51
Joined: Jan 2019
Posts: 73
berlin
L
laz Offline
Junior Member
laz  Offline
Junior Member
L

Joined: Jan 2019
Posts: 73
berlin
gerne doch wink


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1