Mathematical Models in Grid Strategies
Introduction
In the world of trading, there are many trading strategies, each with its own characteristics, advantages, and disadvantages. Among this variety, the grid strategy holds a special place, as it is suitable for both beginners and experienced traders.
This strategy is a specific approach to trading in financial markets, in which traders place buy or sell orders at predetermined intervals, forming a sort of "grid" above or below the current price. The main goal of the grid strategy is to profit from market volatility. Its essence lies in taking advantage of price fluctuations within a specific range by opening and closing positions at predetermined levels.
To successfully implement a grid strategy, it is necessary to carefully analyze the market situation and determine the optimal grid parameters. In this article, we will take a detailed look at the basic principles of the grid strategy, as well as its advantages and disadvantages. We will delve into the mathematical calculations underlying it, analyze various approaches to grid construction, and explore ways to optimize the grid strategy to maximize profits.
A Simple Grid
The classic grid strategy looks very simple. The trader specifies the number of orders in the grid and the distance between their opening prices. At the right moment, the trader opens the grid of orders and waits for well-deserved profit. As the saying goes, everything genious is simple. But we want something more complex. The challenges begin when the professionals get involved. Let us show our professionalism as well and build a simple grid of two orders.
So, let's take a look at the general logic behind opening a grid. All my signals suggest that the price is about to go up. I trust these signals and open a buy position. But the price rarely moves directly toward the target. And then a brilliant idea pops into my head — to place a Buy Limit order as well. My reasoning is this: if the price drops and this order is triggered, I will make more profit when the price subsequently rises.
And this raises the question for me: at what distance from the position's opening price should I place this order? To find the answer, I can use the Strategy Tester. But this approach will take some time. Let's try to answer this question using mathematics.
So, the pending order was triggered, and I now have two open positions. Their total profit can be estimated using the following equation:
![]()
From this equation, I can find the price at which the total profit on the positions will be 0.

As you can see, the breakeven price depends on the volumes of the positions and their opening prices. I will add my desired profit in points to this price, and that will give me the price level at which I'll close both positions.

In order to find the optimal distance between opening prices, these equations need to be slightly modified. I can express the volume of the pending order as follows:
![]()
Here, step is the volume increment, which can be obtained using the SymbolInfoDouble function. I will also rewrite the order's opening price. It can be expressed as the difference between the position's opening price and the distance:
![]()
Now the most interesting part begins. I decided to impose some restrictions on my strategy:
- First, I want the profit target to be reached as quickly as possible (the market is unpredictable: right now the price is moving in the direction I want, but in a second or two it might reverse);
- Second, I want the distance between opening prices to be as large as possible (that way, I can make more profit).
If I combine these conditions, I get this elegant extremum:
![]()
This condition is true only if the following equality holds:

The distance between opening prices is directly proportional to profit — the distance will change by the same factor as the profit. However, the relationship between this distance and the volumes is nonlinear — the larger N is, the closer the distance approaches half the profit.

The formula we've come up with is more general. We have four variables: Lot, N, Profit, and Distance. They are all related by the following equation:
![]()
By setting the values of any three variables, we can easily find the value of the fourth. However, we need to keep in mind that there are certain limitations. Thus, the variable N must be a non-negative integer. The restrictions on the Lot and Distance variables are well described in the article "What Tests Should a Trading Robot Undergo…". Suppose I need to find the optimal value of the variable N. I substitute all the known values into the equation and calculate N. After that, I need to round it to the nearest integer value. This rounding may violate the optimality condition. To avoid this, I will substitute N into the equation and recalculate the value of the Profit variable — it must be positive, and no other constraints apply to it.
Here's an example of how this strategy is used.

It's not particularly important to us right now just how profitable the strategy turned out to be. For us, it is more important to determine whether the distance between opening prices that we calculated was optimal, because, at least in some situations, deviating from this distance by even 5 points can worsen the results.
Making the Grid More Complex
We've covered the simple grid. Now let's try building a grid with two pending orders. The basic idea of the strategy has not changed. First, I open a position, and then I place pending orders. With two orders, I can make more profit when the price reverses direction (if it does). It would seem that there are no complications in sight. But in this case, my approach to the second order will change.
When building this kind of grid, I may encounter the following situations:
- the price will immediately move in the direction I need, the position will yield the target profit, and I will close the position and cancel the pending orders;
- the price will initially move in the opposite direction, the first pending order will be triggered; after that, the price will reverse, and I will make a profit from two positions;
- After the first order is triggered, the price will continue moving and trigger the second order; after that, all I have to do is wait for the price to reverse so I can make a profit from all positions.
The first case is trivial. We've already discussed the second one above. Right now, we are only interested in the third option. I will have several new variables. I want to be able to freely choose the volume of the second order and the distance between opening prices. All other optimality requirements will remain the same: reaching the position closing price as quickly as possible and achieving the maximum possible distance.
The equation relating all the variables will look like this:

