lot lib
Info
The lot lib is a Library for MetaTrader 4 that so, decide - what can be riskedin each trade. There are many options, and selecting the correct oneis not always easy.
Usage
This tool is typically used for enhancing chart analysis and decision making.
Platform
This Library works exclusively on MetaTrader 4 (both build 600+ and newer versions).
Setup
Place the downloaded file in MQL4/Libraries folder via File ? Open Data Folder in MetaTrader 4.
How to Install and Use lot lib
1. Storage: Place library files in the MQL/Libraries directory to ensure they are accessible to your projects.
2. Implementation: Include the library in your code using the #import directive, ensuring you match the exact function names and parameters.
3. Compilation: Ensure the library is present in the directory before you compile your main EA or script, as the compiler links them during this phase.
4. Management: Keep libraries organized in sub-folders if you manage many custom functions to maintain a clean project structure.
Frequently Asked Questions
Q: What is a library file used for? A: Libraries store reusable code modules, allowing you to centralize common logic used by multiple EAs or indicators.
Q: Is a library executable? A: No, libraries are non-executable files containing functions; they must be imported into an EA, indicator, or script to function.
Q: Can I update a library while the platform is running? A: You should compile your EA or script after updating a library to ensure the latest code changes are integrated.
What this tool does
So, decide - what can be riskedin each trade.
Typical Use Case
This Library excels in automated trading and technical analysis on MetaTrader 4.
Compatible Platform & Setup
This Library works on MetaTrader 4. Place the file in the MQL4/Libraries folder and restart the terminal.
Description & Settings
Related: String Array Suite - another powerful library for MetaTrader 4 traders.
So, decide - what can berisked
in each trade? There are many options, and selecting the
correct one
is not always easy. To solve this problem, I made the
Also recommended: Orders Suite - similar library with strong performance on MetaTrader 4.
lot_lib.mqhlibrary
Usage:
-
download
the lot_lib.mqh to the MetaTrader 4\experts\include folder; -
add
the following lines to the EA: - 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):
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.
You may also like: Time Suite - excellent alternative for library users on MetaTrader 4.
⚠ Limitations & Risk Warning
- This tool is provided for educational and testing purposes only.
- Past performance does not guarantee future results.
- Trading involves substantial risk of loss. Use on a demo account first.
- Results may vary depending on market conditions, broker, and settings.
- We recommend thorough backtesting and forward testing before using with real funds.