Pipsing_CLOSE_on_DROP

Download script
Author: AP
Pipsing_CLOSE_on_DROP

This script is a specialized trading tool designed to streamline the management of active market positions by allowing for quick, manual actions directly on the price chart. Instead of navigating through complex menus, it enables a "drag and drop" style interaction to close out trades based on their proximity to the cursor.

Core Functionality

The tool works by detecting where a user "drops" their mouse on the trading chart. When you hold down the left mouse button and release it near a specific price point, the script automatically identifies nearby open positions and executes a closing action.

Operational Modes

The logic operates in two distinct behaviors depending on how it is configured:

  • Individual Target Mode: The script identifies the single trade closest to the exact price where you released your mouse and closes that specific position.
  • Regional Area Mode: The script looks at a defined "region" around your drop point (a specific number of pips above and below). It will then identify and close every single trade that falls within that designated area.

Key Logic Details

  • Smart Detection: The script continuously monitors all currently open trades. When a drop action is detected, it calculates the distance between the drop price and the prices of your active trades to determine which ones meet the criteria for closing.
  • Automated Execution: Once the relevant orders are identified, the script handles the heavy lifting of closing them. It distinguishes between different types of orders (such as active Buy/Sell positions versus pending Limit or Stop orders) and performs the appropriate action (closing vs. deleting).
  • Reliability Features: To ensure high success rates in volatile markets, the script includes a "retry" mechanism. If a trade fails to close on the first attempt due to a temporary connection hiccup or price shift, it will automatically refresh the data and try again several times in rapid succession.
  • Visual Feedback: While the script is designed to be fast and nearly instantaneous, it provides a brief red text notification on the chart to confirm which specific operation was performed (e.g., confirming whether it closed a Buy or a Sell).

User Experience

The logic is built for high-speed trading environments where efficiency is paramount. It essentially turns a manual menu-clicking process into a physical gesture, allowing a trader to manage their exposure by interacting directly with the visual elements of the chart.

Orders Execution
It Closes Orders by itself Checks for the total of open orders
9 Views
0 Downloads
0 Favorites
Pipsing_CLOSE_on_DROP
//+------------------------------------------------------------------+
//|                                        Pipsing_CLOSE_on_DROP.mq4 |
//|                                                    Aleksandr Pak |
//|                                                   ekr-ap@mail.ru |
//+------------------------------------------------------------------+
#property copyright "AP"
#property link      "ekr-ap@mail.ru"

// http: // forum.mql4.com/ru/11202/page5
// The script closes warrants of all types on which is thrown by a mousy.
// The instruction: we take a script, (it is clamped by his{its} left key of the mouse) 
// We bear{carry} on a line of the warrant, on an edge of an arrow{a pointer}, we release{we let off} the mouse.
// We (throw).
// Two modes: 0-to close the nearest warrant.
// 1-to close all warrants in area in the nearest region.
// If in the field of throwing = +/-Region there are no warrants the script will close nothing.
// If to cause a script in the usual way too most - any reaction.
// On erroneous actions the script is silent.
// Has two modes - a group sex and on one.
// Switching a mode and the task of parameters only through recompilation
// The red inscription with the name of operation is visible only at delays
// In norm it is not visible, since there is less than second
//...............................................................
//http://forum.mql4.com/ru/11202/page5
//Ñêðèïò çàêðûâàåò îðäåðà âñåõ òèïîâ íà êîòîðûå áðîøåí ìûøêîé.
// â ò.÷. ëèìèòíèêè
//Èíñòðóêöèÿ: áåðåì ñêðèïò, (çàæèìàåì åãî ëåâîé êëàâèøåé ìûøè) 
//íåñåì íà ëèíèþ îðäåðà, ïî íîñèêó ñòðåëêè, îòïóñêàåì ìûøü.
//( áðîñàåì.)
//äâà ðåæèìà: 0- çàêðûòü áëèæàéøèé îðäåð.
//            1- çàêðûòü âñå îðäåðà â îáëàñòè â áëèæàéøåì ðåãèîíå.
//åñëè â îáëàñòè áðîñàíèÿ =+/-Region íåò îðäåðîâ, òî ñêðèïò íè÷åãî íå çàêðîåò.
//åñëè âûçâàòü ñêðèïò îáû÷íûì ñïîñîáîì òîæå ñàìîå - íèêàêîé ðåàêöèè.
//íà îøèáî÷íûå äåéñòâèÿ ñêðèïò ìîë÷èò.
//èìååò  äâà ðåæèìà - ãðóïïîâóõà è ïî îäíìó.
// ïåðåêëþ÷åíèå ðåæèìà è çàäàíèå ïàðàìåòðîâ òîëüêî ÷åðåç ïåðåêîìïèëÿöèþ
// êðàñíàÿ íàäïèñü ñ íàçâàíèåì îïåðàöèè âèäíà òîëüêî ïðè çàäåðæêàõ
// â íîðìå íå âèäíà,ò.ê. íàìíîãî ìåíüøå ñåêóíäû

