HELP, failed cancel order [Invalid stops] problem! - page 2

 
Edison Chen #: thx, I've done all the validation in my code, including the Freeze Levels, Stop levels, etc.But the check is still wrong, I feel very confused!

Sorry, but I have difficulty believing that you are checking the Freeze Level and Stops Levels properly, because from your original post it was clear you were not aware of them, and simply stating "etc."  shows that you don't even know what other checks you need to do, or else you would have referenced them.

If you want us to help check your code then you are going to have to show your code. Just because you "say" you are doing it does not mean you are doing it correctly. If you don't want to show your code, then you will have to debug it and figure it yourself and try to find what it is doing wrong.

 
Fernando Carreiro #:

抱歉,但我很难相信您正在正确检查冻结级别和停止级别,因为从您的原始帖子中可以清楚地看出您不知道它们,并且只是说“等”。表明您甚至不知道您需要执行哪些其他检查,否则您会引用它们。

如果您希望我们帮助检查您的代码,那么您将不得不展示您的代码。仅仅因为你“说”你在做这件事并不意味着你做对了。如果您不想显示您的代码,那么您将不得不调试它并自己弄清楚它并尝试找出它做错了什么。


TradeClass::TradeClass(const string _symbol,
                       OrderInfo *OInfoObj,
                       const int Magic = DEFULT_TRADE_MAGIC,
                       const int _Slippage = DEFULT_TRADE_SLIPPAGE,
                       const int _MaxSpread = DEFULT_TRADE_SPREAD,
                       const string comment = DEFULT_TRADE_COMMENT) {
   symbol = _symbol;
   MaxLot = SymbolInfoDouble(_symbol,SYMBOL_VOLUME_MAX);
   MinLot = SymbolInfoDouble(_symbol,SYMBOL_VOLUME_MIN);
   point = SymbolInfoDouble(_symbol,SYMBOL_POINT);
   //FreezeLevel = SymbolInfoInteger(_symbol,SYMBOL_TRADE_FREEZE_LEVEL)*point;
   //StopLevel = SymbolInfoInteger(_symbol,SYMBOL_TRADE_STOPS_LEVEL)*point;
   StopLevel = (MathMax(SymbolInfoInteger(_symbol,SYMBOL_TRADE_STOPS_LEVEL),SymbolInfoInteger(_symbol,SYMBOL_TRADE_FREEZE_LEVEL)))*point+50*point;
   //StopLevel = SymbolInfoInteger(_symbol,SYMBOL_TRADE_STOPS_LEVEL)*point+SymbolInfoInteger(_symbol,SYMBOL_TRADE_FREEZE_LEVEL)*point;
   SymbolStartTime = SymbolInfoInteger(_symbol,SYMBOL_START_TIME);
   SymbolExpirationTime = SymbolInfoInteger(_symbol,SYMBOL_EXPIRATION_TIME);
   Slippage = _Slippage;
   MaxSpread = _MaxSpread*point;
   MagicNumber = Magic;
   Comment = comment;
   OrderObj = OInfoObj;
   t.SetAsyncMode(false);
}
bool TradeClass::MoveOrder(const ENUM_ORDER_TYPE _OP,
                           const int Ticket,
                           const double _Price,
                           const double _SL=0,
                           const double _TP=0) {
   if(!OrderSelect(Ticket)) return false;
   if(MathAbs(_Price - OrderGetDouble(ORDER_PRICE_OPEN)) < point || MathAbs(_Price - OrderGetDouble(ORDER_PRICE_OPEN)) < SymbolInfoDouble(symbol,SYMBOL_TRADE_TICK_SIZE)) return false;
   if(!CheckPrice(_OP,_Price)) return false;
   static double SL,TP;
   SL = _SL != 0 ? SLTransfer(symbol,_OP,_SL,_Price) : _SL;
   TP = _TP != 0 ? TPTransfer(symbol,_OP,_TP,_Price) : _TP;
   if(!CheckTP(_OP,TP,SL)) return false;
   if(!CheckSL(_OP,SL,TP)) return false;
   //if(!OrderValid(_OP,_Price,SL,TP)) return false;
   ExeState = t.OrderModify(Ticket,_Price,SL,TP,0,0,0);
   if(ExeState) {
      PendingMoveTime[PositionGetInteger(POSITION_TYPE)] = TimeCurrent();
      OrderObj.RefreshOrderInfo();
   } else printf(__FUNCTION__+" Failed to move order price : "+GetLastError());
   return ExeState;
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool TradeClass::PositionMoveTP(const int Ticket,const double TP) {
   if(!PositionSelectByTicket(Ticket)) return false;
   if(MathAbs(TP - PositionGetDouble(POSITION_TP)) < point) {
      return false;
   }
   if(PositionGetInteger(POSITION_TYPE) == ORDER_TYPE_SELL) {
      if(!CheckTP(ORDER_TYPE_SELL,TP,PositionGetDouble(POSITION_PRICE_OPEN))) return false;
      if(!CheckSL(ORDER_TYPE_SELL,PositionGetDouble(POSITION_SL),PositionGetDouble(POSITION_PRICE_OPEN))) return false;
   } else if(PositionGetInteger(POSITION_TYPE) == ORDER_TYPE_BUY) {
      if(!CheckTP(ORDER_TYPE_BUY,TP,PositionGetDouble(POSITION_PRICE_OPEN))) return false;
      if(!CheckSL(ORDER_TYPE_BUY,PositionGetDouble(POSITION_SL),PositionGetDouble(POSITION_PRICE_OPEN))) return false;
   }
   //if(!PositionValid(PositionGetInteger(POSITION_TYPE),PositionGetDouble(POSITION_SL),TP)) return false;
   ExeState = t.PositionModify(Ticket,PositionGetDouble(POSITION_SL),TP);
   if(ExeState) {
      TPMoveTime[PositionGetInteger(POSITION_TYPE)] = TimeCurrent();
      OrderObj.RefreshOrderInfo();
   } else printf(__FUNCTION__+" Failed to move takeprofit : "+GetLastError());
   return ExeState;
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool TradeClass::PositionMoveSL(const int Ticket,const double SL) {
   if(!PositionSelectByTicket(Ticket)) return false;
   if(MathAbs(SL - PositionGetDouble(POSITION_SL)) < point) return false;
   if(PositionGetInteger(POSITION_TYPE) == ORDER_TYPE_SELL) {
      if(!CheckSL(ORDER_TYPE_SELL,SL,PositionGetDouble(POSITION_PRICE_OPEN))) return false;
      if(!CheckTP(ORDER_TYPE_SELL,PositionGetDouble(POSITION_TP),PositionGetDouble(POSITION_PRICE_OPEN))) return false;
   }
   if(PositionGetInteger(POSITION_TYPE) == ORDER_TYPE_BUY) {
      if(!CheckSL(ORDER_TYPE_BUY,SL,PositionGetDouble(POSITION_PRICE_OPEN))) return false;
      if(!CheckTP(ORDER_TYPE_BUY,PositionGetDouble(POSITION_TP),PositionGetDouble(POSITION_PRICE_OPEN))) return false;
   }
   //if(!PositionValid(PositionGetInteger(POSITION_TYPE),SL,PositionGetDouble(POSITION_TP))) return false;
   ExeState = t.PositionModify(Ticket,SL,PositionGetDouble(POSITION_TP));
   if(ExeState) OrderObj.RefreshOrderInfo();
   else printf(__FUNCTION__+" Failed to move stoploss : "+GetLastError());
   return ExeState;
}

bool TradeClass::CheckPrice(const ENUM_ORDER_TYPE OP,const double _Price) {
   if(OP == ORDER_TYPE_BUY_LIMIT) {
      if(_Price < BID(symbol) - StopLevel-1*point) {
         return true;
      }
   } else if(OP == ORDER_TYPE_SELL_LIMIT) {
      if(_Price > ASK(symbol) + StopLevel+1*point) {
         return true;
      }
   } else if(OP == ORDER_TYPE_SELL_STOP) {
      if(_Price < BID(symbol) - StopLevel-1*point) {
         return true;
      }
   } else if(OP == ORDER_TYPE_BUY_STOP) {
      if(_Price > ASK(symbol) + StopLevel+1*point) {
         return true;
      }
   }
   return false;
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double TradeClass::SLTransfer(const string s,const int OP,const double Sl,const double price=0) {
   if(OP == ORDER_TYPE_BUY) {
      return (ASK(symbol) - Sl*point);
   }
   if(OP == ORDER_TYPE_BUY_LIMIT || OP == ORDER_TYPE_BUY_STOP) {
      return (price - Sl*point);
   }
   if(OP == ORDER_TYPE_SELL) {
      return (BID(symbol) + Sl*point);
   }
   if(OP == ORDER_TYPE_SELL_LIMIT || OP == ORDER_TYPE_SELL_STOP) {
      return (price + Sl*point);
   }
   return 0;
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double TradeClass::TPTransfer(const string s,const int OP,const double Tp,const double price=0) {
   if(OP == ORDER_TYPE_BUY)
      return (ASK(symbol) + Tp*point);
   if(OP == ORDER_TYPE_BUY_LIMIT || OP == ORDER_TYPE_BUY_STOP)
      return (price + Tp*point);
   if(OP == ORDER_TYPE_SELL)
      return (BID(symbol) - Tp*point);
   if(OP == ORDER_TYPE_SELL_LIMIT || OP == ORDER_TYPE_SELL_STOP)
      return (price - Tp*point);
   return 0;
}

I did try everything and had no problems running EA on MT5. The code is posted, please help me to have a look, thank you!

 

No! You can't just interchange Stops Levels and Freeze Levels. They are completely different concepts and limits.

StopLevel Minimum Distance Limitation.

A trade operation will not be performed if any of the following conditions is disrupted.

Order Type
Open Price StopLoss (SL) TakeProfit (TP)
Buy
Modification is prohibited
Bid-SL StopLevel TP-Bid StopLevel
Sell
Modification is prohibited SL-Ask StopLevel Ask-TP StopLevel
BuyLimit
Ask-OpenPriceStopLevel OpenPrice-SL StopLevel TP-OpenPrice StopLevel
SellLimit
OpenPrice-Bid StopLevel SL-OpenPrice StopLevel OpenPrice-TP StopLevel
BuyStop
OpenPrice-Ask StopLevel OpenPrice-SL StopLevel TP-OpenPrice StopLevel
SellStop
Bid-OpenPrice StopLevel SL-OpenPrice StopLevel OpenPrice-TP StopLevel

FreezeLevel Limitation (Freezing Distance).

Market orders can not be closed if the StopLoss and TakeProfit values violate the FreezLevel parameter requirements.
StopLoss or TakeProfit orders can not be modified if StopLoss or TakeProfit values violate the StopLevel parameter requirements.
Pending orders can not be deleted or modified if the declared open price violates the FreezeLevel parameter requirements.

Order Type
Open Price StopLoss (SL) TakeProfit (TP)
Buy
Modification is prohibited Bid-SL > FreezeLevel TP-Bid > FreezeLevel
Sell
Modification is prohibited SL-Ask > FreezeLevel Ask-TP > FreezeLevel
BuyLimit
Ask-OpenPrice > FreezeLevel Regulated by the StopLevel parameter
Regulated by the StopLevel parameter
SellLimit
OpenPrice-Bid > FreezeLevel Regulated by the StopLevel parameter Regulated by the StopLevel parameter
BuyStop
OpenPrice-Ask > FreezeLevel Regulated by the StopLevel parameter Regulated by the StopLevel parameter
SellStop
Bid-OpenPrice > FreezeLevel Regulated by the StopLevel parameter Regulated by the StopLevel parameter
 
The checks a trading robot must pass before publication in the Market
The checks a trading robot must pass before publication in the Market
  • www.mql5.com
Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.
 

You are very detailed, it is very useful indeed, thank you very much!

 
Edison Chen:


Do not double post!

I have deleted your duplicated topic.

 
TradeClass::TradeClass(const string _symbol,
                       OrderInfo *OInfoObj,
                       const int Magic = DEFULT_TRADE_MAGIC,
                       const int _Slippage = DEFULT_TRADE_SLIPPAGE,
                       const int _MaxSpread = DEFULT_TRADE_SPREAD,
                       const string comment = DEFULT_TRADE_COMMENT) {
   symbol = _symbol;
   MaxLot = SymbolInfoDouble(_symbol,SYMBOL_VOLUME_MAX);
   MinLot = SymbolInfoDouble(_symbol,SYMBOL_VOLUME_MIN);
   point = SymbolInfoDouble(_symbol,SYMBOL_POINT);
   FreezeLevel = SymbolInfoInteger(_symbol,SYMBOL_TRADE_FREEZE_LEVEL)*point+50*point;
   StopLevel = SymbolInfoInteger(_symbol,SYMBOL_TRADE_STOPS_LEVEL)*point+50*point;
   SymbolStartTime = SymbolInfoInteger(_symbol,SYMBOL_START_TIME);
   SymbolExpirationTime = SymbolInfoInteger(_symbol,SYMBOL_EXPIRATION_TIME);
   Slippage = _Slippage;
   MaxSpread = _MaxSpread*point;
   MagicNumber = Magic;
   Comment = comment;
   OrderObj = OInfoObj;
   t.SetAsyncMode(false);
}

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool TradeClass::CheckTP(const ENUM_ORDER_TYPE _OP,const double _TP,const double _Price) {
   switch(_OP) {
   case ORDER_TYPE_BUY: {
      if(_TP - BID(symbol) > MathMax(StopLevel,FreezeLevel)) {
         return true;
      }
   }
   break;
   case ORDER_TYPE_SELL: {
      if(ASK(symbol) - _TP > MathMax(StopLevel,FreezeLevel)) {
         return true;
      }
   }
   break;
   case ORDER_TYPE_BUY_LIMIT: {
      if(_TP - _Price > StopLevel) {
         return true;
      }
   }
   break;
   case ORDER_TYPE_BUY_STOP: {
      if(_TP - _Price > StopLevel) {
         return true;
      }
   }
   break;
   case ORDER_TYPE_SELL_LIMIT: {
      if(_Price - _TP > StopLevel) {
         return true;
      }
   }
   break;
   case ORDER_TYPE_SELL_STOP: {
      if(_Price - _TP > StopLevel) {
         return true;
      }
   }
   break;
   }
   return false;
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool TradeClass::CheckSL(const ENUM_ORDER_TYPE _OP,const double _SL,const double _Price) {
   switch(_OP) {
   case ORDER_TYPE_BUY: {
      if(BID(symbol) - _SL > MathMax(StopLevel,FreezeLevel)) {
         return true;
      }
   }
   break;
   case ORDER_TYPE_SELL: {
      if(_SL-ASK(symbol) > MathMax(StopLevel,FreezeLevel)) {
         return true;
      }
   }
   break;
   case ORDER_TYPE_BUY_LIMIT: {
      if(_Price - _SL > StopLevel) {
         return true;
      }
   }
   break;
   case ORDER_TYPE_BUY_STOP: {
      if(_Price - _SL > StopLevel) {
         return true;
      }
   }
   break;
   case ORDER_TYPE_SELL_LIMIT: {
      if(_SL - _Price > StopLevel) {
         return true;
      }
   }
   break;
   case ORDER_TYPE_SELL_STOP: {
      if(_SL - _Price > StopLevel) {
         return true;
      }
   }
   break;
   }
   return false;
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool TradeClass::CheckPrice(const int _OP,const double _Price) {
   switch(_OP) {
   case ORDER_TYPE_BUY_LIMIT: {
      if(ASK(symbol) - _Price > MathMax(StopLevel,FreezeLevel)) {
         return true;
      }
   }
   break;
   case ORDER_TYPE_BUY_STOP: {
      if(_Price-ASK(symbol) > MathMax(StopLevel,FreezeLevel)) {
         return true;
      }
   }
   break;
   case ORDER_TYPE_SELL_LIMIT: {
      if(_Price-BID(symbol) > MathMax(StopLevel,FreezeLevel)) {
         return true;
      }
   }
   break;
   case ORDER_TYPE_SELL_STOP: {
      if(BID(symbol) - _Price > MathMax(StopLevel,FreezeLevel)) {
         return true;
      }
   }
   break;
   }
   return false;
}

I recalculated STOPLEVEL and FREEZELEVEL according to the method provided in the article, but it still couldn't pass directly. I had to add some extra points. At present, I added 50 points to pass the verification, but it was wrong for my trading system. Who can help me to see if there is a problem with the code? Thanks very much!

Reason: