Cubesteak Central

Archive for August, 2006

3 Fold Trading Hours Filter for EA

August 25, 2006 9:46 pm

In 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.

MetaQuotes 4 Language File 3FoldTradingFilterforEA.mq4
3FoldTradingFilterforEA.zip

More Array Functions for MT4

August 23, 2006 1:59 am

One 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:

  1. If the value exists in the array’s second dimension
  2. 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:

  1. 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”.
  2. 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!

MetaQuotes 4 Language File CSArrayFunctions.mq4
CSArrayFunctions.zip 

3 Fold Trading Hours

1:17 am

A forex-tsd buddy was working on a project that needed to filter trading by 3 separate timeframes.  Answering his question prompted me to make this little indicator that displays a comment telling you if it is currently within your preset trading hours.

The hours to trade can be set either in the code directly, or through the indicator settings window.  Of course, all hours are in relation to your broker’s server.

Current limitation: it only works on whole hours, no minutes.

Possible future: I can make it work with objects, rather than a comment, giving more flexibility on placement and opening up the possibility of putting it in a separate indicator window.  Let me know if that would be usefull! 

MetaQuotes 4 Language File 3FoldTradingHours.mq4
3FoldTradingHours.zip

A “Better” Way to Search Arrays in MT4

August 21, 2006 5:00 pm

MT4 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!

MetaQuotes 4 Language File ArraySearch.mq4
ArraySearch.zip

How to debug custom DLL’s with MT4

August 9, 2006 2:18 am

It could be that I’m just dense and this was readily apparent to everyone else, but I’ll post this anyway for the sake of community. For those of you who would like to know how to use Visual Studio to debug your custom dll, do the following – I’ll use the example of the ExpertSample.dll that comes with MT4.

After getting the sample up and running, do the following:

1. Setup your sampledll.mqh file to point directly to your DEBUG library file (C:blah\experts\samples\DLLSample\Debug\ExpertSample.dll)

2. If you just try to just select Debug from the VS IDE, you will be asked to select an EXE file. DON’T do this. If you did go ahead and select terminal.exe as the exe, the whole process simply exits and the debugger won’t catch anything. Maybe someone smarter than me can explain why..

3. But, to really be able to debug your DLL, you have to attach to the terminal.exe process explicitly from VS. Go to Debug, Processes and select your terminal .exe process. Now, the debugger will start, and you can set breakpoints, etc.

It is really the combination of using the correct DLL that VS has source access to and attaching to the running terminal process that does the trick.

WCCI Pattern Label Index

August 5, 2006 12:42 pm

This is a silly little indicator for MetaTrader that is designed to be used with the various Woodies CCI pattern indicators that are floating around the web.

It simply puts a new indicator window up and makes labels for what the pattern numbers correspond to. Silly, but helpful. You can configure the corner of the window to start the labeling from, if the labels should be displayed in one or two columns, as well as the text color and size.

MetaQuotes 4 Language File WCCIPatternLabels.mq4
WCCIPatternLabels.zip