Useful features from KimIV - page 19

 

GetProfitOpenPosInCurrency() function.

This function returns the total profit of the currently opened positions. Profit is returned in units of the deposit currency. More accurate selection of positions to be taken into account is specified using external parameters:

  • sy - Name of market instrument. If you specify this parameter, the function will consider only positions of this 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. The default value -1 means any position.
  • mn - Position identifier, MagicNumber. Default value -1 means any identifier.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает суммарный профит открытых позиций в валюте депозита |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
double GetProfitOpenPosInCurrency(string sy="", int op=-1, int mn=-1) {
  double p=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=="") && (op<0 || OrderType()==op)) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (mn<0 || OrderMagicNumber()==mn) {
            p+=OrderProfit()+OrderCommission()+OrderSwap();
          }
        }
      }
    }
  }
  return(p);
}
HH. Attached is a script to test GetProfitOpenPosInCurrency() function.
 
KimIV:
scorpionk:

Wrong code, it's not 131, it's 130.

I see...

Try normalising as follows:

int dg=MarketInfo(Symbol(), MODE_DIGITS);
return(NormalizeDouble(Value, dg));
I do so and I don't encounter error 130.

This is how I bring in the price, only using Digits. But it doesn't help, the error doesn't always pop up, only in some cases, and I haven't found out what it depends on. I should probably use my hands to check output values trying to be set by robot.

 

In the light of possible developments in the future, this addition will come in handy:

p+=OrderProfit()+OrderTaxes()+OrderCommission()+OrderSwap();
// !!! OrdertTaxes() мифический парамер налогов !!!
I wonder if there will be one in five...? ;)))

Thanks again for the tip on how to summarise the data in the loop!

 

Yo-yo, are you kidding me? My compiler doesn't understand OrderTaxes().

 

Of course!!! :)))

You can see it in the highlighting, and in the comment clearly pointed it out...

Although it is one of the properties of an order, or rather a position, and such a function should in principle be...

-

And if the compiler doesn't understand...

The GNI will teach us ;)

 

GetProfitOpenPosInPoint() function.

This function returns the total profit of the currently opened positions. The profit is returned in points. A more accurate selection of positions to be taken into account is specified by external parameters:

  • sy - Name of market instrument. If you specify this parameter, the function will only consider positions of this 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. The default value -1 means any position.
  • mn - Position identifier, MagicNumber. Default value -1 means any identifier.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает суммарный профит открытых позиций в пунктах         |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
int GetProfitOpenPosInPoint(string sy="", int op=-1, int mn=-1) {
  double p;
  int    i, k=OrdersTotal(), pr=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 (mn<0 || OrderMagicNumber()==mn) {
          p=MarketInfo(OrderSymbol(), MODE_POINT);
          if (p==0) if (StringFind(OrderSymbol(), "JPY")<0) p=0.0001; else p=0.01;
          if (OrderType()==OP_BUY) {
            pr+=(MarketInfo(OrderSymbol(), MODE_BID)-OrderOpenPrice())/p;
          }
          if (OrderType()==OP_SELL) {
            pr+=(OrderOpenPrice()-MarketInfo(OrderSymbol(), MODE_ASK))/p;
          }
        }
      }
    }
  }
  return(pr);
}
HH. Attached is a script to test GetProfitOpenPosInPoint() function.
 
KimIV:

In this thread I will post the codes of my functions in MQL4, give examples of their use, and answer questions related to their use. Each function will be published in two posts. The first post will contain the function code, the second - examples of use and short explanations. Sometimes I will attach the code of a script to the second post to actually test the function and display the results.

I will start with functions to handle orders as per Lukyanov's request in the topic "How to Run Two EAs At the Same Time" in the end.

Let the first be the order setting function (MT4 tester version):

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

Hi, please help me write an EA. I don't know much about it but it sounds like a good idea. The meaning of the idea in the following if it is certainly possible.
It is necessary that would open two bids simultaneously one Sell one Buy with parameters (lots=0.1____TafeProfit=30______StopLoss=10) and at the closing of losing opened the second positive with parameters (lots=1____TafeProfit=30______StopLoss=10)
If you're not going to send me on the hoo... then ideally the two initial bets open at minimum or maximum candle or at the appearance of a new candle, but if the second part of write long make at least the first part of my ramblings

Thanks a lot in advance !!!! If you write it, put it here vms.80@mail.ru



 

GetTicketLastPos() function.

This function returns the ticket of the last open position or -1. A more accurate selection of positions to be taken into account is specified by external parameters:

  • sy - Name of market instrument. If you specify this parameter, the function will only consider positions of this 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. The default value -1 means any position.
  • mn - Position identifier, MagicNumber. Default value -1 means any identifier.
//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. aka KimIV,  http://www.kimiv.ru                   |
//+----------------------------------------------------------------------------+
//|  Версия   : 19.02.2008                                                     |
//|  Описание : Возвращает тикет последней открытой позиции или -1             |
//+----------------------------------------------------------------------------+
//|  Параметры:                                                                |
//|    sy - наименование инструмента   (""   - любой символ,                   |
//|                                     NULL - текущий символ)                 |
//|    op - операция                   (-1   - любая позиция)                  |
//|    mn - MagicNumber                (-1   - любой магик)                    |
//+----------------------------------------------------------------------------+
int GetTicketLastPos(string sy="", int op=-1, int mn=-1) {
  datetime o;
  int      i, k=OrdersTotal(), r=-1;

  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 (o<OrderOpenTime()) {
                o=OrderOpenTime();
                r=OrderTicket();
              }
            }
          }
        }
      }
    }
  }
  return(r);
}
HH. Attached is a script to test GetTicketLastPos() function.
 
WoodyVMS:

I don't know what to do with it but I have an idea what to do with it. I have no idea about it, but it may be a good idea. The gist of it is as follows, if it is possible.
It is necessary that would open two bids simultaneously one Sell one Buy with parameters (lots=0.1____TafeProfit=30______StopLoss=10) and when closing the loss-making opened the second positive with parameters (lots=1____TafeProfit=30______StopLoss=10)
If you're not going to send me on the hoo... then ideally the two initial bets open at minimum or maximum candle or at the appearance of a new candle, but if the second part of write long make at least the first part of my ramblings

Thanks a lot in advance !!!! If you write it, put it here vms.80@mail.ru

Or maybe send the comrade all the CodeBase EAs to this address.

Sorry, Igor, for being off-topic. If anything, I will delete it.

 
Vinin писал (а):
Or maybe send the comrade all the CodeBase EAs to this address.
He'll find it himself...
Reason: