# optimization of code

Manage agents in your profile. It is easy!
132
2016.11.07 14:36

Hi,

I found somewhere there that i way to optimize the code is to use multiple if() instead of &&. So I made changes in my code and stopped working properly. So where is the bug? Plz help

This worked slowly but properly

void  SLman()
{  int type, ticket;
int total = OrdersTotal();
double pip2dbl = Point;
if(Digits  == 3  || Digits == 5) {pip2dbl = 10.0 * Point; }
double openPrice, stopPrice, zigzag0, zigzag1, zigzag2, SLforSell, SLforBuy,SLD1s,SLD1b;
double SAR0 = iSAR(Symbol(),PERIOD_H1,SARstep,SARmaxstep,0);
double SAR1 = iSAR(Symbol(),PERIOD_H1,SARstep,SARmaxstep,1);
double ATRd = iCustom(NULL,PERIOD_D1,"MTR",MTRperiod,0,1);
SLD1b = MathRound((iLow(NULL,PERIOD_D1,0)- marginesSL*ATRd)/Point)*Point;
SLD1s = MathRound((iHigh(NULL,PERIOD_D1,0)+marginesSL*ATRd)/Point)*Point;
bool c;

double ZZ3val[3];
int k=0;
int l=0;
for(l=0,k=0 ; l<200 && k<=2 ; l++)
{ if(iCustom(NULL,PERIOD_H1,"ZigZag",ZZDepth,ZZDeviation,ZZBackstep,0,l)>0)
{ ZZ3val[k]=iCustom(NULL,PERIOD_H1,"ZigZag",ZZDepth,ZZDeviation,ZZBackstep,0,l);
k++;
}
zigzag0=ZZ3val[0];
zigzag1=ZZ3val[1];
zigzag2=ZZ3val[2];
}

SLforSell= MathRound((zigzag1+0.04*ATRd)/Point)*Point;
//-----------------------------------------------//-----------------------------------------------//
for(int i=total-1;i>=0;i--)
{     if(OrderSelect(i, SELECT_BY_POS))
{  ticket = OrderTicket();
type = OrderType();
openPrice = OrderOpenPrice();
stopPrice = OrderStopLoss();
//----------------------------------------------  ------------------------------------------------
if(type == OP_SELL
&& OrderSymbol()==Symbol() &&  OrderMagicNumber()  == MAGICMA
&& zigzag0< zigzag2 && zigzag0<zigzag1 && zigzag1>zigzag2 && Close[1]<zigzag2
&& OrderProfit()>0
&& SLforSell< OrderOpenPrice()
&& zigzag1-zigzag0> ATRd*0.5

)
c= OrderModify(ticket,OrderOpenPrice(),SLforSell,OrderTakeProfit(),0,Orange);
//----------------------------------
if(type == OP_SELL
&& OrderSymbol()==Symbol() &&  OrderMagicNumber()  == MAGICMA
&& iHigh(NULL,PERIOD_D1,0)-Close[0]>ATRd  // petla żeby sprawdzić kilka sweiczek
&& SLD1s < OrderStopLoss()
)
c= OrderModify(ticket,OrderOpenPrice(),SLD1s,OrderTakeProfit(),0,Orange);
//----------------------------------------------
if(type == OP_SELL
&& OrderSymbol()==Symbol() &&  OrderMagicNumber()  == MAGICMA
&& zigzag0> zigzag2 && zigzag0>zigzag1 && zigzag1<zigzag2 && Close[1]>zigzag2
&& Bid > SAR0
&& OrderOpenPrice()<=Bid
&& SAR0<SAR1
)

//----------------------------------------------  ------------------------------------------------
&& OrderSymbol()==Symbol() &&  OrderMagicNumber()  == MAGICMA
&& zigzag0> zigzag2 && zigzag0>zigzag1 && zigzag1<zigzag2 && Close[1]<zigzag2
&& OrderProfit()>0
&& -zigzag1+zigzag0> ATRd*0.5
)
//----------------------------------
&& OrderSymbol()==Symbol() &&  OrderMagicNumber()  == MAGICMA
&& -iLow(NULL,PERIOD_D1,0)+Close[0]>ATRd
&& SLD1b > OrderStopLoss()
)
c= OrderModify(ticket,OrderOpenPrice(),SLD1b,OrderTakeProfit(),0,Orange);
//----------------------------------------------
&& OrderSymbol()==Symbol() &&  OrderMagicNumber()  == MAGICMA
&& zigzag0< zigzag2 && zigzag0<zigzag1 && zigzag1>zigzag2 && Close[1]<zigzag2
&& Bid < SAR0
&& OrderOpenPrice()>=Bid
&& SAR0>SAR1
)
}
}
}

