Friday, December 28, 2007

London calling . . .

Today's post will be fairly brief, on a topic that usually requires a lot of time and explanation: Importing data from other packages.

(So brevity is a good sign here.) For those that obtain data from other providers it is of course important to use it in RightEdge easily. Well, it's always possible to use your provider's API and write a plugin that allows RE to pull from it directly. For most retail users this may be overkill and so the developers created a very flexible Bar Data Import Tool. I'm subscribing to Sharescope (www.sharescope.co.uk) and I want to import FTSE100 price data into RE (you could get the symbols from Yahoo! and use that data ... if you trust it).

So a few simple steps:

  1. Open Sharescope and bring up the Export Options (File > Export > Export options)
  2. Make sure you tick 'Include OHLC ...' and 'Use adjusted prices ... '
  3. Choose a date format of your liking, the nice thing is it doesn't matter to RE.
  4. Click 'Ok' and go to File > Export > Export prices for shares in list
  5. Select a format - I used Metastock because that's what was easiest to import into Wealth-Lab, but again it doesn't matter.
  6. Pick a folder and hit 'Ok'
  7. Open RE and go to Tools > Import Bar Data
  8. In this window, browse to the folder and select the files you'd like to import
  9. The go to the 'fields' tab and make sure you've selected the right delimiter
  10. Doing that you should see that the lower part of the dialog box shows your fields with 'Ignore' as the column label
  11. Go to the 'Format' tab and change the date format to what you told Sharescope to export; if you did something wonky you can piece it together under 'Advanced'
  12. No click on the column headings and tell RE which field is which (tip for Sharescope the first historical prices are the same, i.e. no difference between OHLC so it's hard to see which is which. If you've clicked the right box in (2) then it'll be in the order OHLC and Volume.
  13. Et voila - click the button and RE will create a new watchlist with a symbol for each file, which you can rename manually.
  14. If you rely on having the right currency and contract information attached to each of your symbols you can add this information under 'Fields' by simply adding a custom field with 'Currency Type' set to 'GBP'.
That's it. Hope it helps.

Wednesday, December 26, 2007

One more thing . . .

Ok, so I quickly wanted to test a three bar pullback pattern as referenced in Active Trader, October 2007. I coded up the following. Notice that it seems that the LeadBar property set for the Backtest seems to not work, hence the additional if statement. For beginners, also note that I'm using a very simply additional function that returns true if the bar passed to it was a 'down' bar.


using System;
using System.Drawing;
using System.Collections.Generic;
using RightEdge.Common;
using RightEdge.Indicators;

public class SystemMain : SystemBase
{
public override void Startup()
{
// Perform initialization or set system wide options here
PositionManager.PositionTimeOut = 7;
PositionManager.ProfitTarget = 0.03;
}

public override void NewSymbolBar(Symbol symbol, BarData bar)
{
// This line of code runs the actions you have set up in in the Project Form
IList bars = Bars[symbol];
int t = bars.Count-1;
// Should not be necessary but apparently LeadBars Property is ignored
if (bars.Count > 12)
{
if (bars[t-1].High < bars[t-2].High && bars[t-2].High < bars[t-3].High)
{
if(bars[t].High > bars[t-1].High)
{
if (DownBar(bars[t-1]) && DownBar(bars[t-2]) && DownBar(bars[t-3]))
OpenPosition(symbol, PositionType.Long, OrderType.MarketOnOpen);
}
}
}
Actions.RunActions(symbol);
}
private bool DownBar(BarData bar)
{
return (bar.Close < bar.Open);
}
}


... and if someone could tell me how to force blogger to indent code properly, I'd be most grateful.

Details . . .

As promised, this post aims to provide some background to what's happening in our first system.

Initially, we defined an indicator by calling:

Indicators["MA"].CreateIndicator(new SMA(20))

  1. Indicators["KEY"] is a property of the SystemBase class that returns an instance of the IndicatorManager Class. More precisely, it returns an object of type SymbolIndicatorCollection for which "KEY" is the key to a specific indicator instance held in the collection.
  2. CreateIndicator is a member method of this selection and takes an argument of type ISeries, which we supply by creating a new object SMA(int period).
My C# is a bit rusty but I think CreateIndicator actually expects any object that implements the ISeries interface, so not really an object (well, I guess an interface is also an object). Any C# buffs out there please advise what the correct interpretation is here.

The remaing commands set the inputs for our SMA to the open price and tell RE to show them on any charts the user calls up from the system.

  1. SetInputs is a member method of the SymbolIndicatorCollection it takes a generic 'object' as an input. This method is overriden for each Indicator, so that it deals with the parameters as necessary. Here we have used a member of the BarElement enumeration, which encompasses commonly used constants such as Open, High, Low, Close, Volume, etc.
  2. AddToCharts is also a member method of the SymbolIndicatorCollection but it does not require any further inputs.
Moving on to the NewSymbolBar function which is called for each new bar while running through the data for each symbol in a watch list, we first note that it takes two parameters:

  1. Symbol which is a class in the RightEdge.Common namespace and maintains information about the asset the symbol represents (Assetclass, Contract Type, etc). This may not be very interesting for stocks but could be very important for futures, options, etc. (think of roll-over dates, etc.)
  2. BarData which is also a class in the RightEdge.Common namespace and contains the information for the current day (for the current symbol). Typical information such as Open, High, Low and Closing price but also Volume and Open Interest are passed to your system through this variable.
The next thing we do in this function is to define local variables that represent the indicator and other data items we might use to figure out whether we're buying today or not.

  1. ISeries MA = Indicators["MA"][symbol] defines a variable MA of type ISeries (which is a basic object/interface defined in RightEdge.Common). As we have seen above, we use Indicators["MA"][symbol] to retrieve the moving average timeseries from the Indicator collection. The first part of the expression returns the collection of calculated indicator time series (SymbolIndicatorCollection); the second angular bracket returns the specifc calculate time series for the current symbol (and is of type ISeries).
  2. IList bars = Bars[symbol] defines a variable bars that is actually a list of BarData items (see above). It does this by using the Bars property of the SystemBase class (which returns an object of type list).
In particular this last nugget of information is crucial and took me a while to figure out by looking at some of the examples and some forum posts (again, the Developer's manual only lists the pieces, these posts are trying to show you how they fit together). You will likely need to refer to information about yesterdays, or last week's bars a lot and this is how you get the data into your system. In Wealth-Lab, this would be akin to functions like PriceOpen(), etc.

The if statement is then fairly straightforward - we access yesterday's moving average value by using the indexer [] provided by the ISeries object, which returns the value at a specified index from the start of the simulation period (i.e. ZERO to today, hence the use of the count property to return the index for today's value). Note that we are using yesterday's value: NewSymbolBar looks at today, so placing an order today, potentially for today's close on today's data would not be a good idea (looking ahead). We use two conditions to identify a crossover, based on closing prices.

Finally, we use the OpenPosition method of the SystemBase class to execute a buy order at today's open price for this symbol and bar. Notice that this function would also take a limit price and, where required, a number of shares.

That's it for tonight. Hope it helps and sheds some light.

Footnote: Some of the function calls are confusing because the go back to methods of the base class and it seems that some variables are conjured up out of thin air. For example the Indicators["MA"] call is really a this.Indicators[] call and access an object defined in the base class. If you're well versed in Object Oriented Programming then this is easy, but it helps me (semi-novice) in my thinking.

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).

Preparations

Alright, with the software installed let's try and write a simple system. We will do this by using actual code, which may be slightly more complex than using the Drag'n'drop (DnD) designer but allows us to do more powerful things later on. I may write a post in the future on this topic, in the meantime, if someone wants to write an intro to DnD, let me know, I'll be glad to post/link it.

Before we do anything, we need to get some historic data. RE does come with a plug-in that allows us to retrieve data from Yahoo Finance. First, however, righ-click on the 'Symbol Lists' in the Watchlist Pane and select 'Configure Folder'.

This brings up a menu for the configuration of this 'service'. (For all intends and purposes a service is a plug-in that provides data. Why the distinction? Not sure.) Make sure you select a suitable start date to get at least a few years of data. Click ok and then select the watchlist you'd like to get the data for by ticking the checkbox in the watchlist pane, e.g. DOW30. Then click on the 'Update Historical Data (Checked Symbols)' icon, which is the second from the left in that pane. Depending on the speed of your connection, the time of day and the length of requested history it'll take a while.

Once this is done, right click on one of the symbols in the watchlist and select 'Quick Chart'. This should bring up a chart for that symbol (assuming you did download data for it).

You can now try and drag indicators, lines, labes, etc. onto the chart from the Indicators pane. You can save your creation for later use --- I'll write more on this later (or, again, will gladly accept someone else's article/link).


Terminology:

1. Plug-in: RE is plug-in based to allow for extensibility by third-party components. Data providers, Data stores, Indicators, System Evaluators and others are all plug-ins.

2. Historical Data: A general term for all data system may use, typically time series data consisting of at least Open High Low and Closing prices for the day. It may, however, also cover other other forms of data, such as fundamental data, moon phases, etc.

Installation and Setup

Ok, so you've gone to www.rightedgesystems.com and signed up so that you can download the free trial. I'm not going to talk about the installation here as it's pretty straight forward. One thing I'd like to note though is that I haven't managed to get the SQLEXPRESS data store to work and I have written to the developer's forum to get this resolved.

Once the installation is complete, you will find RE in the start menu. After the first startup you're faced with something like this


This may seem somewhat barren and hostile, so let's take it step by step. The RE environment has been separated in several windows or panes (all of which can be detached, hidden, moved around, etc.).

  1. The menu bar is pretty standard
  2. The Indicators pane shares the window with two other panes, Chart Objects and System Components. These provide an overview of the available indicators, other objects one can place on charts (lines, support levels, etc.) and System Components (such as buy triggers, conditions, etc). All of them can be used for dragging and dropping a system together from basic components.
  3. The watchlist window. Displays the available watchlists and is used to select which symbols a system is run on.
  4. The Output pane, will contain messages from the compiler, the system itself or show live, incoming data for a realtime system.
  5. The big grey area in the middle, will house the editor or the drag'n'drop composer
  6. The trading system pane will show the components of the currently open system, such as source files, indicators used, etc.
  7. The property pane can be used to edit properties of certain objects that are selected (e.g. Starting Capital for a simulation).
For those with a programming background the environment will feel familiar as it is close to most compilers/IDEs.


Key Points/Terminology:

1. Datastore: A term used in RE to denote the database that your historical data is saved in (or that new, incoming data gets written to). RE supports it's own proprietary store (a bunch of xml files), a Jet/Access DB store and a SQL DB store out of the box. For our examples, the standard setup should suffice.

1st Post

This blog will document a beginner's first steps in RightEdge (www.rightedgesystems.com), a trading system development environment built using .Net languages (rather than proprietary scripting languages such as those in Tradestation, etc.).

Some familiarity with C# or Visual Basic is assumed but no detailed or expert knowledge is necessary to follow these articles. In fact, I suspect that I will have to re-learn a lot of programming concepts on the fly. The interested reader is referred to:

http://www.csharp-station.com/Tutorials/Lesson01.aspx