Archive for the 'MT4 Includes' category
3 Fold Trading Hours Filter for EA
August 25, 2006 9:46 pmIn a previous post, I made available a small indicator that simply mentioned if you should be trading or not based on three different configurable timeframes.
In the process of changing the logic for use in an EA, I also improved on the previous indicator. You can now use hours and minutes, not just hours for the configured time frames.
The file below is not a new indicator, but is intended to be included in an EA. Its is the 3 Fold Trading Hours logic turned into a boolean function. Simply wrap all your EA logic in the call to HourlyFilter(); It returns true when the time is within your specified hours, and false when it’s not.
Pseudo code for placement in the Start() function is:
if HourlyFilter()
{
PerformEALogic();
}
else return (0);
UPDATE: There was an error in the source, which has now been fixed. Filter 3 was being checked twice, and Filter 2 was being ignored. Sorry for any inconvenience.
3FoldTradingFilterforEA.mq4
3FoldTradingFilterforEA.zip
Categories: MT4 Includes
2 Comments »
More Array Functions for MT4
August 23, 2006 1:59 amOne of the things that has bugged me, is the inability to search a multi-dimensional array in MT4.
So, I made a function to do so. In this case, we really want the function to give us two things:
- If the value exists in the array’s second dimension
- If it does exist, the actual coordinates of where
Ideally, you could do this with as little “usage hassle” as possible. So, the function that I’ve written does the following:
- It uses a 2 bucket single-dimension array to hold the coordinates of our search value. This array should be declared prior to calling the search function. The search function updates the value of the array because the array is “passed by reference”.
- The function itself of a boolean type, so a return of “true” means it was found, and “false” means it wasn’t.
Here is the function spec:
SearchSecondDim (ReturnArray, ArrayToSearch, ValueToSearch, SearchDirection)
Return Values:
True or False
With ReturnArray passed by reference so that after function is run:
ReturnArray [0] = ArrayToSearch first dimension location
ReturnArray [1] = ArrayToSearch second dimension location
if ReturnArray [0] or ReturnArray [1] = -1 value was not found.
NOTE: ReturnArray[2] need to be declared prior to using this function!
Example 1:
double MySearchArray[10][5];
ArrayInitialize(MySearchArray,0);
MySearchArray [7][5] = 7;
double foo[2];
bool FoundIt = SearchSeconDim (foo,MySearchArray,7,MODE_ASCEND);
This will set foo[0]==7 and foo[1]==5. Also, FoundIt will be “true”.
Example 2:
double MySearchArray[10][5];
ArrayInitialize(MySearchArray,0);
MySearchArray [7][5] = 7;
double foo[2];
bool FoundIt = SearchSeconDim (foo,MySearchArray,10,MODE_ASCEND);
This will set foo[0]==-1 and foo[1]==-1 since the value isn’t anywhere in the array’s second dimension. Also, FoundIt will be “false”.
Some Extra Goodies!
Also included in this file, is the single dimension array search function that I posted earlier (see that post for more details) and 2 simple array “printing” functions. These printing functions output to the “Experts” tab of MT4’s terminal window.
Printing a 1 dimensional array:
PrintSingleArray(double ArrayToPrint[])
Return Values:
None / Void.
A single comma separated line is printed to the Experts output window.
Printing a 2 dimensional array:
PrintDblArray(double ArrayToPrint[][])
Return Values:
None / Void.
Each comma separated “row” is printed on a new line in the Experts output window.
ENJOY!
CSArrayFunctions.mq4
CSArrayFunctions.zip
Categories: MT4 Includes
No Comments »
A “Better” Way to Search Arrays in MT4
August 21, 2006 5:00 pmMT4 provides the function ArrayBSearch to search arrays. It seems a bit “non-standard” to me, in that according to the documentation, it “returns the index of the first occurrence of a value in the first dimension of array if possible, or the nearest one, if the occurrence is not found.”
It wasn’t readily apparent to me what the heck that meant, so I did a little experimenting. I did the following:
double foo[10] = {1,2,3,4,5,6,7,8,9,10};
ArraySort(foo);
int index = ArrayBsearch(foo,4);
Print ("found: "+index);
index = ArrayBsearch(foo,11);
Print ("not found: "+index);
And got:
found: 3
not found: 9
And then did this:
double foo[10] = {1,2,3,4,5,6,7,8,9,10};
ArraySort(foo);
int index = ArrayBsearch(foo,4);
Print ("found: "+index);
index = ArrayBsearch(foo,-1);
Print ("not found: "+index);
And got:
found: 3
not found: 0
So, it seems that this means that ArrayBsearch returns either the first or the last index position if your number isn’t found in the array. To me, that’s kind of dumb, as it means you still have no idea if the first or last index position actually contains the item you are searching for! It could mean it found your value, or it could mean it DIDN’T find your value. Not really the sort of information I’m looking for in a “search” function.
If you do a Google search for “array bsearch”, you’ll find several examples of these kinds of searches. They pretty much boil down to the following:
bsearch returns a pointer to an occurrence of key in the array pointed to by base. If key is not found, the function returns NULL.
So, I created this little function as a more “sane” array search for MT4. Since we don’t have access to pointers, I just used a “-1″ return instead. I hope you like it!
ArraySearch.mq4
ArraySearch.zip
Categories: MT4 Includes
1 Comment »

