Lot Calculation with Stoploss mql4

 

Hello 

I'm looking for correct lot calculation in mql4 that solves my test as below with simple and clear explanation.


We have BUY Stoploss 400*Point and Takeprofit 400*Point
We Have  SELL Stoploss 800*Point and Takeprofit 800*Point

Risk amount will be set to 1000$ and account balance 100000$

Result expected will be profits and losses that are about 1000$.

What I'm trying to achieve is same risk no matter what stoploss is set.


Additional: having minimum lot and stoploss there is a minimum risk amount that should be checked

 
Your topic has been moved to the section: MQL4 and MetaTrader 4
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 

Your question has been asked countless times and answered even more times than that.

Please search before you post.

 

Forum on trading, automated trading systems and testing trading strategies

How to calculate take profit from currency

Fernando Carreiro, 2022.09.05 17:00

These are all the same equation written in different ways ...

[Volume]      = [Money Value] * [Tick Size] / ( [Tick Value] * [Stop Size] )
[Stop Size]   = [Money Value] * [Tick Size] / ( [Tick Value] * [Volume]    )
[Money Value] = [Volume]      * [Stop Size] * [Tick Value]   / [Tick Size]

[Volume] in lots
[Stop Size] in quote price change
[Money Value] in account currency
 

Forum on trading, automated trading systems and testing trading strategies

How to calculate proper lot size ?

Fernando Carreiro, 2022.02.11 19:28

This is untested, uncompiled code, only to serve as a guide for you to further study and implement depending on your own requirements ...

// Calculate Max Lot Size based on Maximum Risk
double dblLotsRisk( double dbStopLoss, double dbRiskRatio )
{
   double
      dbLotsMinimum  = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MIN       ),
      dbLotsMaximum  = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MAX       ),
      dbLotsStep     = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_STEP      ),
      dbTickSize     = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE  ),
      dbTickValue    = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_VALUE ),
      dbValueAccount = fmin( fmin( 
                       AccountInfoDouble( ACCOUNT_EQUITY      )  , 
                       AccountInfoDouble( ACCOUNT_BALANCE     ) ),
                       AccountInfoDouble( ACCOUNT_MARGIN_FREE ) ),
      dbValueRisk    = dbValueAccount * dbRiskRatio,
      dbLossOrder    = dbStopLoss * dbTickValue / dbTickSize,
      dbCalcLot      = fmin(  dbLotsMaximum,                  // Prevent too greater volume
                       fmax(  dbLotsMinimum,                  // Prevent too smaller volume
                       round( dbValueRisk / dbLossOrder       // Calculate stop risk
                       / dbLotsStep ) * dbLotsStep ) );       // Align to step value

   return ( dbCalcLot );
};
 

Was expecting answer that is fully fulfilling my expectations and by saying that i was expecting someone to actually try such test.

From what i can see here that untested code should:

 dbValueRisk    = dbValueAccount * dbRiskRatio,

 + this changed to dbValueRisk = 1000; should give me 1000$ per trade or 1000$ loss but results are around 20, 19.99.


Not sure what is wrong but that would be it. 

 
Damian Mateusz Wojtowicz #: Was expecting answer that is fully fulfilling my expectations and by saying that i was expecting someone to actually try such test.

You only looked at my posts, which gave you the calculations and example code, yet you did not put any more effort into researching it or searching the forum for more information.

Don't expect others to fulfil your expectations if you yourself are not willing to put in the effort.

 
Answer would be better I was not trying to post before checking but i wont waste more time thanks anyway.
 

Answer 1 (Could be bit slow but simple)

We will calculate lot with stoploss value: lot*point value*stoploss

   double risk = RiskBalance; // YOUR RISK %
   double tickSize = MarketInfo(Symbol(),MODE_TICKSIZE);
   double point = MarketInfo(Symbol(),MODE_POINT);
   double tickValue = MarketInfo(Symbol(),MODE_TICKVALUE);
   double minLot = MarketInfo(Symbol(),MODE_MINLOT);
   double maxLot = MarketInfo(Symbol(),MODE_MAXLOT);
   double ticksPerPoint = tickSize/point;
   double pointValue = tickValue/ticksPerPoint;
   double moneyRisk = risk*AccountBalance()/100;
   double lot = minLot;
   
   while(true)
   {
      if(lot*pointValue*stoplossIn<moneyRisk) // YOUR STOPLOSS
      {
         lot = lot+minLot;
      }
      else
      {
         break;
      }
   }
   
   if(lot<minLot) lot=minLot;
   if(lot>maxLot) lot=maxLot;


Answer 2 

Money / stoploss give us value per point. Minimum lot * point value gives us point value of minimum lot. Value per point / Min lot value gives us multiplier which means we know how many minimum lots we have to use.

   double risk = RiskBalance; // YOUR RISK %
   double tickSize = MarketInfo(Symbol(),MODE_TICKSIZE);
   double point = MarketInfo(Symbol(),MODE_POINT);
   double tickValue = MarketInfo(Symbol(),MODE_TICKVALUE);
   double minLot = MarketInfo(Symbol(),MODE_MINLOT);
   double maxLot = MarketInfo(Symbol(),MODE_MAXLOT);
   double ticksPerPoint = tickSize/point;
   double pointValue = tickValue/ticksPerPoint;
   double moneyRisk = risk*AccountBalance()/100;
   double lot = minLot;
   
   moneyRisk = riskBalance*AccountBalance()/100;
   multip = (moneyRisk/stoplossIn)/(minLot*pointValue); // YOUR STOPLOSS
   lot = multip*minLot;
   
   if(lot<minLot) lot=minLot;
   if(lot>maxLot) lot=maxLot;
   
   return lot;  


Additional:

minimum lot * point value * stoploss

if(minLot*pointValue*stoplossIn>moneyRisk) return 0;


Hope this helps bye