Block_err_130

Download script
Author: Roll

This script serves as a safety filter for MetaTrader 4, designed to prevent "Error 130." In trading terms, Error 130 occurs when you try to place or modify an order with a Stop Loss (SL) or Take Profit (TP) level that is too close to the current market price. Every broker has minimum distance requirements (Stop Levels) and "frozen" zones where prices cannot be changed; this script automatically checks if your intended trade parameters comply with those rules before sending the command to the server.

How the Logic Works

The core purpose of the script is to act as a "pre-flight check." Instead of letting the trading platform attempt an invalid action—which results in an error and can disrupt automated trading—this logic verifies that your requested prices are mathematically valid based on real-time market conditions.

1. Dynamic Market Data Retrieval The script begins by pulling current data from the broker, specifically:

  • Point Value: The smallest price movement for the asset.
  • Stop Level: The minimum distance required between the current price and your Stop Loss/Take Profit.
  • Freeze Level: The price range where an order becomes "locked" and cannot be modified.

2. Trade Type Identification The script categorizes every action into a specific "case." It recognizes different types of market interactions, including:

  • Immediate Actions: Opening new Buy or Sell positions.
  • Pending Orders: Placing Buy/Sell Limits and Buy/Sell Stops.
  • Modifications: Adjusting the SL or TP on an already active trade.

3. The Validation Process For every action you take, the script performs a two-step validation:

  • Distance Check: It calculates if the distance between the current market price and your desired Stop Loss/Take Profit is greater than the broker's required "Stop Level."
  • Freeze Zone Check: It ensures that if you are trying to modify an existing trade, the new price isn't inside the "Freeze Level" (the area where the broker prevents changes).

4. Success and Failure Handling

  • Validation Passed: If the script determines that your requested prices are far enough away from the market price to be accepted by the broker, it returns a "True" signal. This tells the rest of your trading system that it is safe to proceed.
  • Validation Failed: If the requested price is too close to the current market price, the script identifies this as an invalid move. By catching this early, it prevents the platform from sending a faulty request that would otherwise trigger an error.

Summary for the User

Think of this script as a "Rule Checker." Before your trading software tries to place a trade, this script looks at the broker's rules and your desired price. If your price is too close to the current market (which the broker forbids), the script flags it immediately. This ensures that your automated trades run smoothly without being interrupted by technical execution errors.

