Designing a Simple Strategy and Introduction to Pseudo Code

Today we will be looking at designing a simple strategy and we will also start to write some pseudo code in MetaEditor. This is of course a multi part tutorial series and if you have not read the previous articles I suggest you do so before moving on to this article. In the previous article on Expert Advisors (EA) and indicators we touched on the different ways we can automate trading, today we will focus on Expert Advisors (EA).

 

Strategy Design

Designing a trading strategy could be the focus of whole encyclopedia, what we discuss here will simply be a quick overview. In it’s simplest from a trading strategy is simply  a set of predefined rules for executing trading decisions. By using an automated trading strategy emotional bias is removed. There are several different types of trading strategies that have been used over the years and usually fall into one of the following categories: 

  • Fundamental Analysis based systems.
  • Technical Analysis Based systems.
  • Volatility based systems
  • Mean reversion based systems.
  • Trend Following systems.

Building a Trend Following Based System

We will be building a Trend Following system, this will be a very basic system and will help highlight the development process. When developing a trading system it is very tempting to try and re-invent the wheel, however we will start with a tried and tested system that is publically available. The trading system we will be implementing is the original Turtle Trading System and you will need to download a copy of the rules in order to continue with the tutorial.

Learning to Write Pseudo Code

Before reading any more make sure you have printed out and read through the original Turtle Trading System Rules document. This step is very important as the only way to learn this materiel is by taking action and carrying out the steps outlined.

STOP!

Now we can get on with our discussion of pseudo code. When it comes to writing computer programs or in this case automated trading systems translating English into programming code can be fraught with danger. Pseudo Code on the other hand refers to a more structured English for describing a process or program. It is important that you learn to write quality pseudo code as it will make a big difference when you come to coding your system your self or hiring a programmer.

There are many benefits in writing your algorithms or trading logic in pseudo code these include:

  • You will gain a detailed understanding of the program or algorithm you are developing.
  • Errors in logic picked up early.
  • Not constrained by a single programming language.

Below is a brief example of pseudo code:

IF movingAverage1 == movingAverage2 THEN
    SEND buyOrder
END IF

Structured Pseudo Code

In order for us to write quality code we have to take a step back and think about what it is we are actually trying to achieve. Rather then using a trading example we will use an example that everyone is familiar with. Think about the process of going to the shop and buying 1 item let’s say it is milk. What I will ask you to do is sit down and write a step by step process that you could give to a an alien that describes the process in as much detail as possible.

STOP!

Do not move on until you have done this.  Below is the first iteration of solving the get milk problem:

Get into the car –> Start the car –> Drive to the closest shop that sells milk –> Get Out of Car –> Lock Car

Enter shop –> Locate milk –> Determine quantity –> Pay for milk –> Locate car –> Unlock Car –> Drive home.

As you can see if you think about the steps required to carry out this task it is much more complex then first thought. Let’s try and make this a bit more detailed iteration two:

DEFINE approxMilkCost

DEFINE moneyInWallet

 

IF moneyInWallet < approxMilkCost THEN

           CALL enterCar WITH keys

           CALL driveTo WITH bank

           CALL leaveCar

           CALL getMoneyFromBank WITH accountDetails

           CALL driveTo WITH shop

           CALL leaveCar

ELSE

          CALL enterCar WITH keys

          CALL driveTo WITH shop

          CALL leaveCar

ENDIF

 

enter shop

CALL locateItem WITH milk

CALL payForItem WITH money

leave shop

CALL enterCar WITH keys

CALL driveTo WITH home

CALL leaveCar

You can quickly see that by using a more formal language it breaks down complex tasks into simple steps. If we wanted to write a program to solve this problem we would probably go through several more iterations of pseudo code before actually starting coding. I have created a PDF file and have uploaded it so you can fully understand the pseudo code presented and can practice writing some pseudo code for yourself.  In the next article we will be looking at writing our trend following system out in pseudo code so make sure you review everything presented here.

Popularity: 14% [?]

Tagged with:
 

Moving to Mac: You Can Have it All

Just recently my trusty old Pentium 4  trading computer died and I was faced with a dilemma what computer system am I going to buy. Really there are only two options Mac or PC. I have been using both windows and Mac computers for well over ten years and I have to say that I prefer Mac and OS X over Windows any day of the week. The problem is there is very little trading software written that supports Mac. I asked myself some simple questions:

