Useful features from KimIV - page 36

 

Examples of how to use SetRegression().

  • Red line regression channel based on bars of last 2 days.
    datetime d0=StrToTime(TimeToStr(TimeCurrent(), TIME_DATE));
    datetime d1=TimeCurrent()-24*60*60;
    while (TimeDayOfWeek(d1)==0 || TimeDayOfWeek(d1)==6) d1-=24*60*60;
    d1=StrToTime(TimeToStr(d1, TIME_DATE));
    SetRegression(Red, "", d1, 0, True);

  • Brown channel of the linear regression based on the bars of the last 2 days.
    datetime d0=StrToTime(TimeToStr(TimeCurrent(), TIME_DATE));
    datetime d1=TimeCurrent()-24*60*60;
    while (TimeDayOfWeek(d1)==0 || TimeDayOfWeek(d1)==6) d1-=24*60*60;
    d1=StrToTime(TimeToStr(d1, TIME_DATE));
    datetime d2=d1-24*60*60;
    while (TimeDayOfWeek(d2)==0 || TimeDayOfWeek(d2)==6) d2-=24*60*60;
    d2=StrToTime(TimeToStr(d2, TIME_DATE));
    SetRegression(Brown, "", d2, d0, True, STYLE_DASH);

PS. Attached is a script to test the SetRegression() function.

Files:
 

The EquationDirect() function.

The equation of a straight line. This function calculates the value of the ordinate Y for the abscissa X at the intersection with a line drawn through two arbitrary points on the graph. The function takes the following mandatory parameters:

  • x1, y1 - Coordinates of the first point.
  • x2, y2 - Coordinates of the second point.
  • x - Value, abscissa for which the Y ordinate is to be calculated.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 12.10.2007                                                     |
//|  Описание : Уравнение прямой.                                              |
//|             Вычисляет значение Y для X в точке пересечения с прямой.       |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    x1,y1 - координаты первой точки,                                        |
//|    x2,y2 - координаты второй точки,                                        |
//|    x     - значение, для которого вычислить Y                              |
//+----------------------------------------------------------------------------+
double EquationDirect(double x1, double y1, double x2, double y2, double x) {
  if (x2==x1) return(y1);
  return((y2-y1)/(x2-x1)*(x-x1)+y1);
}
 

Examples of how to use EquationDirect().

  • The red point is at the intersection of the line drawn through the minima of bars 23 and 11 and the zero bar vertical.
    double y=EquationDirect(23, Low[23], 11, Low[11], 0);
    SetArrow(108, Red, "", 0, y);

  • A straight line from the red points through two arbitrary points on the graph.
    double p, x[2], y[2];
    int    cd=115, i, k, ot;
    string on;
    
    ArrayInitialize(x, 0.0);
    ArrayInitialize(y, 0.0);
    
    k=ObjectsTotal();
    for (i=k-1; i>=0; i--) {
      on=ObjectName(i);
      ot=ObjectType(on);
      if (ot==OBJ_ARROW) {
        if (ObjectGet(on, OBJPROP_ARROWCODE)==cd) ObjectDelete(on);
      }
    }
    k=ObjectsTotal();
    for (i=0; i<k; i++) {
      on=ObjectName(i);
      ot=ObjectType(on);
      if (ot==OBJ_ARROW) {
        if (x[0]==0) {
          x[0]=iBarShift(NULL, 0, ObjectGet(on, OBJPROP_TIME1));
          y[0]=ObjectGet(on, OBJPROP_PRICE1);
        } else {
          if (x[1]==0) {
            x[1]=iBarShift(NULL, 0, ObjectGet(on, OBJPROP_TIME1));
            y[1]=ObjectGet(on, OBJPROP_PRICE1);
          }
        }
      }
    }
    if (x[0]==0 || x[1]==0 || y[0]==0 || y[1]==0)
      Message("НЕ инициализированы исходные данные!");
    else {
      if (x[0]>x[1]) k=x[0]; else k=x[1];
      for (i=k; i>=0; i--) {
        if (i!=x[0] && i!=x[1]) p=EquationDirect(x[0], y[0], x[1], y[1], i);
        else {
          if (i==x[0]) p=y[0];
          if (i==x[1]) p=y[1];
        }
        SetArrow(cd, Red, "arr"+i, Time[i], p);
      }
    }

SZY. Attached is a script to test EquationDirect().

Files:
 

GetArrowInterval() function.

I wrote this function to improve the convenience of developing signal indicators, i.e. those indicators that give clear buy or sell signals. The signal of such indicator I usually make as a down arrow above the Khai of a bar or as an up arrow below the low of the corresponding bar. The value of "under" and "over" was usually a few points, so the arrow would not "run over" the bar. It wasn't much of a problem in case of a single timeframe indicator designed for one hour bars only, for example. For each timeframe I had different values "under" and "over". However, for a multiframe indicator I had to use the GetArrowInterval() function:

//+------------------------------------------------------------------+
//| Возвращает интервал установки сигнальных указателей              |
//+------------------------------------------------------------------+
int GetArrowInterval() {
  int p = Period();

  switch (p) {
    case 1:     return(4);
    case 5:     return(5);
    case 15:    return(6);
    case 30:    return(8);
    case 60:    return(10);
    case 240:   return(20);
    case 1440:  return(40);
    case 10080: return(80);
    case 43200: return(150);
  }
}

This is an old variant that has very quickly transformed into a more compact and versatile form:

//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 12.10.2007                                                     |
//+----------------------------------------------------------------------------+
//|  Описание : Возвращает интервал установки сигнальных указателей            |
//|  Параметры:                                                                |
//|    pr - процент относительно ценового размера окна                         |
//+----------------------------------------------------------------------------+
int GetArrowInterval(int pr=7) {
  if (pr<=0) pr=7;
  return((WindowPriceMax()-WindowPriceMin())/100*pr/Point);
}
 

Examples of how to use GetArrowInterval().

  • Sell signal on the current bar.
    SetArrow(242, Red, "", Time[0], High[0]+GetArrowInterval(15)*Point, 2);

  • Buy signal on the previous (first) bar.
    SetArrow(233, Blue, "", Time[1], Low[1]-GetArrowInterval()*Point, 1);
    

SZY. Attached is a script to test function GetArrowInterval().

 
Dear Igor. In e-SOTrailing, you need to modify the take and stop positions.
 
khorosh писал (а) >>
In e-SOTrailing EA you need to make modification of take and stops.

Thank you! Done... The corrected EA is on my website.

 
KimIV писал (а) >>

The EquationDirect() function.

The equation of a straight line. This function calculates the value of the ordinate Y for the abscissa X at the intersection with a line drawn through two arbitrary points on the graph. The function takes the following mandatory parameters:

  • x1, y1 - Coordinates of the first point.
  • x2, y2 - Coordinates of the second point.
  • x - Value, the abscissa for which you want to calculate the Y ordinate.

Please write the second part of the line equation.

Calculate the value of the ordinate Y for the abscissa of X at the point of intersection with a line drawn through one arbitrary point on a graph with a given angle. The function takes the following mandatory parameters:

  • x1, y1 - Coordinates of the first point.
  • a - Angle in degrees.
  • x - Value, the abscissa for which you want to calculate the y ordinate.

Thank you.

[Deleted]  
Real programmer Igor. Big respect and respect to you.
 
djday писал (а) >>

Please write the second part of the equation of a straight line.

Calculate the value of the ordinate Y for the abscissa X at the point of intersection with a line drawn through one arbitrary point on a graph with a given angle. The function takes the following mandatory parameters:

  • x1, y1 - Coordinates of the first point.
  • a - Angle in degrees.
  • x - Value, the abscissa for which the Y ordinate is to be calculated.

Thank you.

A 45° angle occurs when the cathetuses are equal. When the unit of measurement of the cathetuses is metric, there is no problem. One cathetus is two meters and the other is also two meters. So that's a 45° angle. But what about when one cathetus is the number of bars or time, and the other cathetus is the price? How do you ensure they are equal so that the angle is 45°? It is a question of scaling. Give me a fulcrum and I'll move the ground (c).