Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1664

 
Taras Slobodyanik #:

You still need to check the conditions on each new tick, and if it didn't work on the previous tick, it will change on the new one.

And error handling has to be done.

If you do it through an array, then check only the ticks of the modified orders written into it against all the other ones.

MakarFX #:
About right, yes, thank you. In that case, if you save to objects, you'd better have unique majicies instead of tickets, otherwise there will be dozens of objects with the same name.

StringToInteger(ObjectName(0,i,0)
A typo? I think both functions can only take one parameter.
 
Документация по MQL5: Графические объекты / ObjectName
Документация по MQL5: Графические объекты / ObjectName
  • www.mql5.com
ObjectName - Графические объекты - Справочник MQL5 - Справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Nerd Trader #:
If you do it through an array, then check only the ticks of modified orders written to it against all the others.

About right, yes, thanks. In that case, if we're saving to objects, we'd better have unique majicks instead of tickets, otherwise there will be dozens of objects with the same name.

A typo? I think both functions can only take one parameter.

They won't.

if(ObjectName(i)!=OrderTicket()) ObjectDelete(0,ObjectName(i));

Sorry

StringToInteger(ObjectName( i));
 
MakarFX #:

They won't.

Sorry

By the way, do you happen to know why the following error occurs periodically? For 100 successfully opened orders, there are about 10 of these.

2021.10.10 16:18:09.853	2021.01.05 21:15:00  test EURUSD,M5: 4051 | order.sl_price: 2.813499706815862e-262
2021.10.10 16:18:09.853	2021.01.05 21:15:00  test EURUSD,M5: OrderSend error 4051
2021.10.10 16:18:09.853	2021.01.05 21:15:00  test EURUSD,M5: unknown trade cmd 406958564 for OrderSend function

order.sl_price: 2.813499706815862e-262 - stoploop price, clearly this number should not be.
4051 - "Invalid value of function parameter"
- is this the order type? Shouldn't be a problem with that either.

class Order{
  public:
    double open_price;
    double sl_price;
    double tp_price;
    color  arrow_color;
    string error_text;
    int cmd;

    Order(){};

    void InitForSell(string type = ""){
      if(type == ""){
        Print("Ошибка: 'type' должен иметь значение");
        return;
      }

      sl_price = NormalizeDouble(db_last.hight + (sl_indent*Point), Digits); // (цена + (кол-во пипсов*Point)


      arrow_color = clrRed;

      if(type == "sellstop"){
        open_price = NormalizeDouble(db_last.low - (order_indent*Point), Digits);

        if(tp_size != 0)
          tp_price = NormalizeDouble(db_last.low - (order_indent+tp_size)*Point, Digits);
        else
          tp_price = 0;

        cmd = OP_SELLSTOP;
        error_text = "Ошибка открытия селстопа ";
      }

      if(type == "sell"){
        open_price = Bid;

        if(tp_size != 0)
          tp_price = NormalizeDouble(Bid - tp_size*Point, Digits);
        else
          tp_price = 0;

        cmd = OP_SELL;
        error_text = "Ошибка открытия села ";
      }
    };

    void InitForBuy(string type = ""){
      if(type == ""){
        Print("Ошибка: 'type' должен иметь значение");
        return;
      }

      sl_price = NormalizeDouble(db_last.low - (sl_indent*Point), Digits); // (цена + (кол-во пипсов*Point)


      arrow_color = clrGreen;

      if(type == "buystop"){
        open_price = NormalizeDouble(db_last.hight + (order_indent*Point), Digits);

        if(tp_size != 0)
          tp_price = NormalizeDouble(db_last.hight + (order_indent+tp_size)*Point, Digits);
        else
          tp_price = 0;

        cmd = OP_BUYSTOP;
        error_text = "Ошибка открытия байстопа ";
      }

      if(type == "buy"){
        open_price = Ask;

        if(tp_size != 0)
          tp_price = NormalizeDouble(Bid + tp_size*Point, Digits);
        else
          tp_price = 0;

        cmd = OP_BUY;
        error_text = "Ошибка открытия бая ";
      }
    };

    ~Order(){};
};

//-------------------------------------------

if(...)
  {
    ResetLastError();
    int order_send = OrderSend(Symbol(), order.cmd, 0.01, order.open_price, 1, 
    order.sl_price, order.tp_price, "", 0, 0, order.arrow_color);

    if(order_send == -1) return;
  }
 
MakarFX #:
Do you mean "min and max" by ticket or price?

By ticket

 
Nerd Trader #:

By the way, do you happen to know why the following error occurs periodically? For 100 successfully opened orders, there are about 10 of these.

order.sl_price: 2.813499706815862e-262 - stop price, clearly this number should not be.
4051 - "Invalid value of function parameter"
- is this the order type? There should not be any problems with it either.

Try to move "NormalizeDouble"

if(...)
  {
    ResetLastError();
    int order_send = OrderSend(Symbol(), order.cmd, 0.01, NormalizeDouble(order.open_price, Digits), 1, 
    NormalizeDouble(order.sl_price, Digits), NormalizeDouble(order.tp_price, Digits), "", 0, 0, order.arrow_color);

    if(order_send == -1) return;
  }

 
EVGENII SHELIPOV #:

By ticket

//+----------------------------------------------------------------------------+
//|  Автор    : Ким Игорь В. 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) {
          if (OrderTicket()== GetTicketMaxMinOrder(0) || OrderTicket()== GetTicketMaxMinOrder(1) ) {
             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);
}
 
MakarFX #:

Try moving "NormalizeDouble"

unchanged.

 
MakarFX #:

Makar has inserted code into the EA so far only to compare profit in currency and profit in pips as far as I understand it can be understood when these functions have zero values


Reason: