Am Getting Error 4108 for my OrderModify function

92 Views Asked by At

I am trying to program an EA on MQL4, and after multiple backtests, I keep getting error 4108 for my OrderModify() function.

Here is the code for my Expert Advisor:

//Global Variables
   int magicNumber = 43;
   
   //Bollinger Bands
   input int bbPeriod = 50; //SMA period for the Boolinger Band indicators
   input int entryBandStd = 2; //Standard Deviation for the Position entry Bollinger Band
   input int profitBandStd = 1; //Standard Deviation for the profit Bollinger Band
   input int exitBandStd = 6; //Standard Deviation for the Stop Loss Bollinger Band
   
   //RSI Period & Levels
   input int rsiPeriod = 14; //Period for the RSI indicator
   input int rsiLowerLevel = 40; //The lower RSI value
   input int rsiUpperLevel = 60; //The higher RSI value
   
   //Risk Percentage
   input double riskPercentage = 0.02; //2%
   
   //TP & SP Price Variables
   double stopLossPrice;
   double takeProfitPrice;
   
   //OrderSend vriable
   int orderID;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   Alert("");
   Alert("Starting Expert Advisor");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   Alert("BB Algorithm has been disabled.");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
   //Variables
      //Profit Bollinger Band
         double bbBand1Lower = iBands(NULL, 0, bbPeriod, profitBandStd, 0, PRICE_CLOSE, MODE_LOWER, 0);
         double bbBand1Upper = iBands(NULL, 0, bbPeriod, profitBandStd, 0, PRICE_CLOSE, MODE_UPPER, 0);
      
      //Entry Bollinger Band & Middle Band
         double bbMiddleBand = iBands(NULL, 0, bbPeriod, entryBandStd, 0, PRICE_CLOSE, 0, 0);
         double bbBand2Lower = iBands(NULL, 0, bbPeriod, entryBandStd, 0, PRICE_CLOSE, MODE_LOWER, 0);
         double bbBand2Upper = iBands(NULL, 0, bbPeriod, entryBandStd, 0, PRICE_CLOSE, MODE_UPPER, 0);
      
      //Exit Bollinger Band
         double bbBand3Lower = iBands(NULL, 0, bbPeriod, exitBandStd, 0, PRICE_CLOSE, MODE_LOWER, 0);
         double bbBand3Upper = iBands(NULL, 0, bbPeriod, exitBandStd, 0, PRICE_CLOSE, MODE_UPPER, 0);
      
      //RSI
         double rsiValue = iRSI(NULL,0,rsiPeriod,PRICE_CLOSE,0);
   
   if(!CheckIfOpenOrdersByMagicNumber(magicNumber)) //If there are no open orders, try to enter a new position
   {
      if(Ask < bbBand2Lower && Open[0] > bbBand2Lower && rsiValue < rsiLowerLevel) //If the price is lower than the set band, and the RSI for the currency is lower than 40, AND if the candle's value is not too far below the bollinger band.
      {
         Print("Sending a buy order.");
         
         //Take Profit price
            takeProfitPrice = NormalizeDouble(bbBand1Upper, Digits);
         
         //Stop Loss Price
            stopLossPrice = NormalizeDouble(bbBand3Lower, Digits);
         
         Print("Entry Price: " + Ask);
         Print("Take Profit Price: " + takeProfitPrice);
         Print("Stop Loss Price: " + stopLossPrice);
         
         //Declaring and using Include Function to calculate the Lot Size for the order
            double lotSize = OptimalLotSize(riskPercentage, Ask, stopLossPrice);
         
         //Sending Buy order
            orderID = OrderSend(NULL, OP_BUYLIMIT, lotSize, Ask, 10, stopLossPrice, takeProfitPrice, NULL, magicNumber);
         
         //If the order gets rejected, it will display the exact error number for reference so you can debug the problem
            if(orderID < 0) Alert("Order rejected. Order error: " + GetLastError());
      }
      else if(Bid > bbBand2Upper && Open[0] < bbBand2Upper && rsiValue > rsiUpperLevel) //If the price is higher than the set band, and the RSI for the currency is higer than 70, AND if the candle's value is not too far above the bollinger band.
      {
         Print("Sending a short order.");
         
         //Take Profit Price
            takeProfitPrice = NormalizeDouble(bbBand1Lower, Digits);
         
         //Stop Loss Price
            stopLossPrice = NormalizeDouble(bbBand3Upper, Digits);
         
         Print("Entry Price: " + Bid);
         Print("Take Profit Price: " + takeProfitPrice);
         Print("Stop Loss Price: " + stopLossPrice);
         
         //Declaring and using Include Function to calculate the Lot Size for the order
            double lotSize = OptimalLotSize(riskPercentage, Bid, stopLossPrice);
         
         //Sending Buy order
            orderID = OrderSend(NULL, OP_SELLLIMIT, lotSize, Bid, 10, stopLossPrice, takeProfitPrice, NULL, magicNumber);
         
         //If the order gets rejected, it will display the exact error number for reference so you can debug the problem
            if(orderID < 0) Alert("Order rejected. Order error: " + GetLastError());
      }
      else //If there is already an open order, then the Take Profit price will get updated
      {
         if(OrderSelect(orderID,SELECT_BY_TICKET)==true)
         {
         
            int orderType = OrderType(); //Long = 0, Short = 1
            
            double desiredTakeProfit;
            
            if(orderType == 0) //Long Position
            {
               desiredTakeProfit = NormalizeDouble(bbBand1Upper, Digits);
            }
            else //Short Position
            {
               desiredTakeProfit = NormalizeDouble(bbBand1Lower, Digits);
            }
            
            double TP = OrderTakeProfit();
            double TPdistance = MathAbs(TP - desiredTakeProfit); 
            
            if(TP != desiredTakeProfit && TPdistance > 0.0001)
            {  
               bool Ans = OrderModify(OrderTicket(), OrderOpenPrice(), OrderStopLoss(), desiredTakeProfit, 0);
               
               if (Ans == true)
               {
                  Print("Order Modified: ", orderID);
                  return;
               }
               else
               {
                  Print("Unable to modify order, Error: ", GetLastError());
               }
               
               
            }
            
         }
      }
   }
  }

Here is also code from an Include file I call in the EA to check if there are asny open orders:

bool CheckIfOpenOrdersByMagicNumber(int magicNB)
{
   int openOrders = OrdersTotal(); 
   
   for (int i = 0; i < openOrders; i++) 
   {
      if(OrderSelect(i, SELECT_BY_POS)==true) 
      {
         if(OrderMagicNumber() == magicNB)
         {
            return true;
         }
      }
   }
   return false;
}

Here are the error messages I am getting on the Journal from my backtests:

I am trying to have my EA modify the Take Profit for my open order using the OrderModify() function.

0

There are 0 best solutions below