Useful features from KimIV - page 52

 

The IIFc() function.

A very handy function in terms of constructing forks. If so, this colour. And if it is not, then another colour. Function IIFc() takes three mandatory parameters:

  • condition - A logical expression. It can be true or false.
  • ifTrue - The colour that will be returned by IIFc() if condition is true.
  • ifFalse - The colour that will be returned by IIFc() if condition is false.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 18.07.2008                                                     |
//|  Описание : Возвращает одно из двух значений взависимости от условия.      |
//+----------------------------------------------------------------------------+
color IIFc(bool condition, color ifTrue, color ifFalse) {
  if ( condition) return( ifTrue); else return( ifFalse);
}
 

The IIFd() function.

Similar convenience for real numbers. If so, such a real number. If it is not, then another number. The IIFd() function takes three mandatory parameters:

  • condition - A logical expression. It can be true or false.
  • ifTrue - The real number that will be returned by IIFd() if condition is true.
  • ifFalse - A real number that will be returned by IIFd() if condition is false.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 01.02.2008                                                     |
//|  Описание : Возвращает одно из двух значений взависимости от условия.      |
//+----------------------------------------------------------------------------+
double IIFd(bool condition, double ifTrue, double ifFalse) {
  if ( condition) return( ifTrue); else return( ifFalse);
}
 

The IIFi() function.

Similar convenience for integers. If so, such an integer. If not, then another number. The IIFi() function takes three mandatory parameters:

  • condition - A logical expression. It can be true or false.
  • ifTrue - An integer number that will be returned by IIFi() if the condition expression is true.
  • ifFalse - An integer that will be returned by IIFi () if condition is false.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 01.02.2008                                                     |
//|  Описание : Возвращает одно из двух значений взависимости от условия.      |
//+----------------------------------------------------------------------------+
int IIFi(bool condition, int ifTrue, int ifFalse) {
  if ( condition) return( ifTrue); else return( ifFalse);
}
 

IIFs() function.

Similar convenience for strings. If so, such a string. If not, then another string. The IIFs() function takes three mandatory parameters:

  • condition - A logical expression. It can be true or false.
  • ifTrue - The string that will be returned by IIFs() if condition is true.
  • ifFalse - The string that will be returned by IIFs( ), if condition is false.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 01.02.2008                                                     |
//|  Описание : Возвращает одно из двух значений взависимости от условия.      |
//+----------------------------------------------------------------------------+
string IIFs(bool condition, string ifTrue, string ifFalse) {
  if ( condition) return( ifTrue); else return( ifFalse);
}
 

Example of using the IIFc() function.

If the given price level is higher than the current Bid, then set a horizontal line at the given price level in blue, otherwise in red.

extern double p1=1.36;
void start() {
  color cl= IIFc( p1>Bid, Blue, Red);
  SetHLine( cl, "", p1);
}

Without use of IIFc() function the above code would look as follows:

extern double p1=1.36;
void start() {
  color cl;
  if ( p1>Bid) cl=Blue; else cl=Red;
  SetHLine( cl, "", p1);
}

P.S. Attached is a script to test the IIFc() function.

Files:
test_iifc.mq4  3 kb
 

Example of using the IIFs() function.

The IIFs() function is, in most cases, used in the commenting block of the EA operation. For example, it is used when outputting values of external parameters of the EA.

extern int StopLoss=50;
extern int TakeProfit=0;
void start() {
  Comment( IIFs( StopLoss<=0, "", "StopLoss="+DoubleToStr( StopLoss, 0)+"п.  ")
         , IIFs( TakeProfit<=0, "", "TakeProfit="+DoubleToStr( TakeProfit, 0)+"п.")
         );
}

P.S. Attached is a script to test the IIFs() function.

Files:
test_iifs.mq4  2 kb
 

An example of the use of the IIFd() function.

It is convenient to use IIFd() function when initializing some variables. For example, price levels of stop and take before opening a position.

extern int StopLoss=50;
extern int TakeProfit=0;
void start() {
  double ll=0.1;
  double sl= IIFd( StopLoss  >0, Ask- StopLoss  *Point, 0);
  double tp= IIFd( TakeProfit>0, Ask+ TakeProfit*Point, 0);

  OpenPosition(NULL, OP_BUY, ll, sl, tp);
}

Without the IIFd() function, this code would look as follows:

extern int StopLoss=50;
extern int TakeProfit=0;
void start() {
  double ll=0.1, sl, tp;
  if ( StopLoss  >0) sl=Ask- StopLoss  *Point; else sl=0;
  if ( TakeProfit>0) tp=Ask+ TakeProfit*Point; else tp=0;

  OpenPosition(NULL, OP_BUY, ll, sl, tp);
}
 

Example of using trading functions in the tester.

To show how trading functions intended for use in the tester only can be applied, I wrote e_ForTester, a swing Expert Advisor. The entry signal is a pair of unidirectional candlesticks. If the candlesticks are directed upwards, the EA closes the buy and sells. If the candlesticks are directed downwards, the Expert Advisor closes the sale and buys. There is only one position in the market at all times. Exit is either by the opposite signal, or by a stop/stop/stick. Stop and Take are two external parameters that can be optimized.
The following tester-only functions are used in the EA:

Trades of the e_ForTester Expert Advisor on the EURUSD H1 chart:

P.S. Attached:

  • e_ForTester.mq4 - Expert Advisor intended for use in the tester only.
  • e_ForTester.rar - Tester report.
  • e_ForTester.txt - Tester log.
Files:
 
Functions intended to be used only in MetaTrader 4 tester are fully published. Therefore the corresponding b-ForTest library has been posted.
 
I was reading the book by V.Yakimkin. "Forex market - your way to success" and came across the following lines "...differential from (DJI x Nikkei)..." Can you tell me, Igor, if it is possible to implement it and output it as an indicator...