Useful features from KimIV - page 53

 
KimIV >> :

IIFc() function.

...

Trying to make a missing ternary operator?

Score, thanks, with my habit of putting curly braces wherever I can, it will be very useful.

 
CMEPTHiK писал (а) >>
I was reading the book by V.Yakimkin. "Forex market - your way to success" and came across such lines "...differential from (DJI x Nikkei)..." Tell me, Igor, is it possible to implement and output it in the form of indicator...

The differential is the speed shown by the ROC indicator.

 

Igor, don't you have a function in your stash for closing all positions at once, including closing with counter positions in mind?

I.e. to first close all counter positions, as much as possible, taking into account the mismatch of counter positions' lots, then the losing, profitable ones...

 
alexx_v писал(а) >>

Igor, do you have a function in your storeroom for closing all positions at once, including closing with counter positions in mind?

I.e. to first close all opposite positions, as far as possible, taking into account the mismatch of lots of opposite positions, then the losing, profitable ones...

No, I don't have it yet.

 

The ExistInHistoryCloseBetween() function.

Returns a flag for the existence in the history of a position or order closed (deleted) between dates. Function ExistInHistoryCloseBetween() receives the following parameters:

  • sy - Name of market instrument. "" - any symbol, NULL - current symbol. Default value is "".
  • op - Trade operation. Valid values: -1, OP_BUY, OP_SELL, OP_BUYLIMIT, OP_BUYSTOP, OP_SELLLIMIT, OP_SELLSTOP. Default value -1 - any operation.
  • mn - Identifier of trade operations, MagicNumber. Default value -1 - any magic number.
  • d1 - Position closing time (pending order deletion). The default value is 0 - any closing time (delete). Time d1 must be shorter than d2.
  • d2 - Position closing time (pending order delete). Default value - 0 - any close (delete) time. Time d2 must be longer than time d1.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 27.08.2008                                                     |
//|  Описание : Возвращает флаг существования в истории позиции или ордера,    |
//|           : закрытой (удалённого) между датами.                            |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая операция)                 |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//|    d1 - время закрытия             ( 0   - любое время закрытия)           |
//|    d2 - время закрытия             ( 0   - любое время закрытия)           |
//+----------------------------------------------------------------------------+
bool ExistInHistoryCloseBetween(string sy="", int op=-1, int mn=-1,
                                datetime d1=0, datetime d2=0) {
  int i, k=OrdersHistoryTotal();

  if ( sy=="0") sy=Symbol();
  for ( i=0; i< k; i++) {
    if (OrderSelect( i, SELECT_BY_POS, MODE_HISTORY)) {
      if ((OrderSymbol()== sy || sy=="") && ( op<0 || OrderType()== op)) {
        if ( mn<0 || OrderMagicNumber()== mn) {
          if ( d1<=OrderCloseTime() && ( d2==0 || d2>=OrderCloseTime())) return( True);
        }
      }
    }
  }
  return( False);
}
 

Examples of the use of ExistInHistoryCloseBetween().

  1. Presence of any positions closed yesterday or orders deleted yesterday with MagicNumber=0 by the current chart symbol.
    datetime d1=iTime(NULL, PERIOD_D1, 1);
    datetime d2=iTime(NULL, PERIOD_D1, 0)-1;
    Message( IIFs( ExistInHistoryCloseBetween(NULL, -1, -1, d1, d2), "Есть", "Нет"));
  2. Presence of any positions or orders closed (deleted) in the last three hours.
    datetime d1=TimeCurrent()-3*60*60;
    Message(IIFs(ExistInHistoryCloseBetween("", -1, -1, d1), "Есть", "Нет"));
  3. Whether or not a buy in EUR has been closed in the last 5 minutes.
    datetime d1=TimeCurrent()-5*60;
    Message( IIFs( ExistInHistoryCloseBetween("EURUSD", OP_BUY, -1, d1), "Да", "Нет"));

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

 

The ExistInHistoryOpenBetween() function.

Returns a flag for the existence in the history of a position or order open (set) between dates. The ExistInHistoryOpenBetween() function accepts the following parameters:

  • sy - Name of the instrument. "" - any character, NULL - the current symbol. Default value is "".
  • op - Trade operation. Valid values: -1, OP_BUY, OP_SELL, OP_BUYLIMIT, OP_BUYSTOP, OP_SELLLIMIT, OP_SELLSTOP. Default value -1 - any operation.
  • mn - Identifier of trade operations, MagicNumber. Default value -1 - any magic number.
  • d1 - Position opening time (pending order placing). The default value is 0 - any opening (setting) time. The d1 time should be less than the d2 time.
  • d2 - Time of position opening (pending order placing). The default value is 0 - any open (set) time. Time d2 must be longer than time d1.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 27.08.2008                                                     |
//|  Описание : Возвращает флаг существования в истории позиции или ордера,    |
//|           : открытой (установленного) между датами.                        |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//|    d1 - время открытия             ( 0   - любое время открытия)           |
//|    d2 - время открытия             ( 0   - любое время открытия)           |
//+----------------------------------------------------------------------------+
bool ExistInHistoryOpenBetween(string sy="", int op=-1, int mn=-1,
                               datetime d1=0, datetime d2=0) {
  int i, k=OrdersHistoryTotal();

  if ( sy=="0") sy=Symbol();
  for ( i=0; i< k; i++) {
    if (OrderSelect( i, SELECT_BY_POS, MODE_HISTORY)) {
      if ((OrderSymbol()== sy || sy=="") && ( op<0 || OrderType()== op)) {
        if ( mn<0 || OrderMagicNumber()== mn) {
          if ( d1<=OrderOpenTime() && ( d2==0 || d2>=OrderOpenTime())) return( True);
        }
      }
    }
  }
  return( False);
}
 

Good afternoon!

Igor, could you tell me how to paint the bar in the colour I want.

I will tell you from the beginning. I am writing an indicator for zonal trading - the fourth dimension of Williams' B-market. I need bars to be colored either green, red or gray.

I wrote an indicator and this is what I got:

This is not what I want. How to make a bar to be colored in the middle. I am using a rectangle with two coordinates as a shading method. That's where I have the whole problem, how to choose the coordinates of the start and end of the rectangle.

Please give me a hint.

Here's the code for the indicator.

Files:
zonetrade.mq4  6 kb
 
Duke3D писал(а) >>
Igor, could you please tell me how to shade a bar in the colour I want.
...
How to make a bar to be shaded exactly in the middle
.

Take a look at the Heiken Ashi indicator. Its source code is included in MetaTrader 4.

 

Function ExistInHistoryToDay().

Returns the flag of existence in the history of a position or order opened (set) today. Function ExistInHistoryToDay() receives the following parameters:

  • sy - Name of the instrument. "" - any symbol, NULL - the current symbol. Default value is "".
  • op - Trade operation. Valid values: -1, OP_BUY, OP_SELL, OP_BUYLIMIT, OP_BUYSTOP, OP_SELLLIMIT, OP_SELLSTOP. Default value -1 - any operation.
  • mn - Identifier of trade operations, MagicNumber. Default value -1 - any magik.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 06.08.2008                                                     |
//|  Описание : Возвращает флаг наличия ордера или позиции в истории за сегодня|
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
bool ExistInHistoryToDay(string sy="", int op=-1, int mn=-1) {
  int i, k=OrdersHistoryTotal();

  if ( sy=="0") sy=Symbol();
  for ( i=0; i< k; i++) {
    if (OrderSelect( i, SELECT_BY_POS, MODE_HISTORY)) {
      if (OrderSymbol()== sy || sy=="") {
        if ( op<0 || OrderType()== op) {
          if ( mn<0 || OrderMagicNumber()== mn) {
            if (TimeDay  (OrderOpenTime())==Day()
            &&  TimeMonth(OrderOpenTime())==Month()
            &&  TimeYear (OrderOpenTime())==Year()) return( True);
          }
        }
      }
    }
  }
  return( False);
}
Reason: