Fonctions utiles de KimIV - page 24

 

La fonction NumberOfLossPosToday().

Cette fonction renvoie le nombre de positions perdantes fermées aujourd'hui. La sélection des positions à prendre en compte se fait à l'aide de paramètres externes :

  • sy - Nom de l'instrument de marché. Si ce paramètre est défini, la fonction ne prendra en compte que les positions de cet instrument. La valeur par défaut "" signifie tout instrument de marché. La valeur NULL signifie l'instrument actuel.
  • op - Opération commerciale, type de poste. Valeurs valides : OP_BUY, OP_SELL ou -1. La valeur par défaut -1 signifie une position quelconque.
  • mn - Identificateur de position, MagicNumber. La valeur par défaut -1 signifie n'importe quel identifiant.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает количество убыточных позиций, закрытых сегодня.     |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
int NumberOfLossPosToday(string sy="", int op=-1, int mn=-1) {
  datetime t;
  int      i, k=OrdersHistoryTotal(), kp=0;

  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) {
              t=OrderCloseTime();
              if (Year()==TimeYear(t) && DayOfYear()==TimeDayOfYear(t)) {
                if (OrderProfit()<0) kp++;
              }
            }
          }
        }
      }
    }
  }
  return(kp);
}
P.S. Vous trouverez ci-joint un script pour tester la fonction NumberOfLossPosToday().
Dossiers :
 

La fonction PriceCloseLastPos().

Cette fonction renvoie le prix de clôture de la dernière position fermée. La sélection des positions à prendre en compte est spécifiée par des paramètres externes :

  • sy - Nom de l'instrument de marché. Si ce paramètre est défini, la fonction ne prendra en compte que les positions de cet instrument. La valeur par défaut "" signifie tout instrument de marché. La valeur NULL signifie l'instrument actuel.
  • op - Opération commerciale, type de poste. Valeurs valides : OP_BUY, OP_SELL ou -1. La valeur par défaut -1 signifie une position quelconque.
  • mn - Identificateur de position, MagicNumber. La valeur par défaut -1 signifie n'importe quel identifiant.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает цену закрытия последней закрытой позиций.           |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
double PriceCloseLastPos(string sy="", int op=-1, int mn=-1) {
  datetime t;
  double   r=0;
  int      i, 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();
                r=OrderClosePrice();
              }
            }
          }
        }
      }
    }
  }
  return(r);
}
ZZY. Vous trouverez ci-joint un script pour tester la fonction PriceCloseLastPos().
Dossiers :
 
Cher KimIV Ce serait bien si vous pouviez poster une bibliothèque de toutes vos fonctions à la fin de votre fil de discussion.
 

Je suis d'accord. Vous pourriez même le faire de cette façon. Pas toute la bibliothèque. Mais plusieurs bibliothèques, et pas à la fin, mais au fur et à mesure que les fi lières s'accumulent sur un sujet particulier.

Par exemple, bibliothèque séparée sur l'ouverture/la fermeture (ouverture f-i juste plus ouverture par Market Watch et fermeture)

La bibliothèque suivante - avec des f.i. qui renvoient l'existence de positions par ces ou ces attributs.

Et ainsi de suite.

Il est seulement nécessaire de recueillir ici les suggestions des personnes présentes sur l'ensemble des bibliothèques.

 

La fonction PriceOpenLastPos().

Cette fonction renvoie le prix d'ouverture de la dernière position ouverte. La sélection des positions à prendre en compte est spécifiée par des paramètres externes :

  • sy - Nom de l'instrument de marché. Si ce paramètre est défini, la fonction ne prendra en compte que les positions de l'instrument spécifié. La valeur par défaut - "" signifie tout instrument de marché. La valeur NULL signifie l'instrument actuel.
  • op - Opération commerciale, type de poste. Valeurs valides : OP_BUY, OP_SELL ou -1. La valeur par défaut -1 signifie une position quelconque.
  • mn - Identificateur de position, MagicNumber. La valeur par défaut -1 signifie n'importe quel identifiant.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает цену открытия последней открытой позиций.           |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
double PriceOpenLastPos(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()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) {
              if (t<OrderOpenTime()) {
                t=OrderOpenTime();
                r=OrderOpenPrice();
              }
            }
          }
        }
      }
    }
  }
  return(r);
}
ZS. Vous trouverez ci-joint un script pour tester la fonction PriceOpenLastPos().
Dossiers :
 

La fonction PriceOpenLastClosePos().

