forked from nique_372/MQLArticles
332 lines
11 KiB
Markdown
332 lines
11 KiB
Markdown
<p align="center">
| |
<img src="./Images/MQLArticles.png" alt="MQLArticles Logo" width="1150" height="175"/>
| |
</p>
| |
| |
| |
<p align="center">
| |
A comprehensive collection of MQL5 implementations for risk management and position management in algorithmic trading.
| |
This repository contains the source code from articles published on MQL5.com by nique_372..
| |
</p>
| |
| |
<p align="center">
| |
<img src="https://img.shields.io/badge/Language-MQL5-1B6CA8?style=flat-square"/>
| |
<img src="https://img.shields.io/badge/Platform-MetaTrader%205-0D1B2A?style=flat-square"/>
| |
<img src="https://img.shields.io/badge/Author-nique__372-C9D6DF?style=flat-square&logoColor=white"/>
| |
<img src="https://img.shields.io/badge/Articles-mql5.com-1B6CA8?style=flat-square"/>
| |
</p>
| |
| |
| |
| |
---
| |
| |
## Main features
| |
| |
### RM
| |
| |
#### Oco order
| |
```cpp
| |
| |
COcoOrder oco;
| |
| |
void Function()
| |
{
| |
.
| |
.
| |
unsigned long ticket1 = trade.ResultOrder();
| |
.
| |
.
| |
unsigned long ticket2 = trade.ResultOrder();
| |
| |
oco.AddOrders(ticket1,ticket2);
| |
}
| |
```
| |
| |
#### Risk management
| |
```cpp
| |
CRiskManagement rm;
| |
// Note: "rm" requires that the lot, type, maximum profit and loss be reset, etc., before calling functions like CalculateSL, GetLote, etc.
| |
| |
// Check superated
| |
if(g_loss_profit_manager.MaxLossIsSuperated())
| |
{
| |
risk.CloseAllPositions();
| |
CanTrade = false;
| |
}
| |
| |
// Calcule lot size
| |
double entry_price = 1000.0;
| |
double l = risk.GetLote(ORDER_TYPE_BUY, entry_price, 100, 0);
| |
| |
// Calcule SL
| |
long sl = risk.GetSL(ORDER_TYPE_BUY, entry_price, 100, 0)
| |
| |
// And more (GetPositionsTotal, SetStopLoss, CloseAllOrders, GetPositions, etc..)
| |
```
| |
| |
| |
### Strategy
| |
#### Basic configuration
| |
```cpp
| |
CAtrUltraOptimized* atr_ultra = new CAtrUltraOptimized();
| |
atr_ultra.SetVariables(PERIOD_CURRENT, _Symbol, 0, 14);
| |
atr_ultra.SetInternalPointer();
| |
strategy.AddLogFlags(InpStrategyLogLevel); // Log Flags
| |
strategy.SetAtrTP_SL(atr_ultra, INP_STRATEGY_ATR_MULTIPLIER_TP, INP_STRATEGY_ATR_MULTIPLIER_SL); // TP sl by atr
| |
strategy.SetOperateMode(TR_BUY_SELL, INP_STRATEGY_TYPE_TPSL); // Operate mode
| |
strategy.SetTP_SL(INP_STRATEGY_SL_POINT, INP_STRATEGY_TP_POINT); // TP SL by point
| |
if(InpRmLoteType == Fijo)
| |
strategy.FixedLotSize(InpRmLote); // Fixed lot size
| |
```
| |
| |
#### Filters
| |
```cpp
| |
// Filter RSI
| |
if(INP_FILTER_RSI_ENABLE)
| |
{
| |
CSFilterRsi* f = new CSFilterRsi();
| |
strategy.AddFilter(f);
| |
}
| |
| |
// Filter Stochastic
| |
if(INP_FILTER_STOCH_ENABLE)
| |
{
| |
CSFilterStochastic* f = new CSFilterStochastic();
| |
strategy.AddFilter(f);
| |
}
| |
| |
// Filter Bollinger Bands
| |
if(INP_FILTER_BANDS_ENABLE)
| |
{
| |
CSFilterBands* f = new CSFilterBands();
| |
strategy.AddFilter(f);
| |
}
| |
| |
// Codigo
| |
g_strategy_filter_general_parser.AddLogFlags(InpFIlterLogLevel);
| |
strategy.CodeCompra<CStrategyFilterEmptyFuncFac>(StringFormat("#group oAnd (%s,%s,%s,%s,%s,%s,%s) == 0 oAnd ([%s] == 0 oOr [%s] == 2)",
| |
CSFIlterAD_NAME, CSFilterSuperTrend_NAME, CSFilterFvg_NAME, CSFilterBands_NAME, CSFilterRsi_NAME, CSFilterStochastic_NAME, CSFilterLiqEstimed_NAME, CMediationsByZoneFilterName, CMediationsByZoneFilterName), 5);
| |
strategy.CodeVenta<CStrategyFilterEmptyFuncFac>(StringFormat("#group oAnd (%s,%s,%s,%s,%s,%s,%s) == 1 oAnd ([%s] == 1 oOr [%s] == 2)",
| |
CSFIlterAD_NAME, CSFilterSuperTrend_NAME, CSFilterFvg_NAME, CSFilterBands_NAME, CSFilterRsi_NAME, CSFilterStochastic_NAME, CSFilterLiqEstimed_NAME, CMediationsByZoneFilterName, CMediationsByZoneFilterName), 5);
| |
| |
// Summary
| |
strategy.PrintFilters();
| |
```
| |
| |
### Position management
| |
#### Breakeven
| |
```cpp
| |
//--- Atr
| |
atr_ultra_optimized.SetVariables(_Period, _Symbol, 0, 14);
| |
atr_ultra_optimized.SetInternalPointer();
| |
| |
//--- We set the breakeven values so its use is allowed
| |
break_even.SetBeByAtr(InpBeAtrMultiplier, InpBeAtrMultiplierExtra, GetPointer(atr_ultra_optimized));
| |
break_even.SetBeByFixedPoints(InpBeFixedPointsToPutBe, InpBeFixedPointsExtra);
| |
break_even.SetBeByRR(InpBeRrDbl, InpBeTypeExtraRr, InpBeExtraPointsRrOrAtrMultiplier, GetPointer(atr_ultra_optimized));
| |
break_even.SetInternalPointer(InpTypeBreakEven);
| |
break_even.obj.AddLogFlags(InpLogLevelBe);
| |
```
| |
#### Partial closures
| |
| |
```cpp
| |
g_partials.AddLogFlags(InpLogLevelPartials);
| |
g_partials.Init(InpMagic, _Symbol, InpVolumenQueSeQuitaraDeLaPosicionEnPorcentaje, InpPartesDelTpDondeSeTomaraParciales);
| |
```
| |
| |
#### Conditional partial closures
| |
```cpp
| |
if(InpPartialsIsEnable)
| |
{
| |
// Atr creation
| |
g_atr = new CAtr();
| |
g_atr.Create(_Period, _Symbol, 14, true, true);
| |
g_atr.SetAsSeries(true);
| |
CAutoCleaner::AddPtr(g_atr); // Añadimos para que se auto elimine
| |
| |
| |
// We assign to g_partilas the dynamic instance returned by CConditionalPartialsFactory::Create(...)
| |
g_partials = CConditionalPartialsFactory::Create(InpPartialsClassManagerType); // Sera elimnado globalmente
| |
| |
// Initial log flags
| |
g_partials.AddLogFlags(InpPartialsLogLevel);
| |
| |
// Create condition
| |
CConditionalPartialsIndRsi* rsi_condition = new CConditionalPartialsIndRsi(); // True dado que sera eliminado por partials
| |
| |
// Rsi condition config
| |
if(!rsi_condition.Init(InpPartialsRsiTimeframe, _Symbol, InpPartialsRsiPeriod, InpPartialsRsiOverBoughtLevel, InpPartialsRsiOverSoldLevel))
| |
return INIT_PARAMETERS_INCORRECT;
| |
| |
// Condition config
| |
ConditionalPartialConfig config;
| |
config.condition = rsi_condition;
| |
config.min_distance_to_close_pos = CreateDiffptr(MODE_DIFF_BY_ATR, _Symbol, g_atr, 0, InpPartialsMinDistanceInAtrMul);
| |
config.str_percentage_volume_to_close = InpPartialsVolumeToClosePercentage;
| |
config.magic_number = InpMagic;
| |
| |
// Init partials
| |
if(!g_partials.Init(config))
| |
return INIT_PARAMETERS_INCORRECT;
| |
else
| |
CAutoCleaner::AddPtr(g_partials); // Añadimos al cleaner
| |
| |
if(InpPartialsClassManagerType == CONDITIONAL_PARTIAL_CLASS_TYPE_CONSTANT)
| |
{
| |
CConditionalPartialsConst* partial = (CConditionalPartialsConst*)g_partials;
| |
partial.ForzeToClose(true);
| |
}
| |
}
| |
```
| |
| |
### Utils
| |
| |
#### Fibbonaci
| |
```cpp
| |
CFibbo fibbo;
| |
fibbo.Create(ChartID(), "Fibbo", 0, clrGreen, STYLE_SOLID, 1, "Fibbonaci");
| |
fibbo.SetLevels("0.0,0.2,1.0,2.0", "clrRed,clrRed,clrRed,clrRed", STYLE_SOLID, 1, ',');
| |
| |
fibbo.Move(D'2025.01.01 10:00:00', D'2026.01.01 10:00:00', 1000.0, 1050.0);
| |
| |
double tp = fibbo.GetLevelPrice(2.0);
| |
double sl = fibbo.GetLevelPrice(0.2);
| |
| |
```
| |
| |
#### GraphicObjects
| |
```cpp
| |
RectangleCreate(...);
| |
EventCreate(...);
| |
ArrowRightPriceCreate(..);
| |
.
| |
.
| |
.
| |
// +10 Functions
| |
```
| |
| |
| |
#### SetFile
| |
```cpp
| |
void OnStart()
| |
{
| |
//---
| |
CSetFile sf;
| |
sf.Init("BotSimple");
| |
sf.AddParamLineNumber("InpMagic", 100, 100, 1, 999);
| |
sf.AddParamLineReal("InpRisk", 1.0, 2, 0.1, 0.1, 10.0);
| |
sf.AddParamLineBool("InpUseFilter", false);
| |
sf.AddParamLineDatetime("InpStartDate", D'2024.01.01', D'2020.01.01', 86400, D'2025.12.31');
| |
sf.AddParamLineColor("InpLineColor", clrRed);
| |
sf.ModifyParamValueNumber("InpMagic", 999);
| |
sf.ModifyParamValueRealNumber("InpRisk", 5.5, 2);
| |
sf.ModifyParamValueBoolean("InpUseFilter", true);
| |
sf.ModifyParamValueDatetime("InpStartDate", D'2025.06.01');
| |
sf.ModifyParamValueColor("InpLineColor", clrBlue);
| |
sf.ModifyParamOptDatetimeStart("InpStartDate", D'2023.01.01');
| |
sf.ModifyParamOptDatetimeStep("InpStartDate", 3600);
| |
sf.ModifyParamOptDatetimeStop("InpStartDate", D'2026.01.01');
| |
sf.Imprimir();
| |
}
| |
```
| |
| |
| |
> These are only 5-10% of the classes available in MQLArticles; only the most important classes in the library were represented.
| |
| |
| |
| |
| |
---
| |
| |
| |
## Requirements
| |
See the dependencies.json file.
| |
| |
---
| |
| |
## Installation Methods
| |
- Clone the git repository into shared projects via cmd.
| |
- Contact me privately on MQL5 chats (user: nique_372) to be added as a collaborator with your MQL5 nickname (read-only access), which will make the repository automatically appear in your Shared Projects folder.
| |
- Fork the repository.
| |
| |
| |
---
| |
| |
## Repository Structure
| |
| |
| Folder | Description |
| |
|---------|-------------|
| |
| **Defines** | Markdown files containing optional defines to activate or increase logging verbosity for specific classes in the repository. |
| |
| **Examples** | Basic examples that implement various libraries from the MQLArticles repository, such as risk management, position management, etc. |
| |
| **Images** | Screenshots and visual assets from repository examples (breakeven manager, lot size calculator, order blocks indicator). |
| |
| **IndicatorsCts** | Wrapper library for implementing technical indicators. |
| |
| **Ob** | Order Blocks indicator implementation and example Expert Advisors from published articles (author: nique_372). |
| |
| **PosMgmt** | Position management libraries including:<br>- Breakeven management<br>- Partial position closure<br>- Conditional partial closure with indicator-based conditions |
| |
| **RM** | Complete Risk Management (RM) library modules. |
| |
| **Utils** | Core utility library for EAs, indicators, and libraries.
| |
| **Sets** | Preset configuration files (.set) used in articles published by nique_372. |
| |
| **Strategy** | Strategy implementation framework for the MQLArticles ecosystem. |
| |
| |
| |
---
| |
| |
| |
## Examples
| |
- Examples\\GUI\\BE\\Ea.mq5
| |
| |

| |
| |
| |
- Examples\\GUI\\Risk_Management_Panel.mq5
| |
| |

| |
| |
| |
- Ob\\Indicator\\OrderBlockIndPart2.mq5
| |
| |

| |
| |
---
| |
| |
## Implemented Article Series
| |
| |
### Risk Management
| |
| |
| Part | Main Topic | Article Link |
| |
|-------|----------------|-------------------|
| |
| **Part 1** | Risk management fundamentals | [[EN]](https://www.mql5.com/en/articles/16820) |
| |
| **Part 2** | Lot size calculation | [[ES]](https://www.mql5.com/es/articles/16985) |
| |
| **Part 3** | Base class construction | [[ES]](https://www.mql5.com/es/articles/17249) |
| |
| **Part 4** | Completing key functions of the CRiskManagement class | [[ES]](https://www.mql5.com/es/articles/17508) |
| |
| **Part 5** | Integrating risk management into an EA (Order Block) | [[ES]](https://www.mql5.com/es/articles/17640) |
| |
| |
> **Important Update**: The RiskManagement library has been completely renovated since the last publication (part 5).
| |
| |
| |
### Position Management - Breakeven
| |
| |
| Part | Focus | Article Link |
| |
|-------|---------|-------------------|
| |
| **Part 1** | Base class and breakeven by fixed points | [[ES]](https://www.mql5.com/es/articles/17957) |
| |
| **Part 2** | Breakeven by ATR and RRR | [[ES]](https://www.mql5.com/es/articles/18111) |
| |
| |
| |
### Position Management - Partial Closes
| |
| Focus | Article Link |
| |
|-------|--------------------|
| |
| Implementation of partial closes in MQL5 | [[ES]](https://www.mql5.com/es/articles/19682) |
| |
| |
| |
### Position Management - Conditional partial closure
| |
| Focus | Article Link |
| |
|-------|--------------------|
| |
| Implementation of the base class in MQL5 | [[ES]](https://www.mql5.com/es/articles/20048) |
| |
| |
| |
### Order Block Indicator
| |
| |
| Part | Focus | Article Link |
| |
|-------|---------|-------------------|
| |
| **Part 1** | Initial implementation of Order Blocks in an indicator | [[EN]](https://www.mql5.com/en/articles/15899) |
| |
| **Part 2** | Signal implementation in the Order Block indicator | [[EN]](https://www.mql5.com/en/articles/16268) |
| |
| |
|