4click Trade Opener_v1.1

Download script
Profit factor:
0.00

This script is a specialized trade management tool designed for the MetaTrader 5 platform. Its primary purpose is to automate the calculation and execution of trade positions based on specific risk management rules. Instead of simply opening a single trade, it focuses on intelligently distributing your risk across multiple positions.

Core Functionality

1. Risk Management Flexibility The script offers two distinct ways to manage your trading risk:

  • Percentage-based: You can define the maximum amount of your account equity you are willing to risk on a single trade setup (e.g., 2% of your total balance).
  • Fixed Amount: Alternatively, you can set a hard dollar amount for your maximum allowed loss.
  • The logic ensures these two settings do not conflict; you must choose one method or the other for the script to function correctly.

2. Smart Position Sizing & Splitting One of the most advanced features of this script is its ability to calculate how many separate "sub-positions" it can open based on your available risk:

  • It identifies your desired Stop Loss (SL) and Take Profit (TP) levels.
  • It calculates the potential loss for a single unit of trade volume at those price points.
  • It then determines how many positions of that volume you can afford to open without exceeding your total allowed risk (Risk Amount or Percentage).
  • If there is remaining risk "budget," it distributes the remaining amount across multiple orders, ensuring your exposure is balanced.

3. Automated Execution & Safety Checks The script performs several automated "health checks" before executing trades:

  • Permission Check: It verifies that your trading terminal actually has permission to perform automated trades.
  • Minimum Lot Verification: It ensures you have the required minimum position size allowed by your broker.
  • Single Entry Requirement: It enforces a rule that there must be exactly one base position of the minimum lot size open before it attempts to calculate and execute the additional "split" positions.
  • Trade Specifics: For every trade it opens, it automatically handles price normalization, applies the correct slippage tolerances, and ensures the Stop Loss and Take Profit levels are formatted correctly for your specific broker's requirements.

How it works in practice

When you use this tool, you define your "safety net" (how much money you are willing to lose). The script then looks at the current market price and your chosen exit points. It calculates the safest way to enter the market by breaking down your total risk into several smaller orders. For example, if you have enough risk for three trades but only enough exact "budget" for two, the script will automatically adjust and inform you that it can only execute two positions.

Additionally, when opening multiple positions in a single sequence, the script can slightly offset Take Profit targets to diversify your exit points, helping to manage trade flow more effectively.

Orders Execution
It automatically opens orders when conditions are reached
Miscellaneous
It issuies visual alerts to the screen
18 Views
0 Downloads
0 Favorites
4click Trade Opener_v1.1
ÿþ//+------------------------------------------------------------------+

//|                                 4click Trade Opener_v1.1.mq5     |

//|                                 Copyright © 2020, behzad.mvr     |

//|                                         Behzad.mvr@gmail.com     |

//+------------------------------------------------------------------+



//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

// Set riskPercent=0 if you want to use riskAmount

double riskPercent=2; //Risk Percent (%)

double riskAmount=0; //Risk Amount ($)

double minLot=0.01; //Minimum Lot Allowed by Broker to open Orders

int totalPositions=1; //Number of Positions to open

int Slipage=20; //Max order slipage(deviation) (points)





ulong magic=0;

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

