Source code library - Expert Advisors, Indicators and Scripts

Try product
Smart Harmony trial
Author: champions763
Indicator
DRAW_COLOR_CANDLES
Author: MetaQuotes
Screenshot
GBPUSD, M30
Real
Subscribe to signal
Aaarobotics
106.90%, 182.33 USD

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

2012.08.12 16:44

## GetLotForOpeningPos [ru]

182
Views:
852
Rating:
Files:

### Description:

This function returns the lot size depending on the amount of money in the deposit currency being used. Input parameters are represented by three variables:

```double GetLotForOpeningPos
(
string symbol,                 // Symbol of the pair for the calculation of the lot size
ENUM_POSITION_TYPE direction,  // Direction of the trade
double lot_margin              // The amount of money in the deposit currency being used, for the calculation of the lot size
)```

The function uses the library GetLotForOpeningPos.mqh (to be copied into the terminal_data_directory\MQL5\Include) the contents of which should be included in the developed code using the #include directive prior to using the function at the global level:

`#include <GetLotForOpeningPos.mqh>`

Translated from Russian by MetaQuotes Software Corp.
Original code: http://www.mql5.com/ru/code/961

Last comments | Go to discussion (2)

onewithzachy | 12 Aug 2012 at 17:44

WRONG !

```//---- normalizing the lot size to the nearest standard value
lot = LOTSTEP*MathFloor(lot/LOTSTEP);```

That lot min-lot step calculation is wrong !.

What we should do is :

1. Subtract the (raw) lot with minimum lot, because we always have have to start calculating lot from the minimum lot defined by broker. If the result is less than 0 then we have no lot.

```lot -= MinLot;
if (lot < 0) lot = 0; //--- not even qualify for minimum lot```

2. Calculate how many step required for step lot to get to the lot. Don't use MathFloor() function, coz the return type of MathFloor() is double which risking having an error when final lot not compliance with broker requirement. Use some integer type variable.

```int the_step;

the_step = lot/LOTSTEP;```

3. Final calculation, get them all together

`lot = MinLot + the_step*STEPLOT;`

4. A complete calculation

```//---- normalizing the lot size to the nearest standard value
lot -= MinLot;
if (lot < 0) lot = 0;

int the_step;
the_step = lot/LOTSTEP;

lot = MinLot + the_step*STEPLOT;

//---- checking the lot for the minimum allowable value
if (lot < MinLot) lot=0;

//---- checking the lot for the maximum allowable value
if(lot>MaxLot) lot=MaxLot;```

5. Terrible - this lot calculation does not include money management which any common sense trader should and must have. Money management calculate the risk of opening position, which is - but not limited to - the cost of losing money to Stop Loss.

To avoid losing money is part of the game.

PhoenixRising | 15 Aug 2012 at 15:36

Hi Nikolay,

I'm new to MT5 and am wondering if you can suggest an indicator that does this well as you suggest?

Thank you for your informative post.

Cheers