TakeProfitZero

Download script
TakeProfitZero

This script is designed to help traders manage multiple open positions by calculating a single "Breakeven" point for all active trades of the same type (all Buy orders or all Sell orders) on a specific currency pair. Instead of managing individual profit targets or loss limits for every single trade, this tool allows you to move them to a unified level where your net profit/loss across those positions becomes zero.

How the Logic Works

1. Data Gathering The script first scans all currently open trades in your account. It separates them into two categories: Buy orders and Sell orders. For each category, it calculates two key pieces of information:

  • The total volume (lot size) of all active positions.
  • The total current financial profit or loss, including commissions and overnight swap costs.

2. Breakeven Calculation Once the script knows the total profit and total volume for your positions, it performs a mathematical calculation to determine the exact price point where the net profit would be zero.

  • For Buy positions, it calculates the price level based on the current market price and your total gains/losses.
  • For Sell positions, it performs a similar calculation relative to the selling price.

3. Safety and Validation Checks Before suggesting any changes, the script runs several checks to ensure the trade remains valid:

  • Existence Check: It verifies if there are actually any open trades. If no trades exist for a specific direction, it notifies you that no modification can be made.
  • Distance Check: It checks the "Stop Level" (the minimum distance required by your broker). If the calculated breakeven price is too close to the current market price to be legally placed, the script will inform you that the modification is impossible due to broker restrictions.

4. User Interaction and Execution Instead of automatically moving your prices, the script acts as a consultant:

  • It displays a popup message showing you the exact calculated breakeven price for your trades.
  • Depending on whether that price is currently above or below the market price, it determines if moving to that level would set your "Take Profit" (to lock in a win) or your "Stop Loss" (to protect against further loss).
  • It asks for your permission via a "Yes/No" prompt. If you agree, the script automatically updates all relevant orders simultaneously to the new unified price.

Summary of Benefits

This tool simplifies risk management by allowing a trader to quickly transition a basket of losing or fluctuating trades into a "risk-free" state. By moving all positions to the breakeven point in one click, you ensure that if the market moves against you further, the loss will be capped at zero, while still leaving room for the trade to potentially turn into a profit.

Orders Execution
Checks for the total of open ordersIt can change open orders parameters, due to possible stepping strategy
Miscellaneous
It opens Message Boxes to the user
5 Views
0 Downloads
0 Favorites
TakeProfitZero
// TakeProfitZero.mq4
// Ñêðèïò âûñòàâëÿþùèé TP èëè SL â áåçóáûòîê (ñðåäíèé óðîâåíü äëÿ âñåõ îðäåðîâ Sell èëè Buy).
#include <WinUser32.mqh>

void Modify(int type, double level, bool tp)
{
 double stop;
 double take;
 
 for (int i=0; i<OrdersTotal(); i++)
 {
  if (!OrderSelect(i,SELECT_BY_POS)) break;
  if (OrderSymbol()!=Symbol()) continue;
  if (OrderType()==type)
  {
   if (tp)
   {
    stop=OrderStopLoss();
    take=level;
   }
   else
   {
    stop=level;
    take=OrderTakeProfit();
   }
   if (!OrderModify(OrderTicket(),OrderOpenPrice(),stop,take,0,CLR_NONE)) Print("LastError = ", GetLastError());
  }  
 }
}

void start()
{
 double BuyLots=0;
 double SellLots=0;
 double BuyProfit=0;
 double SellProfit=0;
 for (int i=0; i<OrdersTotal(); i++)
 {
  if (!OrderSelect(i,SELECT_BY_POS)) break;
  if (OrderSymbol()!=Symbol()) continue;
  if (OrderType()==OP_BUY)
  {
   BuyLots=BuyLots+OrderLots();
   BuyProfit=BuyProfit+OrderProfit()+OrderCommission()+OrderSwap();
  }  
  if (OrderType()==OP_SELL)
  {
   SellLots=SellLots+OrderLots();
   SellProfit=SellProfit+OrderProfit()+OrderCommission()+OrderSwap();
  } 
 }
 double LotSize=MarketInfo(Symbol(),MODE_LOTSIZE);
 if (BuyLots>0) double BuyLevel=NormalizeDouble(Bid-(BuyProfit/(LotSize*BuyLots)),Digits); else BuyLevel=0;
 if (SellLots>0) double SellLevel=NormalizeDouble(Bid+(SellProfit/(LotSize*SellLots)),Digits); else SellLevel=0;
 double StopLevel=MarketInfo(Symbol(),MODE_STOPLEVEL)*Point;
 if (BuyLevel==0) MessageBox("Íåò îðäåðîâ íà ïîêóïêó     ","Ìîäèôèöèêàöèÿ íåâîçìîæíà",MB_OK|MB_ICONINFORMATION);
 else
 {
  if (BuyLevel<(Bid+StopLevel) && BuyLevel>(Bid-StopLevel)) MessageBox("Öåíà ñëèøêîì áëèçêà ê óðîâíþ áåçóáûòêà     ","Ìîäèôèöèêàöèÿ íåâîçìîæíà",MB_OK|MB_ICONINFORMATION);
  else 
  {
   string msg="Áåçóáûòîê äëÿ îðäåðîâ íà ïîêóïêó = "+DoubleToStr(BuyLevel,Digits)+"     ";
   if (BuyLevel>Bid)
   {
    int res=MessageBox(msg,"Ìîäèôèöèðîâàòü TakeProfit â áåçóáûòîê?",MB_YESNO|MB_ICONQUESTION);
    if (res==6) Modify(OP_BUY,BuyLevel,true);
   }
   else
   {
    res=MessageBox(msg,"Ìîäèôèöèðîâàòü StopLoss â áåçóáûòîê?",MB_YESNO|MB_ICONQUESTION);
    if (res==6) Modify(OP_BUY,BuyLevel,false);
   } 
  } 
 } 
 if (SellLevel==0) MessageBox("Íåò îðäåðîâ íà ïðîäàæó     ","Ìîäèôèöèêàöèÿ íåâîçìîæíà",MB_OK|MB_ICONINFORMATION);
 else
 {
  if (SellLevel<(Bid+StopLevel) && SellLevel>(Bid-StopLevel)) MessageBox("Öåíà ñëèøêîì áëèçêà ê óðîâíþ áåçóáûòêà     ","Ìîäèôèöèêàöèÿ íåâîçìîæíà",MB_OK|MB_ICONINFORMATION);
  else 
  {
   msg="Áåçóáûòîê äëÿ îðäåðîâ íà ïðîäàæó = "+DoubleToStr(SellLevel,Digits)+"     ";
   if (SellLevel<Bid)
   {
    res=MessageBox(msg,"Ìîäèôèöèðîâàòü TakeProfit â áåçóáûòîê?",MB_YESNO|MB_ICONQUESTION);
    if (res==6) Modify(OP_SELL,SellLevel,true);
   }
   else
   {
    res=MessageBox(msg,"Ìîäèôèöèðîâàòü StopLoss â áåçóáûòîê?",MB_YESNO|MB_ICONQUESTION);
    if (res==6) Modify(OP_SELL,SellLevel,false);
   }
  } 
 }
}

Comments