Reply

MONEY MANAGEMENT CALCULATION ERROR

13 replies

loonet

Subscriber, bbp_participant, community, customer, sq-ultimate, 15 replies.

Visit profile

9 years ago #113539

Hello Mark I’â„¢m Riccardo from Sapienza Finanziaria
I would notice you that in my opinion EAW has a bugs in the calculation of the Money Management Risk with Fixed% of account equity.
On the trading system that I’m planning I fixed a risk per trade of 1%
Try looking for example what happens in the trade # 79 which I highlight in the attached image.

traderesult.jpg

 
Before the #79 trade in my account I have a balance of 21680 USD, and I set a Risk per Trade 1%.
It means I could lose about 217 USD if I get the SL.
The trade # 79 is a BUY operation with Entry price at 1.3703 and SL at 1.3640.
The operation goes to market with 3:40 lots, so each pip is 34 USD .
The transaction closes with a 1241 pips loss (= 63 x 34).
Actually to respect a Risk per Trade of 1%, the trading system should have open that trade with  0.34 lots and not 3.40 lots.

 

I’m a MQL programmer so I tried to correct the  EAW  sqMMFixedRisk  function replacing it with a my own function that I report here below in order to help you to solve the bug:

    //------ sqMMFixedRisk fixed by Riccardo
     
    double sqMMFixedRisk (int orderMagicNumber, int orderType)
    {
    double LotSize=0;
    double _riskInPercent = RiskPercent;
    double _maximumLots = 5.0;
    double slSize = sqMMGetOrderStopLossDistance(orderMagicNumber, orderType) * gPointPow;
     
     
    double riskPerTrade=AccountBalance() *(_riskInPercent/100);
    if(slSize <= 0) {
    Verbose("Computing Money Management - Incorrect StopLossPips size, it must be above 0");
    return(0);
    }
    Verbose("Risk per trade: ", riskPerTrade);
     
    Verbose("Computing Money Management - SL: ", slSize, ", Account Balance: ", AccountBalance(),", Tick value: ", MarketInfo(Symbol(), MODE_TICKVALUE),", Point: ", MarketInfo(Symbol(), MODE_POINT), ", Lot size: ", MarketInfo(Symbol(), MODE_LOTSIZE),", Tick size: ", MarketInfo(Symbol(), MODE_TICKSIZE));
     
    double TickValue=MarketInfo(Symbol(),MODE_TICKVALUE);
     
    if(Digits == 1 || Digits == 3 || Digits == 5) {
    slSize = 10 * slSize; // conversion from pips to points
    }
     
    if(slSize>0 && TickValue>0)
    {
    LotSize = MarketInfo(Symbol(),MODE_TICKSIZE) * riskPerTrade / (slSize * TickValue * MarketInfo(Symbol(),MODE_POINT) );
     
    int err=GetLastError();
    if(err==4013)
    { //ERR_ZERO_DIVIDE
    Verbose("Err: division by zero: StopLoss:",slSize," TickValue:",TickValue," LotSize:",LotSize);
    return(-1);
    }
    }
     
    //--- MAXLOT and MINLOT management
    double Smallest_Lot = MarketInfo(Symbol(), MODE_MINLOT);
    double Largest_Lot = MarketInfo(Symbol(), MODE_MAXLOT);
     
    if (LotSize < Smallest_Lot) LotSize = Smallest_Lot;
    if (LotSize > Largest_Lot) LotSize = Largest_Lot;
     
    if(LotSize > _maximumLots) {
    LotSize = _maximumLots;
    }
    //--------------------------------------------
     
     
     
    //--- LotSize rounded regarding Broker LOTSTEP
    if(MarketInfo(Symbol(),MODE_LOTSTEP)==1)
    {
    LotSize=NormalizeDouble(LotSize,0);
    }
    if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.1)
    {
    LotSize=NormalizeDouble(LotSize,1);
    }
    if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.01)
    {
    LotSize=NormalizeDouble(LotSize,2);
    }
    if(MarketInfo(Symbol(),MODE_LOTSTEP)==0.001)
    {
    LotSize=NormalizeDouble(LotSize,3);
    }
    //--------------------------------------------
     
    return (LotSize);
     
    }
    //--------------------------------------------------------------------

Using that function it is also possible to avoid to set  the   Lots Decimal  and the   Base Currency Exch Rate  MM parameters that will become ininfluent.

 

0

JS17

Customer, bbp_participant, community, 73 replies.

Visit profile

9 years ago #129896

Riccardo

 

Thanks for this, But I have a number of questions

 

1. Which part of the EAW code needs to be deleted

2. Do any new variables need to be declared for you code to work

 

Thankyou 🙂

0

loonet

Subscriber, bbp_participant, community, customer, sq-ultimate, 15 replies.

Visit profile

9 years ago #130005

1. You have only to replace the sqMMFixedRisk function with the code I post.

2. You don’t need to declare any additional variable (take in account that with my function the Lots Decimal  and the  Base Currency Exch Rate  Ea Wizard Money Management parameters become ininfluent)

0

Mark Fric

Administrator, sq-ultimate, 2 replies.

Visit profile

9 years ago #130015

Hello Riccardo,

 

sorry it takes me too long to respond. I already looked at your function and tested it, but it is quite different from our own function and I haven’t had time to test it thoroughly in all environments, including trading of CFDs etc., which is different than forex.

 

I’d like to get to it any day, I’m just too busy with unexpected things all the time.

Mark
StrategyQuant architect

0

loonet

Subscriber, bbp_participant, community, customer, sq-ultimate, 15 replies.

Visit profile

9 years ago #130020

Thanks Marc,

I use it basically on Forex trading.

I compared the calculation between the original function and the modified one.

Original function give me a strange behaviour in lot calculation : in some cases it seems don’t taking in account the risk percent I fixed.

If you want I can send you an example the backtest results obtained using both the functions.

0

JS17

Customer, bbp_participant, community, 73 replies.

Visit profile

9 years ago #130049

Riccardo,

 

Thanks for your reply and explanation – as Mark has now replied I will wait for him to finish his testing – thanks again

0

Mark Fric

Administrator, sq-ultimate, 2 replies.

Visit profile

9 years ago #130154

I tested it and all seems to be working correctly, so I just released an EA Wizard autoupdate that contains Money Management fix from Riccardo.

Mark
StrategyQuant architect

0

JS17

Customer, bbp_participant, community, 73 replies.

Visit profile

9 years ago #130163

Can I ask that if when using this feature I enter 0 in % risk, then the function will use the value entered in “lots if no MM” ?

0

tomas262

Administrator, sq-ultimate, 2 replies.

Visit profile

9 years ago #130205

Risk in % field value is required to be greater then zero i.e. 0.1, 1 etc.

0

NirvanaFx

Customer, bbp_participant, community, 5 replies.

Visit profile

8 years ago #130401

I tried using the Risk in % and no matter what I try, whatever percent I put in the field (i.e., 1,20,.06. .3, etc) that is the exact fixed lot size that gets traded. It isn’t varying the lot size at all.. It is not defaulting to Lots if no MM or anything like that and I have the stoploss for risk to be calculated. Is this a bug that anyone else has experienced lately? I have the latest update as well.

0

tturtle

Subscriber, bbp_participant, community, 6 replies.

Visit profile

8 years ago #131241

Hi Marc and Tomas,

 

This looks like the closest thread to what I am experiencing.

 

MM to fixed % of equity is set as well as Global Stop Loss. All trades are set to use these settings.

 

In the TEster in MT4, all lot sizes are defaulting to 0.01 Lots on EURUSD.

 

Attached is the code I’m using.  Please let me know if you can see anything.

 

I’m using MT4 Build 840

 

Thanks and regards

Tim

 

0

tomas262

Administrator, sq-ultimate, 2 replies.

Visit profile

8 years ago #131257

Ok, could you rather attach SQW file?

0

och

Subscriber, bbp_participant, community, 2 replies.

Visit profile

8 years ago #133734

Hello, 
Not sure either this is the right place to post this remark, but I guess also there is an error in StrategyQuant (v3.8.1) Lot calculation:

 

Line 14 : if(MathMod(lotMM*100, lotStep*100) > 0) {

 

should be

 

               if(MathMod(lotMM1*100, lotStep*100) > 0) {

double computeMMFromRiskPerTrade(double riskPerTrade, double slSize) {
   if(slSize  0) {
      lotMM = lotMM1 - MathMod(lotMM1, lotStep);
   } else {
      lotMM = lotMM1;
   }

   lotMM = NormalizeDouble( lotMM, LotsDecimals);

   if(MarketInfo(Symbol(), MODE_LOTSIZE)==10000.0) lotMM=lotMM*10.0 ;
   lotMM=NormalizeDouble(lotMM,LotsDecimals);

   //Log("Computing lots, risk: ", riskPerTrade, ", lotMM1: ", lotMM1, ", lotStep: ", lotStep, ", lots: ", lotMM);
   double Smallest_Lot = MarketInfo(Symbol(), MODE_MINLOT);
   double Largest_Lot = MarketInfo(Symbol(), MODE_MAXLOT);

   if (lotMM  Largest_Lot) lotMM = Largest_Lot;

   if(lotMM > MaximumLots) {
      lotMM = MaximumLots;
   }

   //Log("SL size: ", slSize, ", LotMM: ", lotMM);

   return (lotMM);
}

Regards,

och

0

Mark Fric

Administrator, sq-ultimate, 2 replies.

Visit profile

8 years ago #133753

Hello Olivier,

 

you are right, there is this mistake. I’ll release an update tomorrow with a fix.

Mark
StrategyQuant architect

0

Viewing 13 replies - 1 through 13 (of 13 total)