Useful features from KimIV - page 11

 
KimIV:
Every man has the right to the left... hee... I'm going to have a bit of fun with this

Ilnar, in the topic Pending Fractal Orders, asked, How to make pending orders on fractals?


I guess Ilnar had some difficulties with fractal price levels. That's why I suggest using my function:


//+----------------------------------------------------------------------------+
//| Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                    |
//+----------------------------------------------------------------------------+
//| Версия   : 07.10.2006                                                      |
//| Описание : Поиск ближайшего фрактала.                                      |
//+----------------------------------------------------------------------------+
//| Параметры:                                                                 |
//|   sy - наименование инструмента     (NULL - текущий символ)                |
//|   tf - таймфрейм                    (  0  - текущий ТФ)                    |
//|   mode - тип фрактала               (MODE_LOWER|MODE_UPPER)                |
//+----------------------------------------------------------------------------+
double FindNearFractal(string sy="0", int tf=0, int mode=MODE_LOWER) {
  if (sy=="" || sy=="0") sy=Symbol();
  double f=0;
  int d=MarketInfo(sy, MODE_DIGITS), s;
  if (d==0) if (StringFind(sy, "JPY")<0) d=4; else d=2;
 
  for (s=2; s<100; s++) {
    f=iFractals(sy, tf, mode, s);
    if (f!=0) return(NormalizeDouble(f, d));
  }
  Print("FindNearFractal(): Фрактал не найден");
  return(0);
}
Function FindNearFractal() searches for the nearest fractal of the specified type at the specified symbol, at the given timeframe, and returns its price level. Knowing the price level of the location of the fractal, it is already easy to place an order at that level.
Is it possible for the function to return two values, e.g., the number of the bar where the fractal is located, or should we connect another one?
 
xrust писал (а):
Is it possible for the function to return two values, e.g. also the bar number where the fractal is located, or should I connect another one?
And why return two values when one - the bar number - is enough? For this purpose, in the statement:
return(NormalizeDouble(f, d));
write the value of the bar counter s instead of the price level. On the output you will get the bar number of the fractal. And you will get the price level with one of iHigh () or iLow() functions depending on the fractal type.
 
No, it's all clear here, I'm basically asking. Thank you, your posts are very helpful.
 
xrust:
No, it's clear, I'm basically asking.
ah... Well, if in principle, you can use an array passed by reference. Then the number of returned parameters would be limited by the size of the array.
 

Function ExistOrdersByLot().

Returns a flag for the existence of an order with the specified lot size. True - order exists (set), False - order doesn't exist (not set). You can limit the list of orders to be checked using the function parameters:

  • sy - Name of instrument. If you set this parameter, the function will only check the orders of the specified instrument. NULL means current instrument, and "" (by default) means any instrument.
  • op - Operation, type of pending order. Valid values: OP_BUYLIMIT, OP_BUYSTOP, OP_SELLLIMIT, OP_SELLSTOP or -1. The default value of -1 indicates any order type.
  • mn - Order identifier (MagicNumber). The default value of -1 means any MagicNumber.
  • lo - Size of a lot with an accuracy of two decimal places. The default value is 0 - any lot size.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 28.11.2006                                                     |
//|  Описание : Возвращает флаг существования ордера по размеру лота.          |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любой ордер)                    |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//|    lo - лот                        ( 0   - любой лот)                      |
//+----------------------------------------------------------------------------+
bool ExistOrdersByLot(string sy="", int op=-1, int mn=-1, double lo=0) {
  int i, k=OrdersTotal(), ot;
  lo=NormalizeDouble(lo, 2);

  if (sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      ot=OrderType();
      if (ot>1 && ot<6) {
        if ((OrderSymbol()==sy || sy=="") && (op<0 || ot==op)) {
          if (mn<0 || OrderMagicNumber()==mn) {
            if (lo<=0 || NormalizeDouble(OrderLots(), 2)==lo) return(True);
          }
        }
      }
    }
  }
  return(False);
}
 

