Algo Trading for Dummies  -  Implementing an Actual Trading Strategy (Part 4)

Strategy Development and Implementation

While most strategies that are successful long term are based on a mix of technical and fundamental factors, the fundamental behaviors which are exploited are often very nuanced and vary hugely, so its hard to generalize for an article. As such, we’ll be focusing more on the tools and methods for making strategies based on technical analysis.

Image from iOS (8).png

Visual Strategy Creation and Refinement

There are many great financial charting tools available, with various different specialties, my personal favourite free option being tradingview.com.

1_Vy5CbHA0AdW7pwSmiwoZng (1).png

One of the most useful features for strategy creation is its simple scripting language to create both trading indicators and back-testable strategies. While the back-testing tool is rather limited in its functionality, it serves as a good first step sanity check.

Simple creation of trading indicators which are then overlaid directly onto the chart allows for rapid testing and debugging of ideas, as its much quicker to create a script and visually check it against the market than to fully implement and back test it.

This rapid development process is a good first step to making certain types of strategies, particularly for active trading strategies that act on single symbols at a time. However, it won’t do you any good for portfolio strategies or those which incorporate advanced hedging.

For that, you’ll want to create your own tools for visualising full back-tests with multiple trading pairs. This is where the logging features of your back-tester will come in. With the end results being plotted in your graphing tool of choice, such as matplotlib (for Python).

logo2.png

Full Back-tester Framework:

(Simple example of a multi-symbol back-tester based on position handler from  previous article  — full script at end of this post)

(Simple example of a multi-symbol back-tester based on position handler from previous article — full script at end of this post)

Various plots, such as scatter graphs or hierarchical clustering, can be used to efficiently display and contrast different variations of the back-tested strategy and allow fine tuning of parameters.

Implementing and Back-testing

One of the easiest traps to fall into with the design of any predictive system is over-fitting to your data. It’s easy to see amazing results in back-tests if a strategy has been trained to completely fit the testing data. However, the strategy will almost certainly fall at the first hurdle when tested against anything out of sample, so is useless.

Meanwhile, at the other end of the spectrum, it is also possible to create a system which is overgeneralised. For example, a strategy which is supposed to actively trade the S&P 500 could easily turn a profit long term by always signaling long. But that completely defeats the purpose of trying to create the bot in the first place

The best practices for back-testing a system:

  1. Verify against out of sample data. If the strategy has been tuned against one set of data, it obviously going to perform well against it. All back-tests should be performed against a different set of data, whether that be a different symbol in the same asset class or the same symbol over a different time sample.
  2. Verify all strategies against some kind of benchmark. For a portfolio strategy you’d want to compare risk-adjusted returns metrics. For an active trading strategy you can look at risk:reward and win rate.
  3. Sanity check any strategies that pass the back-test. Where possible, look back over the specific set of steps it takes to make any trading signals. Do they make logical sense? If this isn’t possible (for example with Machine Learning), plot a set of its signals for out of sample data. Do they appear consistent and reasonable?
  4. If the strategy has gotten this far, run live tests. Many platforms offer paper-trading accounts for strategy testing. If not, you may be able to adapt your back-testing tool to accept live market data.

Once you finally have a fully tested and working strategy which you are happy with, you can run it with small amounts of capital on a testing account. While the strategy may be perfect, there is always the possibility of bugs in the trading bot itself.

Final Thoughts

Creating any effective trading strategy is hard, especially so when you also have to deal with defining it in objective terms that can be converted into code. It can be especially frustrating when nothing seems to produce reliable results. However, sticking to good practices when it comes to the data science of back-testing and refining a strategy will pay off vs learning those same lessons when a strategy under-performs with real money.

By Matthew Tweed

Full back-tester framework:

/

9 Great Tools for Algo Trading

In the last 5–10 years algorithmic trading, or algo trading, has gained popularity with the individual investor. The rise in popularity has been accompanied by a proliferation of tools and services, to both test and trade with algorithms. I’ve put together a list of 9 tools you should consider using for your algo trading process.

Photo by  Adrian Curiel  on  Unsplash

Photo by Adrian Curiel on Unsplash

Web Services:

The following are managed-services that you can use through web browsers, and don’t require much setup from the user. As someone who’s recently started in this field, I found it easy for new algo traders to try out.

(1) Quantopian:

1_xmqbJh3dGQIXylJTSCD7dA.png

A Boston-based crowd-sourced hedge fund, Quantopian provides an online IDE to backtest algorithms. Their platform is built with python, and all algorithms are implemented in Python. When testing algorithms, users have the option of a quick backtest, or a larger full backtest, and are provided the visual of portfolio performance.

Live-trading was discontinued in September 2017, but still provide a large range of historical data. They also have a serious community of developers, and now hold an ongoing DAILY contest with 10 winners awarded each day for a total of $5,000 per month in prize money (*updated from previously written as "periodically hold contests"). Quantopian provides capital to the winning algorithm. 

(2) QuantConnect:

1_eQopy9i2kr63RFeCFQq-sA.png

QuantConnect, is another platform that provides an IDE to both backtest and live-trade algorithmically. Their platform was built using C#, and users have the options to test algorithms in multiple languages, including both C# and Python.

QuantConnect also embraces a great community from all over the world, and provides access to equities, futures, forex and crypto trading. They offer live-trading integration with various names such as InteractiveBrokers, OANDA, and GDAX.

(3) QuantRocket:

QuantRocket is a platform that offers both backtesting and live trading with InteractiveBrokers, with live trading capabilities on forex as well as US equities. It’s specifically designed for trading with InteractiveBrokers, and sets itself apart with its flexibility.

