Lessons in Automated Trading Systems

With the launch of our public API, @TweetJoeLee shares his experience in developing trading bots. Its success formed the foundational code for Magnr’s trading engine and provided capital for the startup’s first investment.

Wikipedia defines Automated Trading Systems as “a computer program that creates orders and automatically submits them to a market center or exchange”

Automated trading creates vast pools of liquidity for the global financial markets. With the digitization of financial markets, equities trading and foreign exchange becoming increasingly prominent, the amount of automated trading is set to dwarf human execution. In 2009, studies showed that AT (algorithmic trading) already accounted for over 73% of all trade executions[1]. Six years on, in 2015, I expect this percentage to be closer to 90%.

In 2011, I set out to learn about the secretive world of High Frequency Trading (HFT), Algorithmic Trading and Automated Trading Systems.

Over the following two years, I coded and ran a handful of Bitcoin trading bots. In that time an initial $100 test purchase of Bitcoins turned into $200,000. During the same period a buy and hold strategy would have yielded a return of $20,000.

The world of finance has always fascinated me. In 2011, there was talk about Bitcoin becoming an asset type. With a low market cap, it was the ideal asset class to explore lessons and learn more about trading. Bitcoin was in its infancy with an enormous network effect (it still is). This is what I learned.

Manual Arbitrage

“Throughout 2011–2013 exchanges had huge price differences”

To observe differences, I used publicly available API data from exchanges plugged into spreadsheets that automatically updated price information.

Price differences at the time were common and significant. The spreadsheet I used provided a way of rapidly comparing price information after fees are applied.

Comparing exchange rate information between MtGox and Btc-e as well as exchange rates

The method followed some basic principals of the economic theory of supply and demand. This equates to more liquid exchanges providing more competitive prices and tighter spreads. As with any financial transaction, the more risk factors you can control, the greater the chance you will achieve of your ultimate goal. Profit.

Automating Order Placement

After a handful of manual trades, I set my sights on developing a way to automate order placement. The mathematical model was basic. Taking fees into account, if the ask price of one exchange dropped below the bid price of another exchange, there is an opportunity to profit.

After weeks of development, the arbitrage bot was complete

Further development of the trading engine required a rules-engine to be created.

The trading dashboard showing arbitrage trades over a 24 hour period

This created a way for me to define a set of conditions to be met before a trade was executed. This was what I would have done using the spreadsheet manually. Rules engines can be useful to reduce execution related risks. Their job is simple, to ensure a set of qualifying criteria is valid before deciding whether or not to execute a trade. The engine I built took into account volume, price, fees and current levels of liquidity before deciding if a trading opportunity was worth executing against.

Once completed, all that was required was to push the switch. Once live the bot would excute against its pre-determined set of commands and if all went well, act as a mechanism to provide liquidity to smaller exchanges which needed them.

Algorithmic Trading

Counterparty risk in arbitrage trading remained high throughout 2011–2012 with many of the exchanges under pressure by local banks to shut down. The established, larger players in the market showed a greater amount of resistance. Your strategies could be the most successful in the world, but with exchanges often disappearing without trace this caused problems in cashing out or what the industry calls realised profits.

“The biggest threat to unrealised profits, was unrealised losses caused by counterparty risk.”

With the levels of risk often exceeding reward it was time for a switch in strategy. The ecosystem during that period was is still in its infancy and options for reducing counterparty risk were limited.

Enter algorithmic trading. Algo trading uses pre-determined investment strategies to automatically decide when to go long or when to go short. Mathematical models for algorithmic trading can be more complex and as a result, the risks greater. On the flip side, counterparty risk is reduced as you need fewer exchanges to trade at. There is an added benefit of being able to withdraw Bitcoins from the exchange during periods when you are holding to go long. The benefits of Bitcoin’s near instant settlement really shine here.

The nature of algorithmic trading is often a tightly guarded secret, but with some research, it becomes a puzzle that is easy to piece together. The million-dollar question frequently asked to those in the industry, is what strategy to use.

Strategies can be as basic as following technical analysis indicators to counter market psychology or as complex as scalping by using hundreds of small trades.

One of the most frequently cited indicators in FX is the use of MACD. Your goal when using technical analysis is to be correct more times then you are wrong. By trading fixed amounts each time your indicator signals a buy or a sell, you are exposing yourself to pre-calculated risks. It is important to note that your goal is not to expect indicators to predict the future, but to give an indicator with a pre calculated degree of confidence based on historical data, what will happen next if the market follows the trend.

Regardless of the model or indicator you wish to use for your algorithmic trading engine, one thing remains the same throughout. Your strategy needs to produce one of three signals on a regular interval. Buy, Hold, Sell.

Exchange APIs allow for historical data to be downloaded. The benefit of having a robust dataset is the ability to chart data using a database query library. Alternatively, using methods shown above, spreadsheets may be used to collect and process tick or trade data.

Long and short trading indicators based off MACD

Programming libraries can be applied to any data-set to allow programmatic number crunching required to produce valid trading signals. Libraries like TA-Lib are open source and allow technical analysis indicators to be applied to any dataset. http://ta-lib.org

A key step to consider when choosing a strategy is to back-test the performance of your approach. Technical analysis indicators often act as price averaging tools to spot trends, using this assumption, back testing can provide additional metrics to consider such as risk-adjusted return, win-to-loss ratio and correlation co-efficient.

Using these methods and switching from arbitrage trading to algorithmic trading enabled me to reduce execution risk and go from placing hundreds of trades per day to a handful a month. What worked most effectively for me? Surprisingly, the most basic moving average crossovers provided the best return over any complex strategy on a 24 hour candle chart. At the time with liquidity provided across the entire market by a single exchange, MtGox, there simply was not enough data to trade at a more granular timescale. In 2015, I expect similar strategies to be effective across highly liquid 1 hour periods.

For Bitcoin trading, further reading on automated trading should follow Forex (FX) market strategies. Equities trading has less in common with Bitcoin as automated trading strategies can depend heavily on fundamentals (core business metrics).

Conclusion

We are excited to announce that we have recently enabled client access to our trading API. Log in to view your API keys and develop your automated trading strategy. To read more about our API, please see our documentation on GitHub.

Magnr provides a safe Bitcoin-only trading environment which allows you to manage your risk by only providing trading collateral when required in the form of a Bitcoin deposit. Trading carries a risk of loss, your Bitcoin is at risk.

Further reading

To learn more about our trading platform, check here.

Below are links to our educational blog posts on Technical Analysis:

Disclaimer

This blog post should not be interpreted as financial advice. It’s author is not a licensed financial advisor by the FCA in UK. Magnr and its associated products carry risk of loss. You, the reader should be aware of and observe all applicable laws and regulations of the jurisdiction you reside in.

Magnr is in the process of seeking regulatory guidance with the UK based FCA and the US based CFTC.

[1] http://onlinelibrary.wiley.com/doi/10.1111/j.1540-6261.2010.01624.x/abstract

[2] http://www.wired.com/2011/11/mf_bitcoin/

[3] https://www.google.co.uk/search?client=safari&rls=en&q=json+in+google+spreadsheet&ie=UTF-8&oe=UTF-8&gfe_rd=cr&ei=2bUwVpHMDM2q8wf6_rAI#q=import+json+into+google+spreadsheet