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: 13% [?]

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:
 

Building an automated currency trading system from scratch

In the next few weeks we are going to explore the basics of designing an automated currency trading system. We are going to use MetaTrader as the basis for the trading system. The actual strategy we design will be for illustrative purposes only and NOT tradable. That being said the tips and techniques you learn over the course of this series will give you skills needed to develop your own trading strategy.

Before you start

Before we start designing the automated trading system I will assume you are familiar with some basic FOREX or currency market concepts. If you are new to the finance world you may like to read the following articles.

To get you quickly up to speed with MetaTrader you can download the software from MetaQuotes. You may also benefit from reading the following basic articles.

So now you have a basic understanding of the FOREX or currency market and have MetaTrader installed on your computer we are ready to get started. Below is a roadmap for next few articles.

  • What is an automated Trading System and how can we build one in MetaTrader?
  • Expert Advisors and Indicators in MetaTrader.
  • Designing a simple strategy and pseudo code.
  • From pseudo code to a basic trading system.
  • Garbage in garbage out: improving modeling quality.
  • Testing, Testing, Optimization and more testing.
  • Forward testing and performance evaluation.
  • Automated Trading System version 2.0.
  • So you are ready to go live: Choosing a broker.
  • So you are ready to go live: Setting up a Virtual Private Server.

As you can see there is a lot of content that needs to be covered and I am sure as this series progresses there will be content added or removed depending on your input. If there is any materiel you want covered please send me an email or comment below.

Popularity: 1% [?]

Tagged with: