How to code this for() condition?

Posted By: tomaslolo

How to code this for() condition? - 08/28/19 10:52

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
Posted By: MatPed

Re: How to code this for() condition? - 08/28/19 12:38

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
Posted By: tomaslolo

Re: How to code this for() condition? - 08/28/19 21:08

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??
Posted By: tomaslolo

Re: How to code this for() condition? - 08/29/19 08:42

It´s working fine. Had to restart Zorro.

Thank you very much MatPed, I really apreciate.

Vielen Dank!
Posted By: laz

Re: How to code this for() condition? - 08/29/19 15:36

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
Posted By: tomaslolo

Re: How to code this for() condition? - 08/29/19 22:10

Good point! Thank you for noting the break rule.

Vielen Dank laz!!
Posted By: laz

Re: How to code this for() condition? - 08/29/19 22:51

gerne doch wink
© 2024 lite-C Forums