extern int ALL_on_region=1;//1= ñìåòàåò âñå îðäåðà  â ðåãèîíå. 0- òîëüêî ñàìûé áëèæíèé.
                           //1=ALL orders from region, 0=first order
extern int Region=10;      // ðàçìåð ðåãèîíà äåéñòâèÿ â ïóíêòàõ ââåðõ è âíèç ò.å. 2*Region
                           //Size of region for action
extern int Slippage=6;     //ñêîëüæåíèå äëÿ ÄÖ
                           //Slippage for Broker
extern int Repetitioons=6; //ïîïûòîê çàêðûòü
                           //Repetitioons of Close action by error

int count_order,minrop,rticket[1000],rop[1000];
double rprice[1000];


int deinit()
{

ObjectDelete("PipsWork");
}

int start()
  {int i,tck;
   double r=WindowPriceOnDropped( ) ;
    if(ObjectFind("PipsWork")<0)
              ObjectCreate("PipsWork",OBJ_TEXT,0,iTime(NULL,0,10),High[10]);
    if(r==0) return (0);
     order_monitor(r,Region*Point);
     if(ALL_on_region==1)
     {
     for(i=0;i<count_order;i++)
          {
            close_(rticket[i],rop[i]);          
          }
     } 
     else
     {
      tck=bestgoal(r);
      close_(tck,minrop);
     }
   
   
   return(0);
  }
//+------------------------------------------------------------------+
  int close_(int ticket, int cmd)
{
   bool   result;
   double price;
   int    error,i=0; 
   string tr;        
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) 
            { 
               switch(cmd)
               {
               case 0:tr="CLOSE OP_BY"; break;
               case 1:tr="CLOSE OP_SELL"; break;
               case 2:tr="DELETE OP_BUYL_IMIT"; break;
               case 3:tr="DELETE OP_SELL_LIMIT"; break;
               case 4:tr="DELETE OP_BUY_STOP"; break;
               case 5:tr="DELETE OP_SELL_STOP"; break;
               }
              ObjectSetText("PipsWork", tr, 14,"",Red);
              while(true)
               {i+=1;
                   RefreshRates();               
                  {
                  if(cmd==OP_BUY)      price=Bid; 
                  if(cmd==OP_SELL)      price=Ask;
                        
                  if(cmd==OP_BUY||cmd==OP_SELL) result=OrderClose(OrderTicket(),OrderLots(),price,Slippage,CLR_NONE);
                  if(cmd==OP_BUYSTOP||cmd==OP_SELLSTOP||cmd==OP_BUYLIMIT||cmd==OP_SELLLIMIT)
                  result=OrderDelete(OrderTicket());
                        error=GetLastError(); 
                        if(result==TRUE) error=0; 
                        if(error!=0) {Sleep(500); RefreshRates();} 
                  else break;
                  if(i>Repetitioons)break; 
                  if(!OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) break;
                  }
              }
            }
return (error);
}
///////////////////////////////////////////////
int bestgoal(double p)
{int i,ticket;
double min=9999999,op,x,y;
       for(i=0;i<count_order;i++)
       {
       x=MathAbs(p-rprice[i]);
       if(min>x){min=x; ticket=rticket[i]; minrop=rop[i];}
       }    
return (ticket);
}
//////
void order_monitor(double p, double dp)
{
int i,k,m,n,cmd,typs;
double t,x,op;
k=OrdersTotal();
   for(i=k;i>=0;i--)
   {   
   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) 
      {
        if(Symbol()==OrderSymbol())
        {
          op=OrderOpenPrice();
          x=op-p;
         rticket[n]=OrderTicket();
           if(MathAbs(x)<dp)
            {  
            cmd=OrderType();
             t=OrderCloseTime();
             //  if(t==0) 
               { 
                    t=OrderOpenTime();
                    { 
                     rticket[n]=OrderTicket();
                     rop[n]=cmd;
                     n++;
                    }
               } 
           }
        }  
     } 
   } 
   count_order=n; 
}

////////////////////////////

Comments