int OnStart()

  {

   if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED))

     {

      Alert("Check if automated trading is allowed in the terminal settings.");

      return(0);

     }



   if(riskPercent!=0 && riskAmount!=0)

     {

      Alert("Set one of risk parameters = 0 to other parameter works.");

      return(0);

     }

   if(riskPercent==0 && riskAmount==0)

     {

      Alert("Don't set both of risk parameters = 0");

      return(0);

     }



   double sl=0,tp=0;

   double lots=0;



   double equity=AccountInfoDouble(ACCOUNT_EQUITY);

   double tick=SymbolInfoDouble(_Symbol,SYMBOL_TRADE_TICK_VALUE);

   double maxLoss=0;

   if(riskPercent!=0)

      maxLoss=(riskPercent/100)*equity;

   else

      if(riskAmount!=0)

         maxLoss=riskAmount;

   double usedLoss=0;

   double remainedLoss=0;



   ENUM_POSITION_TYPE orderType=-1;



   MqlTradeRequest request;

   MqlTradeResult  result;

   int total=PositionsTotal();



   int minLotCnt=0;

   for(int i=0; i<total; i++)

     {

      //--- parameters of the order

      ulong  position_ticket=PositionGetTicket(i);// ticket of the position

      string position_symbol=PositionGetString(POSITION_SYMBOL); // symbol

      double volume=PositionGetDouble(POSITION_VOLUME);    // volume of the position

      if(position_symbol!=_Symbol || volume!=minLot)

         continue;

      minLotCnt++;

      int    digits=(int)SymbolInfoInteger(position_symbol,SYMBOL_DIGITS); // number of decimal places

      magic=PositionGetInteger(POSITION_MAGIC); // MagicNumber of the position

      sl=PositionGetDouble(POSITION_SL);  // Stop Loss of the position

      if(sl==0)

        {

         Alert("Please Set the StopLoss Line on Chart.");

         return(0);

        }

      tp=PositionGetDouble(POSITION_TP);  // Take Profit of the position

      orderType=(ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE);  // type of the position

     }

   if(minLotCnt==0)

     {

      Alert("First there must be a "+DoubleToString(minLot,2)+" lot open position for "+_Symbol);

      return(0);

     }

   if(minLotCnt>1)

     {

      Alert("There must be only one "+DoubleToString(minLot,2)+" lot open position for "+_Symbol);

      return(0);

     }

//----



   if(orderType==POSITION_TYPE_BUY)

     {

      int lotDivider=2;

      double ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);

      usedLoss=minLot*(ask-sl)*tick/_Point;

      remainedLoss=maxLoss-usedLoss;

      for(int k=1; k<=totalPositions; k++)

        {

         if(remainedLoss<k*usedLoss)

           {

            Alert("There is only enough risked eguity to split order into "+IntegerToString(k-1)+" position.");

            totalPositions=k-1;

            break;

           }

        }

      if(totalPositions==0)

         return(0);

      lots=NormalizeDouble((remainedLoss/((ask-sl)*tick/_Point)/totalPositions),2);

      for(int i=0; i<totalPositions; i++)

        {

         sendOrder(ORDER_TYPE_BUY,sl,tp,lots,"");

         tp=tp+50*_Point;

        }

     }

//----

   if(orderType==POSITION_TYPE_SELL)

     {

      int lotDivider=2;

      bool secondTrade=true;

      double bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);

      usedLoss=minLot*(sl-bid)*tick/_Point;

      remainedLoss=maxLoss-usedLoss;

      for(int k=1; k<=totalPositions; k++)

        {

         if(remainedLoss<k*usedLoss)

           {

            Alert("There is only enough risked eguity to split order into "+IntegerToString(k-1)+" position.");

            totalPositions=k-1;

            break;

           }

        }

      if(totalPositions==0)

         return(0);

      lots=NormalizeDouble((remainedLoss/((sl-bid)*tick/_Point)/totalPositions),2);

      for(int i=0; i<totalPositions; i++)

        {

         sendOrder(ORDER_TYPE_SELL,sl,tp,lots,"");

         tp=tp-50*_Point;

        }

     }

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

   return(0);

  }

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

uint sendOrder(ENUM_ORDER_TYPE orderType,double SL,double TP,double Volume,string comment)

  {

//--- prepare a request

   MqlTradeRequest request= {};

   request.action=TRADE_ACTION_DEAL;

   request.type=orderType;

   request.magic=magic;

   request.symbol=_Symbol;

   request.volume=Volume;

   request.sl=NormalizeDouble(SL,_Digits);

   request.tp=NormalizeDouble(TP,_Digits);

   request.deviation=Slipage;

   request.comment=comment;

// request.type_filling=GetFilling(request.symbol);

   if(request.type==ORDER_TYPE_SELL)

      request.price=SymbolInfoDouble(Symbol(),SYMBOL_BID);

   else

      request.price=SymbolInfoDouble(Symbol(),SYMBOL_ASK);

//--- send a trade request

   MqlTradeResult result= {0};

   bool success=OrderSend(request,result);

   if(!success)

     {

      Alert(_Symbol," Error in order send: ",orderType," ",result.retcode," lots= ",Volume);

     }

//--- return code of the trade server reply

   return result.retcode;

  }

Comments