What do I need from a computer

  1. Large Screen.
  2. Needs to run all trading applications.
  3. Needs to be fast. (Not ultra fast).
  4. Ability to develop for multiple devices.
  5. Decent amount of RAM.
  6. Large hard drive.
  7. Simple.
  8. Good Value for money.

With all these requirements in mind I decided to go with the new 27 Inch iMac model. You can read about the full specifications of this machine 27” iMac, below is a quick rundown:

  • 3.06 Intel Core 2 Duo
  • 4 gig of RAM.
  • 1 terabyte hard drive
  • 27 inch backlight LED screen (love the resolution of this machine)

Running my Trading Software

Here is where things get a little more complicated if you want to run most trading software you will need to run Windows. Essentially you have three options for running Windows based software:

  1. You can use a virtual machine.
  2. You can use Crossover Office or Darwine (note: .NET not supported).
  3. Use boot camp and dual boot with OS X.

Each option has it’s strengths and weaknesses, however after weighing all the options I have decided to go with Parallels Desktop version 5. Parallels has excellent integration within OS X and coherence mode allows me to run my Windows applications and OS X applications side by side.  Below is a screenshot:

Windows on OS X - Click for Larger image

So far in my limited testing I have not come across an issue running my applications in a virtualized environment. The applications run fast enough for my needs and I can export my virtual machine and run my trading system from multiple locations. It will be interesting to see how this virtualized environment performs with some statistical arbitrage systems. I am also looking forward to getting 64 bit version of Matlab up and running under OS X.

Popularity: 1% [?]

Tagged with:
 

Building an Automated FOREX System: Expert Advisors & Indicators

IndThis is article number two in our tutorial series Building an Automated Currency Trading System from Scratch. If you have not read the introduction to this tutorial series I recommend you read building an ATS before reading this article.

MetaTrader 4

The current version that is most widely used and supported is MetaTrader 4 however beta versions of MetaTrader 5 are currently in testing. In order to build an automated trading system using MetaTrader 4 we will need to write a program that will run inside MetaTrader. There are three types of programs that run inside MetaTrader, these are Custom Indicators, Expert Advisors and Scripts. The screen shot on the right shows the built in indicators that ship with MetaTrader.

MetaTrader- MetaQuotes Language

Custom Indicators, Expert Advisors and scripts are all written in a programming language called MetaQuotes Language, often abbreviated MQL.  Although all three application types are written in MQL there are slight differences between program types.

MetaTrader- Indicators and Custom Indicators

Custom indicators are written in MQL the client terminal will execute the indicator every time a tick is received. The main use of indicators is to graphically display some user defined or built in data relationship. The most common use for indicators is for Technical Analysis. Below are some screenshots which show a built-in indicator being applied to the daily AUD/USD currency chart.

  1. With AUD/USD chart open double click on desired indicator.
  2. Enter parameter settings and execute.

Indicator Setting MetaTrader

MACD Indicator

MetaTrader- Expert Advisors

An Expert Advisor or EA for short share similar properties to Indicators. Expert Advisors or EA’s are written in MQL and are executed on tick data. Expert Advisors or EA’s have trade logic written into them. Basically an EA does everything an indicator can do, however an EA will also open, close and modify trades programmatically. To attach an EA to a currency pair the process is exactly the same as it is for an indicator. There are two sample EA’s that ship with MetaTrader 4 these are the MACD Sample and Moving Average.

MetaTrader- Scripts

Scripts are the final type of application that are available in MetaTrader. A script is designed to be executed only once. Scripts are usually used to automate client side non-trading actions, like modifying data. They can however be used to perform simple trade logic tasks.

MetaTrader – MetaEditor

All the code we write for our automated trading system will be done MetaTrader’s Integrated Development Environment called MetaEditor. MetaEditor allows you to store and manage source code for all projects in an easy to use way. If you are used to Visual Studio or X-Code IDE’s then you will be disappointed with the functionality of MetaEditor. MedEditor does offer syntax highlighting and code completion which is very handy. There is also a built in on-line library that covers most topics. Below is a screenshot of the IDE:

MetaEditor

To open MetaEditor from MetaTrader simply press F4 or from the menu bar:

Tools –> MetaQuotes Language Editor

Home Work

Before next article make sure you understand the three different program types that are used in MetaTrader. Also try attaching and using some of the built in indicators in MetaTrader. Before moving on I would also recommend downloading the MQL4 book from MetaQuotes. This is a free book that covers the basics of programming in MQL and in later articles I will assume you are up to speed with the concepts presented in the book.

In the next article we will discuss designing a simple trading strategy and pseudo code.

Popularity: 2% [?]

Tagged with: