1. Trading Systems: Introduction
  2. Trading Systems: What Is A Trading System?
  3. Trading Systems: Different Markets and Types
  4. Trading Systems: Components of a Trading System
  5. Trading Systems: Building a Trading System
  6. Trading Systems: Other Considerations

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

from pandas.io.data import DataReader

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['short_ma'] = pd.rolling_mean(ticks['ask'], self.short_window, min_periods=1)
signals['long_ma'] = pd.rolling_mean(ticks['ask'], self.long_window, min_periods=1)

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.

Trading Systems: Other Considerations
Related Articles
  1. Trading

    Backtesting: Interpreting the Past

    We explain what backtesting is and offer some tips on how it can help refine your current trading strategies.
  2. Personal Finance

    11 Things You Pay For That Libraries Have For Free

    If it's been a few years since you ventured into a library, you'll be amazed at what they offer - for free!
  3. 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.
  4. Trading

    The Pros And Cons Of Automated Trading Systems

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

    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.
  6. Investing

    Steve Cohen's Point72 Invests $250M in Quantopian

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

    No Forex Strategy Of Your Own? Try Mirror Trading

    There are many advantages to trading a mirror strategy, yet markets are dynamic, and regardless there is always a risk of losses.
Frequently Asked Questions
  1. Are 401(k) contributions tax deductible?

    Find out what the tax advantages of a 401(k) retirement plan are. Learn what tax brackets gets the most benefit and why deferred ...
  2. Shipping Freight: CFR versus CIF

    In shipping freight, find out about the difference between cost and freight (CFR) and cost, insurance and freight (CIF), ...
  3. If a country's currency is determined by the strength of its economy, why isn't the U.S. dollar worth more than the British pound?

    When one country's currency is worth more than another, it does not necessarily indicate a stronger economy. Here's why.
  4. Annuities: Present value versus future value

    Learn about the benefits and differences between the future value and present value of a fixed annuity, and how to use these ...
Trading Center