OrderModify error 130

 

Hi guys, I'm new to coding in MQL4 and I was looking for some help.

I'm following a course on coding your first MT4 robot and the EA is now fully coded. I've back tested the EA on around 520 trades and for the most part it works fine. I am not going to use the EA nor is it exactly profitable, however I am getting a error code "OrderModify error 130" on around 5 trades throughout the whole 10 year back test and I want to understand why. The EA uses Bollinger bands with various Standard Deviations together with RSI values to determine the Direction, Entry, SL and TP. However, the TP is dynamic in the sense it updates when the Bollinger Band it is calculated from does. It also checks if the TP value is different from the active TP by more than 1pip and only updates to the new one if it is. I've looked into the journal to locate an effected trade and then rerun the back test in visual mode to de bug this issue, and I think I know the cause.

It seems to be happening when the TP tries to update when it is too close to current market price, and therefore throws up an error. After a search on this and many other forums, I understand there is a minimum stop loss level and I've figured my TP is trying to update while in this zone. (Please check my screenshots and inform me if you think otherwise).

I've run a check to see what the minimum stop loss level is for my broker and it returns a value of "0", so I assume incorporating this value into my code would be useless?

What I can't find information on, is firstly what value to use when your broker doesn't specify one (so my TP/SL stays out of this zone), and secondly, how I would incorporate this into my EA? I would appreciate if someone could take the time to explain to me what, where and why I need to incorporate into my code to avoid this issue for future reference and for  my own personal development.

Any advice would be greatly appreciated!

(I have commented out the lot size for back testing purposes and replaced it with a set value of 0.01. I have also commented out the print statements that clog up my journal)

//+------------------------------------------------------------------+
//|                                                        BB MR.mq4 |
//|                                           Copyright 2023, Bruets |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, Bruets"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property show_inputs
#include  <CustomFunctions.mqh>

input int bbPeriod = 50;
input int bandStdEntry = 2;
input int bandStdTP = 1;
input int bandStdSL = 6;
input int rsiPeriod = 14;
//input double riskPerTrade = 0.02;
input int rsiLowerLevel = 40;
input int rsiUpperLevel = 60;

int openOrderID;
int magicNo = 1234;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   Alert("Starting Strategy BB MR");

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   Alert("Stopping Strategy BB MR");
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
  
   double bbLowerBandEntry = iBands(NULL,0,bbPeriod,bandStdEntry,0,PRICE_CLOSE,MODE_LOWER,0);
   double bbUpperBandEntry = iBands(NULL,0,bbPeriod,bandStdEntry,0,PRICE_CLOSE,MODE_UPPER,0);
   double bbMidBand = iBands(NULL,0,bbPeriod,bandStdEntry,0,PRICE_CLOSE,0,0);
   
   double bbLowerBandTP = iBands(NULL,0,bbPeriod,bandStdTP,0,PRICE_CLOSE,MODE_LOWER,0);
   double bbUpperBandTP = iBands(NULL,0,bbPeriod,bandStdTP,0,PRICE_CLOSE,MODE_UPPER,0);
   
   double bbLowerBandSL = iBands(NULL,0,bbPeriod,bandStdSL,0,PRICE_CLOSE,MODE_LOWER,0);
   double bbUpperBandSL = iBands(NULL,0,bbPeriod,bandStdSL,0,PRICE_CLOSE,MODE_UPPER,0);
   
   double rsiValue = iRSI(NULL,0,rsiPeriod,PRICE_CLOSE,0);
   
   if(!CheckIfOpenOrdersByMagicNo(magicNo))// If no open orders then try to enter new a position
   {
      if(Ask < bbLowerBandEntry && Open[0] > bbLowerBandEntry && rsiValue < rsiLowerLevel) // Go Long
      {
         //Print("Price is bellow Lower Band and RSI Value is below " + rsiLowerLevel + " , Sending buy order");
         double stopLossPrice = NormalizeDouble(bbLowerBandSL,Digits);
         double takeProfitPrice = NormalizeDouble(bbUpperBandTP,Digits);;
         //Print("Entry Price = " + Ask);
         //Print("Stop Loss Price = " + stopLossPrice);
         //Print("Take Profit Price = " + takeProfitPrice);
         
         //double lotSize = OptimalLotSize(riskPerTrade,Ask,stopLossPrice);
         
         openOrderID = OrderSend(NULL,OP_BUYLIMIT,0.01,Ask,10,stopLossPrice,takeProfitPrice,NULL,magicNo);
         if(openOrderID < 0) Alert("Order rejected. Order error: " + GetLastError());
      }
      else if(Bid > bbUpperBandEntry && Open[0] < bbUpperBandEntry && rsiValue > rsiUpperLevel) //Go Short
      {
         //Print("Price is above Upper band and RSI Value is above " + rsiUpperLevel + " Sending sell order");
         double stopLossPrice = NormalizeDouble(bbUpperBandSL,Digits);
         double takeProfitPrice = NormalizeDouble(bbLowerBandTP,Digits);
         //Print("Entry Price = " + Bid);
         //Print("Stop Loss Price = " + stopLossPrice);
         //Print("Take Profit Price = " + takeProfitPrice);
          
          //double lotSize = OptimalLotSize(riskPerTrade,Bid,stopLossPrice);

          openOrderID = OrderSend(NULL,OP_SELLLIMIT,0.01,Bid,10,stopLossPrice,takeProfitPrice,NULL,magicNo);
          if(openOrderID < 0) Alert("Order rejected. Order error: " + GetLastError());
      }
   }
   
   
   else // Else if already in a position, update orders
   {
      if(OrderSelect(openOrderID,SELECT_BY_TICKET)==true)
      {
            int orderType = OrderType(); // Long = 0, Short = 1

            double newTP;
            
            if(orderType == 0)//Long
            {
               newTP = NormalizeDouble(bbUpperBandTP,Digits);
               
            }
            else //Short
            {
               newTP = NormalizeDouble(bbLowerBandTP,Digits);
            }
            

            double oldTP = OrderTakeProfit();
            double TPdistance = MathAbs(oldTP - newTP);
            if(oldTP != newTP && TPdistance > 0.0001)
            {
               bool Ans = OrderModify(openOrderID,OrderOpenPrice(),OrderStopLoss(),newTP,0);
            
               if (Ans==true)                     
               {
                  Print("Order modified: ",openOrderID);
                  return;                           
               }
               else
               {
                  Print("Unable to modify order: ",openOrderID);
               }   
            }
         }
      }
   }
   
   

//+------------------------------------------------------------------+
Files:
Before.png  236 kb
After.png  227 kb
 

I did not analyse your code, but reading the following may help you identify the problem yourself ... Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial

 

The following may also help ...

Reason: