Reply

Money Management Problem – Fixed % and lot size

5 replies

Julianrob

Customer, bbp_participant, community, 54 replies.

Visit profile

9 years ago #113227

Hi Mark,

 

Firstly thankyou so much for your helpful support so far. I seem to have difficulties with my money management with this EA – I am using predefined MM in my strategy rules.

 

Under the strategy section I have variables for lot size and fixed percentage defined as double value, but when I run this EA on AUDUSD or EURUSD with Lotsize at 0.10, it always opens trades at 0.01 lot size.

 

When I set AccountRiskPerTrade to 5, it opens 5 lots trade sizes for every trade, but does not compound the earnings. Please refer to pics attached.

 

For stop loss and take profit, I am using fixed values which are set as variables, set as single digit and type is int (integer)

 

Any help would be great thanx.

0

Mark Fric

Administrator, sq-ultimate, 2 replies.

Visit profile

9 years ago #128549

Hello,

 

in your choice of MM the lots are computed dynamically depending on SL and current account equity. LotSize is used only if MM cannot be computed – usually when you have no SL defined for your order.

But you have Risk Per Trade set to 0, this doesn’t make sense.

 

If you want to use fixed lot size then you should use “No money management”, otherwise you should set how much % of account you want to risk in every trade.

 

 

The problem with computed size is that you have LotsDecimals variable set to 2, which means it will round lots to 2 decimal numbers.

 

It does not compound the earnings – it will work when you’ll set risk per trade to a nonzero value.

Mark
StrategyQuant architect

0

Julianrob

Customer, bbp_participant, community, 54 replies.

Visit profile

9 years ago #128585

Thanks Mark, got it.

0

loonet

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

Visit profile

9 years ago #129589

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.

 

 

So I tried to correct the  EAW    sqMMFixedRisk  function replacing it with a my own function that I report here below :

//------ 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 && 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  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  it you can also avoid to set  the   Lots Decimal  and the   Base Currency Exch Rate  MM parameters that become ininfluent.

0

geektrader

Customer, bbp_participant, community, 522 replies.

Visit profile

9 years ago #129667

SQ seems to have some trouble with correct position sizing in regards to StopLoss / Account Base Currency and the pair traded, as all those factors must be taken into account to correctly calculate the resulting lot-size to trade. To help out, here is my code which I´ve refined and be using for 5 years in MT4. It takes into account SL, the currency the pair is nominated in and the currency the traded account is nominated in so that always the correct % is lost if a SL is hit. It additionally checks for min and max allowed lot size and lot-step size and the margin required and makes sure the margin is used only up to 73% when a full SL is hit so that you won´t get stopped out because of to low margin (which would be crazy overtrading anyway). Maybe Mark can adapt it over for SQ.

 

You just have to pass it the SL and the max amount of trades you open subsequently (I use that for averaging in and when I still only want to lose a specific amount on all these trades – for straight forward trading you just pass “1” to it):

double GetLotSize(double TotalRiskPercentageAllTrades, double Stop_Loss, int MaximumTrades)
{
    int DigitsMulti = 1;

    if (Digits == 5 || Digits == 3)
        DigitsMulti = 10;

    Stop_Loss = Stop_Loss * DigitsMulti;
    double divided_by = 0;
    double Lots       = 0;

    if (TotalRiskPercentageAllTrades != 0)
    {
        double RiskBalance = AccountBalance() * (TotalRiskPercentageAllTrades / 100.0);
        if (MarketInfo(Symbol(), MODE_TICKSIZE) != 0 && Stop_Loss != 0)
            divided_by = MarketInfo(Symbol(), MODE_TICKVALUE) * (MarketInfo(Symbol(), MODE_POINT) / MarketInfo(Symbol(), MODE_TICKSIZE));
        if (divided_by == 0)
            Print("Money Management Temporarily Disabled Due To Division By: ", divided_by);
        else
            Lots = RiskBalance / divided_by / Stop_Loss;
    }

    Lots = Lots / MaximumTrades;
    Lots = MathMax(Lots, MarketInfo(Symbol(), MODE_MINLOT));
    Lots = MathMin(Lots, MarketInfo(Symbol(), MODE_MAXLOT));
    if (MarketInfo(Symbol(), MODE_MARGINREQUIRED) != 0)
        Lots = MathMin(Lots, AccountEquity() / MarketInfo(Symbol(), MODE_MARGINREQUIRED) * 0.73);
    Lots = MathCeil(Lots / MarketInfo(Symbol(), MODE_LOTSTEP)) * MarketInfo(Symbol(), MODE_LOTSTEP);
    Lots = MathMax(Lots, MarketInfo(Symbol(), MODE_MINLOT));

    return(Lots);
}


🚀 Unlock Your Edge in Automated Forex Strategy Development 🚀

Historical Forex Data Starting From 1987, 28 Pairs, M1, 99% Error-Free, Lifetime Free Updates

0

Julianrob

Customer, bbp_participant, community, 54 replies.

Visit profile

9 years ago #129673

Thanks for the info.

 

I discovered how to enable both Money management risk % or fixed lot size option in the variables in coding the EA. The simple solution I used was to create a variable with boolean value true/false and call it; “UseRiskPercent”

 

Then I have a rule that opens the trade using money management risk% only if variable UseRiskPercent is set to TRUE. A second rule which is a clone of the first and says if UseRiskPercent is set to false, use a fixed lot size instead for the trade.

 

Julian

0

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