Articles by: David Williams

EA Bugs? Find Them, Stomp Them, Move Forward

EA Bugs

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.” 
   – Brian W. Kernighan

As a long time programmer, I really appreciate that quote.

Brian Kernighan was one the developers of the famous C programming language.  He and his partner, Dennis Ritchie, wrote the original UNIX operating system.  You might say that code is the basis for the entire internet.

One of my first jobs after graduation was in the college’s business incubator.  It was a great opportunity to work with some very talented people. Grad students and professors alike.  (Still not sure how I made that cut 🙂 )

Anyway, we were working on a telephone system for commercial aircraft.  Remember this was 20 years ago, when a cell phone was the size of a shoe box and its battery lasted about 15 minutes!

We were building the hardware and writing the software at the same time, so there were plenty of opportunities for nasty bugs.

One day I was poking around in the lab trying to nail down an issue. I was testing a few different theories when “Professor V” approached and asked what I was doing.

I explained that I had changed the order of some subroutines because I was trying to solve a boot-up problem.

He politely asked what I expected to happen. I really didn’t know what I expected and I told him that.  This was not the right answer.

He sighed and gave me one of those looks. You know, the kind of look that makes you feel like you ran the football in the wrong direction and scored a touchdown for the other team. Not just wrong.  Really wrong.  Like  “How can you possibly not know this?” wrong.

He explained that I had no chance of success using my current approach..

“David, just trying things and seeing what happens won’t cut it.  Way too low of a probability of success.  You need to be able to explain exactly what should happen and why. And when it doesn’t happen – that’s the source of the problem – that’s where you look”.

I learned that debugging is a little like being a trial lawyer: You never ask a question that you don’t know the answer to.

This was natural for Professor V.  He couldn’t even contemplate making changes without having an expected outcome.

I had to learn it that day – and I’ve never forgotten it.

Years later, I’ve applied that lesson, along with many others, to debugging Expert Advisors.  I’ve tried to sum up my method in a recent forum post. You can see it here:

http://www.iexpertadvisor.com/forum/messages.aspx?TopicID=230&MessageID=696#post696 

NOTE: Here’s another tip for debugging:  Explain the problem to someone else.  Anyone. They don’t even need to understand what you are saying.  But the simple act of verbalizing the problem often helps you find the issue.

Bugs!

The first software bug was a moth!

 

 

The Reason Giants Don’t Roam the Earth? RTM

RTM:  The Reason Giants Don’t Roam the Earth

I want to share a story with you that’s kind of hard for me to write about.  but I think it may help you out some day.

First off, let me tell you –  when it comes to trading, I’m a cowboy.  I like to take risks.  Maybe it’s because I’m an optimist and tend to focus on the upside of a trade, not the downside.

This, as you probably know, is a huge flaw for a trader.  I mean huge.  Gigantic.

But this story isn’t just about risk.  It’s also about greed and hubris.

(Of course it is. What trading horror story would be complete without greed and hubris?)

A few years ago, there were some very popular Expert Advisors on the trading forums that used a grid technique.

With a grid, you basically set a bunch of horizontal lines on a chart and space them about 20 points (or pips) apart.  The EA opens and closes trades as the price moves through the grid of lines.

These grid systems work great in a range-bound market. Remember that.  Range-bound.  It’s important.

Now, most of these systems have a rule for buying into a losing position.  The better EA’s cap the buying at the top or bottom of  the grid.

But I’m a programmer.  I can make it better, right?  So I created my own grid-like EA. Except I use a pure Martingale strategy to double-up on losing positions.

If you don’t know, a Martingale strategy is when you double your position size on each loss.

This is the situation:  The price has moved against you.  You are holding  a losing trade.  So you double-up your position. It’s generally accepted that a currency pair will not continue to move in the same direction forever.  The price of the currency will reverse at some point.

And when the price does reverse, just a little bit, you close all of your positions.  Most of them for a loss, but the last position is closed with enough profit to cover all the other losses – and then some.

This is what I told myself: In nature there is a rule called “Reversion to the mean” (RTM).  This rule states that things always move back to their average (or mean).  If they didn’t, the world would be filled with extremes.

For example, if big people had big children, and the big people’s children had big children – if the trend continue without reverting back to the mean, well, then the world would be filled with giants!

But here’s the kicker.  The “reversion to the mean” rule does hold true,  but … drum roll please …  the mean can change!

The actual value of the average can and will change. (People really are getting a little bigger, on average.)

And the average price of a currency pair can change so that it never returns to the price you bought it at!!  I use exclamation points because I’m yelling at myself (again).

Of course, anyone with any trading sense whatsoever knows that a Martingale system will eventually blow up. Anyone.

But I ran it on demo accounts for a long time.  And it worked.  It worked really well. So I moved it to live accounts.  And again it worked really well –  for a while.

Then the EURUSD stopped moving within a range.  It broke out. It went straight up, like a rocket. Without ever retracing.  Not even a little bit.

I blew two accounts.  Big accounts. One was a commercial account, setup in my company’s name.  That’s not so bad, I could deal with that.

The other was my IRA account. Yes, you read that correctly.   Who is so friggin’ stupid to run a martingale EA on their retirement  account?

Me. (At least me from a few years ago, not me now.)

So please listen. As I provide you with tools to create super-sophisticated EAs, remember to keep your greed and hubris in check.  Some things in life you just need to learn the hard way.

This should not be one of them.