Cette fonction renvoie le prix d'ouverture de la dernière position fermée. La sélection des positions à prendre en compte est spécifiée par des paramètres externes :

  • sy - Nom de l'instrument de marché. Si ce paramètre est défini, la fonction ne prendra en compte que les positions de cet instrument. La valeur par défaut - "" signifie tout instrument de marché. La valeur NULL signifie l'instrument actuel.
  • op - Opération commerciale, type de poste. Valeurs valides : OP_BUY, OP_SELL ou -1. La valeur par défaut -1 signifie une position quelconque.
  • mn - Identificateur de position, MagicNumber. La valeur par défaut -1 signifie n'importe quel identifiant.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 03.06.2008                                                     |
//|  Описание : Возвращает цену открытия последней закрытой позиций.           |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
double PriceOpenLastClosePos(string sy="", int op=-1, int mn=-1) {
  datetime t;
  double   r=0;
  int      i, 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();
                r=OrderOpenPrice();
              }
            }
          }
        }
      }
    }
  }
  return(r);
}
HH. Vous trouverez ci-joint un script pour tester la fonction PriceOpenLastClosePos().
Dossiers :
 
rid:

Je suis d'accord. Vous pourriez même le faire de cette façon. Pas toute la bibliothèque. Mais plusieurs bibliothèques, et pas à la fin, mais au fur et à mesure que les fi lières s'accumulent sur un sujet particulier.

Par exemple, bibliothèque séparée sur l'ouverture/la fermeture (ouverture f-i juste plus ouverture par Market Watch et fermeture)

La bibliothèque suivante - avec des f.i. qui renvoient l'existence de positions par ces ou ces attributs.

Et ainsi de suite.

Je voudrais faire quelques suggestions sur l'acquisition de bibliothèques.

Bon après-midi.

Chère KimIV.

Vous faites une chose très utile pour les écrivains novices et experts.

Je me joins au pétitionnaire précédent.

Si ce n'est pas difficile, pouvez-vous rassembler dans un "modèle expert " les principales fonctions.

-Contrôles préliminaires

-ouverture d'un poste

-Ordre de passage

-Fermeture d'une position

-Suppression des commandes

-Soutien (modification) des ordres et des positions.

Et chacun écrira lui-même un bloc avec des appels à ces fonctions.

Je ne peux pas écrire un EA à partir de rien moi-même.

C'est pourquoi j'ai commencé par modifier le code de quelqu'un d'autre.

J'ai commencé à ajouter vos fonctions en blocs - ce que j'obtiens est désordonné.

Après plusieurs jours de corrections et d'ajouts, le conseiller expert d'essai a fonctionné, mais très mal.

C'est pourquoi j'ai fait cette demande, pour créer un modèle simple de conseiller expert.


Le modèle de Roche est quelque peu difficile à comprendre pour moi et redondant au départ - d'après l'article de MetaEditor : Building on the power of templates.

 

La fonction PriceOpenNearPos().

Cette fonction renvoie le prix d'ouverture de la position la plus proche. La distance minimale en pips entre le prix d'ouverture de la position et le prix actuel du marché sert de critère pour la "fermeture" d'une position. La sélection des positions à prendre en compte est fixée par des paramètres externes :

  • sy - Nom de l'instrument de marché. Si ce paramètre est défini, la fonction ne prendra en compte que les positions de l'instrument spécifié. La valeur par défaut "" signifie tout instrument de marché. La valeur NULL signifie l'instrument actuel.
  • op - Opération commerciale, type de poste. Valeurs valides : OP_BUY, OP_SELL ou -1. La valeur par défaut -1 signifie une position quelconque.
  • mn - Identificateur de position, MagicNumber. La valeur par défaut de -1 correspond à un identifiant quelconque.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 04.06.2008                                                     |
//|  Описание : Возвращает цену открытия ближайшей позиции.                    |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
double PriceOpenNearPos(string sy="", int op=-1, int mn=-1) {
  double mi, oop=0, p;
  int    i, k=OrdersTotal(), pp=0;

  if (sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if ((OrderSymbol()==sy || sy=="") && (op<0 || OrderType()==op)) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (mn<0 || OrderMagicNumber()==mn) {
            if (OrderType()==OP_BUY)  mi=MarketInfo(OrderSymbol(), MODE_ASK);
            if (OrderType()==OP_SELL) mi=MarketInfo(OrderSymbol(), MODE_BID);
            p=MarketInfo(OrderSymbol(), MODE_POINT);
            if (p==0) if (StringFind(sy, "JPY")<0) p=0.0001; else p=0.01;
            if (pp==0 || pp>MathAbs(OrderOpenPrice()-mi)/p) {
              pp=MathAbs(OrderOpenPrice()-mi)/p;
              oop=OrderOpenPrice();
            }
          }
        }
      }
    }
  }
  return(oop);
}

SZY. Vous trouverez ci-joint un script pour tester la fonction PriceOpenNearPos().

Si cela intéresse quelqu'un, comparez DistMarketAndPos() et PriceOpenNearPos() par vous-même. Notez les différences.

Dossiers :
 

La fonction TicketNearPos().

Cette fonction renvoie le ticket de la position la plus proche du marché. La distance minimale en points entre le prix d'ouverture et le prix actuel du marché est utilisée comme critère de "fermeture" d'une position. La sélection des postes à prendre en compte est fixée par des paramètres externes :

  • sy - Nom de l'instrument de marché. Si ce paramètre est défini, la fonction ne prendra en compte que les positions de l'instrument spécifié. La valeur par défaut "" signifie tout instrument de marché. La valeur NULL signifie l'instrument actuel.
  • op - Opération commerciale, type de poste. Valeurs valides : OP_BUY, OP_SELL ou -1. La valeur par défaut -1 signifie une position quelconque.
  • mn - Identificateur de position, MagicNumber. La valeur par défaut -1 signifie n'importe quel identifiant.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 05.06.2008                                                     |
//|  Описание : Возвращает тикет ближайшей к рынку позиции по цене открытия.   |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
int TicketNearPos(string sy="", int op=-1, int mn=-1) {
  double mi, p;
  int    i, k=OrdersTotal(), pp=0, ti=0;

  if (sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if ((OrderSymbol()==sy || sy=="") && (op<0 || OrderType()==op)) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (mn<0 || OrderMagicNumber()==mn) {
            if (OrderType()==OP_BUY)  mi=MarketInfo(OrderSymbol(), MODE_ASK);
            if (OrderType()==OP_SELL) mi=MarketInfo(OrderSymbol(), MODE_BID);
            p=MarketInfo(OrderSymbol(), MODE_POINT);
            if (p==0) if (StringFind(sy, "JPY")<0) p=0.0001; else p=0.01;
            if (pp==0 || pp>MathAbs(OrderOpenPrice()-mi)/p) {
              pp=MathAbs(OrderOpenPrice()-mi)/p;
              ti=OrderTicket();
            }
          }
        }
      }
    }
  }
  return(ti);
}
ZS. Vous trouverez ci-joint un script pour tester la fonction TicketNearPos().
Dossiers :
 

Fonction TypeNearPos().

Cette fonction renvoie le type(0-Buy, 1-Sell) de la position la plus proche du marché au prix d'ouverture ou -1. La distance minimale en pips entre le prix d'ouverture d'une position et le prix actuel du marché sert de critère pour la "fermeture" d'une position. La sélection des positions à prendre en compte est fixée par des paramètres externes :

  • sy - Nom de l'instrument de marché. Si ce paramètre est défini, la fonction ne prendra en compte que les positions de l'instrument spécifié. La valeur par défaut "" signifie tout instrument de marché. La valeur NULL signifie l'instrument actuel.
  • op - Opération commerciale, type de poste. Valeurs valides : OP_BUY, OP_SELL ou -1. La valeur par défaut -1 signifie une position quelconque.
  • mn - Identificateur de position, MagicNumber. La valeur par défaut -1 signifie n'importe quel identifiant.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 07.06.2008                                                     |
//|  Описание : Возвращает тип ближайшей к рынку позиции или -1.               |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
int TypeNearPos(string sy="", int op=-1, int mn=-1) {
  double mi, p;
  int    i, k=OrdersTotal(), pp=0, ty=-1;

  if (sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if ((OrderSymbol()==sy || sy=="") && (op<0 || OrderType()==op)) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (mn<0 || OrderMagicNumber()==mn) {
            if (OrderType()==OP_BUY)  mi=MarketInfo(OrderSymbol(), MODE_ASK);
            if (OrderType()==OP_SELL) mi=MarketInfo(OrderSymbol(), MODE_BID);
            p=MarketInfo(OrderSymbol(), MODE_POINT);
            if (p==0) if (StringFind(sy, "JPY")<0) p=0.0001; else p=0.01;
            if (pp==0 || pp>MathAbs(OrderOpenPrice()-mi)/p) {
              pp=MathAbs(OrderOpenPrice()-mi)/p;
              ty=OrderType();
            }
          }
        }
      }
    }
  }
  return(ty);
}
ZS. Vous trouverez ci-joint un script pour tester la fonction TypeNearPos().
Dossiers :