Author: Verdi
Profit factor:
0.00

This automated trading tool is designed to manage risk by monitoring your account's floating losses and automatically balancing your trading positions. It acts as a safety net that intervenes when your current open trades move into a negative state beyond a specific limit you define.

Core Functionality

The script works based on three main components: risk monitoring, position auditing, and automated correction.

1. Risk Monitoring (The "Safety Trigger")

The tool continuously tracks your account's balance against its current equity (the actual value of your account including open profits and losses). It specifically looks for "Floating Loss"—situations where your open trades are currently losing money.

  • You can set a specific percentage threshold (for example, 4%).
  • The script calculates how much of your balance is currently at risk due to open losses. If that loss exceeds your chosen percentage, the tool prepares to take action.

2. Position Auditing

The script constantly scans all your open trades on a specific currency pair. It performs a "volume check" by summing up:

  • The total number of lots currently in Buy positions.
  • The total number of lots currently in Sell positions.

By comparing these two totals, the tool identifies if your portfolio is "unbalanced." For instance, if you have much larger Sell positions than Buy positions (or vice versa), the script notes this discrepancy.

3. Automated Correction (The Balancing Act)

When both conditions are met—meaning your losses have exceeded your safety limit and your trading positions are unbalanced—the tool automatically executes a "hedging" or "balancing" order.

  • If you are over-exposed on Sells: The script will automatically open a Buy order to offset the imbalance.
  • If you are over-exposed on Buys: The script will automatically open a Sell order to offset the imbalance.

The size of this new corrective order is precisely calculated to match the difference between your Buy and Sell volumes, aiming to neutralize the imbalance and stabilize your exposure.

User Experience

  • Visual Feedback: The tool displays the current risk level and status directly on your trading chart so you can see your "FlowLoss" percentage in real-time.
  • Safety Notifications: If the script is not enabled or if it encounters an issue opening a balancing order, it will trigger an alert and a message on your screen to keep you informed.
  • Automation: Once configured, the logic runs automatically, attempting to "lock" your risk by balancing out heavy positions whenever they hit your predefined loss threshold.
Orders Execution
Checks for the total of open ordersIt automatically opens orders when conditions are reached
Miscellaneous
It issuies visual alerts to the screen
10 Views
0 Downloads
0 Favorites
STOP_v1
//+------------------------------------------------------------------+
//|                                                         STOP.mq4 |
//|                                                            Verdi |
//|                                                  nemo811@mail.ru |
//+------------------------------------------------------------------+
#property copyright "Verdi"
#property link      "nemo811@mail.ru"

extern double RM = 4.000; //Ìàêñèìàëüíî äîïóñòèìûé % ïðîñàäêè äåïî

double Balans,Equity,FlowLoss;

int init()
  {

   if (!IsTesting())
    {
     if(!IsExpertEnabled())
      {
       Alert("ÒÎÐÃÎÂËß ÑÎÂÅÒÍÈÊÀÌÈ ÇÀÏÐÅÙÅÍÀ !");
       Comment("ÒÎÐÃÎÂËß ÑÎÂÅÒÍÈÊÀÌÈ ÇÀÏÐÅÙÅÍÀ !");
       return(0);
      }
     else
      {
       Comment("Èíèöèàëèçàöèÿ ïðîøëà óñïåøíî. Æä¸ì ñâåæóþ êîòèðîâêó.");
       Print("Èíèöèàëèçàöèÿ ïðîøëà óñïåøíî. Æä¸ì ñâåæóþ êîòèðîâêó.");
      }
    }

   return(0);
  }

int deinit()
  {

   if (!IsTesting())
    {
     Print("Ñîâåòíèê äåèíèöèàëèçèðîâàí (âûêëþ÷åí).");
    }

   return(0);
  }

int start()
  {

   RefreshRates();

   if(!IsTradeAllowed())
    {
     Comment("Òîðãîâëÿ çàïðåùåíà â íàñòðîéêàõ òåðìèíàëà, ëèáî òîðãîâûé ïîòîê çàíÿò.");
     return(0);
    }

   Balans=AccountBalance();
   Equity=AccountEquity();

   if (Balans<=Equity)
    {
     FlowLoss = 0;
    }
   else
    {
     FlowLoss=NormalizeDouble(100-Equity*100/Balans,3);
    }

   Comment("RM - ",RM," %\n",
           "FlowLoss - ",FlowLoss," %\nwww.mt4.ucoz.com");

   double LotR;
   double SchBuyLots  = NormalizeDouble(SchBuyLots_b(),8);
   double SchSellLots = NormalizeDouble(SchSellLots_b(),8);
   LotR=SchBuyLots-SchSellLots;

   if (FlowLoss>RM && SchBuyLots!=SchSellLots)
    {
     for (int i=1; i<=1; i++)
      {
       if (LotR<0)
        {
         OPENORDER ("Buy");
        }
       if (LotR>0)
        {
         OPENORDER ("Sell");
        }
      }
    }

   return(0);
  }
//+------------------------------------------------------------------+

double SchBuyLots_b()
 {
  double SchBuyLots=0;
  int i;
  string SMB=Symbol();
  for (i=OrdersTotal()-1;i>=0;i--)
   {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
     {
      Alert ("Îøèáêà ",GetLastError()," âûáîðà îðäåðà!");
      Print ("Îøèáêà ",GetLastError()," âûáîðà îðäåðà!");
     }
    else
     {
      if(OrderSymbol()==SMB && OrderType()==OP_BUY)
       {
        SchBuyLots=SchBuyLots+OrderLots();
       }
     }
   }
  return(SchBuyLots);
 }

double SchSellLots_b()
 {
  double SchSellLots=0;
  int i;
  string SMB=Symbol();
  for (i=OrdersTotal()-1;i>=0;i--)
   {
    if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
     {
      Alert ("Îøèáêà ",GetLastError()," âûáîðà îðäåðà!");
      Print ("Îøèáêà ",GetLastError()," âûáîðà îðäåðà!");
     }
    else
     {
      if(OrderSymbol()==SMB && OrderType()==OP_SELL)
       {
        SchSellLots=SchSellLots+OrderLots();
       }
     }
   }
  return(SchSellLots);
 }
 
void OPENORDER(string ord)
 {
  int ticket = -1;

  double Lot;
  double SchBuyLots  = SchBuyLots_b();
  double SchSellLots = SchSellLots_b();

  Lot=MathAbs(SchBuyLots-SchSellLots);

  while (ticket<0)
   {
    if (ord=="Buy")  ticket=OrderSend(Symbol(),OP_BUY, Lot,Ask,3,0,0,"Óñòàíîâëåí çàìîê",0,0);
    if (ord=="Sell") ticket=OrderSend(Symbol(),OP_SELL,Lot,Bid,3,0,0,"Óñòàíîâëåí çàìîê",0,0);
    if (ticket==-1)
     {  
      Alert ("Îøèáêà ",GetLastError()," óñòàíîâêè ëîêîâîãî îðäåðà!");
      Print ("Îøèáêà ",GetLastError()," óñòàíîâêè ëîêîâîãî îðäåðà!");
     }
    else
     {   
      Alert ("Óñòàíîâëåí çàìîê ñîãëàñíî RM ",RM,"%\nwww.mt4.ucoz.com");
      Print ("Óñòàíîâëåí çàìîê ñîãëàñíî RM ",RM,"%");
      return (0);
     }
   }
  return;
 } 
//+------------------------------------------------------------------+

Comments