Example 4. Technical task “Adviser averaging grid”
The Expert Advisor exposes an averaging network against the price movement. The volume of each order increases by a coefficient (K_Lot), the step between orders also depends on the coefficient(K_Step). If the coefficient is greater than 1, the grid gradually expands, if less, it narrows. The network is limited by the number of orders. The volume of the first position is set in the parameters. Positions are closed by SL and TP. TP moves with the opening of each new position behind the price at a TakeProfit distance from the point of the total breakeven of the entire direction. The Buy and Sell directions work independently of each other. SL is set from the extreme position at a distance of StopLoss
Parameters
Buy = true – allow placing Buy positions
Sell = false – allow placing Sell positions
Orders = 5 – the maximum number of positions in each direction
Step =10 – distance between positions
K_Step =1.1 – distance coefficient between orders
Lot = 0.1 – lot of the first position
K_Lot = 1.5 – the coefficient of increasing the lot of subsequent positions
StopLoss =20 – stop loss of the entire series of orders (placed from the last position)
TakeProfit = 15 profit from the total breakeven point of the entire direction
Example of work:
Consider the option for one direction buy (Sell=false)
At the start, the ADVISER opens the first buy position with a 0.1 lot at the current price of 1.20000
we immediately put TP 15 points above the price of 1.20015
if the price goes up, then it will simply close the profit position and everything is new
if the price goes down 10 p, then we open the averaging buy position taking into account the K_Lot coefficient, with a lot of 0.1 x 1.5 = 0.15 at the price of 1,19990,
while we transfer the take profit for both positions lower since it will be recalculated from the point of total breakeven. Thus, with the opening of each new position, the TP approaches the current price.
if the price went even lower, only now by 11 p, taking into account the step coefficient (10 x 1.1 = 11) 1.19979, we open a new averaging position buy with a lot of 0.15 x 1.5 = 0.225 (0.23 taking into account rounding to 2 digits)
, we also further transfer TP for all three positions
if the price turned around and went up, then everything was closed by TP and we start over again.
If the price goes down, then after opening the 5th averaging position, we put a stop loss below it by 20 points for all open buy positions.