An object oriented language for Empty4

Post Reply
roger-write
Trader
Posts: 63
Joined: Sat May 19, 2012 10:34 am

An object oriented language for Empty4

Post by roger-write »

Greetings! For the most part, I'm a lurker on forex forums, gleaning what I can from anything promising in the way of profitable strategies. But I feel I have something to contribute to the Empty4 development community and decided that Steve's classy forum was the right place to introduce it.

First, a bit of background. For the past 30 years I have exclusively made my living as a software engineer. Three years ago, forex got under my skin and I've been pretty much working exclusively on indicators and EA's ever since. A lot has transpired, and needless to say I've learned quite a bit, but also realized just how difficult it is to develop consistently successful automated trading software that doesn't bet the farm. During my stint with forex coding, I've developed numerous coding frameworks for trading....what is a coding framework, you ask?

If you've spent any time writing EA's and indicators for Empty4, you already probably have your own coding framework, whether or not you call it that. Basically, it's your own set of libraries, shells, and/or methodologies that allow you to quickly and (hopefully) efficiently create EA's and indicators on a whim. Okay...(I'm getting to OO for Empty4 in a sec, just bear with me)...so, as I said, I've developed many frameworks for Empty4 and just started my latest one. Maximum flexibility, with ease of use and maintenance, which IMHO should be the goal of every framework. And I was reminded again of just how outdated mql4 really is. Seriously...come on.

So I decided to write my own Empty4 language. Well....sort of. I created a pre-processor that takes a new language I'll call ObjectQuotes (with extension .oq) and spits out some nasty mql code. Nasty to read, that is, but compiles in Empty4 and does what the ObjectQuotes code dictates. Since an OO language in my opinion is an absolute must for serious software development, I've decided to make this freely available to the community. I haven't decided on a specific license agreement (I.E. open source, etc.) yet, but suffice it to say, I intend for ObjectQuotes to always be freely available for personal or commercial use.

My time is somewhat limited as the bulk of my efforts are now going toward working on my latest trading framework (sorry, I won't be giving this away for free). But as I take breaks, I should have some time to spend on this thread. If I waited for a full manual to be written, it would be some time and I wanted to get this out to the community now. For the time being, I'll be using the thread to teach how to use ObjectQuotes for those who are interested. One way or another, eventually a real manual will get written, but reading through the thread will have to suffice at the moment.

I will have a link for downloading the ObjectQuotes engine by tomorrow, along with some examples.

One more note which is really important: If you plan to use ObjectQuotes, I highly recommend downloading the brilliant SciTE IDE posted on another thread here. Eventually I or someone else here can update it properly to accommodate .oq files, but until then, select either C# or MQL languages (C# is closer to the way the code looks, but MQL will give you all the correct keyword highlighting and API, etc.).

Until tomorrow, TTFN.

Roger
oq-097.zip
Build.txt
BuildExample.txt
Convert.oq
Data.oq
You do not have the required permissions to view the files attached to this post.
Last edited by roger-write on Tue Feb 19, 2013 10:53 pm, edited 9 times in total.
garyfritz

Re: An object oriented language for Empty4

Post by garyfritz »

Sounds excellent, Roger. I assume your OQ language will handle both indicators and EAs? I'll look forward to seeing what it looks like!
roger-write
Trader
Posts: 63
Joined: Sat May 19, 2012 10:34 am

Re: An object oriented language for Empty4

Post by roger-write »

Yes, indies, EA's, scripts, whatever. The builder is now posted in the original post in a zip file. It contains a single .exe file called oq.exe which is used to take a build file and do all the heavy lifting. More on the build file shortly.

So far, the Examples.zip file contains:
- Build.txt (a quick description of the build file contents)
- default.bld (an example build file)
- Data.oq (example .oq file containing instance classes w/inheritance)
- Convert.oq (example .oq file containing a static class)

I'll give a more detailed discussion of the build file later today. But a couple of quick notes: modules can be either original .mq4 files or .oq files. The extension name really doesn't matter, since oq is smart enough to read native mql or the newer oq format and sort it out. The only caveat with native mq4 files is that you should not use the #include directive to include other modules. Its ok to use if you are referencing some external system thing, but not for one of your own modules that will be listed as part of the set of modules being built.

Generally speaking, oq files don't use includes for other modules, but is more like C# in this regard. You will note that the project gets built as a whole, rather than compiling individual source files. In this regard you can think of it somewhat like a pseudo managed project, in the same vein as Microsoft talks about managed code.
roger-write
Trader
Posts: 63
Joined: Sat May 19, 2012 10:34 am

Re: An object oriented language for Empty4

Post by roger-write »

About the build files and the OQ builder in general: even if you have no interest in learning about object oriented Empty4 coding, the builder can be of use to your current mql projects. Here are some of the benefits:

- Keep your source maintained outside of the Empty4 directory structure in whatever directory heirarchy you choose, and then have the OQ build file point to them.

- For complex projects, avoid the need to use #include statements, as the OQ builder will already know to include any source under its control.

- Optionally have the builder combine all individual source files into a single source output. This is handy if you want to hand the source off to a client, for example.

- Optionally have the builder secure the combined source file by garbling the names (actually this is more useful when using the oq language, but not so much native mql code, because native names aren't under the builders direct control). But writing oq code and using this option will produce a big nasty mql file that will (a) compile and work correctly, but (b) make any programmer go insane trying to figure it out. That way if you need to deliver source but don't want people prying into your top secret strategies, this is the ticket.

- It will use the Empty4 compiler to create the ex4 file.

- You can specify where to deploy the ex4 file once it gas been created.
roger-write
Trader
Posts: 63
Joined: Sat May 19, 2012 10:34 am

Re: An object oriented language for Empty4

Post by roger-write »

oq.exe is a command line program that takes one argument, the build file. If you don't specify a build file, it will try to open "default.bld" in the current directory. The build file is a text file that can be created in any text editor like notepad. It allows you to specify various options as follows:

Target = c:\target_directory

In the examples zip file, see Build.txt for a quick reference of all the build parameters. And I will discuss some here as well. After all the options have been specified, you give it a list of modules, which can either be native mql source files or oq files (ObjectQuotes object oriented language). And both can be happily built into the same project, EA, indicator or whatever. In fact, you can embed oq code in the middle of your native mq4 file if you wish.

Target

You must include this directory reference. Think of Target as a staging area before it calls the Empty4 compiler. MAKE SURE this is an empty directory, as each time oq.exe is run, it deletes the contents of the target directory and before staging the sorce. For example, it will convert your .oq file into .mq4 files and put them in the target directory.

Empty4

Make this point to your Empty4 directory for the sole purpose of finding the Empty4 compiler.

Deploy

This is the directory where the resulting .ex4 file will be copied to.

Verbose

Takes Y or N and if Y, it displays to the console everything the builder is doing, as it does it.

When you are done specifying your properties, use the keyword MODULES, all in uppercase, and then all lines after this will be the list of modules or source files you wish to include in your project, one to a line. But you can also specify a directory, in which case it will load all source files with the .oq extension from the specified directory. This conveniently allows you to encapsulate a set of modules designed or some purpose and then just reference the directory to suck them all into your project at once.
roger-write
Trader
Posts: 63
Joined: Sat May 19, 2012 10:34 am

Re: An object oriented language for Empty4

Post by roger-write »

So those of you who aren't software engineers and don't know what object oriented coding means, and also those wanting to get a peek at what the oq language will give you:

First of all, oq isn't a full on language, but more of an adaption to coexist with mql and allow better structuring of code. So you won't have to throw out your knowledge of mql in order to use oq. But taking your knowledge of mql, and then also embracing object oriented coding principle you will become a better coder and your code will have longer term ramifications and be more useful to future projects, for example. Not only that, but used properly, you will be able to dynamically extend the behavior of your EA's without having to rewrite code, because you'll be able to inherit functionality for previous work you've done and then tweak it for a special circumstance. Elegantly engineered classes have so many benefits I can't begin to go into them all right now. But let me give you just one example:

Let's say someone on here defines how we should interact with an indicator in a very generic way. It would have various methods such that any source code would interact with an indicator in exactly the same manner. So they write an oq file defining the indicator class. Then for each kind of indicator (TMA, HulllMA, whatever) you define it as a subclass of the indicator type. You use these in EAs or indicators or whatnot. So someone on the forum then creates some new indicator in this way and posts it. All you have to do to use it is include their oq file in your build and provide a single call to instantiate it, and voila, you just plugged a new indicator into your project, meaning your EA suddenly becomes "new indicator aware", instantly. This is one simple example, but the uses are unlimited. This kind of coding is the basis to developing some really complex stuff in a sane and logical manner. If you want to take your coding and projects to the next level, it is my humble opinion that you must embrace object oriented coding whether it be oq or find another platform that supports it.

OQ allows you a new keyword called 'class'. It would appear in your source as follows:

Code: Select all

class <name>
{
   <class definition>
}
So for example, here is a very simple class:

Code: Select all

class Chain
{
   int Next;
   int Prev;
   int Value;

   New(int value)
   {
      int ID = @new();
      @Next = -1;
      @Prev = -1;
      @Value = value;
      return(ID);
   }
}
Well, it's not showing my indentations. (fixed -- Mod) Look in the example files for a more aesthetically pleasing view of what a class looks like.
roger-write
Trader
Posts: 63
Joined: Sat May 19, 2012 10:34 am

Re: An object oriented language for Empty4

Post by roger-write »

Inside of a class there can be various kinds of elements as follows:

#define's: these look just like regular mql defines but the define name is local to only that class. This way you don't have to worry if you've used the same define name elsewhere, and this leads to using more relevant, none obscure names. For example:

Code: Select all

class MyClass
{
    #define Null 0
    #define Number 1
    #define String 2
}
roger-write
Trader
Posts: 63
Joined: Sat May 19, 2012 10:34 am

Re: An object oriented language for Empty4

Post by roger-write »

Properties: These are simply variable definitions inside of a class, just like you would normally specify globally or in a function in mql. For example:

Code: Select all

class MyClass
{
    #define MaxNames 8

    int IValue;
    color Arrow;
    string Names[@MaxNames];
}
On the topic of properties, there are two kinds of class properties: static and instance. If you simply specify the properties as in the above example, they are instance properties, which mean they only exist once an object from this class has been instantiated (or created). Alternatively, if you preceded a property definition with the word static then it is a static property and always exists one time (as opposed each instance of an object). Here's an example:

Code: Select all

class MyClass
{
    #define MaxNames 8

    static int IValue;
    static color Arrow;
    string Names[@MaxNames];
}
In this example, IValue and Arrow will always exist whether a MyClass object is created or not, whereas Names will only exist when a MyClass object is created, and more importantly, each MyClass object that is created will have its own copy of IValue and Arrow.
Last edited by roger-write on Fri Jan 18, 2013 2:45 pm, edited 1 time in total.
roger-write
Trader
Posts: 63
Joined: Sat May 19, 2012 10:34 am

Re: An object oriented language for Empty4

Post by roger-write »

Methods: These are basically just functions, but the OO community likes to call them methods when you are talking about them within the context of a class. This is where the work gets done and there are 3 fundamental kinds of methods within the ObjectQuotes language.

static methods: Like static properties, static methods are not associated with an object, but rather can be called anytime whether an object of this class has been created or not. For all intents and purposes, static methods might as well be considered the same as functions within mql. But making them part of a class forces you to categorize the function into something relevant. Even apart from OO coding, many of us do this sort of thing already. Just look at mql's time functions and how the function names all begin with 'Time'. In oq, you would put all the time functions in a Time class and then use static methods to define the intended behavior. For example:

Code: Select all

class Time
{
    datetime Current()
    {
        return(TimeCurrent());
    }
    <etc...>
}
Whether you actually encapsulate Time as a class is your business, but this is merely an example.

Incidentally, the way you make a call to the above example would be as follows (but more on this later):

Code: Select all

if( Time:Current() >= MarketOpen ) ...
roger-write
Trader
Posts: 63
Joined: Sat May 19, 2012 10:34 am

Re: An object oriented language for Empty4

Post by roger-write »

constructor methods: These methods sole reason to exist is to instantiate, or create an object of this class. The way you distinguish the fact that the method is a constructor is that you don't specify any return type, whatsoever. In other kinds of methods, ones that don't return any values, will still use the word 'void'. But in the case of a constructor, you would leave off the return type as follows:

Code: Select all

class TimeRange
{
    datetime From;
    datetime To;

    New(datetime From, datetime To)
    {
        int ID = @new();
        @From = from;
        @To = to;
        return(ID);
    }
}
I'll save the '@' symbol for a later discussion, but you will note that the definition of the New method doesn't specify a return type. This is simply because all constructor types always return an object ID. In one way or another, a constructor must do whatever it needs to do to create an object of this class type and return the ID (which, internally is an 'int'). The most primal way to do this is using the call to @new(), which will always be available for every class. If for some reason an object can't create the objects for whatever reason, it should return -1 as the object ID.

On a side note, real OO languages have better error handling such as throwing and catching exceptions. But alas, there is simply no way to facilitate this in mql, so we are stuck with antiquated checking of object ID's to make sure they are created properly. But as a coder, you should know when such checks are needed and when they are not. In the above example, this constructor will never fail, because @new() will never fail. Or....if it does, something on your machine has gone terribly awry (like exceeded memory or swap space or something)., in which case you're screwed anyway. :lol:
Post Reply

Return to “Coders Hangout”