Trabajo finalizado
![](https://c.mql5.com/i/avatar/avatar_na2.png)
Tarea técnica
Develop a class-driven sub-routine that can be implemented in a variety of existing Expert Advisor with minimum complications of integration. It should implement a good OOP concept for reusability and derive sub-mechanisms.
This sub-routine will work on algorithms running on MT5 terminal; we may need to develop an expert advisor opening a long or short position for test. This subroutine purpose is to implement a strategy of averaging down.
This is that we open:
BUY 1 LOT EURUSD at 1.19209
This sub-routine will open automatically
BUY 1.3 LOT EURUSD at 1.19172 with a comment "Level_1"
Resulting on an average price in EURUSD of 1.19195 at 2.30 LOT, so this sub will draw a BLUE line at this average level. RED if SELL position.
Attention: For different LOT of positions, the average price is calculated using a mathematical formula.
Finally, will open automatically if the price continue against:
BUY 1.69 LOT EURUSD at 1.19078 with a comment "Level_2"
This next average level as is not open yet so will draw a line in "Bisque" color with the parameters of the averager sub-routine in this case the LOT factor increases to a 30% or 1.3X and the amount of PIPs in the following format: 1.30X/1.69N/94p indicating the average %, resulting lot size and pips in distance.
If this average level is triggered then the average price of the EURUSD position is 1.19145 at 3.99 LOT.
The PIPs difference between 1.19209 and 1.19172 and the next 1.19145 is provided as a parameter for the sub-routine.
The averager sub-routine will have a maximum amount of positions to open. And a FLOOR parameter that sets the minimum distance, this distance can be set in price or points. If in the example below we set a price floor of 1.19130 the next level provided can not be 1.19145 and will be floored at 1.19130. If we set a points floor of 70 pips the next level can not be 67 pips as provided and will be floored at 70 pips.
The subroutine will increase distance in the same way to increase volume, this is that an increment can be set of a 10% or 1.1X added to the distance provided.
If the instrument maximun volume is 50, and the next average down is 56. We will fraction the order in 50 (the max.) and another position at the same price level at 6, with the same comment. If the broker have a maxium limit of positions and can not open, we will pop-up a message alterting with the limitation of opening new positions.
Finally, the sub-routine will use a time delay to evaluate if a different distance in pips is supplied.
Inputs:
AveragerFunc(
bool AverageUSE=true,
int Distance=1,
ENUM MinDistType=1, //1-Price, 2-Points
double MinDistVal=0,
bool UseCalcDelay=true,
ENUM CalcDelay=1M, //1M,2M,5M,15M,1H,1D,1W, TF defaults in example
DistanceFactor=1.1, //points + 10% in this case
LotsFactor=1.3, //volume + 30% in this case
MaxOrders=10, //maximum of averaging positions
string s="Averager " //default comment for opening positions and concatenate
)
Output of the subroutine will be:
LOTs BUY: 2.3 //actual volume size
LONG Avg: 1.19195 //actual average
LONG N Avg: 1.19145 //next average
The average calculation have to take into account any commission and swap charges in any given period. So if this positions stays for more than a day and a swap is charged, have to be reflected in the resulting average price every day.
Attached an image of the horizontal lines.