Need moneymanagement LOT size formula based on SL and Account Risk! - page 4

 
darelco:

... in this part of code is a problem with new compilation (error ---> 'MarketInfo' - illegal switch expression type) perhaps it was all OK until the update to MT4 build 600+ ... but since then it doesn't work any longer.

 So, could you please post some newer version ... if  of course you're still around.


I think if you change

                switch ( MarketInfo( strSymbol, MODE_DIGITS ) )

 to

                int dig=MarketInfo( strSymbol, MODE_DIGITS ) ;
                switch ( dig )

 It will compile ok

 
darelco:

... in this part of code is a problem with new compilation (error ---> 'MarketInfo' - illegal switch expression type) perhaps it was all OK until the update to MT4 build 600+ ... but since then it doesn't work any longer.

 So, could you please post some newer version ... if  of course you're still around.


   switch((int)MarketInfo(strSymbol,MODE_DIGITS))
 

https://book.mql4.com/operators/switch

"The values of Expression and of Parameters can only be the values of int type. The Expression can be a constant, a variable, a function call, or an expression. Each variation 'case' can be marked by an integer constant, a character constant, or a constant expression. A constant expression cannot include variables or function calls."

 
angevoyageur:
   switch((int)MarketInfo(strSymbol,MODE_DIGITS))

Once again, you come up with a simpler and better solution.
 
GumRai:
Once again, you come up with a simpler and better solution.
We all learn from each other.
 
                int dig=MarketInfo( strSymbol, MODE_DIGITS ) ;
                switch ( dig )
or
   switch((int)MarketInfo(strSymbol,MODE_DIGITS))
or the object style (works except for pointer casts)
   switch( int(MarketInfo(strSymbol,MODE_DIGITS)) )
 

In my different EA, it's write like this :

extern double      Risk_Percent                   = 3;
extern int         StopLoss                       = 50;

//+------------------------------------------------------------------+
  {
   double lot = MathCeil(AccountFreeMargin() * Risk_Percent / 1000) / 100;
   if(lot<MarketInfo(Symbol(),MODE_MINLOT))lot=MarketInfo(Symbol(),MODE_MINLOT);
   if(lot>MarketInfo(Symbol(),MODE_MAXLOT))lot=MarketInfo(Symbol(),MODE_MAXLOT);
   return (MathMin(NormalizeDouble(lot,PipMultiplier),MaxLotSize));
  }
//+------------------------------------------------------------------+
   if(_Digits==5 || _Digits==3)PipMultiplier=10;
   else PipMultiplier=1;
   slippage=Slippage*PipMultiplier;
   if(_Digits<4)
     {
      point=0.01;
     }
   else
     {
      point=0.0001;
     }
   return(0);
//+------------------------------------------------------------------+

 
Sebastien Pelle: In my different EA, it's write like this :

   double lot = MathCeil(AccountFreeMargin() * Risk_Percent / 1000) /


  1. Free margin has nothing to do with your risk. It is your brokers stop loss (50% of your account.)
  2. You should have read the entire thread and not posted. Summarized:
    • You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    • Account Balance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the SPREAD, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
    • Do NOT use TickValue by itself - DeltaPerLot
    • You must normalize lots properly and check against min and max.
    • You must also check FreeMargin to avoid stop out
Reason: