An example for calculating the lot value in accordance with the risk per trade.
Update: 28 Dec 2016 -> version 1.001
What's new:
added two methods for outputting (printing) the calculated lot. Method 1 — when setting StopLoss to zero.
Method 2 — see the code listing below:
Also, now the result of a trading operation is checked:
First check: if the end of the Buy method operation returned "true", but the ResultDeal() method returned "0" (this may happen in case of requotes) — the "count" counter should be decreased by one.
Second check: if the end of the Buy method returned false "false", the "count" counter should be decreased by one.
Why do we need to decrease "count" by one? In this case we do not have to wait 980 ticks again, but we can try to enter on the next tick.
How it works:
Set risk percent per trade (the % risk parameter) and Stop Loss (StopLoss (in pips) parameter). This defines parameters of possible deposit loss.
The following loop is implemented to simulate trading:
Initial value count=-21 is set to "warm up" the strategy tester. Then the remainder after devision of count by 980 (this number was chosen randomly) is calculated. It means that every 980 ticks lot calculation cycle is started, in which lot is calculated taking into account the risks per trade.
The lot calculation cycle depending on risk per trade (calculation for the Buy position):
Step one
Then the calculated lot value for a Buy position taking into account StopLoss is received to the check_open_long_lot variable using the CheckOpenLong method of the CMoneyFixedRisk class:
The following parameters are printed to the Experts journal: StopLoss, calculated lot value in accordance with the risk per trade, trade account balance at the time of calculation, margin at the moment of calculation.
If the calculation returns "0.0", exit:
Step two
Then we receive the lot value of the Buy position for which we have sufficient funds; the value is received to the chek_volime_lot variable using the CheckVolume method of the CTrade class. The following parameters are passed here: m_symbol.Name() — symbol name, check_open_long_lot — position volume we want to open (this parameter was calculated earlier):
Step three
If the CheckVolume method returns a value other than "0.0", then we check the condition: do we have enough money to open a position with the lot calculated in accordance with the risk.
If we have enough money, open the position, if not — the lot value calculated in accordance with the risk per trade (DoubleToString(check_open_long_lot,2)) and the lot value for wich we have enough funds (DoubleToString(chek_volime_lot,2)) are printed to the Experts journal.
Example (when you test in the Strategy Tester, choose EURUSD, M1, testing period from 2016.11.28, deposit $3000):
A Buy position was opened (extract from the tester journal) — the calculated lot based on risk per trade is 2.23.
It turned out that funds are not enough to open the second position.
Pay attention to the fact, that when StopLoss is not specified (StopLoss=0.0), the calculated lot is equal to the minimum allowed lot value.
As a result, the first position was closed by stop loss with a loss of 298.82:

This corresponds to almost 10% risk of $3000 deposit.
MetaQuotes Ltd tarafından Rusçadan çevrilmiştir.
Orijinal kod: https://www.mql5.com/ru/code/17199
Hercules A.T.C. 2006
The Expert Advisor trades moving average breakouts. It uses the following indicators: two iMAs (Moving Average, MA), iRSI(Relative Strength Index, RSI) and two iEnvelopes (Envelopes).
The Puncher
Trades using indicators: iStochastic (Stochastic Oscillator) and iRSI (Relative Strength Index, RSI).
ShowImportantParams
Displays important symbol and account parameters.
BigBarSound
The Expert Advisor plays sound alerts when candlestick size exceeds a certain value.