Learning MQL4 need help understanding basic concept of "Presetting"

 

Dear Forum Users,

I have just started learning MQL4 via the MQL4 Book, and while mostly I have no problem understanding coding examples and concepts, this one has me a little confused... maybe I'm just tired and will look back at this and think it's silly I didn't understand such a simple concept, but for the moment I am stumped.

In the book there's an example of a simple script which is supposed to close the order closest to the price point where the script has been dropped by the mouse cursor.

The script example can be found via this link (closeorder.mq4):

https://book.mql4.com/trading/orderclose

The thing that I don't understand is the *method* used to calculate which order is closest to the price where the script has been dropped.

The following variables are defined:

int start()                                     // Special function 'start'
  {
   string Symb=Symbol();                        // Symbol
   double Dist=1000000.0;                       // Presetting
   int Real_Order=-1;                           // No market orders yet
   double Win_Price=WindowPriceOnDropped();     // The script is dropped here

What is this random value of one million??? (i.e. "double Dist=1000000.0;")

Later on in the calculations the following block of code is presented:

         double Price=OrderOpenPrice();         // Order price
         if (NormalizeDouble(MathAbs(Price-Win_Price),Digits)< //Selection
            NormalizeDouble(Dist,Digits))       // of the closest order       
           {
            Dist=MathAbs(Price-Win_Price);      // New value
            Real_Order=Tip;                     // Market order available
            int Ticket=OrderTicket();           // Order ticket
            double Lot=OrderLots();             // Amount of lots
           }

To my understanding (after reading this), the function NormalizeDouble() when applied to the variable "Dist=1000000.0" will result in (assuming Digits = 5) 1000000.00000! What exactly does this give us?

Also the condition:

if (NormalizeDouble(MathAbs(Price-Win_Price),Digits) < NormalizeDouble(Dist,Digits))

...will always result in being False since there are no market prices that I know of which are even close to one million, and this script will never proceed beyond this condition.


So can someone please explain to a newbie like myself what exactly am I missing here? How is this method of calculation supposed to work?


Thank you very much for your time and help!!


Kind Regards,

Roman

 

Setting Dist = BIGNUMBER is a common coding technique when trying to find a minimum value (and Dist = - BIGNUMBER to find maximum value)

Then the pseudo-code logic becomes

MyMinimumGoal = 1000000000 // set to ridiculously high value
// find item with minimum value
for (each item in list)
{
  if(InterestingPropertyOf(item) < MyMinimumGoal)
  {
    SaveKey = KeyOf(item)
    MyMinimumGoal = InterestingPropertyOf(item)
  }
}
DoSomethingWith(ItemWithKeyOf(SaveKey))
 

...will always result in being False since there are no market prices that I know of which are even close to one million, and this script will never proceed beyond this condition.

More like results in being True.

if (NormalizeDouble(MathAbs(Price-Win_Price),Digits) < NormalizeDouble(Dist,Digits))
Looks to me that Price-Win_Price will more likely be less(<)then Dist. From what I can gather, looks like the book wants that if() to always be true. Most prices are 1.12345 and Dist is 1000000.12345.

 

When you start comparing one thing with other things you need to have something to start comparing it with, so the thing you start comparing it with is "preset".

Thus you get brewmanz answer and since you are interested in the preset always being false or true (depending on how the if is stated) the preset is always replaced by at least one of the things in you non-empty list (the things in your question are trading orders) .

 
if (NormalizeDouble(MathAbs(Price-Win_Price),Digits) < NormalizeDouble(Dist,Digits))

will be true the FIRST time it is processed, and then ...

Dist will be set to Abs(Price-Win_Price) (ubzen failed to spot this - ignore his comment)

On subsequent passes, it will only be true when Abs(Price-Win_Price) is smaller than the previous saved example

 

Hi all,

Thank you very much for your time and help, I think I'm starting to understand this now!!

Yeah it seems I was tired and just basically completely misinterpreted the condition:

if (NormalizeDouble(MathAbs(Price-Win_Price),Digits) < NormalizeDouble(Dist,Digits))

Again, thank you to brewmanz, ubzen & Ickyrus for your help! :)

Roman

Reason: