CExpertMoney

Lisiane  

Hello,

I need your help for translating this MM module in mql5 using CExpertMoney

double MoneyManagement()

{

   double lot = 0;  

   double Free =AccountInfoDouble(ACCOUNT_EQUITY);

   double LotVal =SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE);

   double Min_Lot =SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);

   double Max_Lot =SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);

   double Step =SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);

   double leverage = (double)AccountInfoInteger(ACCOUNT_LEVERAGE);

   double ticksize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);



   if(MM_type == fixed_lot)lot=FixedLotSize;



   if(MM_type == percent_risk_of_equity)      

   {

      lot =MathFloor((Free*MM_percent/100)/(StopLoss*LotVal)/Step)*Step;

      if(lot<Min_Lot) lot=Min_Lot;

      if(lot>Max_Lot) lot=Max_Lot;

   }  

   

   if(MM_type == percent_of_equity)

   {

      lot = NormalizeDouble(Free * MM_percent * 0.01  / leverage  /  (LotVal  /  ticksize), 2);

      if(lot<Min_Lot) lot=Min_Lot;

      if(lot>Max_Lot) lot=Max_Lot;

   }



   return(lot);

}

Thanks in advance 

Fernando Carreiro  
Lisiane: I need your help for translating this MM module in mql5 using CExpertMoney

We can only help, if you show your effort (your code) in attempting to do so. Otherwise, the suggestion will be to use the Freelance section. We are not going to code it for you.

Trading applications for MetaTrader 5 to order
Trading applications for MetaTrader 5 to order
  • 2022.10.06
  • www.mql5.com
The largest freelance service with MQL5 application developers
Fernando Carreiro  

That being said, there is already classes for fixed lot and fixed risk percentage built into the Standard Library, namely ...

Money Management classes

This section contains technical details of working with money and risk management classes and description of the relevant components of the MQL5 standard library.

The use of these classes will save time when creating (and testing) trading strategies.

MQL5 Standard Library (in terms of money and risk management classes) is placed in the terminal directory, in the Include\Expert\Money\ folder.

Class

Description

CMoneyFixedLot

This class implements money management algorithm based on trading with predefined fixed lot size.

CMoneyFixedMargin

This class implements money management algorithm based on trading with predefined fixed margin.

CMoneyFixedRisk

This class implements money management algorithm based on trading with predefined risk.

CMoneyNone

This class implements money management algorithm based on trading with minimal allowed lot size.

CMoneySizeOptimized

This class implements money management algorithm based on trading with variable lot size, depending on results of the previous deals.

Study the source code for these classes in the Standard Library for a better understanding of their functionality.
Fernando Carreiro  

I also suggest reading the following threads ...

Forum on trading, automated trading systems and testing trading strategies

SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE) sometimes zero

Fernando Carreiro, 2022.08.23 16:51

Instead of using the Tick Value directly, consider using the function OrderCalcProfit, because it will apply the correct profit calculation method depending on the CALC_MODE for that symbol.

Forum on trading, automated trading systems and testing trading strategies

SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE) sometimes zero

Fernando Carreiro, 2022.08.23 17:41

You can! These are the steps I take. I supply the function with a lot size equal to the “Max Lot Size” allowed for the symbol in question, then calculate the ratio needed to achieve the fractional risk that I wish to apply, to get the correct volume for the order. I then align that with the “Lot Step” and finally check it against both the maximum and minimum allowed lots for the symbol.

The reason I use the “maximum” lots instead of just “1.0” lots as a reference value is because there is no guarantee that the value of 1.0 is within the minimum and maximum values allowed. Given that using 1.0, or the maximum, gives equivalent results anyway (by using the ratio method), I choose to use the “max lots” as the reference point which also offers the most precision for the calculation.

Something like this ...

// This code will not compile. It is only a example reference

if( OrderCalcProfit( eOrderType, _Symbol, dbLotsMax, dbPriceOpen, dbPriceStopLoss, dbProfit ) )
{
   dbOrderLots = fmin( fmax( round( dbRiskMax * dbLotsMax / ( -dbProfit * dbLotsStep ) )
               * dbLotsStep, dbLotsMin ), dbLotsMax ); 
      
   // the rest of the code ...
};

Forum on trading, automated trading systems and testing trading strategies

How to calculate lots using multiplier according to number of opened orders?

Fernando Carreiro, 2017.09.01 21:57

Don't use NormalizeDouble(). Here is some guidance (code is untested, just serves as example):

// Variables for Symbol Volume Conditions
double
   dblLotsMinimum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MIN  ),
   dblLotsMaximum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MAX  ),
   dblLotsStep    = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_STEP );
   
// Variables for Geometric Progression
double
   dblGeoRatio = 2.8,
   dblGeoInit  = dblLotsMinimum;
   
// Calculate Next Geometric Element
double
   dblGeoNext  = dblGeoInit * pow( dblGeoRatio, intOrderCount + 1 );
   
// Adjust Volume for allowable conditions
double
   dblLotsNext = fmin( dblLotsMaximum,                                     // Prevent too greater volume
                   fmax( dblLotsMinimum,                                   // Prevent too smaller volume
                     round( dblGeoNext / dblLotsStep ) * dblLotsStep ) );  // Align to Step value

Forum on trading, automated trading systems and testing trading strategies

Volume Limit Reached - Validation for new Expert Advisor error

Fernando Carreiro, 2022.07.22 18:22

Your EA must be coded to read the broker's contract specifications, such volume limitations, and prevent that from happening.

SYMBOL_VOLUME_MIN

Minimal volume for a deal

double

SYMBOL_VOLUME_MAX

Maximal volume for a deal

double

SYMBOL_VOLUME_STEP

Minimal volume change step for deal execution

double

SYMBOL_VOLUME_LIMIT

Maximum allowed aggregate volume of an open position and pending orders in one direction (buy or sell) for the symbol. For example, with the limitation of 5 lots, you can have an open buy position with the volume of 5 lots and place a pending order Sell Limit with the volume of 5 lots. But in this case you cannot place a Buy Limit pending order (since the total volume in one direction will exceed the limitation) or place Sell Limit with the volume more than 5 lots.

dou

Forum on trading, automated trading systems and testing trading strategies

Complete formula for calculating forex pip value for XAUUSD with account funded in euros

Fernando Carreiro, 2022.08.29 15:43

One more thing that should be considered by the OP or those following this thread ...

After you determine your volume (lots) for your risk amount, you should then check that against the free margin, useing the OrderCalcMargin (MQL5) or order_calc_margin (Python), to verify that the amount of margin that will be required is available in your free margin.

In fact, make sure that the required margin plus the risk is not greater than your free margin and that it will not cause a margin call or stop out.

I personally set a margin % limit and reduce the lot size if required. For example, I set a maximum % margin of 5-10% and use the OrderCalcMargin to adjust the volume to reduce the volume should the margin be higher than my limit.

The reason I set it to 5-10% is because I have to account for multiple positions in the market if I am trading on multiple symbols at the same time. If I were to allow a maximum margin on my balance, then I would not have any free margin left to trade on other symbols.

Lisiane  
Fernando Carreiro #:

We can only help, if you show your effort (your code) in attempting to do so. Otherwise, the suggestion will be to use the Freelance section. We are not going to code it for you.

This code is issued by a freelance. I try to understand by mysef in order to optimize it and fix the issue in the "percent of equity" calculation.

Lisiane  
Fernando Carreiro #:

I also suggest reading the following threads ...

Here is my new code for this module, any comment ? is it correct ?

   // MM function
   
double MoneyManagement()
{
   double lot = 0,  
          MarginFor1Lot = 0,
          Free = AccountInfoDouble(ACCOUNT_MARGIN_FREE),
          LotVal = SymbolInfoDouble(Symbol(),SYMBOL_TRADE_TICK_VALUE),
          Min_Lot = SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN),
          Max_Lot = SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX),
          Step = SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP),
          leverage = (double)AccountInfoInteger(ACCOUNT_LEVERAGE),
          ticksize = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_SIZE);

   if(MM_type == fixed_lot)
   {
      lot=FixedLotSize;
      if(lot<Min_Lot) lot=Min_Lot;
      if(lot>Max_Lot) lot=Max_Lot;
   }

   if(MM_type == percent_risk_of_free_margin)      
   {
      lot =MathFloor((Free*MM_percent/100)/(StopLoss*LotVal/Step)*Step;		//StopLoss = hard StopLoss in points
      if(lot<Min_Lot) lot=Min_Lot;
      if(lot>Max_Lot) lot=Max_Lot;
   }  
   
   if(MM_type == percent_of_free_margin)
   {
      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_BUY)
      {
         if(OrderCalcMargin(ORDER_TYPE_BUY,_Symbol,1,SymbolInfoDouble(_Symbol,SYMBOL_ASK),MarginFor1Lot))
         if ( MarginFor1Lot != 0 && Step != 0)
         lot=MathFloor(Free*MM_percent/100/MarginFor1Lot/Step)*Step;
      }
      else
      if(PositionGetInteger(POSITION_TYPE) == POSITION_TYPE_SELL)
      {
         if(OrderCalcMargin(ORDER_TYPE_SELL,_Symbol,1,SymbolInfoDouble(_Symbol,SYMBOL_BID),MarginFor1Lot))
         if ( MarginFor1Lot != 0 && Step != 0)
         lot=MathFloor(Free*MM_percent/100/MarginFor1Lot/Step)*Step;
      }         
      if(lot<Min_Lot) lot=Min_Lot;
      if(lot>Max_Lot) lot=Max_Lot;
   }

   return(lot);
}
Reason: