Tuesday, March 17, 2009

Last Week's Results

I can hardly believe it myself, but last week my NAV gains totaled 97.5%, which is entirely incredible. Too bad I'm such a small participant.

No, unfortunately, this had nothing to do with my recent forays into developing an expert advisor. It seems that I've stumbled across some charting factors that provide me some measure of insight into market movements.

The market doesn't always behave, but when she does, it's great!

Edit: I forgot to mention that after the great results last week, I'm almost afraid to trade this week. How silly is that?

Saturday, March 7, 2009

Sample MQ4 Code

Here is a bit of code that demonstrates what I've been blogging about. It's a simple way to manage pending position requests.

The idea is that a signal his been raised but that this code will let you wait for an improvement in the price before actually opening a position.


// *******************************************************************
// Name: AMS_PEND.MQ4
// Date: 01-Mar-2009
// Prog: Rookie
//
// Desc: A pending position queue manager. Pending positions can be
// held until a period of time or some favorable conditions.
// *******************************************************************
#property copyright "Copyright © 2009, Rookie"
#property link "http://trying-forex.blogspot.com/"

static double pendlask[4096]; // Ask price for a pending long
static int pendltime[4096]; // Time used to calculate pending time
static int pendlqty = 0; // How many items in pendlask queue

static double pendsbid[4096]; // Bid price for a pending short
static int pendstime[4096]; // Time used to calculate pending time
static int pendsqty = 0; // How many items in pendsbid queue

// ****************************************************************************
// Add a pending position entry to the long list
// ****************************************************************************
void addpendl(double ask)
{
static double last = 0;

if ( Time[0] > last )
{
pendltime[pendlqty] = Time[0];
pendlask[pendlqty] = Ask;
pendlqty++;
}
last = Time[0];
}

// ****************************************************************************
// Add a pending position entry to the short list
// ****************************************************************************
void addpends(double bid)
{
static double last = 0;

if ( Time[0] > last )
{
pendstime[pendsqty] = Time[0];
pendsbid[pendsqty] = Bid;
pendsqty++;
}
last = Time[0];
}

// ****************************************************************************
// A simple way to determining if a pending long position should be
// activated.
// ****************************************************************************
int dopendl_simple()
{
int i;
static int this=0;

// Dead simple "drop" style pending manager
// ----------------------------------------
for (i=0; i {
if ( pendltime[i]>0 )
{
if ( pendlask[i] > Ask )
Print("Pending L #",i," ask was ",pendlask[i]," at ",pendltime[i]," now ",Ask);

// Terminate items that don't fire
// -------------------------------
if ( Time[0] - pendltime[i] > 3000 )
{
Print("Pending L #",i," from ",pendltime[i]," cleared due to time ",Time[0]);
pendltime[i]=0;
continue;
}

// Fire first pending item more than N points down
// -----------------------------------------------
if ( pendlask[i] - Ask > 5.0*Point )
{
if ( Bid < iMA(this,this,5,0,MODE_SMA,PRICE_TYPICAL,0) )
{
Print("Pending L #",i," firing");
pendltime[i] = 0;
pendlask[i] = 0.0;
return(1);
}
}
}
}

return(0);
}

// ****************************************************************************
// A simple way to determining if a pending short position should be
// activated.
// ****************************************************************************
int dopends_simple()
{
int i;
static int this=0;

// Dead simple "drop" style pending manager
// ----------------------------------------
for (i=0; i {
if ( pendstime[i]>0 )
{
if ( pendsbid[i] < Bid )
Print("Pending S #",i," bid was ",pendsbid[i]," at ",pendstime[i]," now ",Bid);

// Terminate items that don't fire
// -------------------------------
if ( Time[0] - pendstime[i] > 3000 )
{
Print("Pending S #",i," from ",pendstime[i]," cleared due to time ",Time[0]);
pendstime[i]=0;
continue;
}

// Fire first pending item more than N points up
// ---------------------------------------------
if ( Bid - pendsbid[i] > 5.0*Point )
{
if ( Bid > iMA(this,this,5,0,MODE_SMA,PRICE_TYPICAL,0) )
{
Print("Pending S #",i," firing");
pendstime[i] = 0;
pendsbid[i] = 0.0;
return(1);
}
}
}
}

return(0);
}

// ****************************************************************************
// Algorithm selection point for long position pend processing
// ****************************************************************************
int dopendl()
{
return(dopendl_simple());
}

// ****************************************************************************
// Algorithm selection point for short position pend processing
// ****************************************************************************
int dopends()
{
return(dopends_simple());
}

You'd use code like the following in the start function to see whether or not to add a pending position and whether or not to open a position based on a prior pending position.

int openl;

// See if you have an "open long" signal. If so, add it to the
// queue.
// ------------------------------------------------------------
openl = ...;
if ( openl>0 )
{
addpendl(Ask);
Print("Pending LONG at ask ",Ask);
}

// Now, see if the queue manager suggests it's time to open a
// long position from within the queue.
// ----------------------------------------------------------
openl = dopendl();

// Open a position here if necessary
// ---------------------------------
if ( openl > 0 )
{
...
}

Notice that this design allows you to simply comment out all the pending code and open positions directly from your original signal. This makes it easy to integrate into your own code in case you want to try this in your own testing platform and see how affects your existing EA.

Friday, March 6, 2009

Expert Adviser Weekend

Finally, the weekend is almost here. I've been stealing a few moments here and there during the week but it's not that productive.

Basically, I've limited myself to testing various "opening" strategies. What I've found is that I can increase earnings across the test set -- but generally at the expense of a bigger draw down. I'd like to come up with a system that tests out a tripling of the original account value while only drawing down 10% or less.

If I can create something like that I'll give it a live run.

Interestingly, even when I create something with a massive drawdown and a huge return over a short period, it's still incredibly inefficient. Using the visual testing tool you can always spot opportunities to enter and exit the market profitably that the system completely ignored.

What I expect, once I get that far along, is that I'll open positions under varying conditions. For example, one reason that the trades mentioned above are not done is that the underlying market conditions are deemed risky by my EA. There is nothing wrong with risk it just means you need to manage potential losses better.

So, entering during a less risky period would mean that the position can be given more leeway and presumably be more likely to generate a profit. Entering during a more risky period would entail letting go of more positions at a loss while maintaining a net positive return expectation on those trades.

Lastly, a quick tip. If you minimize the visual display window the tester will run a lot quicker. However, you can then open the window after the run has completed and look at how things progressed. You can have your record and eat it!

Oh, I almost forgot... if you are using the iHighest(...) function, be sure to use the iHigh(...,...,iHighest(...)) value instead of the High[iHighest(...)] value. If you don't you'll be scratching your head wondering why you aren't getting values from previous bars. What a pain in the ass that was.

Tuesday, March 3, 2009

EA Development by Component

Okay, I've got a few EA components under my belt now.

First, I've made myself a trivial system to determine potential entry points. Basically, this system raises a signal when the price is above or below a moving average for some period of time and then crosses over. Remember, I'm only concerned about making an easy to manage framework at this point.

Second, signals raised above are tossed into a queue pending execution. A simple queue manager currently checks whether or not the price has moved lower or higher, as appropriate, and raises what should be considered a true signal if so.

While this seems trivial, I can assure you that it isn't.

Third, I have a position closing module with the job of letting my winners run. While counter-trend trades don't generally run well it can be extremely valuable to let a position accrue for a significant trend lasting one or more days.

This weekend I'll probably try to improve my code, test various strategies in each component, and come up with something that works. Again, I have to stress that if I do find something that works, I won't be telling you about it or selling it to you. I'll be bragging about it and keeping it to myself!

Monday, March 2, 2009

EA Development

I was able to put some serious effort into developing an expert adviser over the weekend. At this stage I believe I can build something that will be both relatively safe and profitable.

This is not an easy task!

Anyway, I do want to assure you that I have no intention of ever selling an EA. If it works I'll use it for myself. If it doesn't work, then I'd have nothing worth selling in any case. Something I might consider, if I can't build a good one for myself, is selling the EA framework that I'm developing.

What do I mean by framework?

I mean that creating a good trading system involves managing a lot of complexity. I intend to create a framework that will let different EA components perform small sub-tasks as part of a larger whole. This framework would make it easier for any programmer to plug in their own components.

For example, I expect that my trading robot framework will include the following independent components:

1) entry point requester
2) pending entry point executor
3) open position analyzer
4) position closer

Entry Point Requester
When I use the visual testing tool provided by MT4 I inevitably notice that my entry points are not optimal. However, it is very difficult to manage an intended entry over time as other entry signals may be generated. The entry point requester will queue up entry signals for further analysis.

Pending Entry Point Executor
Knowing that an entry point signal has arrived, the executor will loop through the pending requests and see whether or not they should be executed. Think about it. Imagine if you forced all entries to be one or two pips better than they are using your current trading tool?

Open Position Analyzer
Okay, now that you have an open position what should you do with it? I find that putting in hard stops and profit points to be less than optimal. For example, if you are in a long term profitable trend it may be advantageous to allow more profit to accumulate.

Position Closer
This portion is relatively simple. If a position is marked as needing to be closed, then it will be closed. Using a system such as this can allow your longer term trades to execute where they need to execute without being concerned about the order entry limitations of your provider.

Good Trading!