QuantRocket supports multiple engines — its own Moonshot, as well as third party engines chosen by the user. While QuantRocket doesn’t have a traditional IDE, it is integrated well with Jupyter to produce something similar. One thing to keep in mind is that QuantRocket is not free. Pricing plans start at 19.99/month USD, with annual options.

Local Backtesting/LiveTrading Engines:

In today’s software world, you have lots more freedom if you make some effort outside of those managed-services. If you are comfortable this way, I recommend backtesting locally with these tools:

(4) Zipline/Zipline-Live:

quantopian/zipline
zipline - Zipline, a Pythonic Algorithmic Trading Librarygithub.com

Quantopian’s IDE is built on the back of Zipline, an open source backtesting engine for trading algorithms. Zipline runs locally, and can be configured to run in virtual environments and Docker containers as well. Zipline comes with all of Quantopian’s functions, but not all of its data. To balance that, users can write custom data to backtest on. Zipline also provides raw data from backtests, allowing for versatile uses of visualization.

Zipline discontinued live trading in 2017, but there is an open source project Zipline-live that works with Interactive Brokers. It has many of the same features Zipline does, and provides live trading.

(5) BackTrader:

backtrader - Backtesting / Trading
from datetime import datetime import backtrader as bt class SmaCross(bt.SignalStrategy): params = (('pfast', 10)…www.backtrader.com

Backtrader is currently one of the most popular backtesting engines available. It was built using python, and has a clean, simple, and efficient interface that runs locally (no Web Interface). One thing to keep in mind, backtrader doesn’t come with any data, but you can hook up your own market data in csv and other formats pretty easily.

Starting with release 1.5.0, BackTrader has live-trading capabilities. It’s been a popular choice with algo traders, especially after Zipline discontinued live trading.

(6) IBPy:

blampe/IbPy
IbPy - Python API for the Interactive Brokers on-line trading system.github.com

IBPy is an unaffiliated third party python wrapper for InteractiveBroker’s Trade Workstation API. Before IB started providing their official API library for python, this was the only way to connect to TWS for algorithms written in python.

IB has released an official python SDK, and this library is heading towards begin obsolete(while still being relevant for python2 users). But there still remain a significant number of live trading engines/tools that still use this library, and it’s good learning material for whoever wants to learn about implementing API’s.

While it’s good to learn about this library since it’s ubiquitous, if you are starting fresh, we recommend IB’s official python SDK.

Alpaca Trade API Python SDK is even much simpler to use!

Analytical Tools:

Back testing will output a significant amount of raw data. Some IDE’s will provide basic visualization and analysis, usually algorithm performance. If you’re looking for deeper evaluation, I recommend these tools:

(7) Pyfolio:

quantopian/pyfolio
pyfolio - Portfolio and risk analytics in Pythongithub.com

Pyfolio is another open source tool developed by Quantopian that focuses on evaluating a portfolio. What sets Pyfolio apart, is its ability to introduce degrees of uncertainty to a static set of data points, and evaluate Bayesian metrics from the user’s portfolio. The Pyfolio API offers a number of visualizations, which can be found on their GitHub repository.

(8) Alphalens:

quantopian/alphalens
alphalens - Performance analysis of predictive (alpha) stock factorsgithub.com

Alphalens is also an analysis tool from Quantopian. Unlike Pyfolio, Alphalens works well with the raw data output from Zipline, and rather than evaluate the portfolio, is performance analysis of predictive stock factors. Alphalens has its own range of visualizations found on their GitHub repository.

1_HnTL1cG_aBDoyBDyOzfkew.png

Median Daily Returns by Factor Quantile — one of the visualizations that alphalens offers

(9)TradingView:

1_Vy5CbHA0AdW7pwSmiwoZng.png

TradingView is a visualization tool with a vibrant open-source community. It’s entirely web-based, and allows users to visualize data, whether the data is the result of paper trading or algorithmic back-testing. Like Quantopian, TradingView allows users to share their results and visualizations with others in the community, and receive feedback.

(Bonus) Execution Platforms aka Broker-Dealers:

(10)InteractiveBrokers:

1_jAUoYLH-Fa3CyOtsfzsW3w.png

InteractiveBrokers is an online broker-dealer for active traders in general. They have been in the market since 1978. Algo trading isn’t IB’s focus, but multiple engines offer live trading through integration with their Trader Workstation. We’ve mentioned IB several times in this article — they’re just that good!

(11)Alpaca:

1_bhSUXCHVNP_JQmsMHklPlg.png

Finally, Alpaca! Alpaca was founded in 2015, and is an up and coming commission-free, broker-dealer designed specifically for algo trading. Alpaca also has a trade api, along with multiple open-source tools, which include a database optimized for time-series financial data known as the MarketStore.

The brokerage is scheduled to be publicly available this September (you can play around with the MarketStore right now), but if you can’t wait, head over to our website and jump on the waitlist for a chance at early access!

Alpaca | Algo Trading Commission Free with REST API
Lower your cost, Maximize your profits. Algo trading commision free.alpaca.markets

Miscellaneous Tools to Take a Look At:

  • qtpylib — another simplistic python backtesting engine
  • Multicharts — proprietary trading platform for forex and equities
  • WealthLab — desktop tool which allows C# backtesting, with live trading exclusive to Fidelity
  • Enygma Catalyst — for crypto trading
  • MetaTrader — backtesting/livetrading desktop app, de-fact in forex

I hope this quick primer on tools available right now was useful. If you liked it, please leave a clap (or two, I don’t mind). If you think there are tools that I missed, leave a comment below! I always appreciate any, and all feedback.

by Rao Vinnakota

/