How do we get an if statement to perform it’s operation when what is contained in it’s parentheses resolves to FALSE or 0?
Sometimes we want to check on a situation or a number in the hopes that the answer is false or 0. Lets say that we only want to open an order when there are no open orders.
if( TotalOpenOrders() ) { OpenOrder(); }
We want the statement to work when it’s false or has a zero in it and,…. it won’t.
We can not change the way an if statement works. We could say….
if(TotalOpenOrders() == 0) { OpenOrder(); }
That will work because the if statement works when it is TRUE that there are no orders open.
But how do we get the if statement to work when it is FALSE that there are any orders open. What we are going to have to do is to lie to the if statement to make it unwittingly work when it’s contents turn out to be zero or false… we need to lie to it and say.. uh.. yea … that false thing is definitely NOT false. Go ahead and do your thing……Mr. if sir…
Fortunately coding languages come with and excellent liar. Just like a crafty lawyer that can make something that is true seem like it’s false , and something that is false seem like it’s true.
Enter….. the exclamation point sometimes called a bang In coding circles.
What this little liar will do for us is what is called logical negation. When placed in front of something false or a zero. It will turn it into a 1. When placed in front of something true or ANY NUMBER OTHER THAN ZERO, it will change it into a zero. It’s kind of like when people say… “Boy I really love going to the dentist!……NOT!”.. They reverse the whole comment by putting the NOT at the end of the statement. Well the exclamation point does that by being put in the front of the statement instead of at the end.
We use it like this….
if( !TotalOpenOrders() ) { OpenOrder(); }
Now with our little liar in place, If there are no orders open (0), it will turn it into a 1 or true, and the deceived if statement will open an order. if there is ANY NUMBER of orders (except zero of course) open, it will turn it into 0 and the deceived if statement will not let us open an order.
One nice thing about coding it this way, is the way that it is read out loud. When we see an exclamation point we usually say “NOT”. So we would read this … if there are NOT open orders, open a trade.
If we get a signal to take a sell trade and there are buys open we may want to put something in our code like this…..
if( !Hedging_Is_Allowed ) {Alert("Trade signal ignored because of non-hedging rule."); }
Now that you understand that all that it means when something is true in our code is that it is NOT ZERO from a logical point of view, perhaps you will better understand when you see some code that is asking whether an int or a double is true or false.
Let’s say we have a setting for our user to enter the desired stop loss distance. So when checking on how it’s set we can write……
if(StopLoss)
Which means if the StopLoss is true (not 0) our user wants to use a stoploss.
We set the stoploss price (sl) where the stop should be placed so that we can use it in our OrderModify() function.
{sl=OrderOpenPrice()-StopLoss;}
However… we can also do a check to make sure the StopLoss isn’t still set to 0 by saying.
if(!Stoploss)
We would read.. if there is NOT a stoploss.
If the StopLoss is actually still 0 it will be deceitfully changed to 1 (true) and the following occurs.
{sl=Stoploss;}
essentially… sl=0
We do not want to set our stoploss price to the OrderOpenPrice() – 0. This would mean that we are placing our stoploss at the order opening price and that is too close to be allowed and would generate an error. Rather we leave sl at 0 so that it simply enters no stoploss at all on this trade and eliminates the error..
This may seem a bit trivial to you folks that have not had to write a program with thousands of lines in it but, it does save some typing.. for instance let’s say you did not want to enter a certain buy order unless the user had set the stoploss and takeprofit to some value and that hedging was allowed and there were no orders already open in this direction. You could write..
if(StopLoss > 0 && TakeProfit > 0 && Hedging_Is_Allowed == true && BuysOpen() == 0)
or
{OpenBuy();}
if(StopLoss > 0)
if(TakeProfit > 0)
if(Hedging_Is_Allowed == true)
if(BuysOpen() == 0)
{OpenBuy();}
or you could just write..
if(StopLoss && TakeProfit && Hedging_Is_Allowed && !BuyOpen()) {OpenBuy();}
If there’s a stoploss and takeprofit and hedging is allowed and there’s NOT a buy open…
Open a buy. See how simple that is!
The final way in which I want to mention that you should use this is when you are about to divide by any number.
When you divide by a number that is still set to 0 it will crash your program!
So it is a good idea to always check to make sure that the number that you are about to divide by is NOT a 0. In other words, logically, it is true.
Lets say that you have a function that takes all the lot sizes of your open buy orders and combine them into one big lot size and then divide it by the number of buy orders you have open to see what your average lot size is ….. 0.1+0.2+0.3+0.4 / 4 = 0.25 but for some reason this function gets called when you have NO buy orders open and it end’s up trying to do… 0.1+0.2+0.3+0.4 / 0 = CRASH!!!!
double AverageBuyLot()
{
double amount = 0;
double totallots = CombinedOpenBuyLots();
int totalorders = NumOpenBuyOrders();
amount = totallots / totalorders ;
return
(amount);
}
Now this function will work fine as long as there are some buy orders open. But if there are not any open. It will try to divide totallots by zero and your program will crash instantly. I have seen divide by zero errors actually cause your entire platform to close. What we need to do is to make absolutely sure that it is true that there are actually some buy orders open.
double AverageBuyLot();
{
double amount = 0;
double totallots = CombinedOpenBuyLots();
int totalorders = NumOpenBuyOrders();
if(totalorders)//This line will save us from a crash!
{amount = totallots / totalorders ;}
return
(amount);
}
So….
Hi Jim,
Just to say a big thank you for your time and effort in educating us MQL4 and sharing your insights about trading. Really appreciate it.
Thank you once again 🙂
Best wishes,
Norman
Thanks Norman, I hope you can find a way to use this in making your life a better one. Pip Pip