Code - Optimisation des Lots
Download scriptThis script is designed to automatically manage and adjust your trading position sizes (lot sizes) based on your account balance and your recent trading performance. Instead of using a fixed size for every trade, it dynamically calculates a "smart" volume to help manage risk.
Here is the step-by-step logic of how the tool operates:
1. Account Awareness
The script first checks your account type. If you specify that you are trading on a "Mini" account, it automatically defaults the starting volume to the minimum allowed size (0.01). For all other accounts, it begins by assessing your available funds.
2. Risk-Based Calculation
The core logic determines your trade size based on a percentage of your "Free Margin" (the cash currently available in your account). By using a specific risk percentage, the script ensures that the volume of your trade is always proportional to how much money you have available at that moment. This prevents over-leveraging when your balance is low.
3. Performance Analysis (The "Safety Net")
The script looks backward through your trading history to see how your most recent trades performed. It specifically counts how many consecutive losses you have incurred.
- If you are winning: The script maintains the calculated risk volume.
- If you are losing: If the script detects a streak of consecutive losses, it triggers a reduction mechanism.
4. Dynamic Lot Reduction
When a losing streak is detected, the script applies a "Decrease Factor." This reduces your next trade's lot size relative to the number of previous losses. Essentially, if the market is moving against you repeatedly, the script automatically "scales down" your position size to protect your remaining capital from significant drawdowns.
5. Minimum Protection
To ensure the script remains functional and keeps your trades active on the exchange, it includes a safety floor. Regardless of how many losses occur or how much the risk is reduced, the script will never drop the trade volume below a minimum threshold (0.1), ensuring that your positions remain valid and executable.
In summary: This tool acts as an automated risk manager that grows your position size based on your account wealth but automatically shrinks your exposure if you hit a losing streak, aiming to preserve your capital during difficult market conditions.
//+------------------------------------------------------------------+
//| Code - Optimisation des Lots.mq4 |
//| Copyright © 2007, GwadaTradeBoy |
//| gwadatradeboy@yahoo.fr |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, GwadaTradeBoy"
#property link "gwadatradeboy@yahoo.fr"
//---- Optimisation des Lots
extern bool AcountIsMini = false;
extern double Lots = 0.1;
extern double MaximumRisk = 0.02;
extern double DecreaseFactor = 3;
double lot;
int orders, losses, spread;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//---- Expert Advisor
spread = MarketInfo(Symbol(),MODE_SPREAD);
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Calculs preliminaires de l'expert |
//+------------------------------------------------------------------+
//********** Calcul de la taille optimale du lot **********//
double LotsOptimized()
{
if (AcountIsMini)
Lots = 0.01;
lot=Lots;
orders=HistoryTotal(); // Historique des ordres
losses=0; // Nombre de trade perdants consécutif
/* Selection de la taille du lot */
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000,1);
/* Calcul du nombre de perte consecutive */
if(DecreaseFactor>0)
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==False)
{
Print("Erreur dans l historique!");
break;
}
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL)
continue;
//----
if(OrderProfit()>0)
break;
if(OrderProfit()<0)
losses++;
}
if(losses>1)
lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
}
/* Retour de la taille du lot */
if(lot<0.1)
lot=0.1;
return(lot);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
Comments