Cubesteak Central

nPipLines

November 16, 2006 5:10 am

Someone on the MT E and I Yahoo group requested an indicator that drew horizontal lines on the screen every 25 pips. I figured I could bang that out in no time and started to make this indicator. I called it nPipLines because the number of pips between lines is configurable.

Anyway, this was a lot more complicated than I originally figured. There’s a lot of math to slice and dice the current price to make sure that the lines are actually straddled around your current position.

The indicator has several configurable options, including number of lines to draw, the pip gap between the lines, the line size, style and color, as well as if you’d like the lines to continuously update as price goes up and down. This continuous option seems a bit silly to me, as it does a lot of work each tick for really no reason, but some folks may want it to work that way. By default, this is set to false.

Here’s a snapshot:

npiplines.gif

Get this indicator:

MetaQuotes 4 Language FilenPipLines.mq4
nPipLines.zip

Elder’s Safe Zone

November 1, 2006 3:30 am

I found a reference to Elder’s Safe Zone, which is a trailing stop methodology attributed to Alex Elder, the author of The Legendary Approach to Trading for a Living.  Since I’ve read his book, I was intrigued.

The reference that I found had the code for an indicator from MT3, so I recoded it for MT4.  I don’t know how useful it is yet, but I thought I would post and update it if necessary.

Here’s a snapshot:

eldersafezone.gif

Get this indicator:

MetaQuotes 4 Language File EldersSafeZone.mq4
EldersSafeZone.zip

Moving Average of Volume Bonanza

October 31, 2006 5:11 pm

Ever want to have a MA of volume?

Some indicators out there would provide such a thing, but didn’t go as far as I would have liked.  So, I put hacked up someone else’s MA of Volume indicator and added a JMA option, as well as a dynamically changing level that is based upon a configurable number of days average.

Ever want to quickly see if the MA of volume is high or low based on the last 120 days?  30 days?  Now you can!

Here’s a snapshot:

mavolume.gif

Get this indicator:

MetaQuotes 4 Language File MAVolumeWithJMA.mq4
MAVolumeWithJMA.zip

3 Fold Trading Hours in a Separate Window

October 30, 2006 11:27 pm

In a previous post, I made a little time filter indicator that simply was a visual reminder if you should be trading or not based upon three configurable times. It simply used a comment in the chart window to convey the information.

In that post, I made the offer to whip up a version that had its own indicator window, if anyone thought it would be useful. Someone (Barry) did, so now we have three flavors of 3 Fold Trading Hours.

Original: Visual reminder via a chart comment
New: Visual reminder via a new indicator window (available below)
Also: Time filter for an Expert Adviser to actually stop trading from happening

This new indicator also displays a bit more information as well, like listing out the filter times, and if they are used or not. The time filter logic is taken from the EA version, so that you can now filter by hours and minutes, not just hours, as with the original.

Here’s a snapshot:

3fthours.gif

Get this indicator:

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

Adding a Magic Number to an EA

October 27, 2006 12:04 am

Someone asked me if there was a manual on how to add a Magic Number to an EA. Since I haven’t seen one, I put together the following basic steps
and thought that others may find it useful too. These steps can also be used to verify that an EA properly uses a Magic Number, as some don’t!

First off, to the completely uninitiated, a Magic Number is simply an arbitrary number that can be added to an EA that is used to uniquely identify trades that are made from that EA with an eye towards differentiating them from trades made by any other EA. Without a Magic Number, a EA that closes all orders will not only close orders that it has made, but it will also close any orders made by a different EA, and it will even close orders that were made manually, as it has no way of telling which orders came from which EA.

This obviously is not an optimal situation. The Magic Number lets you differentiate these orders programatically so that you can run multiple EA’s on multiple charts from within the same MT4 instance and still be able to trade manually as well.

Anyway, now on to the steps:

1) On a global level in the EA add:

int MagicNumber = 233423; // where 233423 is any old number that is unique from any of your other running EA’s

The easiest way to describe declaring on the “global level” of the EA is a variable that is declared outside of the Start, Init and Deinit functions. The easiest way to ensure that you are on the global level of the EA is to put the MagicNumber declaration immediately after the top comments and any #property, #include or #import statements and BEFORE the Init, Deinit and Start functions.

2) Next, make sure that the OrderSend function uses this magic number. OrderSend uses the following form:

int OrderSend( string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment=NULL, int magic=0, datetime expiration=0, color arrow_color=CLR_NONE)

So, you want to make sure that it is called with the MagicNumber in it, e.g:

OrderSend(Symbol(),OP_BUY,1.0,Ask,2,10,20,”My Buy”,MagicNumber,0,Lime)

