Useful features from KimIV - page 37

 
KimIV писал (а) >>

The isLossLastPos() function.

This function returns the loss flag of the last closed position. Flag up - True - last position was closed with a loss. Flag omitted - False - the last position was closed either at zero, or at a profit. This function doesn't consider swaps and commissions. Selection of positions is defined by external parameters:

  • sy - Name of market instrument. If you specify this parameter, the function will only consider positions of the specified instrument. The default value "" means any market instrument. NULL means the current instrument.
  • op - Trade operation, position type. Valid values: OP_BUY, OP_SELL or -1. Default value -1 means any position.
  • mn - Position identifier, MagicNumber. The default value of -1 means any identifier.
P.S. Attached is a script to test isLossLastPos() function.

In order not to multiply the number of functions related to the last closed position, I suggest the following variant:

int LastClosePos(string sy="", int op=-1, int mn=-1) {
  datetime t;
  int      i, j=-1, 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 (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (t<OrderCloseTime()) {
                t=OrderCloseTime();
                j=OrderTicket();              }
              else
                return(j);
            }
          }
        }
      }
    }
  }
  return(-1);
}

This function defines the ticket number of the last closed position...

Further actions with number of the ticket are at programmer's discretion, depending on a problem to be solved...

 

Function FindNearFractal().

This function searches for the nearest fractal and returns its price level. The function accepts the following optional parameters:

  • sy - Name of the instrument. "" or NULL - current symbol. Default value is NULL.
  • tf - Timeframe. Default value 0 - current symbol.
  • mode - Fractal type. MODE_LOWER and MODE_UPPER are allowed. Default value is MODE_LOWER.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. 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);
}
 

Examples of using FindNearFractal().

  • The price of the nearest lower fractal on the current chart.
    Message(FindNearFractal());
  • Price of the nearest upper fractal on the H4 chart of the current symbol.
    Message(FindNearFractal(NULL, PERIOD_H4, MODE_UPPER));
  • Price of the nearest lower fractal on GBPUSD M30 chart.
    Message(FindNearFractal("GBPUSD", PERIOD_M30, MODE_LOWER));
    

SZY. Attached is a script to test the FindNearFractal() function.

Files:
 
KimIV писал (а) >>

Примеры использования функции SetVLine().

  1. Голубая сплошная вертикальная линия на текущем баре.
  2. Сиреневая толстая линия на восьмом баре.
  3. Сетка пунктирных линий на барах 14:00.

ЗЫ. Во вложении скрипт для тестирования функции SetVLine().

KimIV
писал (а)
>>

Examples of how to use SetVLine().

  1. Blue solid vertical line on the current bar.
  2. Lilac thick line on the eighth bar.
  3. A grid of dotted lines on bars 14:00.

ZS. Attached is a script to test SetVLine() function.

Hi Igor,
Could you please show me an example of how such a function can be used in an indicator.
I am trying to improve PreviousiHighsvAndhLows0 indicator and add a mark to it by time.

Using SetVLine() example and draw the mark at bar opening with a certain time...
So far, my version has drawn some nonsense.
I've already asked for help on the forum, but beginners aren't spoiled here...

Thank you anyway, your functions are very useful for step-by-step learning, but of course it would be good if there were many more hints or examples

 
obabuev писал (а) >>
Could you please show me an example of how such a function can be used in an indicator.
I'm trying to improve the indicator and add a time stamp drawing to it.
Using SetVLine() example and draw a mark at bar opening with a certain time...

Show in the illustration which marks you wish to draw.

 

GetExtremumZZZBar() function.

This function searches for an extremum of the standard custom ZigZag indicator and returns the bar number. The function accepts the following optional parameters:

  • sy - Name of the instrument. "" or NULL - the current symbol. Default value is NULL.
  • tf - Timeframe. The default value is 0 - the current timeframe.
  • ne - Extreme number. 0 - last, 1 - previous, 2 - previous, etc.
  • dp, dv, bs - ZigZaga parameters: ExtDepth, ExtDeviation, ExtBackstep respectively.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 07.10.2006                                                     |
//|  Описание : Возвращает номер бара экстремума ЗигЗага по его номеру.        |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (NULL или "" - текущий символ)          |
//|    tf - таймфрейм                  (      0     - текущий ТФ)              |
//|    ne - номер экстремума           (      0     - последний)               |
//|    dp - ExtDepth                                                           |
//|    dv - ExtDeviation                                                       |
//|    bs - ExtBackstep                                                        |
//+----------------------------------------------------------------------------+
int GetExtremumZZBar(string sy="", int tf=0, int ne=0, int dp=12, int dv=5, int bc=3) {
  if (sy=="" || sy=="0") sy=Symbol();
  double zz;
  int    i, k=iBars(sy, tf), ke=0;

  for (i=0; i<k; i++) {
    zz=iCustom(sy, tf, "ZigZag", dp, dv, bc, 0, i);
    if (zz!=0) {
      ke++;
      if (ke>ne) return(i);
    }
  }
  Print("GetExtremumZZBar(): Экстремум ЗигЗага номер ",ne," не найден");
  return(-1);
}
 

Examples of using GetExtremumZZZBar() function.

  • The number of the bar with the last ZigZag extremum on the current chart.
    Message(GetExtremumZZBar());
  • The number of the bar with the penultimate ZigZag extremum on the H4 chart of the current symbol.
    Message(GetExtremumZZBar(NULL, PERIOD_H4, 1));
  • Find the last ZigZag break on the current chart and draw a vertical line through it.
    int n=GetExtremumZZBar(NULL, 0, 1);
    SetVLine(Magenta, "", Time[n]);

ZS. Attached is a script to test GetExtremumZZZBar() function.

 
KimIV писал (а) >>

Show in the illustration which marks you wish to draw.

Thank you, Igor, for responding,
It took me two days, but I made the mark.
Now I'm trying to do another one.
I need it for testing
Using your i-AnyRange indicator, I want to make
The indicator will process the time with random deviation.
For example -
the indicator has
time1=10:05;
time2=15:35;
and in the indicator itself for calculations of indicator lines it uses
T1=time1(+/-random)
T2=time2(+/-random)
but we need also to set the max deviation limits, for example
max random =5min;
The conundrum is just how to limit randomness to 5 minutes?
You probably have similar examples

Files:
 
obabuev писал (а) >>

but you need to set the max deviation limits as well, e.g.
max random =5min;
The snag is just how to limit the randomness to 5 minutes?
You must have similar examples.

5 minutes is 300 seconds:

void init() {
  MathSrand(TimeLocal());
}

void start() {
  double a=MathRand()/32767.0*300;
  int b=MathFloor(a);
  Comment(b);
}
 
KimIV писал (а) >>

Five minutes is 300 seconds:

Thank you of course, but it doesn't quite answer the questions,
How to add int b to string Time2 minutes now or how to make plus minus random

>> I'll look into it, of course, but I'm having a hard time.