Examples of how to use the ExistOrdersByLot() function.

  1. Check for any order with lot size 0.2
    ExistOrdersByLot("", -1, -1, 0.2);
  2. Check any order with lot size 0.3 on the current chart symbol
    ExistOrdersByLot(NULL, -1, -1, 0.3);
  3. Check if there is a BuyLimit order with the lot size 0.5 for any symbol
    ExistOrdersByLot("", OP_BUYLIMIT, -1, 0.5);
  4. Check presence of SellStop order with the lot size 0.1 and the magic number 123456 by EURUSD
    ExistOrdersByLot("EURUSD", OP_SELLSTOP, 123456, 0.1);
  5. Check presence of any order with lot 0.4 with magic number 987 on USDJPY
    ExistOrdersByLot("USDJPY", -1, 987, 0.4);

Attached, as usual, is a working script with the above examples.

 

GetLotLastOrder() function.

Returns the lot size of the last placed order or -1. You can limit the list of orders to be checked using the function parameters:

  • sy - Name of the instrument. If this parameter is given, the function will only check the orders of the specified instrument. NULL means the current instrument, and "" (by default) means any instrument.
  • op - Operation, type of pending order. Valid values: OP_BUYLIMIT, OP_BUYSTOP, OP_SELLLIMIT, OP_SELLSTOP or -1. The default value of -1 means any order.
  • mn - Order identifier (MagicNumber). Default value -1 means any MagicNumber.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 28.11.2006                                                     |
//|  Описание : Возвращает размер лота последнего выставленного ордера или -1  |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
double GetLotLastOrder(string sy="", int op=-1, int mn=-1) {
  datetime o;
  double   l=-1;
  int      i, k=OrdersTotal();

  if (sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderSymbol()==sy || sy=="") {
        if (OrderType()>1 && OrderType()<6) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (o<OrderOpenTime()) {
                o=OrderOpenTime();
                l=OrderLots();
              }
            }
          }
        }
      }
    }
  }
  return(l);
}
 

Examples of how to use GetLotLastOrder().

  1. Find lot size of the last order placed
    GetLotLastOrder();
  2. Find the lot size of the last order placed at the current symbol
    GetLotLastOrder(NULL);
  3. Find the lot size of the last BuyLimit order at any symbol
    GetLotLastOrder("", OP_BUYLIMIT);
  4. Find the lot size of the last SellStop order with the magic number 123456 on EURUSD
    GetLotLastOrder("EURUSD", OP_SELLSTOP, 123456);
  5. Specify the lot size of the last order with magic number 2345 for the current symbol
    GetLotLastOrder(NULL, -1, 2345);

Attached here is a traditionally working script with the above examples.

Files:
 

GetOrderOpenPrice() function.

Returns the setting price of the last opened order or 0. You can limit the list of orders to be checked with function parameters:

  • sy - Name of the instrument. If this parameter is given, the function will only check the orders of the specified instrument. NULL means the current instrument, and "" (by default) means any instrument.
  • op - Operation, type of pending order. Valid values: OP_BUYLIMIT, OP_BUYSTOP, OP_SELLLIMIT, OP_SELLSTOP or -1. The default value of -1 means any order.
  • mn - Order identifier (MagicNumber). Default value -1 means any MagicNumber.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 28.11.2006                                                     |
//|  Описание : Возвращает цену установки последнего ордера или 0.             |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
double GetOrderOpenPrice(string sy="", int op=-1, int mn=-1) {
  datetime t;
  double   r=0;
  int      i, k=OrdersTotal();

  if (sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderSymbol()==sy || sy=="") {
        if (OrderType()>1 && OrderType()<6) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (t<OrderOpenTime()) {
                t=OrderOpenTime();
                r=OrderOpenPrice();
              }
            }
          }
        }
      }
    }
  }
  return(r);
}
 

Examples of how to use GetOrderOpenPrice().

  1. Get the opening price of the last order placed
    GetOrderOpenPrice();
  2. Find the price of the last order placed at the current symbol
    GetOrderOpenPrice(NULL);
  3. Find the setting price of the last BuyLimit order at any symbol
    GetOrderOpenPrice("", OP_BUYLIMIT);
  4. Find the price of the last SellStop order placed with magic 123456 on EURUSD
    GetOrderOpenPrice("EURUSD", OP_SELLSTOP, 123456);
  5. Specify the price of the last order placed with magic 2345 for the current symbol
    GetOrderOpenPrice(NULL, -1, 2345);

Attached is a perfectly working script with the above examples.