Watch how to download trading robots for free
Find us on Twitter!
Join our fan page
Join our fan page
You liked the script? Try it in the MetaTrader 5 terminal
- Views:
- 11589
- Rating:
- Published:
- 2016.05.16 11:37
- Updated:
- 2016.05.23 09:43
-
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance
So, decide - what can be risked in each trade? There are many options, and selecting the correct one is not always easy. To solve this problem, I made the lot_lib.mqh library
Usage:
- download the lot_lib.mqh to the MetaTrader 4\experts\include folder;
- add the following lines to the EA:
// at the beginning: #include <lot_lib.mqh> // to the init() function: lotlib_PrevLots = 0.0; lotlib_PrevProfit = 0.0; // to the start() function, before placing every order: lotlib_OpenPriceLevel = Ask; // instead of Ask - the real value // of the position opening level lotlib_StopLossLevel = Ask-StopLoss*Point; // instead of Ask-StopLoss*Point - the real value of the Stop Loss level // of the position // to the start() function, where the open position is selected (for example, // before trailing stop): lotlib_PrevLots = OrderLots(); lotlib_PrevProfit = OrderProfit();- the lot size when placing orders is determined by the Lot() function. That is, instead of a variable which defines the lot size (usually 'Lot' or 'Lots') write Lot();
- compile the EA and use it.
Note: if the EA simultaneously holds 2 or more positions open, the lotlib_PrevLots and lotlib_PrevProfit variables need to be additionally assigned the required values before opening a new position. You will have to choose what values to assign yourself: last closed position or last opened position.
External variables:
- LotSizeVariant - variant number, will be described below;
- StartLot - initial lot size (for LotSizeVariant can be from 0 to 5);
- AddLot - the size of the lot "increment" (for LotSizeVariant - 1 and 2);
- KLot - increment ratio (for LotSizeVariant - 3 and 4);
- MaxRisk - percentage of the deposit, which can be risked (for LotSizeVariant - 5, 6, 11-17, 31-37);
- TrueProfitPoints - the amount of points, above which the profit of the position is considered profit and below which the profit is considered a loss (for LotSizeVariant - 1-4);
- BalanceUse - percent of balance to be used by the expert. If trading is to be performed using multiple experts, set to 100/"number of experts", i.e. 33 for 3 experts, 25 for 4, and so on (for LotSizeVariant - 5, 6, 7, 11-17, 31-37)
Possible values of LotSizeVariant:
0 - fixed lot size, defined by the StartLot variable;
1 - if the previous position was profitable, i.e. the profit was greater than TrueProfitPoints points, the lot size is increased by AddLot or decreased if AddLot is less than 0, if the position was unprofitable, i.e. the profit was less than TrueProfitPoints points - StartLot is set;
2 - if the previous position was unprofitable, the lot size is increased by AddLot, if profitable - StartLot is set;
3 - if the previous position was unprofitable, the lot size is increased AddLot times, if unprofitable - StartLot is set;
4 - f the previous position was unprofitable, the lot size is increased AddLot times, if profitable - StartLot is set;
5 - lot size is selected so that the maximum loss of the open position comprises MaxRisk percent of the deposit in case the Stop loss is triggered;
6 - the lot size is selected as a percentage of the balance and controlled by the MaxRisk variable. For example, for eurusd with a balance of 1000 and MaxRisk=10 it will open 0.1 lot, and with a balance of 15000 and MaxRisk=5 it will open 0,8 lot;
7 - lot size is selected according to the formula "1 percent of balance" / "average amplitude of price movements in the last 20 bars" * 0,4. I don't remember how I came up with it;
11-17 - calculation algorithms from the variants 1-7, lot size is limited by the size in 5th variant, i.e. the maximum loss will be MaxRisk percents of the balance;
21-27 - calculation algorithms from the variants 1-7, lot size can not be decreased;
31-37 - calculation algorithms from the variants 1-7, lot size is limited by the size in 5th variant and can not decrease, i.e. if it is greater than in the 5th variant, it will be decreased to the size of the 5th variant, but if at the same time it is less than the previous lot, it will be set equal to the previous lot;
100-128 - all variants in a row (convenient for optimization):
100 - 0
101 - 1 108 - 11 115 - 21 122 - 31
102 - 2 109 - 12 115 - 22 123 - 32
103 - 3 110 - 13 117 - 23 124 - 33
104 - 4 111 - 14 118 - 24 125 - 34
105 - 5 112 - 15 119 - 25 126 - 35
106 - 6 113 - 16 120 - 26 127 - 36
107 - 7 114 - 17 121 - 27 128 - 37
Pre-configuration (if necessary, change in the code):
//---- the lot will not be LESS than the value of MIN_LOT #define MIN_LOT 0.1 //---- the lot will not be GREATER than the value of MAX_LOT #define MAX_LOT 100 //---- if an error occurred in operation (for example, not all the necessary //---- variables had been initialized from the EA), the function returns // the value of DEFAULT_LOT #define DEFAULT_LOT -1
Example of usage results:
I took MACD Sample, added stop loss, integrated the library and tried testing with different values of LotSizeVariant. Chart symbol - EURUSD, chart timeframe - H1, testing period: 01.01.2001 - 01.08.2005, modeling mode - all ticks, modeling quality - 90%.
Figures show several results obtained randomly. I did not choose the best variant and did not optimize the parameters. The first figure has fixed lot.
Translated from Russian by MetaQuotes Ltd.
Original code: https://www.mql5.com/ru/code/7749
![ZH_Dynamic_Arrays_Lib](https://c.mql5.com/i/code/library.png)
A set of functions to emulate the dynamic arrays.
![Rotating text objects](https://c.mql5.com/i/code/script.png)
Example of working with objects: creating object, changing properties, redrawing the chart.
![Gap indicator](https://c.mql5.com/i/code/indicator.png)
The indicator shows the gaps between the closing price of the previous bar and the opening price of the current bar.
![ADXdon](https://c.mql5.com/i/code/indicator.png)
The ADXdon indicator. Another version of the ADX indicator, which shows the strength of the trend.