Saturday, November 17, 2007

Buy-Open Sell-Close system

A Basic System - Buy Every Open and Sell At the Close

Here is a trading system based on 3 lines of code only, Buy Open Sell Close System. You can use the automatic Installer to install the indicator into your NeoTicker.

The system effectively consists of only 3 statements,

longatmarket (true, defaultordersize);
longexitnextclose (true, defaultordersize);
plot1 := currentequity;

What it does is very simple - it buys on every open and then exit at the close of the same bar. The performance of the system is reported through the plotting of the equity level.

I will explain the usage of each function in details.

LongAtMarket (condition, ordersize)

The LongAtMarket function instructs NeoTicker to place an order to buy at market with enough size (contracts or shares) to make the system net long at the ordersize, if the expression condition is evaluated to true (a value not equal to 0).

For example, if the trading system is flat at the moment, then LongAtMarket (true, 100) will buy 100 shares or contracts. On the other hand, if the system is currently short the market 200 shares or contracts, then LongAtMarket (true, 100) will buy 300 shares or contracts to make up the difference so that the system will become net long 100 shares or contracts when it is filled.

The condition parameter true is a constant which always evaluate to 1, that will ensure the LongAtMarket function to place an order every time it is evaluated.

Notice the function DefaultOrderSize used at the ordersize parameter is a trading system function that returns the user defined DefaultOrderSize which is set in the Indicator Setup window, or, through using the Symbol Info Manager.

LongExitNextClose (condition, ordersize)

When the parameter condition is evaluated to true (any value not equal to 0), an order will be placed to reduce the current long position by the size of the ordersize parameter at the close of the next bar. If the trading system is currently in a flat situation (no position at all) or in a short position, then even if the condition is evaluated to true, no order will be placed.

For example, if the system is net long 200 shares, then LongExitNextClose (true, 100) will place an order to sell 100 shares at the close of next bar. Or, if the system is net long 100 shares, then LongExitNextClose (true, 200) will place an order to sell 100 shares only, because the total long position is only 100 shares.

CurrentEquity

The CurrentEquity function is an account information function. It returns the current equity level of the trading system based on the accumulated performance of the system so far throughout the history of the chart.

For this simple system, we have already used 4 different trading system functions!

The System Performance

Here is how the chart looks like when the trading system is applied to daily emini S&P over the past 5 years.

One important issue related to trading system is the settings that you can adjust in the Indicator Setup window. For emini S&P, you need to set the price multiple to 50 and minimum tick size to 0.25. You can also change the Default Order Size right in the same system tab,

It is pretty obvious that this is a system that lose money. Well, the market is not really friendly towards somebody who simply try to take advantage of the classic market bias of going up over time, isn’t it?

A Twist In The System

Here is a new version of the same trading system with basic filtering, Buy Open Sell Close System Filtered. You can use the automatic Installer instead.

What this new system does is that it only trading on days that meet our criteria specified in the parameter.

makeindicator (cond, fml, data1, param1);
longatmarket (cond, defaultordersize);
longexitnextclose (cond, defaultordersize);
plot1 := currentequity;

Since I want the flexibility to change the condition easily for research purpose, the parameter is declared as a formula parameter, that enables the user the ability to access the Formula Editor when necessary. In our code, however, it is simply a string and in order to utilize it, we need to use the fml indicator to evaluate the formula stored within the parameter.

For example, if we specify the following condition to not trade on Tuesday and Friday,

The system will perform so much better (well, at least making some money) I think you will be shocked,

Summary

Remember that robust and profitable systems do not have to be very complex, its the ideas behind that counts. The usual obstacle for most people is that they do not know where to start and how to translate their ideas into something that can be backtested properly.

The subject of trading system writing, however, is very complex; due to the fact that there are so many types of trading systems based on vastly different underlying concepts. I will try my best to walk you through the basics techniques, and provide you with the necessary tools, so that you can translate your ideas into trading systems you want.