Useful define statements
This tool for MetaTrader 5 is specifically engineered to streamline your trading operations. This script functions as a utility program designed for specific, one-time execution tasks. It is utilized to perform targeted actions, such as closing all open orders, managing chart objects, or executing custom administrative commands on demand.
How to Setup and Use Useful define statements
1. Installation: Move your script file into the MQL/Scripts directory and restart the platform.
2. Execution: Drag the script onto a chart; it will perform a one-time action, such as closing all open orders or clearing chart objects.
3. Editing: Use MetaEditor (F4) to modify code, click "Compile," and verify no errors appear in the terminal before running.
4. Removing: Scripts stop automatically, but you can remove them manually by right-clicking the chart and choosing "Remove Script."
Frequently Asked Questions
Q: How are scripts different from EAs? A: Scripts execute a single action and then stop; EAs monitor the market and trade continuously.
Q: Can I assign a hotkey to a script? A: Yes, right-click the script in the Navigator, select "Set Hotkey," and define your preferred keyboard shortcut.
Q: Why did my script stop? A: Scripts are designed to stop immediately after finishing their programmed command; this is normal behavior.
Description & Settings
Also, you can get the ASK and BID values using MqlTick as I did below, or you can call SymbolInfoDouble(_Symbol,SYMBOL_ASK) if you prefer. It does not make any difference in the value returned.
Finally, arrays are also defined for moving averages, atr, or any additional array you need. Example: to use the Average True Range of candle 1, use ATR(1).IMPORTANT: it is assumed that arrays are AsSeries ( e.g., ArraySetAsSeries(g_MA20_arr, true); ). This is critical in the #define statements shown later.
Once you have defined the statements above, you should not need to change anything else. Additional #define statements that fit your EA could also be added if you need to.
Candle Features
Candles have features that are useful when developing an EA, especially if the EA is price-action based. The names of the following definitions tell what they represent. WICK refers to the top candle's shadow and TAIL the bottom candle's shadow. Example: to get the price at the middle of the last candle's body, use CANDLEBODYMIDDLE(1).
In addition, we define two sizes using the ATR as reference. Example: you want to know how big the candle 4 is with respect to the Atr, use ATRCANDLESIZE(4).
Also, candles that are going up (close>open) are said to have a +1 direction, while candle going down are -1. If close==open, direction = 0. Example: if(CANDLEDIRECTION(10)==1) Print("Candle 10 is an up candle");
Two types of "runs" are defined: UP and DOWN. The runUP is define in a going-up candle as Close-Low, otherwise 0. The runDOWN is defined in a going-down candle as High-Close, otherwise 0; "Runs" are used useful to capture strong movement in the price in one direction. Example, to get the run-up of candle 3, use CANDLERUNUP(3).
Candle Feature Inquiries
These definitions are Boolean variables that tell us the behavior of one or a group of candles. They use previous #define statements to make it short in length.
isCANDLERIGHTDIR(i,dir) will be true if the candle(i) direction is equal to dir, otherwise false;
There are two types of fractals: one using five candles, and another (the weak fractal) using three candles. In the fractal #definitions below the candle(i) is the middle candle having the highest high (TOP) or lowest low (BOT). Make sure there is data for candles i-2,i-1,i,i+1,i+2 for the five-candle fractals. There are also other variations using strict inequality ">" or "<", and using "<=" or ">=". Finally, there are #definitions to identify top (TOP) or bottom (BOT) fractals. Looking at the definitions you can figure out which definition to use.
is3CANDLEGAPUP(i,gap,size) is used to find an UP gap (where the high of a candle is below the low of two candles later). Candle(i) will be the newest of the three candles in question. Again, it is assumed that candles are "AsSeries". "gap" is the minimum price-delta of the gap, and "size" is the minimum price-delta of the middle candle body size.
is3CANDLEGAPDOWN(i,gap,size) is used to find an DOWN gap using the same logic.
is3CANDLEGAPUPTREND(i,gap,size) is the same as is3CANDLEGAPUP(i,gap,size) but add an additional condition to be true: that the oldest of the three candles must be positive direction.
Example: if you want to know if candle 20 is a non-strict (using equalities) fractal top use isCANDLEFRACTALEQTOP(20), and the output will be true or false.
As you can see, the definitions are a compressed form of the inquiries, making your EA code shorter and easier to read.
Math Functions and Operations
We now define some mathematical functions and operations that are useful in the EA. Some of them are specific for EAs I have developed, but you may change them or delete them if you so desire.
BRACKET(x,minV,maxV) will return a value of x inside the interval [minV,maxV]. This is useful to box-constrain input variables in the EA.
CONVEXCOMB(a,x1,x2) is a convex combinations of x1 and x2 as a*x1+x2. This function is useful when computing a intermediate value between x1 and x2, but you want more than just the average (a=0.5).
EVALLINE(x,x1,y1,x2,y2,ymin,ymax) is the evaluation of a straight line define by two points [x1,y1] and [x2,y2]. Once evaluated at x, it returns a bracketed value inside [ymin, ymax].
MAPAB11(x,A,B) maps the value x from a bracket [A,B] to a bracket [-1,1]. MAP11AB(x,A,B) maps the value x from a bracket [-1,1] to a bracket [A,B]. These two functions are useful to work with normalized variables in the range of [-1,1].
The last four functions are used in my EAs and may not necessarily be generically useful for everybody, but I left them there, just in case.
Also, as function and operations, there are calculations of differences (as a proxy for slopes).
MA20DIFF(i,n) gives the difference between the two values of the 20-period moving average separated by n candles. The rest of functions follow the same logic. Example: to compute the difference between the 200-period moving average at candle 3 and at candle 13, use MA200DIFF(3,10).
Inquiries are also included as mathemtical functions. Bracketing and convexity is checked below.
Constants
I define some constants that I use. It is your choice to use them.
Debugging
Any seasoned programmer knows that the best tool for debugging your code is the Print statement. The following definitions allows to set up Print statements all over your program, and turn them ON/OFF depending on the value of the level of debugging you are interested in.
First, you need to define the level of debugging using #define DEBUG_LEVEL0, #define DEBUG_LEVEL1, or #define DEBUG_LEVEL2. When using DEBUG_LEVEL0, there will be no printing on the Journal tab of the Metatrader 5 terminal. With DEBUG_LEVEL1, only the PRINTVARn statements will be active and will print on the Journal tab. With DEBUG_LEVEL2, both, the PRINTVARn and the VPRINTVARn statements will print on the journal tab. The DEBUG_LEVEL2 is the "V"erbose case using VPRINTVARn.
PRINTVARn will print n variables. For instance, if you want to print i, x and z, use PRINTVAR3(i,x,z); In order to print a string, use PRINTTEXT("any string"); The print out will include the function name and the line number of the file where the PRINTVARn statement is inserted.