Error 4756 and ReturnCode 10016, how to place order Comment

 

Problem sorted by using Different Magic No for different strategy ...

Hi

I am having problem (most likely) to place check condition for OrderComment field (highlighted in light blue).

Also is it necessary to define ticket, tp, magic etc (highlighted in green), while I just wanted to update / trail Stop Loss only ?

please help to locate what I am doing wrong.

thanks in advance for your support.

//+----------------------------------------------------------------------------------------------------------+
//| Function Trade_iSAR_StopLossTrail() Trailing of Stop Loss of position
//+----------------------------------------------------------------------------------------------------------+
bool Trade_iSAR_StopLossTrail()
{
  int checkIndex = 1; // bar index at which the Stop Loss values will be picked from
  //+--------------------------------------------------------------------------------------------------------+
  //| Call Indicator values into Global Arrays
  //+--------------------------------------------------------------------------------------------------------+
  if(!Get_iBands(Bar_Start,Bar_Count))
    {Print(__LINE__,": Error CopyBuffer data from Get_iBands() Code: ",GetLastError());
     return(false);}
  if(!Get_iSAR(Bar_Start,Bar_Count))
    {Print(__LINE__,": Error CopyBuffer data from Get_iSAR() Code: ",GetLastError());
     return(false);}
    //+------------------------------------------------------------------------------------------------------+
    //| Check Called values are not equal to Zero ...
    //+------------------------------------------------------------------------------------------------------+
    if((iSAR_H01[checkIndex] <= 0) || (BB_Upper_H01[checkIndex] <= 0) || (BB_Lower_H01[checkIndex] <= 0))
      {Print(__LINE__,"Trade_iSAR_StopLossTrail value returned <= Zero");
       return(false);}
  //+--------------------------------------------------------------------------------------------------------+
  //| declare and initialize the trade request and result of trade request
  //+--------------------------------------------------------------------------------------------------------+
  MqlTradeRequest myRequest;
  MqlTradeResult  myResult;
  int positionTotal = PositionsTotal(); // number of open positions   
  //+--------------------------------------------------------------------------------------------------------+
  //| for...loop to iterate over all open positions
  //+--------------------------------------------------------------------------------------------------------+
  for(int i = 0; i < positionTotal; i++)
    {
    //+------------------------------------------------------------------------------------------------------+
    //| Get parameters of the open position / order
    //+------------------------------------------------------------------------------------------------------+
      ulong  positionTicket     = PositionGetTicket(i);                // ticket of the position
      string positionSymbol     = PositionGetString(POSITION_SYMBOL);  // symbol 
      int    symbolDigits       = (int)SymbolInfoInteger(positionSymbol,SYMBOL_DIGITS); // number of decimal places
      ulong  positionMagicNo    = PositionGetInteger(POSITION_MAGIC);  // MagicNumber of the position
      double positionLot        = PositionGetDouble(POSITION_VOLUME);  // volume of the position
      double positionStopLoss   = PositionGetDouble(POSITION_SL);      // Stop Loss of the position
      double positionTakeProfit = PositionGetDouble(POSITION_TP);      // Take Profit of the position
      string positionComment    = PositionGetString(POSITION_COMMENT); // orderComments
      ENUM_POSITION_TYPE positionType =(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);  // type of the position

      //| Print / Output information about the position
      PrintFormat("#%I64u %s  %s  %.2f  %s  sl: %s  tp: %s  [%I64d]",
                  positionTicket,
                  positionSymbol,
                  EnumToString(positionType),
                  positionLot,
                  DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN),symbolDigits),
                  DoubleToString(positionStopLoss,symbolDigits),
                  DoubleToString(positionTakeProfit,symbolDigits),
                  positionMagicNo);
    //+------------------------------------------------------------------------------------------------------+
    //| Match Magic No and Order Comment to select position for StopLoss Trailing
    //+------------------------------------------------------------------------------------------------------+
    if(positionMagicNo == MagicNo  && positionComment == "Trade_pSAR")
      {
        //--- calculate the current price levels
        double positionOpen   = PositionGetDouble(POSITION_PRICE_OPEN);
        double trail_StopLoss = NULL;
        //double trail_TakeProfit = NULL;
      //+----------------------------------------------------------------------------------------------------+
      //| BUY / LONG Trailing Stop Loss update
      //| Check iSAR is BELOW previous Close, not needed as bought when iSAR Dot below the price
      //+----------------------------------------------------------------------------------------------------+
      if(positionType == POSITION_TYPE_BUY)
        {
          trail_StopLoss = NormalizeDouble(iSAR_H01[1],symbolDigits);
          Print("Buy StopLoss Trail ",trail_StopLoss);
        }
      //+----------------------------------------------------------------------------------------------------+
      //| SELL / SHORT Trailing Stop Loss update
      //| Check iSAR is ABOVE previous Close, not needed as bought when iSAR Dot above the price
      //+----------------------------------------------------------------------------------------------------+
      if(positionType == POSITION_TYPE_SELL)
        {
          trail_StopLoss = NormalizeDouble(iSAR_H01[1],symbolDigits);
          Print("Sell StopLoss Trail ",trail_StopLoss);
        }
      //+----------------------------------------------------------------------------------------------------+
      //| Update Trailing Stop Loss of the position
      //+----------------------------------------------------------------------------------------------------+
        //--- zeroing the myRequest and myResult values
        ZeroMemory(myRequest);
        ZeroMemory(myResult);
        //--- setting the operation parameters
        myRequest.action   = TRADE_ACTION_SLTP;  // trade operation for modifying the Stop Loss and Take Profit values of an open position
        myRequest.position = positionTicket;     // ticket of the position
        myRequest.symbol   = positionSymbol;     // symbol 
        myRequest.sl       = trail_StopLoss;     // Stop Loss of the position
        myRequest.tp       = positionTakeProfit; // Stop Loss of the position
        myRequest.magic    = MagicNo;            // MagicNumber of the position
        //--- output information about the modification
        PrintFormat("Modify #%I64d %s %s",positionTicket,positionSymbol,EnumToString(positionType));
        //--- send the myRequest
        if(!OrderSend(myRequest,myResult))
          PrintFormat("OrderSend error %d",GetLastError());  // if unable to send the myRequest, output the error code
        //--- information about the operation   
        PrintFormat("retcode=%u  deal=%I64u  order=%I64u",myResult.retcode,myResult.deal,myResult.order);
      } // END Of if...loop for trailing stop loss
  //---
    } // END Of for...loop for itiration
//---
  return(true);
} // END Of Trade_iSAR_StopLossTrail()
Documentation on MQL5: Common Functions / GetTickCount64
Documentation on MQL5: Common Functions / GetTickCount64
  • www.mql5.com
GetTickCount64 - Common Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5