Wednesday, December 26, 2007

The First System

All right. Here we go. Click on File > New > Trading System, give it a name and click ok. You will now see that the Trading Sysem pane has been populated. Double click on the last file in the list, which should be called 'Your Chosen Name.cs'.

This will open this file in the main work area. As you can see the basic code has been provided for you. It starts with a number of 'using ... statements' that include other libraries. Note the two references to the RightEdge.Common and RightEdge.Indicator namespaces.

Below this, we find our main class: SystemMain, derived from SystemBase. This class will do the majority of the work in a trading system. Note that two skeleton functions have also been provided:

  1. Startup: this is called at the beginning of the Simulation run and all calculations, preparation code, etc. that needs to be executed before we actually run through the system logic should be placed here.
  2. NewSymbolBar: this function is called for each new bar for each symbol as the simulator steps through time. This function should be used to evaluate buying conditions, place orders, etc. Note that two parameters are passed - the symbol and this day's data.
This may seem a little unfamiliar compared to other packages. In Wealth Lab, for example, systems are executed for each symbol individually and a special part of the program called the simulator is used to run a specific system on more than one symbol at a time. In RE, it doesn't make a difference whether the system is executed on 1 or 100 stocks (or FX pairs, or other tradables). In other words, the logic is as follows - the Simulator runs through all symbols in the watch list and for each new Bar it calls NewSymbolBar, passing along the symbol that it is on and the Bar's data.

For this example, let's write a small moving average system: (a) Buy when 'close' crosses above 20 day SMA calculated on open prices, (b) sell after either 10 days or 2.5% Profit.

To do this, let's first create the indicator by adding the following to the Startup function:

Indicators["MA"].CreateIndicator(new SMA(15));
Indicators["MA"].SetInputs(BarElement.Open);
Indicators["MA"].AddToCharts();

This sets up a new indicator for us, which we'll refer to as 'MA' in the code, it is the simply the 20 period Simple Moving Average. The second line makes sure that it is calculated on the Open price of each bar.

Now, let's add the actual functionality to the the NewSymbolBar function:

ISeries MA = Indicators["MA"][symbol];
IList bars = Bars[symbol];

if (MA[MA.Count - 1] < bars[bars.Count-1].Close &&
MA[MA.Count - 2] >= bars[bars.Count -2].Close)

{
OpenPosition(symbol, PositionType.Long, OrderType.MarketOnOpen);
}


If we select a symbol in the watchlist, and then click on System > Run Simulation (CTRL F5), RE will compile our strategy and run the simulator. After some time, it will open a window with results. (Go to Tools > Options and configure the desired output in the System Results tab).

Et voila ... your first RightEdge System, which may even turn a profit for some stocks (and with the benefit of hindsight).

If you are wondering how this all works and why it's happening the way it does then you're in the same boat as me, which is why I am writing these posts. There is a developer's guide available but at this point it pretty much only documents classes and objects in RE and is about as exciting as reading Microsoft's C# language specification

(sorry Daniel, Bill, etc.! ... but this will only be a great product if first-time users are taken by the hand and shown how the pieces fit together :)

I will take the above code apart in my next post to provide a bit of background on what's happening (or at least what I think is happening).

No comments: