Useful features from KimIV - page 46

 
SK. писал (а) >>

That's what I mean. You have to take into account existing constraints, which, generally speaking, change all the time.

Is there some function that analyses all possible constraints?

Same for modifications.

--

(I have 16 functions - 3 for each good order (OP,SL,TP) and 2 for each market order (SL,TP),

and then some more functions analysing combinations of permits).

yes, i also have a separate function for setting an order checking all possible limitations and a separate one (partially similar to the first one) for modifications. i won't give the code - there is so much "left" stuff there that only i need, that - ouch. :-) but in general - it's difficult without it in real trading Expert Advisors. :-)

 
Shu писал (а) >>

Yes, I also have a separate function for setting an order, which checks all possible limitations and a separate one (partially similar to the first one) for modification. I will not give you the code - there are so many "left" tricks, which only I need, that - ouch. :-) but in general - it's difficult without it in real trading Expert Advisors. :-)

Actually, I wanted to check myself - whether I've considered everything. If you don't check them, you can encounter a series of bounces (sometimes with the prospect of getting banned).

I will briefly list my checks.

1) StopLevel. For example, when modifying the SL BuyStop.

It doesn't just check the distance. You should analyze the current position of StopLevel and direction of stop order movement.

If SL is outside the StopLevel corridor, you can move it close or move it indefinitely.

If the SL is within the corridor, the upwards movement is rejected. Down movement is accepted, but with consideration of the StopLevel: if the modification distance within the distance corridor is set at 3 p, and the corridor is 30 p, then the actual value accepted for execution is defined by the StopLevel corridor boundary.

2) FreezeLevel.

3. ratio of StopLevel and TICKSIZE. The order may be outside all corridors, but not multiple of TICKSIZE - then it is trouble. It needs to be rounded towards the modification direction. Critical for "non-currency".

4. Lots. Minlote +/- minlote. Take into account that partial and counter closures are limited to minlots too (although in my opinion it is illogical, but that's the way it is).

After personal checks, the order is issued if each check ended positively (no ban).

 

Fully published function libraries:

b-Graphics.rar - work with graphical objects.

b-Indicators.r ar - work with indicators.

 

The ArrayLR() function.

This function generates an array of linear regression values. The function accepts the following mandatory parameters:

  • x is an array of numeric series values. This is the input parameter. This array must contain the values before the function is called.
  • y is an array of linear regression values. This is the output parameter, i.e. the array will be filled after the function is executed.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 20.05.2008                                                     |
//|  Описание : Формирует массив значений линейной регрессии.                  |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    x - массив значений числового ряда                                      |
//|    y - массив значений линейной регрессии                                  |
//+----------------------------------------------------------------------------+
void ArrayLR(double& x[], double& y[]) {
  double a, b, c, sx=0, sx2=0, sxy=0, sy=0;
  int    i, n=ArraySize(x);

  if (n>1) {
    for (i=0; i<n; i++) {
      sx+=i+1;
      sy+=x[i];
      sxy+=(i+1)*x[i];
      sx2+=(i+1)*(i+1);
    }
    a=sx*sy-n*sxy;
    c=sx*sx-n*sx2;
    if (c!=0) a=a/c; else a=0;
    b=(sy-a*sx)/n;
    ArrayResize(y, n);
    for (i=0; i<n; i++) y[i]=a*(i+1)+b;
  } else Print("ArrayLR(): Недостаточное количество элементов ряда! n=", n);
}
 

Example of using the ArrayLR() function.

Let's mark the price levels of the linear regression for the previous 30 bars.

#define r 30
double x[r], y[];
for (int i=0; i<r; i++) x[i]=Close[i+1];
ArrayLR(x, y);
for (i=0; i<r; i++) SetArrow(170, Red, "arr"+i, Time[i+1], y[i]);

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

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

Fully published function libraries:

b-Graphics.rar - work with graphical objects.

b-Indicators.r ar - work with indicators.

Very cool! >> Thank you!

 
Parabellum писал (а) >>

Igor, there is a good indicator that can combine several candles into one. But it only works on the hourly chart. Is it possible to make it universal?

Thank you.

?

 
Parabellum писал (а) >>

?

ooh... sorry...

Such an indicator has already been written, I don't remember by whom and I don't remember where I saw it... I thought you'd find it yourself or someone would give you the link...

 

The ArrayLR() function does not work accurately.

1. Doesn't take into account the temporal location of the bars. It works incorrectly when bars are missing.

2. the algorithm used to calculate coefficients has a drawback in the form of rounding error accumulation, see 'Help me write a linear regression' (the very bottom of Rosh page confirms this pitfall).

3. I propose an algorithm without these drawbacks.

4. If you replace X[i]=Time[i+1]; with X[i]=i+1; the algorithm is similar, but without error accumulation.

5. Bars are skipped not only on weekends :-(((

Here is my variant

//+----------------------------------------------------------------------------+
//|  Автор    : Сергей Привалов aka Prival,  Skype: privalov-sv                |
//+----------------------------------------------------------------------------+
//|  Версия   : 10.09.2008                                                     |
//|  Описание : Формирует массив значений линейной регрессии y(x)=A*x+B.       |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    X    - массив значений числового ряда, ось X                            |
//|    Y    - массив значений числового ряда, ось Y                            |
//+----------------------------------------------------------------------------+

void Array_LR(double &X[], double& Y[])
{
      double mo_X = 0, mo_Y = 0, var_0 = 0, var_1 = 0;
      int    i,N=ArraySize(X);
      double A,B;
     
      if(N>1)  {
         for ( i = 0; i < N; i ++ ) {
            mo_X +=X[i];
          mo_Y +=Y[i];
      }
      mo_X /=N;
      mo_Y /=N;
        
      for ( i = 0; i < N; i ++ )   {
         var_0 +=(X[i]-mo_X)*(Y[i]-mo_Y);
         var_1 +=(X[i]-mo_X)*(X[i]-mo_X);
      }
      // значение коэффициента A
      if(var_1!=0)   A = var_0 / var_1; else A=0;
      // значение коэффициента B
      B = mo_Y - A * mo_X;
      ArrayResize(Y, N);
     for (i=0; i<N; i++) Y[i]=A*X[i]+B;
    } else Print("ArrayLR(): Недостаточное количество элементов ряда! n=", N);
    
}

here is a picture

Work example

#define r 280
void start() {
  double X[r], Y[r];
  for (int i=0; i<r; i++)  {
    Y[i]=Close[i+1];
    X[i]=Time[i+1];
  }
   Array_LR(X, Y);
  for (i=0; i<r; i++) {SetArrow(170, Blue, "arr"+i, X[i], Y[i])}

Checking script attached

Files:
 
Prival писал (а) >>
Here is my variant

OK, Sergei! Let there be two variants of this function.

My variant is good because it is identical to built-in graphic object OBJ_REGRESSION. If someone needs to take prices from this object, my function will help them, while yours, alas... But it could be used for something else... Thank you!