Exp4 Close Minus by Plus
Covers losing positions by identifying and closing winning positions.
This assistant looks for the most loss-making position among all positions in the terminal.
Further, by searching for other positions, it calculates a series of orders that can cover the loss and closes them.
The series of orders can include any position. The maximum number of positions is specified in the settings of the Expert Advisor.
- TypeOfUnitToClose - type of profit and loss calculations. In USD or in points.
- MinLossInUnit - the minimum loss of a position in units (TypeOfUnitToClose), to be taken into account by the EA.
- An order with a loss of "MinLossPoss_in_unit" units (USD or points), should be placed in the negative zone.
- It is the upper limit when searching for unprofitable positions.
- For example: MinLossInUnit = -100, TypeOfUnitToClose = USD.
- Then the EA looks for or waits for a position with a loss greater than or equal to -100 USD, for example: -100 USD, -150 USD, -200 USD and so on.
- A position with a loss of -99 USD is ignored.
- AdditionalProfitInUnit - the amount of profit units for the difference to close a set of positions.
- Position (or several) with the total profit exceeding the loss by "AdditionalProfitInUnit" units.
- For example: AdditionalProfitInUnit= 10, MinLossInUnit= -100, TypeOfUnitToClose = USD.
- The EA will look for or wait for at least 2 positions, where one position has a loss of -100 USD and the other has a profit of +110 USD. At this point, the EA decides if it is necessary to activate its functions.
- MaxPos_in_Class - the "MaxPos_in_Class" parameter should limit the number of positions of the entire closing "set".
- For example: MaxPos_in_Class = 3, then the EA will look for a closing set with 3 positions at most (1 unprofitable and 2 profitable ones).
- FirstSymbolClose - search for position pairs and close only those in the currency of the currently processed losing position.
- You can disable the FirstSymbolClose, parameter. Then the EA will close any sets of losing and profitable positions on the entire account, regardless of the currency pair included in calculation.
- When the FirstSymbolClose function is enabled, the EA considers and closes sets of the same currency pair.
- OpenHoura OpenMinutea - the EA operation start hour.
- CloseHoura CloseMinutea - the EA operation end hour.
- OrderToClose - type of positions: -1 = All Orders, 1 = Sell, 0 = Buy;
- MagiсToClose - magic number of positions: -1 = any, or you can specify a unique magic number of your positions to be processed.
- SymbolToClose - currency pair to monitor: -1 = All Symbol, 1 = Current Symbol;
Operation of the Expert Advisor
The operation involves attaching the Expert Advisor to a separate chart, making the necessary settings and continuing to work in our terminal. When the programmed condition triggers, the Expert Advisor comes into play and starts making calculations.
All calculations are written to the log, as follows:
2014.02.05 01:18:38.408 Exp - Exp - CloseMinusByPlus USDCHF,H1: close #22079015 sell 1.00 EURUSD at 1.35141 at price 1.35119 2014.02.05 01:18:38.346 Exp - Exp - CloseMinusByPlus USDCHF,H1: Tickets of prof positions: .22079015 2014.02.05 01:18:38.346 Exp - Exp - CloseMinusByPlus USDCHF,H1: close #22085491 sell 1.00 USDCHF at 0.90423 at price 0.90430 2014.02.05 01:18:38.283 Exp - Exp - CloseMinusByPlus USDCHF,H1: Tickets of loss positions: 22085491 2014.02.05 01:18:38.283 Exp - Exp - CloseMinusByPlus USDCHF,H1: Numbers of positions to Close: 80635972 2014.02.05 01:18:38.283 Exp - Exp - CloseMinusByPlus USDCHF,H1: All profit from open positions: 20 dollars 2014.02.05 01:18:38.283 Exp - Exp - CloseMinusByPlus USDCHF,H1: Maximim profit ticket: 22079015 2014.02.05 01:18:38.283 Exp - Exp - CloseMinusByPlus USDCHF,H1: Maximim profit: 20 dollars 2014.02.05 01:18:38.283 Exp - Exp - CloseMinusByPlus USDCHF,H1: Maximum Loss ticket: 22085491 2014.02.05 01:18:38.283 Exp - Exp - CloseMinusByPlus USDCHF,H1: Maximim loss: -7 dollars 2014.02.05 01:18:38.283 Exp - Exp - CloseMinusByPlus USDCHF,H1: Overlapping loss positions by closing profitable positions started using the currency pair minus:
The above suggests that we have had a lot of open positions (chaotically opened).
We have specified in the settings that the calculation should start when we find the biggest losing position with a loss of -50 (MinLossInUnit = -50) pips (TypeOfUnitToClose = 2) and the total profit being higher by 20 pips (AdditionalProfitInUnit = 20).
We have also specified that not more than 5 orders (MaxPos_in_Class = 5) should be closed.
The Expert Advisor has found a position with a loss of -51 pips, as well as a series of orders with the profit of +72 pips. So it closed both the losing position and the series of winning positions.
- Renamed the parameters:
TypyClose = TypeOfUnitToClose
MinLossPoss_in_unit = MinLossInUnit
ProfitPerLoss_in_unit = AdditionalProfitInUnit
MagikToClose = MagicToClose
Details on the panel in our blog: https://www.mql5.com/en/blogs/post/687140
MinLossPoss_in_unit - should be set in the negative zone. This is the upper limit when searching for unprofitable positions.
If the parameter is set to MinLossPoss_in_unit=-10, then the unprofitable trade (covered by the profitable ones) must be less than -10 dollars (points)
MinLossPoss_in_unit = parameter that limits the EA operation if the negative trade is greater than the MinLossPoss_in_unit. For example, when opening a trade, it profit = -spread.
The algorithm works if there are 2 or more positions to be closed.
- Added enumerations to select calculation and close type.