Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learn Algorithmic Trading

You're reading from  Learn Algorithmic Trading

Product type Book
Published in Nov 2019
Publisher Packt
ISBN-13 9781789348347
Pages 394 pages
Edition 1st Edition
Languages
Authors (2):
Sebastien Donadio Sebastien Donadio
Profile icon Sebastien Donadio
Sourav Ghosh Sourav Ghosh
Profile icon Sourav Ghosh
View More author details

Table of Contents (16) Chapters

Title Page
Copyright and Credits About Packt Contributors Preface Algorithmic Trading Fundamentals Deciphering the Markets with Technical Analysis Predicting the Markets with Basic Machine Learning Classical Trading Strategies Driven by Human Intuition Sophisticated Algorithmic Strategies Managing the Risk of Algorithmic Strategies Building a Trading System in Python Connecting to Trading Exchanges Creating a Backtester in Python Adapting to Market Participants and Conditions Other Books You May Enjoy

Creating a Backtester in Python

By now, we know how to implement a trading strategy idea. We learned how to write the code to make it run in a trading system. The final step before going live with a trading strategy is backtesting. Whether you want to be more confident in the performance of your strategy or you want to show your managers how well your trading idea performs, you will have to use a backtester using a large amount of historical data.

In this chapter, you will learn how to create a backtester. You will improve your trading algorithm by running different scenarios with large amounts of data to validate the performance of your trading strategy. Once a model is implemented, it is necessary to test whether the trading robot behaves as expected in the trading infrastructure.

In this chapter, we will learn how backtesting works, and then we will talk about the assumptions...

Learning how to build a backtester 

Backtesting is key in the creation of trading strategies. It assesses how profitable a trading strategy is by using historical data. It helps to optimize it by running simulations that generate results showing risk and profitability before risking any capital loss. If the backtesting returns good results (high profits with reasonable risk), it will encourage getting this strategy to go alive. If the results are not satisfactory, backtesters can help to find issues.

Trading strategies define rules for entry and exit into a portfolio of assets. Backtesting helps us to decide whether it is worth going live with these trading rules. It provides us with an idea of how a strategy might have performed in the past. The ultimate goal is to filter out bad strategy rules before we allocate any real capital.

Backesting can sound out a run of a...

Learning how to choose the correct assumptions

Backtesting is a required step for deploying trading strategies. We use the historical data stored in databases to reproduce the behavior of the trading strategy. The fundamental assumption is that any methodology that functioned in the past is probably going to function in the future. Any strategies that performed ineffectively in the past are probably going to perform inadequately in the future. This section investigates what applications are utilized in backtesting, what sort of information is obtained, and how to utilize them.

A backtester can be a for-loop or event-driven backtester system. It is always important to consider how much time you will spend in order to achieve higher accuracy. It is impossible to obtain a model corresponding to reality; a backtester will just be a model of reality. However, there are rules...

Evaluating what the value of time is

As we saw in the previous parts of this chapter, backtester accuracy is critical when we build a trading strategy. The two main components creating discrepancies between the paper trading of your trading strategy and the actual performance are as follows:

  • The market behavior that we face when the trading strategy goes live
  • The trading system that you use to trade

We saw that the market impact can be medicated by making assumptions regarding the manner in which the market will respond. This part is very challenging because it is just based on assumptions. As regards the second cause of discrepancies, the trading system itself, we can find an easy solution. We will be able to use the trading system as it is to be the backtester. We will get all the main trading components together and we will have them communicate between one another as if they...

Backtesting the dual-moving average trading strategy

The dual-moving average trading strategy places a buy order when the short moving average crosses the long moving average in an upward direction and will place a sell order when the cross happens on the other side. This section will present the backtesting implementation of the dual-moving average strategy. We will present the implementation of a for-loop backtester and an event-based backtester.

For-loop backtester

  1. As regards the implementation of this backtester, we will use the GOOG data by retrieving it with the same function we used previously, load_financial_data. We will follow the pseudo code that we proposed during the previous section:
for each price update:
create_metric_out_of_prices...

Summary

In this chapter, we highlighted how important backtesting is. We talked about two sorts of backtesters: a for-loop backtester, and an event-based backtester. We showed the two main differences and we implemented an example of both. This chapter concludes the creation path of a trading strategy. We initially introduced how to create a trading strategy idea, and then we explained how to implement a trading strategy. We followed that by explaining how to use a trading strategy in a trading system and then we finished our learning experience by showing how we can test a trading strategy.

In the next chapter, we will conclude this book by talking about your next steps in the algorithmic trading world.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Learn Algorithmic Trading
Published in: Nov 2019 Publisher: Packt ISBN-13: 9781789348347
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}