3) Then, search the EA for each instance of OrderClose, OrderModify or OrderDelete. Usually these are sandwiched by at least one IF statement. Add to that IF statement the following condition: (OrderMagicNumber() == MagicNumber)

So, something like this:

if (OrderSymbol == Symbol())
{
OrderClose(blah blah…);
}

Becomes:

if (OrderSymbol == Symbol() &&
OrderMagicNumber() == MagicNumber)
{
OrderClose(blah blah blah);
}

Voila! Your EA now uses a magic number and should be able to trade with other EAs.

Please note that any money management that an EA uses, like lot optimization based on available margin or equity, etc. will also “interfere” with each other. This is either desirable or undesirable based upon how you want to trade with the specific EA’s.

Hope that helps!

The FINAL word on EX4 decompilation…

October 20, 2006 6:44 pm

OK, the FINAL (I hope) word on decompiling EX4 files in the U.S.

If you want to refute this, please feel free (I would welcome the re-education), but don’t bother to unless you provide a URL or quote that explains a specific statute or law.

OK, here goes:

A copyright doesn’t forbid decompiling. That’s why most software companies have a EULA (end user lic. agreement) that specifically cover things like reverse engineering, decompilation, etc as well as exactly HOW you can use the software.

This is a simple issue - if you decompile something, you aren’t breaking the law (unless you signed a EULA, in which case you are technically in breach of license and can be sued). IF you choose to publish it, sell it, or do anything else with it that falls outside of “fair use” laws, then you are in breach of copyright. This isn’t so different than the whole thing that is going on with music… Saying you can’t decompile a .ex4 file is like saying you can’t rip a song from a CD to an MP3. That’s not illegal (yet).

In fact, I personally believe that it would be difficult to enforce a copyright for most EA’s. Why? Several reasons actually.

To quote wikipedia:

“Copyright law covers only the particular form or manner in which ideas or information have been manifested, the “form of material expression”. It is not designed or intended to cover the actual idea, concepts, facts, styles, or techniques which may be embodied in or represented by the copyright work.”

Most styles of trading have been done and are available in one form or another on many forums, so the “system” can’t be copyrighted. MQL4 is a small enough language where just about EVERY way of doing anything with it is also in the public domain at this point, so most likely the “code” itself can’t be copyrighted. That leaves the exact copying of the code in its entirety. WHICH, frankly goes on and is encouraged left and right in most forums and is even done in this group. Everybody puts the “copyright” statement in their code, yet then we post it to groups such as this or to online forums, freely encouraging people to copy it again and breach the copyright we just claimed. How can one breach be distinguished from the “normal” breaches that are encouraged every day? While ignorance of the law is no excuse, there is certainly plenty of precedence to say that the “copyright” statement in an EA isn’t really a “proper” copyright at all. I think what we really mean is more of an Open Source license. Regardless, this just covers source code. We haven’t even talked about a decompile. So, that’s Strike One.

Beyond this, an actual decompile wouldn’t decompile comments, and therefor the resulting code wouldn’t even contain the copyright message people are saying would cover it! Strike two.

To make matters worse, decompiled code looks nothing like the original code, so you can’t even compare the two and say that it is the same program. You would be saying “that’s my method or my setup or my system”. Well, as stated before, you can’t do that. A perfect example of this fact is Linux and Unix. Linux “copies” Unix is almost every fashion, but since it doesn’t actually use any of the CODE from the original Unix license (now owned by SCO) it is completely legal.

So, Strike Three, you are out. :D

Technically the only things that a copyright COULD cover would be published source code with the copyright notice in it (which is tenuous unless you take further steps), and a compiled EX4 file, which is considered a valid “form of material expression”.

Neither of these things cover a decompile. Seems wrong, doesn’t it? Not really, its just that a copyright isn’t really whats needed for a software program - a software license is.

That said - Your best bet to protect your work is two fold. Register the the source code copyright officially and if you publish it, note that it is a registered copyright with the US government. Heed the following advice from Wikipedia:

“While copyright in the United States automatically attaches upon the creation of an original work of authorship, registration with the Copyright Office puts a copyright holder in a better position if litigation arises over the copyright. A copyright holder desiring to register his or her copyright should do the following:

1. Obtain and complete appropriate form.
2. Prepare clear renditon of material being submitted for copyright
3. Send both documents to U.S. Copyright Office in Washington, D.C.”

And secondly, (and more importantly) if you want to distribute or sell your EA, your best protection is an End User License Agreement that specifically outlines the rights of the buyer, which would of course forbid copying, decompiling, reselling, etc.

As a side note on the EULA, I personally would never agree to a EULA on an EA, as the liability just isn’t worth it.

Hopefully that (more) than closes the topic. :)

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