Useful features from KimIV - page 5

 

Thank you for your acceptance... ;)

What is AND, OR, NOT
is not hard to understand, I'm just interested in these little things in code and their uses.

Or, for instance, this:
- condition a is EQUAL to b is easy to implement a==b
- But a is UNEQUAL to b condition is difficult to implement...

Sorry to bring up the basics again, but this will probably come in handy for many beginners...

Here is a ready-made code with combat training conditions. :)
It has everything... from defining and arranging fields to the programmer's handwriting...
https://www.mql5.com/ru/forum/51521

 
kombat писал (а):
- But the condition a is UNEQUAL to b is already causing difficulties...
a!=b;
 
It seems to me, Igor, that you have deviated a bit from the topic and have gone into explanations.
If you don't mind my saying so I would like to suggest a function which shows something in the taskbar shortcut.
I use it to monitor EAs performance at work without opening the terminal (lots of prying eyes).
it may also be useful in other cases.

//----------- Taskbar visualization function -------------------------------------
void ProfMT(string X)
{
int hwnd1 = WindowHandle(Symbol(), Period());
int hwnd_parent1 = 0;
while (!IsStopped())
{
hwnd1= GetParent(hwnd1);
if (hwnd1 == 0) break;
hwnd_parent1 = hwnd1;
}
if (hwnd_parent1 != 0) SetWindowTextA(hwnd_parent1,X);
}
Thanks.
 

There you go... Where else can you learn the truth so quickly than in a forum... ;)

Thank you! And sorry I slipped off topic.
I'll try to ask more questions in the book thread...

 

The OpenPosition() function for the tester.

Designed to open positions - trading operations of buying or selling at market prices. This function is universal and multicurrency, allowing to open positions for any symbol, not only for the current one. For example, an EA can be run on the EURUSD chart, and trade USDCAD.

//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия  : 13.06.2007                                                      |
//|  Описание : Открытие позиции. Версия функции для тестов на истории.        |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   ("" - текущий символ)                   |
//|    op - операция                                                           |
//|    ll - лот                                                                |
//|    sl - уровень стоп                                                       |
//|    tp - уровень тейк                                                       |
//|    mn - MagicNumber                                                        |
//+----------------------------------------------------------------------------+
void OpenPosition(string sy, int op, double ll, double sl=0, double tp=0, int mn=0) {
  color  clOpen;
  double pp;
  int    err, ticket;
 
  if (sy=="") sy=Symbol();
  if (op==OP_BUY) {
    pp=MarketInfo(sy, MODE_ASK); clOpen=clOpenBuy;
  } else {
    pp=MarketInfo(sy, MODE_BID); clOpen=clOpenSell;
  }
  ticket=OrderSend(sy, op, ll, pp, Slippage, sl, tp, "", mn, 0, clOpen);
  if (ticket<0) {
    err=GetLastError();
    Print("Error(",err,") open ",GetNameOP(op),": ",ErrorDescription(err));
    Print("Ask=",Ask," Bid=",Bid," sy=",sy," ll=",ll,
          " pp=",pp," sl=",sl," tp=",tp," mn=",mn);
  }
}
 

OpenPosition() function for online.

//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 21.03.2008                                                     |
//|  Описание : Открывает позицию и возвращает её тикет.                       |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (NULL или "" - текущий символ)          |
//|    op - операция                                                           |
//|    ll - лот                                                                |
//|    sl - уровень стоп                                                       |
//|    tp - уровень тейк                                                       |
//|    mn - MagicNumber                                                        |
//+----------------------------------------------------------------------------+
int OpenPosition(string sy, int op, double ll, double sl=0, double tp=0, int mn=0) {
  color    clOpen;
  datetime ot;
  double   pp, pa, pb;
  int      dg, err, it, ticket=0;
  string   lsComm=WindowExpertName()+" "+GetNameTF(Period());
 
  if (sy=="" || sy=="0") sy=Symbol();
  if (op==OP_BUY) clOpen=clOpenBuy; else clOpen=clOpenSell;
  for (it=1; it<=NumberOfTry; it++) {
    if (!IsTesting() && (!IsExpertEnabled() || IsStopped())) {
      Print("OpenPosition(): Остановка работы функции");
      break;
    }
    while (!IsTradeAllowed()) Sleep(5000);
    RefreshRates();
    dg=MarketInfo(sy, MODE_DIGITS);
    pa=MarketInfo(sy, MODE_ASK);
    pb=MarketInfo(sy, MODE_BID);
    if (op==OP_BUY) pp=pa; else pp=pb;
    pp=NormalizeDouble(pp, dg);
    ot=TimeCurrent();
    ticket=OrderSend(sy, op, ll, pp, Slippage, sl, tp, lsComm, mn, 0, clOpen);
    if (ticket>0) {
      if (UseSound) PlaySound(NameFileSound); break;
    } else {
      err=GetLastError();
      if (pa==0 && pb==0) Message("Проверьте в Обзоре рынка наличие символа "+sy);
      // Вывод сообщения об ошибке
      Print("Error(",err,") opening position: ",ErrorDescription(err),", try ",it);
      Print("Ask=",pa," Bid=",pb," sy=",sy," ll=",ll," op=",GetNameOP(op),
            " pp=",pp," sl=",sl," tp=",tp," mn=",mn);
      // Блокировка работы советника
      if (err==2 || err==64 || err==65 || err==133) {
        gbDisabled=True; break;
      }
      // Длительная пауза
      if (err==4 || err==131 || err==132) {
        Sleep(1000*300); break;
      }
      if (err==128 || err==142 || err==143) {
        Sleep(1000*66.666);
        if (ExistPositions(sy, op, mn, ot)) {
          if (UseSound) PlaySound(NameFileSound); break;
        }
      }
      if (err==140 || err==148 || err==4110 || err==4111) break;
      if (err==141) Sleep(1000*100);
      if (err==145) Sleep(1000*17);
      if (err==146) while (IsTradeContextBusy()) Sleep(1000*11);
      if (err!=135) Sleep(1000*7.7);
    }
  }
  return(ticket);
}
 

