Discussing the article: "Automating Trading Strategies in MQL5 (Part 7): Building a Grid Trading EA with Dynamic Lot Scaling"

 

Check out the new article: Automating Trading Strategies in MQL5 (Part 7): Building a Grid Trading EA with Dynamic Lot Scaling.

In this article, we build a grid trading expert advisor in MQL5 that uses dynamic lot scaling. We cover the strategy design, code implementation, and backtesting process. Finally, we share key insights and best practices for optimizing the automated trading system.

Grid trading is a systematic approach that places buy and sell orders at predetermined price intervals, allowing traders to capitalize on market fluctuations without requiring precise trend predictions. This strategy benefits from market volatility by continuously opening and closing trades within a defined price range. To enhance its performance, we will integrate dynamic lot scaling, which will adjust position sizes based on predefined conditions, such as account balance, volatility, or previous trade outcomes. Our Grid Trading system will operate with the following key components:

  • Grid Structure – We will define the spacing between orders.
  • Entry and Execution Rules – We will determine when to open grid trades based on fixed distances using a Moving Average indicator strategy.
  • Dynamic Lot Scaling – We will implement an adaptive lot-sizing mechanism that adjusts position sizes based on market conditions or predefined risk parameters.
  • Trade Management – We will incorporate stop-loss, take-profit, and optional breakeven mechanisms to manage risk effectively.
  • Exit Strategy – We will develop logic to close positions based on profit targets, risk limits, or trend reversals.

In a nutshell, here is the whole strategy blueprint visualization for ease of understanding.

GRID LAYOUT

By combining a structured grid system with adaptive lot sizing, we will create an EA that maximizes returns while effectively managing risk. Next, we will implement these concepts in MQL5.

Author: Allan Munene Mutiiria

 
//--- Retrieve recent bar prices for trade signal logic
double low1  = iLow(_Symbol, _Period, 1);
double low2  = iLow(_Symbol, _Period, 2);
double high1 = iHigh(_Symbol, _Period, 1);
double high2 = iHigh(_Symbol, _Period, 2);

these variables are not used... why this code ?

 
testtestmio71 #:

these variables are not used... why this code ?

They are functions to be reused.

 
Allan Munene Mutiiria #:
They are functions to be reused.
on the code ...where you use low1 variabile ?
 
testtestmio71 #:
on the code ...where you use low1 variabile ?

What is the issue with the variables? Any bug? They are functions that can be used anywhere in the code.

If I were to explain further, I would say to get low and high prices for the prior bar and the bar prior + 1.

void ExecuteInitialTrade(double ask, double bid){
   //--- BUY Signal: previous bar's low above MA and bar before that below MA
   if (iLow(_Symbol, _Period, 1) > maData[1] && iLow(_Symbol, _Period, 2) < maData[1]){
      gridSize = ask - gridSize_Spacing;     //--- Set grid trigger below current ask
      TakeProfit = ask + takeProfitPts;      //--- Set TP for BUY
      if(obj_Trade.Buy(LotSize, _Symbol, ask, 0, TakeProfit,"Initial Buy"))
         Print("Initial BUY order executed at ", ask, " with LotSize: ", LotSize);
      else
         Print("Initial BUY order failed at ", ask);
      isTradeAllowed = false;
   }
   //--- SELL Signal: previous bar's high below MA and bar before that above MA
   else if(iHigh(_Symbol, _Period, 1) < maData[1] && iHigh(_Symbol, _Period, 2) > maData[1]){
      gridSize = bid + gridSize_Spacing;     //--- Set grid trigger above current bid
      TakeProfit = bid - takeProfitPts;      //--- Set TP for SELL
      if(obj_Trade.Sell(LotSize, _Symbol, bid, 0, TakeProfit,"Initial Sell"))
         Print("Initial SELL order executed at ", bid, " with LotSize: ", LotSize);
      else
         Print("Initial SELL order failed at ", bid);
      isTradeAllowed = false;
   }
}

Specifically here. You can switch to using the functions or still delete them if you don't want them. Does this clarify? Thanks.

 
double low1  = iLow(_Symbol, _Period, 1);

for example.....for buy signal you used iLow and not Low1 variabile

if (iLow(_Symbol, _Period, 1) > maData[1] && iLow(_Symbol, _Period, 2) < maData[1]){

is only for my study, thanks!!!

 

these four lines can be commented

// double low1  = iLow(_Symbol, _Period, 1);
// double low2  = iLow(_Symbol, _Period, 2);
// double high1 = iHigh(_Symbol, _Period, 1);
// double high2 = iHigh(_Symbol, _Period, 2);
 
testtestmio71 #:

these four lines can be commented

Sure. So is everything okay now?

 

is ok.....best EA .

The 4 lines are confusing for a newbie

 
testtestmio71 #:

is ok.....best EA .

The 4 lines are confusing for a newbie

Okay