Useful #define Statements - MetaTrader 5 Scripts | Trading Script Downloads - MT4/MT5 Resources
// Candles (define g_rates as: MqlRates g_rates[];) #define CANDLELOW(i) g_rates[i].low #define CANDLEHIGH(i) g_rates[i].high #define CANDLEOPEN(i) g_rates[i].open #define CANDLECLOSE(i) g_rates[i].close #define CANDLETIME(i) g_rates[i].time
Alternatively, you can use MqlTick to get the ASK and BID values as I do below, or you can call SymbolInfoDouble(_Symbol,SYMBOL_ASK) if you prefer. It has no effect on the returned value.
// Price (define g_tick as: MqlTick g_tick;)) #Define ask g_tick.ask #Define bid g_tick.bid
Finally, there are arrays defined for moving averages, atr, or any other array you need. Example: To use the average true range of candle 1, use ATR(1).
IMPORTANT: Assume the array is AsSeries (e.g., arraySet as Series (g_MA20_arr, true); ). This is crucial in the #define statement shown later.
// Define these arrays as: double g_MA20_arr[]; // Moving average and ATR #define MOVAVG20(i) g_MA20_arr[i] #define MOVAVG40(i) g_MA40_arr[i] #define MOVAVG50(i) g_MA50_arr[i] #define MOVAVG200(i) g_MA200_arr[i] #define ATR(i) g_atr_arr[i]
Once the above statement is defined, you don't need to change anything else. If necessary, you can also add other #define statements suitable for your EA.
Candlestick charts have very useful features when developing EAs, especially when the EA is based on price action. The names defined below explain what they represent. WICK refers to the shadow of the top candle, and TAIL refers to the shadow of the bottom candle. Example: To get the price in the middle of the last candle body, use CANDLEBODYMIDDLE(1).
//Candle characteristics #define maximum frequency of candle body top(i) (candle open(i), candle close(i)) #define candle body robot(i) minimum frequency (candle open(i), candle close(i)) #define candle median(i) ( 0.5 *(candle high(i)+candle low(i)))) #define candle weighting(i) ( 0.25 *(candle high(i)+candle low(i) +2 ) *Candle Close(i))) #Define Candlestick Typical (i) ( 1 ./3 .*(Candle High(i)+Candle Low(i)+Candle Close(i))) #Define Candle Body(i) ( 0.5 *(CANDLEBODYTOP(i)+CANDLEBODYBOT(i))) #Define Candle Size(i) (Candle High(i)-Candle Low(i)) #define candle body size(i) fab (candle bodyBOT(i)-candle open(i)) #define CANDLEWICKSIZE(i) (CANDLEHIGH(i)-CANDLEBODYTOP(i)) #define CANDLETAILSIZE(i) (CandlebodyBOT(i)-CANDLELOW(i))
In addition, we define two sizes using ATR as a reference. Example: You want to know how big candle 4 is relative to Atr, use ATRCANDLESIZE(4).
#define ATRCANDLESIZE(i) (candle size(i)/ATR(i)) #define ATTR candle body size(i) (candle body size(i)/ATR(i))
Furthermore, rising candles (Close > Open) are said to have a +1 direction, while falling candles have a -1 direction. If closing price == opening price, direction = 0. Example: if(CANDLEDIRECTION(10)==1) Print("Candle 10 is an up candle");
#define candle direction(i) (candle close(i)>candle open(i)? 1 :(candle close(i)<candle open(i)?- 1 : 0 ))
Two types of "runs" are defined: up and down. runUP is defined as the closing low in rising candles, otherwise it is 0. runDOWN is defined as the high close in a descending candle, 0 otherwise; "run" can be used to capture strong price movements in one direction. For example, to get the start of candle 3, use CANDLERUNUP(3).
#define CANDLERUNUP(i) ((CANDLECLOSE(i)>KANDLEOPEN(i))?(CANDLECLOSE(i)-CANDLELOW(i)): 0 ) #define CANDLERUNDOWN(i) ((CANDLECLOSE(i)0)
Candle function query
These definitions are Boolean variables that tell us the behavior of a candle or a group of candles. They use the previous #define statement to shorten the length.
If the candle(i) direction is equal to dir, isCADLERIGHTDIR(i,dir) will be true, otherwise it will be false;
There are two types of fractals: one uses five candles and the other (weak fractal) uses three candles. In fractal #definitions, below candle (i) is the middle candle with either the highest high (TOP) or the lowest low (BOT). Make sure that the five-candle fractal has data for candles i-2,i-1,i,i+1,i+2. There are other variations using strict inequalities "">" or "<" and using "<=" or "">=". Finally, there are #definitions to identify top (TOP) or bottom (BOT) fractals. Looking at the definitions you can find out which one to use.
is3CANDLEGAPUP(i,gap,size) is used to find upward gaps (where the high of one candle is lower than the low of the following two candles). Candle(i) will be the latest of the three candles involved. Assume again that the candle is "AsSeries". "Gap" is the minimum price increment of the gap and "Size" is the minimum price increment of the intermediate candlestick size.
is3CANDLEGAPDOWN(i,gap,size) is used to find downward gaps using the same logic.
is3CANDLEGAPUPTREND(i,gap,size) is the same as is3CANDLEGAPUP(i,gap,size) but adds an additional condition to be true: the earliest of the three candles must be in the positive direction.
There are two ways to query whether a candle is a doji: isCANDLEDOJIPOINTS(i,n) and isCANDLEDOJIFRACTION(i,f). The first version uses n*_Point and the second version uses f*CANDLESIZE(i) to determine whether the candle (true or false) is a doji.
Example: If you want to know if candle 20 is a non-strict (using equation) fractal top, use isCANDLEFRACTALEQTOP(20), the output will be true or false.
As you can see, definitions are condensed forms of queries, making your EA code shorter and easier to read.
// Candlestick feature query (Boolean value) #define isCANDLEUP(i) (CANDLEDIRECTION(i)== 1 ) #define isCANDLEOWN(i) (CANDLEDIRECTION(i)==- 1 ) #define isCANDLEFLAT(i) (CANDLEDIRECTION(i)== 0 ) #define isCANDLEWICKLESS(i) (CANDLEWICKSIZE(i)== 0 ) #define isCANDLETAILLESS(i) (CANDLETAILSIZE(i)== 0 ) #define isCANDLESOLID(i) (CANDLEWICKSIZE(i)== 0 && candle tail size(i)== 0 ) #define isCANDLERIGHTDIR(i,dir) (dir*(CANDLECLOSE(i) - CANDLOPEN(i))> 0 ) #define isCANDLEFRACTALTOP(i) (CANDLEHIGH(i) > CANDLEHIGH(i- 1 ) && CANDLEHIGH(i- 1 ) > candle high(i- 2 ) && CANDLEHIGH(i) > CANDLEHIGH(i+ 1 ) && candle high(i+ 1 ) > candle high(i+ 2 )) #Define isCANDLEFRACTALBOT(i) (CANDLELOW(i) < CANDLELOW(i- 1 ) && CANDLELOW(i- 1 ) < CANDLELOW(i- 2 ) && CANDLELOW(i) < CANDLELOW(i+ 1 ) && CANDLELOW(i+ 1 ) < CANDLELOW(i+ 2 )) #Define isCANDLEFRACTALEQTOP(i) (CANDLEHIGH(i) >= CANDLEHIGH(i- 1 ) && CANDLEHIGH(i- 1 ) >= CANDLEHIGH(i- 2 ) && CANDLEHIGH(i) >= CANDLEHIGH(i+ 1 ) && candle high(i+ 1 ) >= candle high(i+ 2 )) #define isCANDLEFRACTALEQBOT(i) (CANDLELOW(i) <= CANDLELOW(i- 1 ) && CANDLELOW(i- 1 ) <= CANDLELOW(i- 2 ) && CANDLELOW(i) <= CANDLELOW(i+ 1 ) && CANDLELOW(i+ 1 ) <= CANDLELOW(i+ 2 )) #define isCANDLEWEAKFRACTALTOP(i) (CANDLEHIGH(i) > CANDLEHIGH(i- 1 ) && CANDLEHIGH(i) > CANDLEHIGH(i+ 1 )) #define isCANDLEWEAKFRACTALBOT(i) (CANDLELOW(i) < CANDLELOW(i- 1 ) && CANDLELOW(i) < CANDLELOW(i+ 1 )) #define isCANDLEWEAKFRACTALQBOT(i) (CANDLELOW(i) <= CANDLELOW(i-) 1 ) && CANDLELOW(i) <= CANDLELOW(i+ 1 )) #define isCANDLEWEAKFRACTALQTOP(i) (CANDLEHIGH(i) >= CANDLEHIGH(i- 1 ) && CANDLEHIGH(i) >= CANDLEHIGH(i+ 1 )) #define is3CANDLEGAPUP(i,gap,size) (CANDLELOW(i)-CANDLEHIGH(i+ 2 )>gap && CANDLEBODYSIZE(i+ 1 )>=size) #define is3CANDLEGAPDOWN(i,gap,size) (CANDLELOW(i+ 2 )-CANDLEHIGH(i)>gap && CANDLEBODYSIZE(i+ 1 )>= size) #define is3CANDLEGAPUPTREND(i,gap,size) (CANDLELOW(i)-CANDLEHIGH(i+ 2 )>gap&& CANDLEBODYSIZE(i+ 1 )>=size&& isCANDLEUP(i+ 2 )) #define is3CANDLEGAPDOWNTREND(i,gap,size) (CANDLELOW(i+ 2 )-CANDLEHIGH(i)>gap && CANDLEBODYSIZE(i+ 1 )>= size && isCANDLEDOWN(i+ 2 )) #define isCANDLEDOJIPOINTS(i,n) (CANDLEBODYSIZE(i) <= n* _view ) #define isCANDLEDOJIFRACTION(i,f) (CANDLEBODYSIZE(i) <= f*CANDLESIZE(i))
We now define some mathematical functions and operations useful in EA. Some of them are specific to the EA I developed, but you can change or delete them if you wish.
BRACKET(x,minV,maxV) will return x values in the interval [minV,maxV]. This is useful for box constraint input variables in EAs.
CONVEXCOMB(a,x1,x2) is the convex combination of x1 and x2, that is, a*x1+x2. This function is useful when calculating intermediate values between x1 and x2, but you need more than just the average (a=0.5).
EVALLINE(x,x1,y1,x2,y2,ymin,ymax) is the evaluation of a straight line defined by two points [x1,y1] and [x2,y2]. Once evaluated at x, it returns the value in parentheses within [ymin, ymax].
MAPAB11(x,A,B) maps the value x from the bracket [A,B] to the bracket [-1,1]. MAP11AB(x,A,B) maps the value x from brackets [-1,1] to brackets [A,B]. These two functions are useful for working with standardized variables in the range [-1,1].
The last four functions are used in my EA and may not necessarily be useful to everyone, but I leave them there just in case.
#Define brackets (x,minV,maxV) (xmaxV?maxV:x)) #define convex comb (a,x1,x2) (a*x1+( 1.0 -a)*x2) #define EVALLINE(x,x1,y1,x2,y2,ymin,ymax) brackets ((y2-y1)/(x2-x1)*(x-x1)+y1,ymin,ymax) #define MAPAB11(x,A,B) ( 2 ./(BA)*(brackets (x,A,B)-A)- 1 .) #define MAP11AB(x,A,B) ((BA)/ 2 .*(brackets (x,- 1 , 1 )- 1 )+B) #define SIGMOID(x,a) ( 1.0 /( 1.0 + experience value (-axe))) #define NN1(x,w,b) (w*x+b) #define EVALPOLY(X,X0,X1,Y0,Y1,EX,ymin,ymax) Bracket(Y1* POW ((X-X0)/(X1-X0),EX)+Y0,ymin,ymax) #define EVALPOLY2P(X,X0,X1,Y0,Y1,EX,ymn,ymx) bracket((Y1-Y0)* POW ((X-X0)/(X1-X0),EX)+Y0,ymn,ymx)
Additionally, there is a calculation of the difference (as a proxy for slope) as a function and operation.
MA20DIFF(i,n) gives the difference between two values of a 20-period moving average separated by n candles. The rest of the functions follow the same logic. Example: To calculate the difference between the 200-period moving average of the 3rd candle and the 13th candle, use MA200DIFF(3,10).
#define MA20DIFF(i,n) (MOVAVG20(i)-MOVAVG20(i+n)) #define MA40DIFF(i,n) (MOVAVG40(i)-MOVAVG40(i+n)) #define MA50DIFF(i,n) (MOVAVG50(i)-MOVAVG50(i+n)) #define MA200DIFF(i,n) (MOVAVG200(i)-MOVAVG200(i+n)) #define CANDLECLOSEDIFF(i,n) (CANDLECLOSE(i)-CANDLECLOSE(i+n)) #define CANDLEOPENDIFF(i,n) (KANDLEOPEN(i)-KANDLEOPEN(i+n)) #define CANDLEHIGHDIFF(i,n) (CANDLEHIGH(i)-CANDLEHIGH(i+n)) #define CANDLELOWDIFF(i,n) (CANDLELOW(i)-CANDLELOW(i+n)) #define CANDLEMEDIANDIFF(i,n) (CANDLEMEDIAN( i )-CANDLEMEDIAN(i+n)) #Define CANDLETYPICALDIFF(i,n) (CANDLEBODYMIDDLE(i)-CANDLEBODYMIDDLE(i+n)) #Define CANDLETYPICALDIFF(i,n) (CANDLETYPICAL(i)-CANDLETYPICAL(i+n))
Queries are also included in mathematical functions. Next check the bounding and convexity.
#define isINBRACKET(x,minV,maxV) (x < =maxV && x> =minimum V) #define isINBRACKETSTRICT(x,minV,maxV ) (x <maxvoltage && ( x>maximum V || #define isCONCAVESTRICT (yl, yc , yr) ( ylYear)
constant
I have defined some constants that I use. Using them is your choice.
// constant #define PIVALUE ( M_PI ) #define MINSTOPINTS ( 30 ) #define minimum freeze point ( 30 ) #define stop level ( max frequency (MINSTOPINTS, ( double ) symbol information integer ( _symbol , SYMBOL_TRADE_STOPS_LEVEL ))* _view ) #define freeze level ( max frequency (min freeze point, ( double ) symbol information integer ( _symbol , SYMBOL_TRADE_FREEZE_LEVEL ))* _view )
Any experienced programmer knows that the best tool for debugging code is the Print statement. The following definition allows you to set Print statements throughout your program and turn them on/off based on the value of the debug level you are interested in.
First, you need to define the debug level 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. and DEBUG_LEVEL1, only the PRINTVARn statement will be active and will be printed on the Log tab. and DEBUG_LEVEL2, both, PRINT argument and VPRINT argument reports will be printed on the Journal tab. This DEBUG_LEVEL2 is the "V"erbose case VARn using VPRINT.
PRINTVARn will print n variables. For example, if you want to print i, x, and z, use PRINTVAR3(i,x,z); to print a string, use PRINTTEXT("any string"); The printout will include the function name and the line number of the file where the PRINTVARn statement was inserted.
// Use printing for debugging // Define the debug level x={0,1,2} at the beginning of the code as: #define DEBUG_LEVELx #ifdef DEBUG_LEVEL0 #define print text (text) #define print variable(x1) #define print variable1(x1) #define PRINTVAR2(x1,x2) #define PRINTVAR3(x1,x2,x3) #define PRINTVAR4(x1, x2,x3,x4) #define PRINTVAR5 (x1,x2,x3,x4,x5) #define PRINTVAR6(x1,x2,x3,x4,x5,x6) #define VPRINTTEXT (text) #define VPRINTVAR(x1) #define VPRINTVAR1(x1) #define VPRINTVAR2(x1,x2) #define VPRINTVAR3(x1,x2,x3) #define VPRINTVAR4(x1,x2,x3,x4) #define VPRINTVAR5(x1,x2,x3,x4,x5) #define VPRINTVAR6(x1,x2,x3,x4,x5,x6) #endif #ifdef debug_level 1 #define print text (text) print ( "*/*/*/*" , __function__ , "(" , __line__ , "): " , text) #define print variable(x1) print ( "*/*/*/*" , __function__ , "(" , __line__ , "): " , #x1 + "= , (x1)) #define PRINTVAR2(x1,x2) print ( "*/*/*/*" , __function__ , "(" , __line__ , "): ", #x1 + "= , (x1)) #define PRINTVAR2(x1,x2) print("*/*/*/*", __function__, "(", __line__, "): " , # x1 + ” = , (x1), “,” #x2 + ”= , (x2)) #define PRINTVAR3(x1,x2,x3) print( “*/*/*/* ” , __function__ , “ ( ” , __line__ , “ ) : “ , # x1 + ” = , ( x1 ) , “,” #x2 + ” = , (x2), “,” #x3 + ”= , (x3)) #define PRINTVAR4(x1 , x2 ,x3,x4 ) print ( “*/*/*/*” , __function__ , “(” , __line__ , ”): “ , # x1 + ”= , (x1), “, ” #x2 + ” = , (x2), “,” #x3 + ”= , (x3), "" #x3 + ”= , (x3), “,”cessor">#x4 + ”= , (x4), “,” #x5 + ”= , (x5)) #define PRINTVAR6(x1,x2,x3,x4,x5,x6) print ( “*/*/*/*” , __function__ , “(” , __line__ , ”): “ , #x1 + ”= , (x1), “,” #x2 + ”= , (x2), “,” #x3 + ”= , (x3), “,” #x4 + ”= , (x4), “,” #x5 + ”= , (x5), “,” #x6 + ”= , (x6)) #define VPRINTTEXT (text) #define VPRINTVAR(x1) #define VPRINTVAR1(x1) #define VPRINTVAR2(x1,x2) #define VPRINTVAR3(x1,x2,x3) #define VPRINTVAR4(x1,x2,x3,x4) #define VPRINTVAR5(x1,x2,x3,x4,x5) #define VPRINTVAR6(x1,x2,x3,x4,x5,x6) #endif #ifdef debug_level 2 #define print text (text) print ( "*/*/*/*" , __function__ , "(" , __line__ , "): " , text) #define print variable(x1) print ( "*/*/*/*" , __function__ , "(" , __line__ , "): " , #x1 + "= , (x1)) #define PRINTVAR2(x1,x2) print ( "*/*/*/*" , __function__ , "(" , __line__ , "): " , #x1 + "= , (x1)) #define PRINTVAR2(x1,x2) print ( "*/*/*/*" , __function__ , "(" , __line__ , "): " , #x1 + ”= , (x1), “,” #x2 + ”= , (x2)) #define PRINTVAR3(x1,x2,x3) print ( “*/*/*/*” , __function__ , “(” , __line__ , “): “ , #x1 + ”= , (x1), “,” #x2 + ”= , (x2), “,” #x3 + ”= , (x3)) #define PRINTVAR4(x1,x2,x3,x4) print ( “*/*/*/*” , __function__ , “(” , __line__ , ”): “ , #x1 + ”= , (x1), “,” #x2 + ”= , (x2), “,” #x3 + ”= , (x3), "," #x4 + "= , (x4)) #define PRINTVAR5(x1,x2,x3,x4,x5) print ( "*/*/*/*" , __function__ , "(" , __line__ , "):" ,processor">#x1 + ”= , (x1), “,” #x2 + ”= , (x2), “,” #x3 + ”= , (x3), “,” # x4 + ”= , (x4), “,” #x5 + ”= , (x5)) #define PRINTVAR6(x1,x2,x3,x4,x5,x6) print ( "*/*/*/*" , __function__ , "(" , __line__ , ") : " , #x1 + " = , (x1), "," # x2 + " = , (x2), "," #x3 + "= , (x3), "," #x4 + "= , (x4), "," #x5 + "= , ( x5) , ", " #x6 + "= , (x6 )) #define VPRINTTEXT(text) print ( "V/*/*/*", __function__, "(", __line__, "):", text ) #define VPRINTVAR ( x1 ) print ( " V/*/*/*" , __function__ , "(" , __line__ , "):" , #x1 + ” = , (x1)) #define VPRINTVAR1(x1) print ( "V/*/*/*" , __function__ , "(" , __line__ , "):" , #x1 + "= , (x1)) #define VPRINTVAR2(x1 , x2 ) print ( "V/*/*/*" , __function__ , "(" , __line__ , "): " , #x1 + "= , (x1), "," #x2 + "= , (x2)) #define VPRINTVAR3(x1,x2,x3) print ( "V/*/*/*" , __function__ , " (" , __line__ , "): " , #x1 + "= , (x1), "," #x2 + ” = , (x2), “,” # x3 + ”= , (x3)) #define VPRINTVAR4(x1,x2,x3,x4) to print ( “V/*/*/*” , __function__ , “(” , __line__ , ”): “ , #x1 + ”= , (x1), “,” #x2 + ”= , (x2), "" #x2 + ”= , (x2), “,”> #x3 + ”= , (x3), “,” #x4 + ”= , (x4), “,” #x5 + ”= , (x5)) #define VPRINTVAR6(x1,x2,x3,x4,x5,x6) print ( "V/*/*/*" , __function__ , "(" , __line__ , "): " , #x1 + ”= , (x1), “,” #x2 + ”= , (x2), “,” #x3 + ”= , (x3), “,” #x4 + ”= , (x4), “,” #x5 + ”= , (x5), “,” #x6 + ”= , (x6)) #endif
Attachment download
📎 define_statements.mqh (21.46 KB)
📎 tester4define.mq5 (1.47 KB)
Source: MQL5 #56149
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •