Tuesday, June 07, 2005

Thinking Stuff

So here are some screen shots and a bit more of an explanation of what I've been working on for the last 5 months. Hopefully it might inspire someone else to start making one of their own. If so, feel free to reverse-engineer what I've done here. "More life to all", as they say. If so, please contact me somehow - either through leaving a comment or through the Contact Me link to the left.

When you're looking at the screen shots though, remember that it's not finished, and that the numbers you're looking at aren't real.

First off, it stores orders and trades. And, if the company you've selected has an API available, you can send that order or modification to the trade off to the broker.

The order screen is relatively easy:

Thinking Stuff Order Screen


But there were so many fields for a trade that I decided to split them into different tabs:

Thinking Stuff Trade Screen - Details

Thinking Stuff Trade Screen - Entry


Thinking Stuff Trade Screen - During

Thinking Stuff Trade Screen - Exit


Now to the Systems area. A trading "rule" is something like "RSI in overbought". A bunch of rules form a "system".

However, a system is not just about the rules for when to buy and when to sell. There's also rules for how much money to use, times of day you shouldn't buy, and so forth. These extra bits are so very incredibly important, and these are the things which prevent the loss of all your capital.

Briefly, a complete system should have:

  • Entry rules - they generate a "buy" or "don't buy" signal;

  • Entry value - the exact price at which to buy;

  • Initial stop loss value - the exact price at which you are going to sell if the trade goes against you. You ALWAYS have a stop loss - you know exactly your $ at risk;

  • Initial take profit value - the exact price at which you are going to sell if the trade goes in your favour. This is optional. Either you have a take profit, or you rely on your trade management;

  • Trade management rules - when to move your stop loss to a position of decreased risk, to a position of break-even, and eventually to locking-in profits;

  • Trade management values - the exact points at which to move the stop loss;

  • Exit rules - they generate a "get out right now" or not signal;

  • Money management - the exact amount of money you're prepared to risk on this trade. Often people go by the rule of "never risk more than 2% of your entire trading bank on any one particular trade". Or 1%. Or 3%. Or whatever;

  • When not to trade rules - tell you to stay out, even if all of the above looks fantastic. Such rules might be "don't enter any trades less than 2 hours before a major announcement" (with "major" having also been defined). "Don't bother trading 1 week before Xmas, until 1 week after New Years"-kind-of-thing;

Anyway, that's roughly how a system should be. So here are the screenshots:

Thinking Stuff System Screen - Money Management

Thinking Stuff System Screen - Trading Rules


The screen on the left (above) is the Money Management tab. The Last Gasp setting specifies what to do when the price has jumped so quickly that we couldn't get in at our calculated entry. Do we just get in whatever the price is now? Or not?

The screen on the right (above) is the Trading Rules tab. That's the magical part of this software. It allows me to experiment with different settings just through clicks of the mouse, rather than chopping and changing code.

You'll see in that screenshot that I just have one rule each for Long - Entry Rule, Long - Entry Value, etc. But the way I've set it up means you can have as many rules as you like. For example:
    Long - Entry Rules - Close Above High Of Last X Bars
    Long - Entry Rules - Price Closes Above MA(20)
    Long - Entry Rules - MA(3) Crosses Above the MA(5)
    Long - Entry Rules - Slow Stochastic In Oversold
    Long - Entry Values - Close of Current Bar

All Entry Rules must evaluate to being true for the signal to be thrown, and if it is then the entry value is set at the close of the current bar (in this example). Just one of the rules being false and there is no signal. I mean, that's the point of having rules, right?

For this reason there is no "OR" in the entry rules. Actually if you have an OR in your entry rules, like this:
    Long - Entry Rules - Close Above High Of Last X Bars; OR
    Long - Entry Rules - Price Closes Above MA(20)
    Long - Entry Values - Close of Current Bar

Effectively that's two different systems - one where you go long if the close is above the high of the last X bars, and one where you go long if the price closes above the 20-day moving average. If you want to do that, just create two systems with all other settings the same.

That's for entry rules. For exit rules it's a touch different. It's perfectly valid for a system to to have more than one exit rule. Let's say you wanted to exit at market if just one of the following were true (T/P stands for "Take Profit"):
    Long - T/P Mgmt Rules - Close Below High Of Last X Bars; OR
    Long - T/P Mgmt Rules - Price Closes Below MA(20)
    Long - T/P Mgmt Values - Exit At Market

Buuut, my software doesn't throw a signal unless ALL rules evaluate to being true. The above would only exit at market if both of the above rules were true, not if just one of them was.

What I did to get around this was create a secondary setting. Like this:
    Long - T/P Mgmt Rules - Close Below High Of Last X Bars
    Long - T/P Mgmt Values - Exit At Market

    Long - T/P Mgmt Rules #2 - Price Closes Below MA(20)
    Long - T/P Mgmt Values #2 - Exit At Market

But that's it - only two. If I ever come across a case that I *really* can't live without having a 3rd, I might program it in then. But low priority now.

But you can have many different value settings? Like this:
    Long - Entry Values - Close of Current Bar
    Long - Entry Values - Value of MA(20)
    Long - Entry Values - High Of Last X Bars

How does that work? Well, this is where I had to make an assumption, and the assumption is that if indeed there are more than one value to choose from, the system will use the highest entry value, the highest stop loss value, and lowest take profit value (for longs, reverse for shorts).

If you click Add..., or select a Trading Rule and click Edit..., below is the screen where you modify the settings:

Thinking Stuff Systems Update Screen


Next is the Long Description tab (below left). Because the Trading Rules window could get pretty cluttered and hard to read with a lot of rules, I thought I'd automatically generate a description in plain English. But it's low priority so hasn't been implemented yet.

The No Trading tab (below right) specifies, you guessed it, when not to trade. These particular settings make sure the system has been successful enough in the past that I'm prepared to allow it to keep going. Remember that I plan to be drinking beers while this software is doing its thing, so it needs some checks and balances. I also plan to put a couple more fields here, like "Stop altogether if the system has lost $X", where I specify what "X" is.

Thinking Stuff System Screen - Long Description

Thinking Stuff System Screen - No Trading


Profit Factor is (Av Win / Av Loss) * (Pct Winners / Pct Losers). If it equals 1, the system has neither lost nor made money. If it's less than one then the system is losing money, more than one and the system is making money. The bigger the number, the "better" it is. But this calculation falls down in that it doesn't take into account the total number of trades, or the total amount won/lost.

(5/4) * (55/45) equals (500000/400000) * (55/45).

It does.

Really.

Further, just because the last 5 trades have a Profit Factor of 2, doesn't mean the next trade is going to be any good. In fact, a scenario of events could take place where this rule keeps you out of the good trades and let's you get in on all the bad trades. That's why I'm going to add the "stop altogether if..." rule I mentioned above.

And why is it split out between Long Trades (where you profit from the price rising) and Short Trades (where you profit from the price falling)? Because in my back-testing to date, I've discovered that the markets are not mirrored.

Most trading systems I've come across have the exact opposite rules for going long as they do for going short. They've mirrored them, maybe for "neatness" or something. But no, unless those rules have been backtested as mirroring being the best way to go, it should not be this way. Effectively the rules for long trades and the rules for short trades create two different systems - just most people think of them as one system.

If you don't have them mirrored though, you run the chance that your rules tell you to be in both a long trade and a short trade at the same time. Indeed. This is called "hedging". If you are hedging with equal amounts of units purchased, effectively you will neither be making nor losing money, whatever the direction the price is going. Not such a big problem. Unless...

You're using Oanda, who don't allow hedging in the one account - what would happen here is that whatever you did second would close out whatever you did first. You wouldn't be in two trades - you'd be in zero trades. And probably you'd get angry that happened. Fortunately you can create multiple accounts with Oanda, and use one for Long trades, and the other for Short trades. Simple enough to set up with my software - you'd just create your system for the Long trades, then click the Copy button which will copy all the settings across to a new system. Modify the Trading Rules to be for going short ins tead of long, modify the account to be used. That's it. As I said, my theory is that the Long and Short Trading Rules are effectively two systems in one anyway.

By the way, a very simple example of a "mirrored" system is using a Moving Average (MA) of the close price. (I won't go through *all* the rules needed to make a system):

For Longs:
If the old close price was below the MA, and the new close price is above it, go long. Set the initial stop loss to the value of the MA. Every bar adjust the stop loss so it remains at the same value as the MA.

For Shorts:
If the old close price was above the MA, and the new close price is below it, go short. Set the initial stop loss to the value of the MA. Every bar adjust the stop loss so it remains at the same value as the MA.

With this system, the long trade is closed by its stop loss when the price falls through the MA. If the close of that bar is still below the MA, a short trade is entered. And vice versa. It's all very neat, and we can never be in both a long and short trade at the same time.

But as I said, my theory is that the markets are not mirrored. It's worthwhile to have two accounts so you can have different rules for going long than for going short.

Back to screenshots. No, wait - Risk To Return. With a stop loss AND take profit set, you can know exactly before the trade what your potential risk to potential return is. That's not what I'm talking about here. If you don't use a take profit then you can't calculate this. So my setting is based on actual results from the past X trades - how much did we actually risk vs how much did we actually get. If that number is falling too low, might be better to sto p until things get back to normal.

Okay, now back to screenshots. Holidays (left) allows you to specify dates/times that the system should stop trading. Major announcements can cause extreme spikes in the price, even if 20 minutes later it's back to exactly where it started. It could be annoying. Liquidity is thin during holidays. So, here you can tell the software when to not take any new trades, and when to get out if it's in one.

The Accounts tab is where you can specify which accounts to use, which currencies to trade in those accounts, and whose data feed to use to make the decisions.

Thinking Stuff System Screen - Holidays

Thinking Stuff System Screen - Accounts


Finally, the Alerts tab (left). You can specify an email address, or many, to send an email to when the system does stuff - any or all of Place Order, Cancel Order, Enter Trade, Exit Trade. Or the alerts could go to the screen. Or both.

If the alerts goes to the screen, it comes up in this Messages window (right). Nothing really interesting to say about that one.

Thinking Stuff System Screen - Alerts

Thinking Stuff Messages Screen


Phew. Once that's all set up, you go to the Back Testing screen (left). Select one or many systems to test, the time period to test them over, and click Start.

The results are spit out in comma-separated format, but with extra commas so it formats well in Excel. The software has no charting facility at present, but if you specify to make a data file as well, I have an Excel Macro which pretties up a nice chart (right). I can see exactly what the system is trying to do.

You can see on this particular chart that the system didn't do too badly for Long trades, although it missed a major upward move in the middle. On the other hand, Short trades were dismal. Which made me think (along with many other experiments) that the markets are not mirrored.

Thinking Stuff Backtest Screen

Thinking Stuff Sample Chart



And that's pretty much it. Might show how easy it is to create/modify/copy a System at a later date.

Please, I'd appreciate any comments.

 

3 Comments:

Ryan Sheehy said...

G'day Mark!

Interesting program you have here!

I have some questions regarding it, if you don't mind:

1. Where does the program pull its data from to generate live buy and sell signals?

2. Can the process be tested on Oanda's FXGame platform before going live?

3. Can the user input their own system? Or will they need you to program it for them?

4. Is there a system that can strangle the markets at a specified time entered by the user? This would be a great feature to have as sometimes I'd like to strangle the market when economic announcements are released but cannot physically BE in front of the computer when these announcements ARE released. An example of this system would be... at 827 NY EST (3 minutes BEFORE the 830 NY EST announcement) place a buy entry stop and a sell-entry stop at 20-day 2-StDev Bollinger Bands distance away, or the highest high of the last 20-days (and lowest low of the last 20-days), or maybe just an arbitrary figure like 20pips away from whatever the closing price is 2-3 minutes prior to the announcement! I guess the possibilities are endless!

5. Is it possible to set up an OCO order? I know Oanda doesn't allow this, but let's say that if a system mentioned in step 4 were able to be created and I wanted to strangle the market - would it be possible to cancel the order that wasn't hit to prevent any whipsaw action that may happen?

6. If an OCO cannot be set up would it be possible to cancel at a specified time? Or at times different to Oanda's? I guess it would need to rely on the computer's own internal clock (wouldn't want there to be a power surge... but that's okay, I've got a laptop... which brings up another interesting question...)

7. Would it be possible to read the amount of battery power left in a laptop and to have some preset configuration where if the laptop gets to within 10% remaining on its battery life all open positions are closed or all pending positions are closed (or both... depending upon the users settings)? Ok, maybe I'm getting a bit too carried away now!

8. With your systems are they all on the same time frame? Or can the user select their own time frame?

9. Can orders that have been executed be sent to an email address? The benefits of this would be to receive an alert via my mobile phone when I'm out (just need to make sure that the email message doesn't have any more than 150 characters!). Hopefully the alerts that are sent via email are also within the 150 character limit!

10. Just to confirm: this program allows you to automatically trade any system that is programmed into it?

11. Lastly, would it be okay if I informed my visitors about your site?

Thanks Mark. Hear from you soon.

Ryan

Fri Jun 10, 11:27:39 AM EST  
Ron said...

I have a suggestion or you may call it a request when designing your Automatic trading system. I have a trading system I have been trying to make Automatic for some time. Let me explain. The trading system is simple really. I trade three indicators on one currency pair - 1) DMI - CROSS 2) MCAD - CROSS & 3) two-EMA - CROSS. Unlike other trading systems that use multiple indicators to confirm one trade, I trade each indicator on their crosses. Buy when they cross up and Sell when they cross down (each). It is designed to always be in the market. Each position changes direction as the crosses dictate. Obviously I have three trades going simultaneously and on the same currency pair. Also all position need to be set to "Hedge" for each indicator will not always agree to the direction of a trend. I have been trading this system manually for awhile and find good success with it. The parameters I use for each indicator results in little or no loss in a flat market. When a good strong trend hits, each indicator will turn into it and now I'm riding it. I would like to make this automatic for I am not always at my computer when a crosses hit, thus I do not always catch them. This sometimes results in loss when in fact, if I could have caught it at the cross would have been profitable.

Most platforms I have studied so far only offer simple Buy/Sell trades with their automatic features. Some do offer hedging but can not differentiate or coordinate one trade with another. Resulting in the wrong trade being reversed when a new cross is signaled.

Can you create an automated system that can hedge AND can coordinate between trades? If so my system can truly become automatic.

I don't know how to program so this would really be something I would be interested in. I'm excited to see what you come up with.

Thanks for asking for suggestions.

Ron

Tue Jun 14, 04:31:56 AM EST  
Sharky said...

Hi Ron.

With this software you wouldn't be able to put all of those rules into the one "system". Because *all* of the entry rules must evaluate to being true for an order to be placed.

It doesn't matter though - you can just create three different systems and link them all to the same trading account. The end result is exactly the same.

Your first system is the DMI cross.

Second is the MACD cross.

Third is the two-EMA cross.

So the short answer is yes, this software would be able to accomplish your goal.

On the other hand, maybe your broker doesn't allow hedging. Oanda doesn't. In that case, create two accounts - one for your long trades, and one for your shorts.

I'm not sure I explained this well. Please keep the questions coming.

Mark.

Tue Jun 28, 12:09:29 PM EST  

Post a Comment

<< Home