Adding another pending order helped improve the strategy's results. We're on the right track.

Let's make this strategy a little more complex and place more pending orders. At the same time, we will add another complication: the profit when a particular order is triggered can be arbitrary.
Let lot[0] be the position volume, and lot[i] be the order volumes. Dist[i] represents the corresponding distances between opening prices. In that case, the optimal distance for a given order can be calculated using the following formula:

If the resulting value does not satisfy certain constraints, then we need to adjust the distance. After that, we need to recalculate the optimal profit value:

An increase in the number of pending orders can either boost the strategy's profitability or result in significant losses.

Now it is time to discuss the main advantages and disadvantages of grid strategies.
Advantages
- Distributing orders across a defined range reduces the risk of losses. Even if the price moves against the trader, there is always the opportunity to close positions at a profit at other levels.
- A grid strategy can be used in various situations — both when the price is rising and when it is falling.
- Such strategies make it possible to generate profits in unstable, highly volatile market conditions.
Disadvantages
- A grid strategy requires very good signals. Regardless of whether the trader is trading based on the trend or on volatility, they must have a sufficient degree of confidence in how the market will develop in the future.
- To effectively implement this strategy, a trader may need a substantial capital to cover potential losses and maintain open positions.
- If the price keeps moving in one direction, it can lead to significant losses that may be extremely difficult to recoup.
- When many positions are open, managing them can become a complex task that requires additional rules for managing positions and orders.
Some shortcomings can be overcome relatively easily. For example, a properly chosen loss limit can reduce the burden on the deposit and improve trading results.

Preliminary conclusion: grid strategies are suitable for both novice and experienced traders. Novice traders will be able to gain a better understanding of the characteristics of price movement, while experienced traders can adapt these strategies to their own needs and fully leverage their potential.
Conclusion
There are many different trading strategies. However, grid strategies stand out for their versatility and ability to adapt to various market conditions. As shown in this article, this approach, based on placing orders at specified intervals, opens up new opportunities for the trader.
This article demonstrates how mathematical calculations can be used to optimize a grid strategy. Using simple mathematical calculations, we can determine breakeven points, as well as the optimal distances between orders and the volumes of orders and positions. Mathematical models enable us to make more informed decisions and improve the effectiveness of the strategy.
Grid strategies can be scaled — from a simple grid of two orders to more complex systems with multiple orders and any desired profit when those orders are triggered. The article clearly demonstrates how a strategy can be made more complex and improved by adapting it to various price movement scenarios and objectives.
It is important to remember that the effectiveness of a grid strategy depends directly on the quality of market signals, the size of the deposit, and the ability to manage positions. Thorough analysis and sound capital and risk management are essential components of successfully implementing this strategy.
A grid strategy can be a powerful tool in a trader's arsenal. Not only does it allow you to profit from market fluctuations, but it also promotes a deeper understanding of price formation dynamics. Success in applying a grid strategy lies in striking a balance between mathematical precision, market intuition, and a disciplined approach to risk management.
The article includes the following files.| Title | Type | Features |
|---|---|---|
| Distance | script | Shows how the distance between opening prices depends on the selected parameters.
|
| EA Double Grid | Expert Advisor | The Expert Advisor sets up a simple grid consisting of 1 open position and 1 pending order. The direction of the grid is selected at random.
|
| EA Triple Grid | Expert Advisor | A grid with 2 orders. |
| EA Complex Grid | Expert Advisor | A grid with 4 orders. |
| EA Complex Grid SL | Expert Advisor | A grid with a loss limit.
|
Translated from Russian by MetaQuotes Ltd.
Original article: https://www.mql5.com/ru/articles/20018
Warning: All rights to these materials are reserved by MetaQuotes Ltd. Copying or reprinting of these materials in whole or in part is prohibited.
This article was written by a user of the site and reflects their personal views. MetaQuotes Ltd is not responsible for the accuracy of the information presented, nor for any consequences resulting from the use of the solutions, strategies or recommendations described.
Streaming MetaTrader 5 Trade Events to a Local HTTP Server Using WinINet in MQL5
Building a Modular Fair Value Gap (FVG) Detection Engine in MQL5
Building a Divergence System (Part III): The Adaptive SuperTrend EA
Automating Trading Strategies in MQL5 (Part 51): The Bread and Butter Judas Swing Model with Premium and Discount
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use