You should now be familiar with some common elements that make up a trading system, the advantages and disadvantages of using them, some of the different markets and strategies that can be used to build them, and the basic components of a trading system.

We will now look at how to build a basic trading system from scratch. While this trading system won't be optimized for profit, you will learn how all of the different components fit together to make a functional trading system.

Choosing a Market, Strategy & Technology

We will target the foreign exchange (forex) market since the data is freely available from GainCapital and other sources. For the strategy, we will be employing a very basic moving average crossover strategy whereby we go long if a short-term moving average crosses above a long-term moving average. And finally, we will be using the Python programming language and the popular NumPy, pandas, and matplotlib libraries to read the data and execute the strategy.

We will assume that you're familiar with the Python programming language and have it properly installed on your computer. If you're not, visit the Python website for learning resources or you can implement the same functionality in other languages and platforms.

Setting Up the Script

The first step is to create a file, called ma_cross.py, that will house the strategy. In the file, we will start by importing all of the libraries that we will need.

import datetime
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

The pandas library includes a `rolling_mean` function that will create moving averages based on the bid or ask price for each tick in the forex market. Once the moving averages are finished, we will construct a `signal` series by setting the column equal to 1.0 when the short moving average is greater than the long moving average or 0.0 otherwise. We can then use the `positions` to generate trading signals that can be send elsewhere.

Writing the Strategy

The strategy can be implemented in Python.

class MovingAverageCrossOver(Strategy) :
def __init__(self, pair, ticks, short_window=100, long_window=400):
self.pair = pair
self.ticks = ticks
self.short_window = short_window
self.long_window = long_window

def generate_signals(self):
signals = pd.DataFrame(index=self.ticks.index)
signals['signal]] = 0.0

signals['signal'][self.short_window:] = np.where(signals['short_ma'][self.short_window:] > signals['long_ma'][self.short_window:], 1.0, 0.0)

signals['positions'] = signals['signal'].diff()

return signals

This code generates a series of signals whenever a moving average crossover occurs, where 1.0 signals a buy order being placed.

Putting the Code to Use

The next step is to take this code and use it in conjunction with a backtesting strategy to see how it would performed in the past.

Most traders prefer to use online backtesting tools, such as Quantopian, where you can upload code and automatically see the results. Using these tools, backtesting is as easy as importing Quantopian's libraries into Python and pasting your script. Then, you can run a full backtest using simulated dates, account values, and even markets. You can then see returns, alpha, beta, Sharpe ratios, and maximum drawdowns to get an idea of how the strategy would perform.

The next step would be to integrate the strategy into a live trading environment. Many brokerages that offer automated trading will include APIs that you can interface with to place trades. For example, InteractiveBrokers has an API complete with libraries for Python, Java, .NET, and other technologies. Using these libraries, you can easily turn signals generated into trades that are executed through the platform.

Coming Up

In the next section, we will look at some other important considerations to keep in mind.

Related Articles
1. Personal Finance

A Day In The Life Of A System Trader

Systems traders divide their time between trading, developing, backtesting, optimizing and forward testing, to create viable and high-probability trading systems.

Automated Trading Systems: The Pros and Cons

Automated trading systems minimize emotions, allow for faster order entry, lead to greater consistency and resolve "pilot error."

Trade Forex With A Directional Strategy

Opening long or short positions to cash in on a trend is one of the simplest ways to trade forex.
4. Investing

Steve Cohen's Point72 Invests \$250M in Quantopian

Quantopian is a web-based platform that aims to help day traders become hedge funds.

Top 4 Things Successful Forex Traders Do

By blending good analysis with effective implementation, you can dramatically improve your profits in the forex trading market.

Simplicity can be a trader's best friend. Here is a simple day trading strategy which takes advantage of a stock's dynamics.