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. 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.
  2. Trading

    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."
  3. 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.
  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.
  5. 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.
  6. Trading

    Trade Simple, Trade Smart

    Simplicity can be a trader's best friend. Here is a simple day trading strategy which takes advantage of a stock's dynamics.
Frequently Asked Questions
  1. Why would a company buy back its own shares?

    Learn about share buybacks and the reasons a company might choose to repurchase its own stock, including ownership consolidation ...
  2. What is the difference between CAPEX and OPEX?

    In this article, we'll teach you the differences between a company's capital expenditures and its operational expenses.
  3. How do you calculate working capital?

    The formula for calculating working capital is straightforward, but lends great insight into the short-term financial health ...
  4. Is Depreciation Or Amortization In Gross Profit?

    In this article, we'll analyze whether depreciation or amortization are included in calculating gross profit.
Trading Center