Close_Percent_By_Line

Download script
Author: Copyright � 2013, ������� ��������

This script is designed to automate the partial closing of profitable trades based on a specific price target. It acts as a visual and functional tool for traders who want to secure profits automatically once the market reaches a certain point.

How it Works:

1. Initialization and Visual Setup When you run the script, it scans your open trades to identify active positions on the current currency pair. It then calculates a specific price target (the "Take Profit" line) based on your desired distance from the current market price.

To make this visible to the user, the script draws a horizontal line and a text label on your trading chart. This allows you to see exactly at what price point the automation will trigger.

2. Filtering System The script is intelligent enough to filter which trades it should manage:

  • Symbol Matching: It only interacts with trades on the specific currency pair you are currently viewing.
  • Magic Number Filter: You can choose to apply the script to all trades, only your manual trades, or only specific automated trades by using a "Magic Number."

3. The Execution Logic The script constantly monitors the live market prices. It follows these specific rules:

  • For Buy Positions: If the current market price reaches or exceeds the target line and the trade is currently in profit, the script will execute a partial close.
  • For Sell Positions: If the current market price falls to or below the target line and the trade is currently in profit, the script will execute a partial close.

4. Partial Profit Taking Instead of closing your entire position at once, you can define a specific percentage (e.g., 30%). When the price hits the target line, the script will automatically sell off that percentage of your volume. If you set this value to 100%, the script will close the entire trade completely.

5. Completion Once the target is reached and the trades are processed, the script removes the visual line and text from your chart and finishes its operation, notifying you that it has completed its task.

Orders Execution
Checks for the total of open ordersIt Closes Orders by itself
Miscellaneous
It opens Message Boxes to the user
10 Views
0 Downloads
0 Favorites
Close_Percent_By_Line
//--------------------------------------------------------------------
#property copyright "Copyright © 2013, Õëûñòîâ Âëàäèìèð"
#property link      "cmillion@narod.ru"
#property show_inputs
#include <WinUser32.mqh>

/*
Ðàáîòà ñêðèïòà.
Ïðè ñòàðòå ñêðèïò âûñòàâëÿåò ëèíèþ, íà ðàññòîÿíèè TakeProfit îò òåêóùåé öåíû.
Ïðè äîñòèæåíèè öåíîé ýòîé ãîðèçîíòàëüíîé ëèíèè çàêðûâàþòñÿ ïðèáûëüíûå îðäåðà.
Ñêðèïò çàêðûâàåò îò êàæäîãî îðäåðà çàäàííûé ïðîöåíò, ïðè çíà÷åíèè PercentClose = 100% çàêðûâàåò îðäåðà ïîëíîñòüþ.
Äàëåå ñêðèïò çàêàí÷èâàåò ñâîþ ðàáîòó
*/
//--------------------------------------------------------------------
extern int     TakeProfit     = 30;     //ïðèáûëü çàêðûòèÿ îðäåðà â ïóíêòàõ, íà òàêîå ðàññòîÿíèå îò òåêóùåé öåíû ñòàâèì ëèíèþ Close
extern double  PercentClose   = 30;     //ïðîöåíò çàêðûòèÿ îðäåðà
extern int     Magic          = -1;     //åñëè -1 âñå, åñëè 0 ðó÷íûå, åñëè åñòü íîìåð òî ïî êîíêðåòíîìó ìàãèêó.
//--------------------------------------------------------------------
int start()
{
   double PriceLine,OP;
   int OMN,tip;
   string Obj = "Close Percent By Line";
   if (ObjectFind(Obj)==-1) 
   {
      for (int i=0; i<OrdersTotal(); i++)
      {    
         if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         { 
            OMN = OrderMagicNumber();
            if (OrderSymbol()==Symbol() && (OMN==Magic || Magic==-1))
            { 
               tip = OrderType(); 
               if (tip==OP_BUY)             
               {
                  PriceLine = NormalizeDouble(Bid+TakeProfit*Point,Digits);
               }                                         
               if (tip==OP_SELL)        
               {
                  PriceLine = NormalizeDouble(Ask-TakeProfit*Point,Digits);
               } 
            }
         }
      }
      drawline(Obj,Gray,PriceLine);
      drawtext(Obj,PriceLine);
   }
   //---
   while(!IsStopped())
   {
      RefreshRates();
      if (!IsTradeAllowed()) 
      {
         Sleep(1000);
         continue;
      }
      Comment("Ñêðèïò ",Obj," ",TimeToStr(TimeCurrent(),TIME_SECONDS));
      if (ObjectFind(Obj)==-1) 
      {
         int ret=MessageBox("Çàêîí÷èòü ðàáîòó ñêðèïòà","Question", MB_YESNO|MB_ICONQUESTION);
         if (ret==IDYES) break;
         drawline(Obj,Gray,PriceLine);
         drawtext(Obj,PriceLine);
      }
      else 
      {
         if (PriceLine != ObjectGet(Obj,OBJPROP_PRICE1))
         {
            PriceLine = ObjectGet(Obj,OBJPROP_PRICE1);
            drawtext(Obj,PriceLine);
         }
      }
      //---
      for (i=0; i<OrdersTotal(); i++)
      {    
         if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         { 
            OMN = OrderMagicNumber();
            if (OrderSymbol()==Symbol() && (OMN==Magic || Magic==-1))
            { 
               tip = OrderType(); 
               OP = OrderProfit();
               if (tip==OP_BUY && OP>0)             
               {
                  if (PriceLine!=0)
                  {
                     if (Bid>=PriceLine) 
                        if (OrderClose(OrderTicket(),NormalizeDouble(OrderLots()*PercentClose/100,2), NormalizeDouble(Bid,Digits), 150, Blue))
                           {ObjectDelete(Obj);ObjectDelete(StringConcatenate(Obj,"n"));}
                  }
               }                                         
               if (tip==OP_SELL && OP>0)        
               {
                  if (PriceLine!=0)
                  {
                     if (Ask<=PriceLine) 
                        if (OrderClose(OrderTicket(), NormalizeDouble(OrderLots()*PercentClose/100,2), NormalizeDouble(Ask,Digits), 150, Red))
                           {ObjectDelete(Obj);ObjectDelete(StringConcatenate(Obj,"n"));}
                  }
               } 
            }
         }
      }
      Sleep(500);
   }
   Comment("Ñêðèïò ",Obj," çàêîí÷èë ñâîþ ðàáîòó");
   return(0);
}
//--------------------------------------------------------------------
int drawline(string NameL, color col, double Y1)
{
   ObjectCreate(NameL, OBJ_HLINE, 0,0,Y1,0,0);
   ObjectSet   (NameL, OBJPROP_COLOR, col);
   ObjectSet   (NameL, OBJPROP_STYLE, STYLE_SOLID);
   ObjectSet   (NameL, OBJPROP_WIDTH, 1);
   ObjectSet   (NameL, OBJPROP_BACK,  false);
   ObjectSet   (NameL, OBJPROP_RAY,   false);
   return;
}
//--------------------------------------------------------------------
int drawtext(string NameL, double Y1)
{
   string NameLine=StringConcatenate(NameL,"n");
   ObjectDelete (NameLine);
   ObjectCreate (NameLine, OBJ_TEXT,0,Time[WindowFirstVisibleBar()-WindowFirstVisibleBar()/4],Y1,0,0,0,0);
   ObjectSetText(NameLine, NameL ,8,"Arial");
   ObjectSet    (NameLine, OBJPROP_COLOR, ObjectGet(NameL,OBJPROP_COLOR));
}
//--------------------------------------------------------------------

Comments