Скачать MetaTrader 5

ModifyOrder: no error - нашел причину

Авторизуйтесь или зарегистрируйтесь, чтобы добавить комментарий
Как пользоваться редактором сообщений MQL5.community? Узнай из статьи!
Forex Trader
114255
Forex Trader 2005.12.21 06:00 
Тестер ругается, когда вызываем ModifyOrder, price, stop loss & take profit указываем такими же, как в текущем ордере.

Теперь мой OrderModify выглядит так:
int ModifyOrder(int ticket, double price, double sl, double tp) {
  if (!OrderSelect(ticket, SELECT_BY_TICKET)) {
    err = "ModifyOrder: OrderSelect failed. ticket=" + ticket;
    return(0);
  }
  
  double bid = MarketInfo(OrderSymbol(), MODE_BID);
  double ask = MarketInfo(OrderSymbol(), MODE_ASK);
  double point = MarketInfo(OrderSymbol(), MODE_POINT);
  double lev = MarketInfo(OrderSymbol(), MODE_STOPLEVEL)*point;
  int dig = MarketInfo(OrderSymbol(), MODE_DIGITS);

  price = NormalizeDouble(price, dig);
  sl = NormalizeDouble(sl, dig);
  tp = NormalizeDouble(tp, dig);

  double price0 = NormalizeDouble(OrderOpenPrice(), dig);
  double sl0 = NormalizeDouble(OrderStopLoss(), dig);
  double tp0 = NormalizeDouble(OrderTakeProfit(), dig);
  
  if (price == price0 && sl == sl0 && tp == tp0) return;
       
  int type = OrderType();
  if (type == OP_BUY) {
    if (sl > 0 && sl >= bid-lev) {
      err = "ModifyOrder: Invalid SL. " +
        "Type= " + OrderTypeToStr(type) + ". " +
        "Bid= " + DoubleToStr(bid, dig) + ". " +
        "SL= " + DoubleToStr(sl, dig) + ". ";
      return(-1);
    }

    if (tp > 0 && tp <= bid+lev) {
      err = "ModifyOrder: Invalid TP. " +
        "Type= " + OrderTypeToStr(type) + ". " +
        "Bid= " + DoubleToStr(bid, dig) + ". " +
        "TP= " + DoubleToStr(tp, dig) + ". ";
      return(-1);
    }
  }
  
  if (type == OP_BUYLIMIT) {
    if (price >= ask-lev) {
      err = "ModifyOrder: Invalid Price. " +
        "Type= " + OrderTypeToStr(type) + ". " +
        "Price= " + DoubleToStr(price, dig) + ". " +
        "Ask= " + DoubleToStr(ask, dig) + ". ";
      return(-1);
    }
    
    if (sl > 0 && sl >= price-lev) {
      err = "ModifyOrder: Invalid SL. " +
        "Type= " + OrderTypeToStr(type) + ". " +
        "Price= " + DoubleToStr(price, dig) + ". " +
        "SL= " + DoubleToStr(sl, dig) + ". ";
      return(-1);
    }

    if (tp > 0 && tp <= price+lev) {
      err = "ModifyOrder: Invalid TP. " +
        "Type= " + OrderTypeToStr(type) + ". " +
        "Price= " + DoubleToStr(price, dig) + ". " +
        "TP= " + DoubleToStr(tp, dig) + ". ";
      return(-1);
    }
  }

  if (type == OP_BUYSTOP) {
    if (price <= ask+lev) {
      err = "ModifyOrder: Invalid Price. " +
        "Type= " + OrderTypeToStr(type) + ". " +
        "Price= " + DoubleToStr(price, dig) + ". " +
        "Ask= " + DoubleToStr(ask, dig) + ". ";
      return(-1);
    }
    
    if (sl > 0 && sl >= price-lev) {
      err = "ModifyOrder: Invalid SL. " +
        "Type= " + OrderTypeToStr(type) + ". " +
        "Price= " + DoubleToStr(price, dig) + ". " +
        "SL= " + DoubleToStr(sl, dig) + ". ";
      return(-1);
    }

    if (tp > 0 && tp <= price+lev) {
      err = "ModifyOrder: Invalid TP. " +
        "Type= " + OrderTypeToStr(type) + ". " +
        "Price= " + DoubleToStr(price, dig) + ". " +
        "TP= " + DoubleToStr(tp, dig) + ". ";
      return(-1);
    }
  }
  
  if (type == OP_SELL) {
    if (sl > 0 && sl <= ask+lev) {
      err = "ModifyOrder: Invalid SL. " +
        "Type= " + OrderTypeToStr(type) + ". " +
        "Ask= " + DoubleToStr(ask, dig) + ". " +
        "SL= " + DoubleToStr(sl, dig) + ". ";
      return(-1);
    }

    if (tp > 0 && tp >= ask-lev) {
      err = "ModifyOrder: Invalid TP. " +
        "Type= " + OrderTypeToStr(type) + ". " +
        "Ask= " + DoubleToStr(ask, dig) + ". " +
        "TP= " + DoubleToStr(tp, dig) + ". ";
      return(-1);
    }  
  }
  
  if (type == OP_SELLLIMIT) {
    if (price <= bid+lev) {
      err = "ModifyOrder: Invalid Price. " +
        "Type= " + OrderTypeToStr(type) + ". " +
        "Price= " + DoubleToStr(price, dig) + ". " +
        "Bid= " + DoubleToStr(bid, dig) + ". ";
      return(-1);
    }
    
    if (sl > 0 && sl <= price+lev) {
      err = "ModifyOrder: Invalid SL. " +
        "Type= " + OrderTypeToStr(type) + ". " +
        "Price= " + DoubleToStr(price, dig) + ". " +
        "SL= " + DoubleToStr(sl, dig) + ". ";
      return(-1);
    }

    if (tp > 0 && tp >= price-lev) {
      err = "ModifyOrder: Invalid TP. " +
        "Type= " + OrderTypeToStr(type) + ". " +
        "Price= " + DoubleToStr(price, dig) + ". " +
        "TP= " + DoubleToStr(tp, dig) + ". ";
      return(-1);
    }
  }

  if (type == OP_SELLSTOP) {
    if (price >= bid-lev) {
      err = "ModifyOrder: Invalid Price. " +
        "Type= " + OrderTypeToStr(type) + ". " +
        "Price= " + DoubleToStr(price, dig) + ". " +
        "Bid= " + DoubleToStr(bid, dig) + ". ";
      return(-1);
    }
    
    if (sl > 0 && sl <= price+lev) {
      err = "ModifyOrder: Invalid SL. " +
        "Type= " + OrderTypeToStr(type) + ". " +
        "Price= " + DoubleToStr(price, dig) + ". " +
        "SL= " + DoubleToStr(sl, dig) + ". ";
      return(-1);
    }

    if (tp > 0 && tp >= price-lev) {
      err = "ModifyOrder: Invalid TP. " +
        "Type= " + OrderTypeToStr(type) + ". " +
        "Price= " + DoubleToStr(price, dig) + ". " +
        "TP= " + DoubleToStr(tp, dig) + ". ";
      return(-1);
    }    
  }

  bool res = false;
  while (!res) {
    RefreshRates();
    res = OrderModify(ticket, price, sl, tp, 0);
    if (!res) {
      int code = GetLastError();
   	  switch (code) {
   	    case ERR_INVALID_PRICE: break;
   	    case ERR_INVALID_STOPS: break;
   	  }
    
      Print("OrderModify failed: ", ErrorDescription(code));
      Sleep(SleepErr);
    }
  }

  Sleep(SleepOk);
  return(0);
}

Forex Trader
114255
Forex Trader 2005.12.23 07:33  
мдя-а... нехилая модифайка. Добавлю свою для альтернативы.
//+------------------------------------------------------------------+
//| Перенос уровня StopLoss                                          |
//| Параметры:                                                       |
//|   ldStopLoss - уровень StopLoss                                  |
//|   clModify   - цвет модификации                                  |
//+------------------------------------------------------------------+
void ModifyStopLoss(double ldStop, color clModify) {
  bool   fm;
  double ldOpen=OrderOpenPrice();
  double ldTake=OrderTakeProfit();
  int    err;

  if (IsTradeAllowed()) {
    fm=OrderModify(OrderTicket(), ldOpen, ldStop, ldTake, 0, clModify);
    if (fm) {
      if (UseSound) PlaySound(NameFileSound);
    } else {
      err=GetLastError();
      Print("Error(",err,") modifying SL: ",ErrorDescription(err));
    }
  }
}
Авторизуйтесь или зарегистрируйтесь, чтобы добавить комментарий