Small questions

 

Hi all, I've been saving a few small questions to ask in 1 go:

1) is there any disadvantage to use Ask, Bid, Digits, Time[] etc. many times within a function... I see many people assign it to a variable such as "askprice" etc. and then use that thereafter... is this best practice? The same questions goes for the MarketInfo and Accountxxxxxx values. 

 

2) in a similar vein, is it preferable to assign a value to a new variable or calculate it twice. e.g. is one of these 'better'?

int a = /////

int b = a+4;
int c = b*5-b*6;

///or......

int c = (a+4)*5-(a+4)*6; 

 

3) I see that manually opening an order on MT4, you can't see stops, and must modify the order after it's opened.  I also see some scripts do it this way, open and then modify to add stops. But scripts seem to work fine for me, just opening with the stop values. Was there a previous version of MT4 where you couldn't open and set stops at the same time? Should I add error checking to prepare for that?

4) I have the same indicators and scripts for various brokers and have to remember to copy/paste when I edit them.. does anyone have an easier solution to keep them synched?

 
alladir:

Hi all, I've been saving a few small questions to ask in 1 go:

1) is there any disadvantage to use Ask, Bid, Digits, Time[] etc. many times within a function... I see many people assign it to a variable such as "askprice" etc. and then use that thereafter... is this best practice? The same questions goes for the MarketInfo and Accountxxxxxx values. 

 

2) in a similar vein, is it preferable to assign a value to a new variable or calculate it twice. e.g. is one of these 'better'?

 

3) I see that manually opening an order on MT4, you can't see stops, and must modify the order after it's opened.  I also see some scripts do it this way, open and then modify to add stops. But scripts seem to work fine for me, just opening with the stop values. Was there a previous version of MT4 where you couldn't open and set stops at the same time? Should I add error checking to prepare for that?

4) I have the same indicators and scripts for various brokers and have to remember to copy/paste when I edit them.. does anyone have an easier solution to keep them synched?

1.  As, Bid, etc are just predefined variables,  you can use them without any disadvantage compared to any other variable,  using an intermediate is probably down to ignorance.  MarketInfo() and AccountXxxx() are function calls and there will be a time cost associated with running them.

2.  the first will use more memory,  a trivial amount,  to hold the extra variables . . .  I find the 2nd more readable and may well explain what you are doing in a clearer way . . .  it's not always about performance.

3.  a Broker where you can't set stops when you send the order is what I call an ECN "type" Broker . . .  as far as I am aware the automated approach reflects the manual approach needed for a particular Broker.  See this also: https://www.mql5.com/en/forum/143043/page2#746319  #ecntest 

4.  you could do something with batch files so your updated code gets copied to your MT4 installs by running a single bat file . . .

 
I agree with a minor exception to #2 If you do it more than once:
  1. there's more of a chance of getting it wrong, or when modifying the code, missing the change to the other(s).
  2. By using the extra variable, the proper variable name can make the code self documenting ("in a clearer way") E.g. APPEND
  3. If you have to do it more than once with difference values, (or it's more than a few lines,) make it a function.
It's the coder who must decide which style is more clear. Keep in mind you may have to revisit the code months later. Will you still understand everything? Compare these two:
What do these two lines do?
lots = MathRound(lots / MarketInfo(Symbol(), MODE_LOTSTEP)) * MarketInfo(Symbol(), MODE_LOTSTEP);
if (lots < MarketInfo(Symbol(), MODE_MINLOT)) lots = 0;
Which is clearer?
double NormalizeLots(double lots, string pair=""){
    if (pair == "") pair = Symbol();
    double  lotStep     = MarketInfo(pair, MODE_LOTSTEP),
            minLot      = MarketInfo(pair, MODE_MINLOT);
    lots            = MathRound(lots / lotStep) * lotStep;
    if (lots < minLot) lots = 0;    // or minLot
    return(lots);
}
 

Hi,

I have had similar questions and thoughts as alladir for questions 1 and 2.

1. If referring to predefined variables several times I will write their values to a variable I have created as I wasn't sure if it was faster (although by a fraction) to do access the data from my own variables compared to the predefined variables and also wondered if it is quicker or am I just wrong.

2. I agree with both RaptorUK and WHRoeder with using a second variable. Using another variable will take up a miniscule amount more memory but it will make the code more understandable. Generally, although not in the original example, using extra variables saves processing cycles as recalling data from memory and adding each time it is required instead of recalculating the same value repeatedly takes more cycles to perform. I know it is only a small amount but if code can be speed up were possible then you can speed up the execution of an EA, indicator, or script.

As stated, the original example required only two cycles, retrieve the variable a from memory, and add 4 to it so takes 4 cycles to calculate c in the code int c = (a+4)*5-(a+4)*6. But if the calculation for c was longer, say int c = (a+4)*1-(a+4)*2-(a+4)*3-(a+4)*4-(a+4)*5-(a+4)*6 for example, it would take 12 cycles just to get a+4 six times. Using the extra variable b to store a+4 would take 3 cycles, get a, add 4, and store in b. Then the 6 requests for b took 1 cycle each the same bit of code can calculate the same code in 9 cycles (not including the multiplying b parts), which is 25% faster.

I know this is a very simplistic explanation of how a processor works and they often perform such operations in parallel and other types of optimisations, but when the code can be clearer and faster at the same time is there any reason not to use the extra variable. Additionally, it is good programming practice to make code efficient where possible so when you write more complex code in the future, basic optimisation is second nature.

3. As it is an ECN broker you will need to separate the setup of the order and setting stop losses and take profits as RaptorUK stated.

4. Using a batch file is probably the easiest solution and if you use Task Scheduler in Windows you can get it to automatically run the batch file at regular intervals whenever you require which could also be setup to help backups. Another approach would be to add each command the batch file contains to a single scheduled task for simplicity and it will do each command in turn without the need for a batch file. Tasks can be exported and imported from Task Scheduler as XML files so they can be backed up.


Hope this helps,

Noddy

 

Sorry but I just realised a mistake in the explanation for question 2. The first line of the second paragraph should have said it takes 2 cycles to calculate a+4 so that is 4 cycles to calculate a+4 each time in the code int c = (a+4)*5-(a+4)*6.

alladir, there is a load of stuff on the net about setting up scheduled tasks but if you want an example let me know and I'll forward you a task I use. It has nothing to do with Forex but you can use it as a guide to copying files from one location to another to sync data for backups.

Sorry for the error,

Noddy

 
alladir: 4) I have the same indicators and scripts for various brokers and have to remember to copy/paste when I edit them.. does anyone have an easier solution to keep them synched?
  1. What I do is make hard links between them (fsutil hardlink create !new! !orig!)

    I use notepad2 for editing which rewrites the file not delete and create new, so the links remain valid. You'll have to test if editing one updates the other for your editor.

    Likewise, if your compiler's are all the same version, you could try hard linking the ex4s

  2. Alternative create a batch file that xcopy /D the file to the other directories. You can have also it CD to the destination and compile it via ..\metalang ea.mq4
 

Another option for syncing files is to use SyncToy, a free download. It lets you create a folder pairing and it keeps these folders synced with a variety of different options for how the syncing works. Any changes to one folder will then be reflected in the other.

 
Thanks for the replies, excellent answers as usual
Reason: