Code for lot size calculations not working

To add comments, please log in or register
Belal Chami
32
Belal Chami  

Hi,

I have written a script  below to determine the lot size of an order based upon the stoploss size. I am using mt4 pepperstone using the standard demo account (leverage 500, if that matters). However, I get the same lot size, irrespective of the stoploss value. 

Can someone tell me what I am doing wrong?


extern double 

traderisk      = 0.02,    // % risk of free margin 

StopLoss       = 85;      // Stop loss in pips



int start()

  {  

    int

    currentorders = OrdersTotal();                      

    double

    Free   =  AccountFreeMargin(),                    

    Min_Lot = MarketInfo("EURUSD",MODE_MINLOT),           

    One_Lot = MarketInfo("EURUSD",MODE_MARGINREQUIRED),   

    nTickValue = MarketInfo(Symbol(),MODE_TICKVALUE),

    LotSize = 0;

   

   if(Digits==3 || Digits==5)

   {

      nTickValue=nTickValue*10;

   }

   if (currentorders  < 1 )                                

     {

     LotSize=(Free*traderisk)/(StopLoss*nTickValue);

     Alert ("Lot size = ", LotSize);

     }

   if(LotSize < Min_Lot) 

   {

   Alert ("Lot size too small ", LotSize);                                 

   }

   if (LotSize*One_Lot > Free)                      

     {

      Alert(" Not enough money for ", LotSize," lots");

     }

return;

}
Belal Chami
32
Belal Chami  

Bumping this up so others might help

Keith Watford
Moderator
13329
Keith Watford  
Belal Chami:

Bumping this up so others might help

Please don't as instead of making it more likely that you will get help, it annoys people to go to an unread post that they have already read.

FYI

it may be just me, but I often cannot be bothered with reading code that is full of unnecessary line spaces.

Belal Chami
32
Belal Chami  
I actually put the line spaces there so it wouldn't be difficult to read. I actually find reading text without spacing very annoying. Perhaps coding is a different ball game.
Keith Watford
Moderator
13329
Keith Watford  
Belal Chami:
I actually put the line spaces there so it wouldn't be difficult to read. I actually find reading text without spacing very annoying. Perhaps coding is a different ball game.

As I said, it might just be me, but I prefer to read posted code without scrolling.

Fernando Carreiro
3837
Fernando Carreiro  
Belal Chami: I actually put the line spaces there so it wouldn't be difficult to read. I actually find reading text without spacing very annoying. Perhaps coding is a different ball game.

I agree with @Keith Watford ! Too much white-space makes it unreadable!

The following post is a very old and the code is also very old, but it can give you an idea of what you should be doing:

Forum on trading, automated trading systems and testing trading strategies

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

Fernando Carreiro, 2014.01.09 02:37

OK guys! This code was written by me and it is what I use in my own EA's. It is quite complex because, besides the Risk% and StopLoss, it also takes into account the Margin Risk% as well as correct the Lot Size based on Minimum, Maximum and Step Size. It also always uses the minimum value of Current Balance and Equity instead of just using one of them. I feel it is safer that way.

Please note, however, that it does not use the spread in the calculation, because I calculate that separately when calculating the StopLoss to be used. The below function, thus uses a relative stop loss size. In my EA's the argument dblStopLossPips is calculated beforehand depending on various factors such as the strategy, spread, ATR, etc.; so the final value passed on to the dblLotsRisk() function is already a final value in order to calculate the Lot size to be used.

// Function to Determine Tick Point Value in Account Currency
   double dblTickValue( string strSymbol )
   {
      return( MarketInfo( strSymbol, MODE_TICKVALUE ) );
   }     

// Function to Determine Pip Point Value in Account Currency
   double dblPipValue( string strSymbol )
   {
      double dblCalcPipValue = dblTickValue( strSymbol );
      switch ( MarketInfo( strSymbol, MODE_DIGITS ) )
      {
         case 3:
         case 5:
            dblCalcPipValue *= 10;
            break;
      }
      return( dblCalcPipValue );
   }    

// Calculate Lot Size based on Maximum Risk & Margin
   double dblLotsRisk( string strSymbol, double dblStopLossPips,
                       double dblRiskMaxPercent, double dblMarginMaxPercent )
   {
      double
         dblValueAccount  = MathMin( AccountEquity(), AccountBalance() ),
         dblValueRisk     = dblValueAccount     * dblRiskMaxPercent   / 100.0,
         dblValueMargin   = AccountFreeMargin() * dblMarginMaxPercent / 100.0,
         dblLossOrder     = dblStopLossPips * dblPipValue( strSymbol ),
         dblMarginOrder   = MarketInfo( strSymbol, MODE_MARGINREQUIRED ),
         dblCalcLotMin    = MarketInfo( strSymbol, MODE_MINLOT         ),
         dblCalcLotMax    = MarketInfo( strSymbol, MODE_MAXLOT         ),
         dblCalcLotStep   = MarketInfo( strSymbol, MODE_LOTSTEP        ),
         dblCalcLotLoss   = MathRound( dblValueRisk   / dblLossOrder   / dblCalcLotStep ) * dblCalcLotStep,
         dblCalcLotMargin = MathRound( dblValueMargin / dblMarginOrder / dblCalcLotStep ) * dblCalcLotStep,
         dblCalcLot       = MathMin( dblCalcLotLoss, dblCalcLotMargin );
                
         if ( dblCalcLot < dblCalcLotMin ) dblCalcLot = dblCalcLotMin;
         if ( dblCalcLot > dblCalcLotMax ) dblCalcLot = dblCalcLotMax;

         return ( dblCalcLot );
   }
PS! I altered and simplified the code here a bit in this post as apposed to the original post in the reference thread, so as to make it easier for you to understand it.
Belal Chami
32
Belal Chami  

Thank you very much. This was helpful!


p.s: Sorry about the white space

To add comments, please log in or register