10 Views
0 Downloads
0 Favorites
Block_err_130
//+-----------------------------------------------------------------+
//|                                               Block_err_130.mq4 |
//|                                                            Roll |
//+-----------------------------------------------------------------+
#property copyright "Roll"
#property link ""
#property library
#include <stdlib.mqh>
//-----Ïðîâåðêà êîððåêòíîñòè ëþáîé îïåðàöèè âñåõ òèïîâ îðäåðîâ------
bool Block_err_130(int M,int TypeOrder,double OP,double StopLoss,double TakeProfit,
                     double NewOpenPrice,double NewStopLoss,double NewTakeProfit) 
{int Mode=0;int Type=-1;int J=0; 
 double OpenPrice=0;double TP=0;double SL=0;double OPnew=0;double TPnew=0;double SLnew=0;
 double point=MarketInfo(Symbol(),MODE_POINT);
 double StopLevel=MarketInfo(Symbol(),MODE_STOPLEVEL)*point;
 double FreezeLevel=MarketInfo(Symbol(),MODE_FREEZELEVEL)*point;
 Mode=M;Type=TypeOrder;J=Mode*10+Type;
 if(Mode==1){OpenPrice=OP;TP=TakeProfit;SL=StopLoss;}
 if(Mode==2){OPnew=NewOpenPrice;TPnew=NewTakeProfit;SLnew=NewStopLoss;}     
 if(Mode==3){OpenPrice=OP;TP=TakeProfit;SL=StopLoss;OPnew=NewOpenPrice;
             TPnew=NewTakeProfit;SLnew=NewStopLoss;} 
 RefreshRates();           
switch(J){ //----Çàêðûòèå/Óäàëåíèå îðäåðîâ èëè "Ìîäèôèêàöèÿ I"/ïîäãîòîâêà
case 10://Çàêðûòèå OP_BUY:Mode==1,Type==0
 if((Bid-SL>FreezeLevel||SL==0)&&(TP-Bid>FreezeLevel||TP==0)&&Mode==1)return(true);break;
case 11://Çàêðûòèå OP_SELL:Mode==1,Type==1
 if((SL-Ask>FreezeLevel||SL==0)&&(Ask-TP>FreezeLevel||TP==0)&&Mode==1)return(true);break;
case 12://Óäàëåíèå BuyLimit:Mode==1,Type==2
 if((Ask-OpenPrice>FreezeLevel)&&(StopLevel<=OpenPrice-SL||SL==0)&& 
    (StopLevel<=TP-OpenPrice||TP==0)&&Mode==1)return(true);break;
case 13://Óäàëåíèå SellLimit:Mode==1,Type==3
 if((OpenPrice-Bid>FreezeLevel)&&(StopLevel<=SL-OpenPrice||SL==0)&& 
    (StopLevel<=OpenPrice-TP||TP==0)&&Mode==1)return(true);break;
case 14://Óäàëåíèå BuyStop:Mode==1,Type==4
 if((OpenPrice-Ask>FreezeLevel)&&(StopLevel<=OpenPrice-SL||SL==0)&& 
    (StopLevel<=TP-OpenPrice||TP==0)&&Mode==1)return(true);break;
case 15://Óäàëåíèå SellStop:Mode==1,Type==5
 if((Bid-OpenPrice>FreezeLevel)&&(StopLevel<=SL-OpenPrice||SL==0)&& 
    (StopLevel<=OpenPrice-TP||TP==0)&&Mode==1)return(true);break;
       //----Îòêðûòèå îðäåðîâ èëè "Ìîäèôèêàöèÿ II"/öåëü 
case 20://Îòêðûòèå OP_BUY:Mode==2,Type==0
 if((StopLevel<=Bid-SLnew||SLnew==0)&&(StopLevel<=TPnew-Bid||TPnew==0))return(true);break; 
case 21://Îòêðûòèå OP_SELL:Mode==2,Type==1
 if((StopLevel<=SLnew-Ask||SLnew==0)&&(StopLevel<=Ask-TPnew||TPnew==0))return(true);break;
case 22://Îòêðûòèå BuyLimit:Mode==2,Type==2
 if((StopLevel<=Ask-OPnew)&&(StopLevel<=OPnew-SLnew||SLnew==0)&& 
  (StopLevel<=TPnew-OPnew||TPnew==0)&&Mode==2)return(true);break;  
case 23://Îòêðûòèå SellLimit:Mode==2,Type==3
 if((StopLevel<=OPnew-Bid)&&(StopLevel<=SLnew-OPnew||SLnew==0)&& 
  (StopLevel<=OPnew-TPnew||TPnew==0)&&Mode==2)return(true);break;
case 24://Îòêðûòèå BuyStop:Mode==2,Type==4
 if((StopLevel<=OPnew-Ask)&&(StopLevel<=OPnew-SLnew||SLnew==0)&& 
  (StopLevel<=TPnew-OPnew||TPnew==0)&&Mode==2) return(true);break;
case 25://Îòêðûòèå SellStop:Mode==2,Type==5
 if((StopLevel<=Bid-OPnew)&&(StopLevel<=SLnew-OPnew||SLnew==0)&& 
  (StopLevel<=OPnew-TPnew||TPnew==0)&&Mode==2)return(true);break;
case 30://Ìîäèôèêàöèÿ Buy îðäåðà 
 if((Bid-SL>FreezeLevel||SL==0)&&(TP-Bid>FreezeLevel||TP==0))
  {if((StopLevel<=Bid-SLnew||SLnew==0)&&(StopLevel<=TPnew-Bid||TPnew==0))return(true);}break; 
case 31://Ìîäèôèêàöèÿ Sell îðäåðà  
 if((SL-Ask>FreezeLevel||SL==0)&&(Ask-TP>FreezeLevel||TP==0)) 
  {if((StopLevel<=SLnew-Ask||SLnew==0)&&(StopLevel<=Ask-TPnew||TPnew==0))return(true);}break;
case 32://Ìîäèôèêàöèÿ BuyLimit îðäåðà
 if((Ask-OpenPrice>FreezeLevel)&&(StopLevel<=OpenPrice-SL||SL==0)&& 
   (StopLevel<=TP-OpenPrice||TP==0))
  {if((StopLevel<=Ask-OPnew)&&(StopLevel<=OPnew-SLnew||SLnew==0)&& 
    (StopLevel<=TPnew-OPnew||TPnew==0))return(true);}break;                 
case 33://Ìîäèôèêàöèÿ SellLimit îðäåðà
 if((OpenPrice-Bid>FreezeLevel)&&(StopLevel<=SL-OpenPrice||SL==0)&& 
   (StopLevel<=OpenPrice-TP||TP==0)) 
  {if((StopLevel<=OPnew-Bid)&&(StopLevel<=SLnew-OPnew||SLnew==0)&& 
    (StopLevel<=OPnew-TPnew||TPnew==0))return(true);}break;   
case 34://Ìîäèôèêàöèÿ BuyStop îðäåðà
 if((OpenPrice-Ask>FreezeLevel)&&(StopLevel<=OpenPrice-SL||SL==0)&& 
   (StopLevel<=TP-OpenPrice||TP==0))  
  {if((StopLevel<=OPnew-Ask)&&(StopLevel<=OPnew-SLnew||SLnew==0)&& 
    (StopLevel<=TPnew-OPnew||TPnew==0))return(true);}break; 
case 35://Ìîäèôèêàöèÿ SellStop îðäåðà             
 if((Bid-OpenPrice>FreezeLevel)&&(StopLevel<=SL-OpenPrice||SL==0)&& 
   (StopLevel<=OpenPrice-TP||TP==0)) 
  {if((StopLevel<=Bid-OPnew)&&(StopLevel<=SLnew-OPnew||SLnew==0)&& 
    (StopLevel<=OPnew-TPnew||TPnew==0))return(true);}break;}return(0);}
//+---------ÍÎÐÌÀËÈÇÀÖÈß  n/a-------------------+
 double ND(double v){double digits=MarketInfo(Symbol(),MODE_DIGITS); 
          return(NormalizeDouble(v,digits));}
//+--------Function..:OrderError n/a -----------+
 string OrderError(){int iError=GetLastError();
   return(StringConcatenate("Order:",OrderTicket()," GetLastError()=",
   iError," ",ErrorDescription(iError)));}

Comments