Useful features from KimIV - page 23

 

The NumberOfBarCloseLastPos() function.

This function returns the bar close number of the last position or -1. The selection of positions to be taken into account is defined by external parameters:

  • sy - Name of market instrument. If this parameter is set, the function will only consider positions of this instrument. The default value - NULL means the current market instrument.
  • tf - Timeframe. The default value 0 means the current timeframe.
  • op - Trade operation, position type. Valid values: OP_BUY, OP_SELL or -1. The default value -1 means any position.
  • mn - Position identifier, MagicNumber. Default value -1 means any identifier.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает номер бара закрытия последней позиции или -1.       |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   ("" или NULL - текущий символ)          |
//|    tf - таймфрейм                  (    0       - текущий таймфрейм)       |
//|    op - операция                   (   -1       - любая позиция)           |
//|    mn - MagicNumber                (   -1       - любой магик)             |
//+----------------------------------------------------------------------------+
int NumberOfBarCloseLastPos(string sy="0", int tf=0, int op=-1, int mn=-1) {
  datetime t;
  int      i, k=OrdersHistoryTotal();

  if (sy=="" || sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) {
      if (OrderSymbol()==sy) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (t<OrderCloseTime()) t=OrderCloseTime();
            }
          }
        }
      }
    }
  }
  return(iBarShift(sy, tf, t, True));
}
HH. Attached is a script to test NumberOfBarCloseLastPos() function.
 
 
 
KimIV:

DeleteOrders() function. Tester version.

Function DeleteOrders() is designed to delete pending orders BuyLimit, BuyStop, SellLimit and SellStop. op - Type of trade operation, type of pending order. One of five values is possible: -1, OP_BUYLIMIT, OP_BUYSTOP, OP_SELLLIMIT or OP_SELLSTOP. The default value of -1 means any order.
mn
- MagicNumber, identification number of the pending order. The default value -1 means any magik.

Hello. On page 10 this function has been set to be applied.

I have applied it in my Expert Advisor to delete the pending orders. Or rather, I wanted to use it. I placed the function outside the int start section

I pasted it in.

if (isCloseLastPosByTake(NULL,OP_BUY, My order Buy ))   {
              DeleteOrders(NULL, -1,  My order Sell);   }

But when compiling, I get an error

'clDelete' - variable not defined C:\Program Files\MetaTrader - Alpari\experts\0005.mq4 (1155, 40)

How can I eliminate this error?

And when I use the ClosePositions() function - Closepositions at market price, everything is fine - no errors!

/ even if I just insert the DeleteOrders function and don't apply it yet - this error still compiles!

 
Rita писал (а):
But when you compile it you get an error
'clDelete' - variable not defined C:\Program Files\MetaTrader - Alpari\experts\0005.mq4 (1155, 40)
How can I eliminate this error? What is this 'clDelete'?

There, on page 10 in the attachment is the file test_DeleteOrders.mq4

Download it and look for any clDelete references in it.

 

Downloaded it, watched it! So? Turns out all I had to do was set the colour of the closing icon!

But now it's not all right again. Now that error is gone. But another error has appeared - at the ClosePositions() function - which hasn't been there before.

if (L_2Sell==true)                 {  
        ClosePositions(NULL, -1,Magic);
                                      }

ClosePosBySelect' - function is not defined C:\Program Files\MetaTrader - Alpari\experts\0005.mq4 (905, 47)
And there is no script nearby (p. 14) to check

Can you tell me how to fix this error?

 
Rita писал (а):
ClosePosBySelect' - function is not defined C:\Program Files\MetaTrader - Alpari\experts\0005.mq4 (905, 47)

This error means that ClosePosBySelect() is not defined.

Rita wrote (a):
Can you tell me how to fix this error?
Find the code of the ClosePosBySelect() function and paste it into your EA's file. Please refer to page 13.
 

Sorry to bother you again! I put the ClosePositions() function you specified next to ClosePosBySelect(), and six errors appeared after compilation! I feel angry!

I've dealt with four of them myself. Slip, colour, number of close attempts....Two errors remain, -

sound files -

'UseSound' - variable not defined C:\Program Files\MetaTrader - Alpari\experts\0005.mq4 (1002, 13)

NameFileSound' - variable not defined C:³ Files\MetaTrader - Alpari\experts\0005.mq4 (1002, 33)

Where do I take it from? Where do I put them?

Well extern bool UseSound = false;//Switch off

I will put it in EXPERT'S PROPERTIES but what is NameFileSound' and where do I take it?

**************************************************************************

Found it and put it in global.

extern bool UseSound = false;//выключатель звука
extern string NameFileSound="alert.wav";

And then instead of one error, the compiler threw up 4 more:

'GetNameOP' - function is not defined

'ErrorDescription' - function is not defined

'else' - unexpected token

'GetNameOP' - function is not defined

These errors seem to keep coming up endlessly. To use one ClosePositions() function, I had to first create another one - ClosePosBySelect(). And the other in turn requires some sounds - bells and whistles. And when I'm struggling to find these totally unnecessary bells and bells, I get all kinds of strange errors -'else' - unexpected token and other errors. I haven't even started using this function yet. I've only set it among others!

Can you tell me how I can fix these errors? I commented out all these GetNameOP. The errors seem to be gone. But will the function work well without them?

 
Rita писал (а):
And here instead of one error the compiler threw 4 more:
'GetNameOP' - function is not defined

On the first page in the attachment testrSetOrder.mq4

Rita wrote:
ErrorDescription' - function is not defined

In the code of your EA insert the following line

#include <stdlib.mqh>.

Rita wrote:
'else' - unexpected token
When you do as described above, the error will disappear on its own.
 

The NumberOfBarOpenLastPos() function.

This function returns the bar number of the last position opened or -1. The selection of positions to be taken into account is specified by external parameters:

  • sy - Name of market instrument. If this parameter is set, the function will only consider positions of the specified instrument. The default value - NULL means the current market instrument.
  • tf - Timeframe. The default value 0 means the current timeframe.
  • op - Trade operation, position type. Valid values: OP_BUY, OP_SELL or -1. The default value -1 means any position.
  • mn - Position identifier, MagicNumber. Default value -1 means any identifier.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает номер бара открытия последней позиции или -1.       |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   ("" или NULL - текущий символ)          |
//|    tf - таймфрейм                  (    0       - текущий таймфрейм)       |
//|    op - операция                   (   -1       - любая позиция)           |
//|    mn - MagicNumber                (   -1       - любой магик)             |
//+----------------------------------------------------------------------------+
int NumberOfBarOpenLastPos(string sy="0", int tf=0, int op=-1, int mn=-1) {
  datetime t;
  int      i, k=OrdersTotal();

  if (sy=="" || sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderSymbol()==sy) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (t<OrderOpenTime()) t=OrderOpenTime();
            }
          }
        }
      }
    }
  }
  return(iBarShift(sy, tf, t, True));
}
HH. Attached is a script to test NumberOfBarOpenLastPos() function.
Reason: