<#-- Rebranding: Header Logo--> <#-- Rebranding: Footer Logo-->
  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

    The Importance of Backtesting Trading Strategies

    Backtesting is an important aspect of developing a trading system. If done properly, it can help traders optimize and improve their 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. Trading

    Using Technical Indicators to Develop Trading Strategies

    There is no perfect investment strategy that will guarantee success, but you can find indicators and strategies that will work best for your position.
  4. 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.
  5. Trading

    The Best Ways to Learn Technical Analysis

    Discover the best ways to learn technical analysis without risking thousands of dollars in the market.
  6. 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.
  7. Trading

    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. Find out how.
Trading Center