And this is "optimized" version

void  SLman()
{  int type, ticket;
int total = OrdersTotal();
double pip2dbl = Point;
if(Digits  == 3  || Digits == 5) {pip2dbl = 10.0 * Point; }
double zigzag0, zigzag1, zigzag2, SLforSell, SLforBuy,SLD1s,SLD1b;
double SAR0 = iSAR(Symbol(),PERIOD_H1,SARstep,SARmaxstep,0);
double SAR1 = iSAR(Symbol(),PERIOD_H1,SARstep,SARmaxstep,1);
double ATRd = iCustom(NULL,PERIOD_D1,"MTR",MTRperiod,0,1);

SLD1b = MathRound((iLow(NULL,PERIOD_D1,0)- marginesSL*ATRd)/Point)*Point;
SLD1s = MathRound((iHigh(NULL,PERIOD_D1,0)+marginesSL*ATRd)/Point)*Point;
bool c;

double ZZ3val[3];
int k=0;
int l=0;
for(l=0,k=0 ; l<200 && k<=2 ; l++)
{ if(iCustom(NULL,PERIOD_H1,"ZigZag",ZZDepth,ZZDeviation,ZZBackstep,0,l)>0)
{ ZZ3val[k]=iCustom(NULL,PERIOD_H1,"ZigZag",ZZDepth,ZZDeviation,ZZBackstep,0,l);
k++;
}
zigzag0=ZZ3val[0];
zigzag1=ZZ3val[1];
zigzag2=ZZ3val[2];
}

SLforSell= MathRound((zigzag1+marginesSL*ATRd)/Point)*Point;
//-----------------------------------------------//-----------------------------------------------//
for(int i=total-1;i>=0;i--)
{      if(OrderSelect(i, SELECT_BY_POS))
{  ticket = OrderTicket();
type = OrderType();
//---------------------------------------------------------
if(type == OP_SELL  )
if(  OrderSymbol()==Symbol()) if(   OrderMagicNumber()  == MAGICMA )
if(  zigzag0< zigzag2) if(  zigzag0<zigzag1) if(  zigzag1>zigzag2) if(  Close[1]<zigzag2)
if(  OrderProfit()>0)
if(  SLforSell< OrderOpenPrice() ) if(  SLforSell< OrderStopLoss())
if(  zigzag1-zigzag0> ATRd*prAtrdSwing)
c= OrderModify(ticket,OrderOpenPrice(),SLforSell,OrderTakeProfit(),0,Yellow);
//----------------------------------------------------
if(type == OP_SELL   )
if(  OrderSymbol()==Symbol()) if(   OrderMagicNumber()  == MAGICMA )
if(  iHigh(NULL,PERIOD_D1,0)-Close[0]>ATRd*prAtrdSwing)
if(  SLD1s < OrderStopLoss())
c= OrderModify(ticket,OrderOpenPrice(),SLD1s,OrderTakeProfit(),0,Orange);
//--------------------------------------
if(type == OP_SELL  )
if(  OrderSymbol()==Symbol()) if(   OrderMagicNumber()  == MAGICMA )
if(  zigzag0> zigzag2) if(  zigzag0>zigzag1)if(  zigzag1<zigzag2) if(  Close[1]>zigzag2)
if(  Bid > SAR1  )
if(  OrderOpenPrice()>Bid)
if(  SAR0<=SAR1)

//------------------------ --------------------------------
if(  OrderSymbol()==Symbol()) if(   OrderMagicNumber()  == MAGICMA  )
if(  zigzag0> zigzag2) if(  zigzag0>zigzag1) if(  zigzag1<zigzag2 )if(  Close[1]<zigzag2)
if(  OrderProfit()>0)
if(  -zigzag1+zigzag0> ATRd*prAtrdSwing )
//-------------------------------------------------
if(  OrderSymbol()==Symbol()) if(   OrderMagicNumber()  == MAGICMA  )
if(  -iLow(NULL,PERIOD_D1,0)+Close[0]>ATRd*prAtrdSwing )
if(  SLD1b > OrderStopLoss())
c= OrderModify(ticket,OrderOpenPrice(),SLD1b,OrderTakeProfit(),0,Orange);
//-------------------------------------------------------
if(  OrderSymbol()==Symbol()) if(   OrderMagicNumber()  == MAGICMA )
if(  zigzag0< zigzag2) if(  zigzag0<zigzag1) if(  zigzag1>zigzag2 )if(  Close[1]<zigzag2)
if(  Bid < SAR1 )
if(  OrderOpenPrice()<Bid)
if(  SAR0>=SAR1)
c=  OrderClose(ticket,OrderLots(),Bid,10*pip2dbl,Yellow);
}
}
}
Moderator
30752
2016.11.07 14:39

Brt88:

I found somewhere there that i way to optimize the code is to use multiple if() instead of &&.

Don't believe all what you read. It's not true, just keep your "&&".
2348
2016.11.07 15:01
Brt88:

Hi,

I found somewhere there that i way to optimize the code is to use multiple if() instead of &&. So I made changes in my code and stopped working properly. So where is the bug? Plz help

This worked slowly but properly

Sometimes, it can be, but not always and it is is difficult to quantify. I believe it is really up to personal preference and readability of the code.

My personal preference is to split it for more readability's sake and sometimes for speed, but not always.

Stick to what you feel more comfortable with but also consider how readable your code will be (for others or yourself in the future).

Moderator
30752
2016.11.07 15:14
Fernando Carreiro:

Sometimes, it can be, but not always and it is is difficult to quantify. ...

I was understanding "optimize", in the sense of performance, so faster code. In this sense there is no point to use "if()" instead of "&&".

2348
2016.11.07 15:24
Alain Verleyen:

I was understanding "optimize", in the sense of performance, so faster code. In this sense there is no point to use "if()" instead of "&&".

Actually, depending on the situation, using "if()" can sometimes result in faster code, but it is not a set rule, just as there are cases, where the "&&" will be faster. It depends on several factors and is very specific and hard to quantify as I stated. It all boils down to how the compiler refactors the code and translates it into the final "compiled" format.

That is why, I suggested the the OP just stick to what he feels comfortable with or to at least consider readability as a more important factor (than speed) when deciding on which one to use!

14530
2016.11.07 15:26
 Brt88: I found somewhere there that i way to optimize the code is to use multiple if() instead of &&. False premise since February 3, 2014 (Build 600) Updated MQL4 - MQL4 ReferenceShortened conditions check is now used in logical operations, unlike the old MQL4 version where all expressions have been calculated and the check has been performed afterwards.
Updated MQL4 - MQL4 Reference
• docs.mql4.com
Updated MQL4 - MQL4 Reference
Moderator
30752
2016.11.07 15:41
Fernando Carreiro:

Actually, depending on the situation, using "if()" can sometimes result in faster code, but it is not a set rule, just as there are cases, where the "&&" will be faster. It depends on several factors and is very specific and hard to quantify as I stated. It all boils down to how the compiler refactors the code and translates it into the final "compiled" format.

That is why, I suggested the the OP just stick to what he feels comfortable with or to at least consider readability as a more important factor (than speed) when deciding on which one to use!

As stated by WHRoeder above, and as we are talking about mql4/5, I repeat for the third time

there is no point to use "if()" instead of "&&".

But I am always ready to learn, so if you can provide us an example where ""if()" can sometimes result in faster code" that would be interesting.
2348
2016.11.07 15:45
Alain Verleyen:

As stated by WHRoeder above, and as we are talking about mql4/5, I repeat for the third time

there is no point to use "if()" instead of "&&".

But I am always ready to learn, so if you can provide us an example where ""if()" can sometimes result in faster code" that would be interesting.
OK! If I some free time tomorrow, I will try to write some code to demonstrate it!
132
2016.11.07 15:49

"Shortened conditions check is now used in logical operations, unlike the old MQL4 version where all expressions have been calculated and the check has been performed afterwards."

Tell me if my understanding is good.

if(type == OP_SELL

&& OrderSymbol()==Symbol() &&  OrderMagicNumber()  == MAGICMA
&& zigzag0> zigzag2 && zigzag0>zigzag1 && zigzag1<zigzag2 && Close[1]<zigzag2
&& OrderProfit()>0

&& -zigzag1+zigzag0> ATRd*0.5

)

Currently if one of the conditions above is not fulfilled the program will not check the next condition? e.g. OrderMagicNumber()  != MAGICMA  , so zigzag0> zigzag2 and next conditions will not be checked because whole expression is not true.

Boolean Operations - Operations and Expressions - Language Basics - MQL4 Reference
• docs.mql4.com
Boolean Operations - Operations and Expressions - Language Basics - MQL4 Reference
14530
2016.11.07 15:54
 Brt88: Tell me if my understanding is good. Correct. Therefor, for the fastest code, compare the MN before the slower string compare as part of your OrderSelect test.
Moderator
30752
2016.11.07 18:24
Fernando Carreiro:
OK! If I some free time tomorrow, I will try to write some code to demonstrate it!
Thanks. No hurry anyway...
12