vqdokiot_6l

Download script
Author: Yuriy Tokman
vqdokiot_6l

This script is designed to automatically calculate the optimal trade volume (the "lot size") for a financial position based on your account's current health and specific risk parameters. Instead of manually deciding how much money to put into a single trade, the tool evaluates your available capital against market costs and your desired safety margins to provide a recommended figure.

Core Logic Overview

The script functions by evaluating three primary factors to ensure that your trade size is both aggressive enough to be profitable but conservative enough to protect your balance:

1. Dynamic Risk Calculation If the "Fixed Fraction" setting is enabled, the tool looks at your total account balance and compares it against the margin requirements of the current market. It calculates how many units you can afford to trade while leaving a small buffer. This ensures that you aren't over-leveraging your account beyond what the broker requires for a position.

2. Stop-Loss Safety Check The logic also takes your intended "Stop-Loss" into account. It calculates the potential financial impact of hitting that stop-loss based on the current market's value. By comparing this risk to your total balance, the script identifies a maximum lot size that keeps your potential loss within the boundaries you have defined.

3. Balancing and Constraints The tool performs a "safety comparison" between the results of the two calculations mentioned above. It selects the smaller of the two values to ensure that neither the margin requirements nor your personal risk tolerance is exceeded. Finally, it applies a set of safety "floors" and "ceilings":

  • Minimum Floor: It ensures the trade size never drops below a standard starting amount (0.1).
  • Maximum Ceiling: If you have specified a maximum allowable lot size, the script will cap the result to that number, preventing any trades from becoming too large regardless of how much capital you have.

How it Communicates with You

When the script runs, it performs an instant check on your account. It then triggers a notification:

  • If it successfully calculates a valid trade volume based on your balance and settings, it will alert you with the specific number.
  • If the parameters provided are invalid (for example, if the risk settings are mathematically impossible or out of range), it will notify you that no calculation could be performed.
Miscellaneous
It issuies visual alerts to the screen
14 Views
0 Downloads
0 Favorites
vqdokiot_6l
//+------------------------------------------------------------------+
//|                                                       Êîýô_ô.mq4 |
//|                                                     Yuriy Tokman |
//|                                            yuriytokman@gmail.com |
//+------------------------------------------------------------------+
#property copyright "Yuriy Tokman"
#property link      "yuriytokman@gmail.com"

#property show_inputs

 #define STOPOUT 0.8

 extern bool fixedfrac=true;
 extern double f = 0.1 ;
 extern int stoploss = -100 ; // çàâèñèò îò ñèñòåìû
 extern double maxlot = 0 ;
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
//----

   double b = Lots ();
   if (Lots ()>0)Alert("Ëîòû: ",b);
   else Alert("Óïñ! Íåïîäñ÷èòàëî. ",b);   
//----
   return(0);
  }
//+------------------------------------------------------------------+

 double Lots ()
  {
   double a, b, lot;

   if (! fixedfrac) return (f) ;

   if (f < 0.01 || f > STOPOUT || stoploss >= 0)
   return (0.1) ;

   a = NormalizeDouble (AccountBalance () / MarketInfo (Symbol (), MODE_MARGINREQUIRED) - 0.1, 2) ;
   b = NormalizeDouble (AccountBalance () / ((-stoploss*MarketInfo(Symbol (), MODE_TICKVALUE)) / f), 2) ;
   if (a < b)
    lot = a ;
   else lot = b ;

   if (maxlot > 0)
     return (MathMin (maxlot, MathMax (lot, 0.1))) ;
   else
  return (MathMax (lot, 0.1)) ;
 }

Comments