Pending Order Grid EA MT4
- Yardımcı programlar
- Francisco Manuel Vicente Berardo
- Sürüm: 1.6
- Güncellendi: 5 Ekim 2024
- Etkinleştirmeler: 20
The Pending Order Grid is a multi-symbol multi-timeframe Expert Advisor that enables multi-strategy implementation based on pending order grids.
General Description
The Pending Order Grid allows the performing of a user-defined strategy through the creation of pending order grids. The Expert Advisor places pending orders of a given type (Buy Limit, Sell Limit, Buy Stop, or Sell Stop) at equidistant price levels to form each grid. The user might set up different grids to exist simultaneously – it's only needed to attach the EA on multiple chart windows of the intended symbol(s).
The start and stop of a grid might be chosen between two modes: price or time. Depending on the selected modes, the trading robot only starts placing pending orders when the current price is "near" the first target price (Start Price) or the Start Time is reached and stops placing when the last target price exceeds the Stop Price or the Stop Time is reached, where Start Price, Start Time, Stop Price and Stop Time are input parameters.
A pending order placing is regulated by a price interval, whose limits are at the Stop Level and Stop Level + Extra Level distances from the order’s target price, where Extra Level is an input parameter. Every time new quotes are available, and the symbol's price is at this interval, the pending order is placed. The target price of the next order is simply given by the previous one added (Sell Limit / Buy Stop) or subtracted (Buy Limit / Sell Stop) with the Price Level input parameter.
An exception to the previous procedure is when the grid’s first pending order is triggered by the Start Time. In this case, the trading robot tries immediately placing the order at the Stop Level distance. In case of rejection by the trade server, it will add 1 pip to the previous distance and try again for the order placing. This procedure will be repeated until the Stop Level + Extra Level distance is reached.
The maximum number of simultaneous pending orders placed by the EA is given by the expression:
- Ceil ( (Stop Level + Extra Level) / Price Level ), for Price Level > 0,
where Ceil represents the usual maths function. The expression may take the following values:
- 1 for Price Level ≥ Stop Level + Extra Level,
- 2 for (Stop Level + Extra Level)/2 ≤ Price Level < Stop Level + Extra Level,
- 3/more for Price Level < (Stop Level + Extra Level)/2.
Risk Management
The volume used to place a pending order is chosen between a fixed and a variable lot size, available through the Volume and Free Margin % input parameters, respectively. If there isn't enough money in the account for the chosen volume, a request for placing the order is still sent to the trade server. The purpose is to allow the corresponding position opening if the free margin increases enough until the target price is reached. This increase could be due to an account deposit or position profit between the placing and triggering of the pending order.
Input Parameters
PENDING ORDER GRID
- Start Price: Price used to define the grid’s start.
- Start Time: Time used to define the grid’s start.
- Stop Price: Price used to define the grid’s stop.
- Stop Time: Time used to define the grid’s stop.
- Price Level: Distance used between pending orders of the same type (pips).
- Extra Level: Distance from the Stop Level, which in turn is a distance from the target price, both represent the limits of price interval where the symbol's price needs to be for the pending order's placing (pips).
PENDING ORDER PLACING
- Magic Number: Expert Advisor’s identifier.
- Type: Pending order type used to form the grid.
- Volume: Lot size per deal (lots).
- Free Margin %: Percentage of account free margin used to calculate the lot size of the current deal (%).
- Stop Loss: Distance from the pending order's target price for placing a Stop Loss (pips).
- Take Profit: Distance from the pending order's target price for placing a Take Profit (pips).
- Deviation: Maximum allowed slippage from the requested price (pips).
- Expiration Time: Order validity period.
- Comment: Text message displayed in the chart window (to which the EA is attached) after a(n) (re)initialization of the EA, in the Objects list after creating a horizontal/vertical line (at each of the grid’s limits) or placing a pending order, and in the Trade or Account History tabs of the Terminal window after placing a pending order (it only allows 31 characters).
POSITION MODIFYING
- Trailing Stop – SL: Distance from the market price for placing a Stop Loss after a favourable price movement (pips).
- Trailing Start – SL: Distance from the position’s opening price that must be reached for the “Trailing Stop – SL” function’s activation (pips).
- Trailing Step – SL: Distance from the price where the previous Stop Loss modification occurred that must be reached before the placing of a new Stop Loss (pips).
- Trailing Stop – TP: Distance from the market price for placing a Take Profit after an unfavourable price movement (pips).
- Trailing Start – TP: Distance from the position’s opening price that must be reached for the “Trailing Stop – TP” function’s activation (pips).
- Trailing Step – TP: Distance from the price where the previous Take Profit modification occurred that must be reached before the placing of a new Take Profit (pips).
OPTIMIZATION CRITERION
- Math Expression: Mathematical expression used to calculate a custom statistic parameter to sort the optimization results (see the Optimization Criterion section below).
Some of the available parameters accept values that lead to particular options.
- Start Price or Stop Price: A null/negative value means the parameter’s inactive function.
- Start Time or Stop Time: A value before the current time means the parameter’s inactive function.
- Price Level: A null value means that the target price is constant for all pending orders.
- Extra Level: A null value means that the symbol’s price needs to be exactly at the Stop Level distance from the target price for the pending order's placing.
- Volume: A value lower than the minimum allowed volume by the broker is converted in this last. A value higher than the maximum available volume by the free margin is converted in this last.
- Free Margin %: A value whose volume doesn’t reach the minimum allowed volume by the broker is converted in this last. A value whose volume exceeds the maximum available volume by the free margin is converted in this last.
- Stop Loss, Take Profit, Trailing Stop – SL or Trailing Stop – TP: A null value means the parameter’s inactive function. Any value between 0 and the Stop Level is converted in this last.
- Trailing Start – SL or Trailing Start – TP: A null value means the “Trailing Stop – SL” or “Trailing Stop – TP” function’s immediate activation, respectively.
- Trailing Start – SL: The spread value means the “Trailing Stop – SL” function’s activation at breakeven, although such hasn't been guaranteed. The spread + “Trailing Stop – SL” values mean the “Trailing Stop – SL” function’s activation in a profit where breakeven has been guaranteed.
- Trailing Step – SL or Trailing Step – TP: A null value means the “Trailing Stop – SL” or “Trailing Stop – TP” function’s continuous operation, respectively.
The input parameters that define the limits of the grid must obey a few rules.
- Both the start and stop of the grid must be selected, each chosen between a price or time.
- Any combination between the start and stop of the grid can be used: Start Price + Stop Price, Start Price + Stop Time, Start Time + Stop Price and Start Time + Stop Time.
- The Start Price cannot exceed the Stop Price in the Sell Limit or Buy Stop orders grid.
- The Stop Price cannot exceed the Start Price in the Buy Limit or Sell Stop orders grid.
- The Start Time cannot exceed the Stop Time in any pending order grid.
Optimization Criterion
The Expert Advisor allows the creation and use of a new statistical parameter (besides available ones) to sort the optimization results. This custom statistic parameter results from a mathematical expression calculated after the testing. The expression must obey syntax rules and precedence order, being constituted by the following elements:
- Integer and real numbers.
- Statistic parameters.
- Mathematical operators for addition (+), subtraction (-), multiplication (*), division (/) and exponentiation (^).
- Mathematical and trigonometric functions.
- Curved parentheses (()) to define the precedence and contain the function’s argument(s).
- Full stop (.) as decimal point and comma (,) as function’s arguments separator.
The statistical parameters are used by writing the respective identifier initial(s) after the term “STAT”. In case there are two identifiers with the same initial(s), it must also be added “1” or “2”, depending on the order in which both appear in the list. For instance, “STAT_PROFIT” and “STAT_MAX_CONLOSS_TRADES” would be “P” and “MCT2”, respectively. List of identifiers, without “STAT_”, whose initial(s) require “1” or “2”:
CONPROFITMAX (C1), CONPROFITMAX_TRADES (CT1), MAX_CONWINS (MC1), MAX_CONPROFIT_TRADES (MCT1), CONLOSSMAX (C2), CONLOSSMAX_TRADES (CT2), MAX_CONLOSSES (MC2), MAX_CONLOSS_TRADES (MCT2), EQUITYDD_PERCENT (EP1), EXPECTED_PAYOFF (EP2), LOSS_TRADES (LT1), LONG_TRADES (LT2).
The mathematical/trigonometric functions are used by writing the respective name after “Math” and one or two arguments inside parentheses, separated by a comma in this last case. For instance, “MathLog10()” and “MathPow()” would be “Log10(argument)” and “Pow(argument1,argument2)”, respectively. List of names that correspond to the available functions:
Abs, Arccos, Arcsin, Arctan, Ceil, Cos, Exp, Floor, Log, Log10, Max, Min, Mod, Pow, Rand, Round, Sin, Sqrt, Tan.
Note: “MathRand()” is only executed with “GetTickCount()” as the argument of “MathSrand()”, it’s used without anything inside parentheses – simply writing “Rand()”.
Additionally, the expression has the following properties:
- The scientific, engineering and E notations are allowed.
- The multiplication needs to be explicitly indicated (through the respective symbol).
- The system is case-insensitive.
- The space ( ) is allowed and doesn’t affect the expression’s calculation.
- The input expression is limited to 511 characters.
Examples of a number representation using various notations: “0.0000325” (decimal), “3.25*10^-5” (scientific), “32.5*10^-6” (engineering) and “32.5E-6” (E).
IMPORTANT! The EA doesn’t verify if the input expression fulfils all the requirements, namely if it obeys syntax/standard rules, hence, any infringement of these leads to an unreliable result.
Displayed Information
The Expert Advisor possesses a vast number of messages to inform the user about errors and conditions changes that might occur during its operation. The messages are shown through the Alert function (by a pop-up window), its content includes:
- The warning that an input parameter has been incorrectly set.
- The info that the account doesn't have enough money for the chosen volume (see the Risk Management section above).
- The info that the number of permitted orders by the broker has been reached.
- The previous and current value of the symbol’s Stop Level when this is updated.
- The Trade Server Return Codes description.
- The symbol’s quotes (immediately) before the trade request’s formation, followed by the symbol’s quotes (immediately) after the trade server’s decision.
- The Runtime Errors description.
- The standard function in the include file where the runtime error was detected (only relevant to the programmer).
- The Uninitialization Reason Codes description.
Note: Some elements of the list are displayed simultaneously (in the same text line): 5 and 6; 7 and 8.
During the EA’s operation, the messages displayed are grouped by kind of occurrence (related to each list’s element, except the 1, 6 and 8) and counted. Immediately before the EA’s unloading, a final message containing those groups with the respective counts (if these were > 0) is presented.
Following the EA's (re)initialization, two reference lines are displayed in the chart at the grid’s limits (start and stop). Each line is horizontal/vertical when the grid’s limit is the price/time, respectively. Both lines are blue/red when the grid’s pending order type is Buy/Sell, respectively. Tip: Place the mouse pointer over one of those lines to see its object name ("Buy/Sell Limit/Stop – Start/Stop Price/Time") and description (the Comment input parameter). Note: The properties of a graphical object can be edited in the Objects list.
After the EA’s testing/optimization, the result of the mathematical expression, inserted in the Math Expression input parameter, is presented in the Journal / Optimization Results tabs of the Tester window, respectively. After the EA’s testing, the values of the available statistic parameters are also presented in the Journal tab.
Observations
In some cases, the quoting session might start before or end later than the corresponding trading session (with a five-minute difference, for instance). During the time interval when the quoting session is open, but the trading session is still/already closed, the Expert Advisor initiates/continues to process the available ticks, respectively. If the present conditions satisfy the EA's trading criteria, a trade request is formed and sent to the server. However, it won’t succeed, and an error message is displayed: “market is closed”.
During high activity periods, the trade server’s decision on whether a trade request is executed or rejected may suffer significant delays. Some data used in the request sent to the server might become incorrect, leading to the order’s rejection. When the server is evaluating a request and the symbol's quotes are updated, three cases might occur:
- Pending order placing – the pending order’s target price becomes an incorrect distance.
- Position opening/modifying – the position's Stop Loss or Take Profit intended level becomes an incorrect distance.
- Position modifying – the position's Stop Loss or Take Profit previous level takes to its closing.
The symbol’s quotes mentioned in the sixth element of the list in the Displayed Information section are especially useful here (since firsts usually differ from lasts). A careful analysis of these quotes, knowing the implication that certain quote changes have on the request’s evaluation, permits understanding the reason when these cases occur. To avoid the request’s rejection by the trade server due to “invalid stops” (cases 1 and 2), the prices/levels used should exceed the symbol’s Stop Level by a few pips.
The Start Price, Start Time, Stop Price, Stop Time or Type input parameters have a special status in the program. Every time one is changed, two things happen: the EA ends up the current grid and verifies the conditions to create a new one. Then, if all start/stop values pass this checking, the EA begins a new grid. But, if at least one fails, the EA doesn’t go forward with the new grid and terminates its operation.
A Buy/Sell position is opened at the Ask/Bid price and closed at the Bid/Ask price. Since the position’s stop orders (Stop Loss and Take Profit) are triggered at this last price, in a pending order, they are calculated from the order's target price -/+ the current spread (Ask - Bid), respectively.
When placing a pending order, the validity period can’t be less than 10 minutes. During a grid creation, the Expert Advisor doesn’t place pending orders if the current time exceeds the Expiration Time minus 10 minutes (when the validity period is previously selected).
Conclusion
The Pending Order Grid is a helpful and effective tool regarding the automatic creation of pending order grids, especially when the grids consist of a significant number of orders, enabling a simple and intuitive setting of the grids to form.