Examples of using the OpenPosition() function.

1. Buy 0.1 lot of the current instrument

OpenPosition(NULL, OP_BUY, 0.1);

2. Sell 0.2 lots of EURUSD

string sy="EURUSD";
double pa=MarketInfo("EURUSD", MODE_ASK);
double pb=MarketInfo("EURUSD", MODE_BID);
double po=MarketInfo("EURUSD", MODE_POINT);
OpenPosition(sy, OP_SELL, 0.2);

3. Sell 0.12 lots of USDCAD with a 20 pips stop

string sy="USDCAD";
double pa=MarketInfo("USDCAD", MODE_ASK);
double pb=MarketInfo("USDCAD", MODE_BID);
double po=MarketInfo("USDCAD", MODE_POINT);
OpenPosition("USDCAD", OP_SELL, 0.12, pb+20*po);

4. Buy 0.15 lot of USDJPY with 40 pips stop

string sy="USDJPY";
double pa=MarketInfo("USDJPY", MODE_ASK);
double pb=MarketInfo("USDJPY", MODE_BID);
double po=MarketInfo("USDJPY", MODE_POINT);
OpenPosition("USDJPY", OP_BUY, 0.15, 0, pa+40*po);

5. Sell 0.1 lot of GBPJPY with stop 23 and takeaway 44 pips

string sy="GBPJPY";
double pa=MarketInfo("GBPJPY", MODE_ASK);
double pb=MarketInfo("GBPJPY", MODE_BID);
double po=MarketInfo("GBPJPY", MODE_POINT);
OpenPosition("GBPJPY", OP_SELL, 0.1, pb+23*po, pb-44*po);
The trailer is a script to test the OpenPosition() function. The first 4 examples are commented out.
Files:
 
KimIV:

OpenPosition() function for online.

//+----------------------------------------------------------------------------+
//| Автор : Ким Игорь В. aka KimIV, http://www.kimiv.ru |
//+----------------------------------------------------------------------------+
//| Версия : 21.03.2008 |
//| Описание : Открывает позицию и возвращает её тикет. |
//+----------------------------------------------------------------------------+
//| Параметры: |
//| sy - наименование инструмента (NULL или "" - текущий символ) |
//| op - операция |
//| ll - лот |
//| sl - уровень стоп |
//| tp - уровень тейк |
//| mn - MagicNumber |
//+----------------------------------------------------------------------------+
int OpenPosition(string sy, int op, double ll, double sl=0, double tp=0, int mn=0) {
//-- skip --
string lsComm=WindowExpertName()+" "+GetNameTF(Period());
//-- skip --
Why not put lsComm in the function parameters? For instance, like this:
int OpenPosition(string sy, int op, double ll, double sl=0, double tp=0, string lsComm="", int mn=0) {
color clOpen;
datetime ot;
double pp, pa, pb;
int dg, err, it, ticket=0;

if (lsComm=="" || lsComm=="0") lsComm=WindowExpertName()+" "+GetNameTF(Period());
if (sy=="" || sy=="0") sy=Symbol();
Imho, the "flexibility" of the function expands...
 
Lukyanov писал (а):
Why not put lsComm in the function parameters?
Imho, the "flexibility" of the function expands...
I don't mind... I mean I allow you to do it :-)
 
I did that for myself. Just might come in handy for someone. :-)
Reason: