Backtesting Your First Trading Strategy

Backtesting Your First Trading StrategyBacktesting is a fundamental step in testing the viability of your trading ideas and strategies.

Here is a simple backtesting implementation in Python.

Luke PoseyBlockedUnblockFollowFollowingJun 16This article showcases a simple implementation for backtesting your first trading strategy in Python.

Backtesting is a vital step when building out trading strategies.

The core idea here is to develop a strategy that can be used across an asset class.

You want this idea to be implementable any time the conditions of the strategy are met.

Sometimes this only applies to a single stock, but other strategies may be viable across whole sectors, asset classes, etc… Backtesting is all about testing the viability of that strategy.

You can test the strategy with whatever stocks you want over your desired timeframe.

Of course, past results do not guarantee future results, but it is one step towards verifying the credibility of your idea.

Example StrategyWe will do our backtesting on a very simple charting strategy I have showcased in another article here.

Moving averages are the most basic technical strategy, employed by many technical traders and non-technical traders alike.

The goal is to identify a trend in a stock price and capitalize on that trend’s direction.

Some traders think certain behavior from moving averages indicate potential swings or movement in stock price.

For example, a short-term moving average crossing above a long-term moving average is commonly known as a buy signal, while a short-term moving average crossing below a long-term moving average is known as a sell signal.

Showcased in the image below are example crossovers, with red indicating a sell signal and green indicating a buy signal.

On moving averages: LINKBacktesting the StrategyFor our backtesting, we will use the Backtrader library.

This is an excellent backtesting library that is popularly used for its simplicity, documentation, and advanced functionality.

We’ll go through some sample code provided by Backtrader to understand the basic use of this backtesting platform.

Backtrader allows you to implement your own logic or use the many available indicators (122 different indicators) and strategies.

You can implement all of the different types of orders, like Market, Limit, Stop, Stop Limit, Stop Trail, etc… And finally, you can analyze the performance of a strategy by viewing the returns, Sharpe Ratio, and other metrics.

There are also some more advanced features you can implement such as keeping fees in consideration, allocation percentages/maximum allocation, trading multiple stocks with different types of orders, and other advanced features.

This first block is straight from the Backtrader documentation.

We define our moving averages and use the built in Simple Moving Average (SMA) indicators.

You can replace the SMAs with any of the 122 built-in indicators or build your own strategies.

Then, we define the crossover logic for when to enter and exit positions.

This second bit is also very simple to understand.

Cerebro is the backbone of backtrader; it manages and pieces together the strategies, observers, analyzers, etc.

After adding a Cerebro instance we define the timeframe for trading the strategy and then plot the below plot.

We also return the Sharpe Ratio for this strategy.

The above is a plot output from the crossover strategy, backtested from May of 2016 to May of 2019.

This strategy overall saw some profitable trades and also some not so profitable trades.

Profitable trades are indicated with blue dots and trades that ended in the red are signified by red dots.

We can also use the Sharpe Ratio tool to view this trade’s Sharpe Ratio.

The Sharpe Ratio for this trade was very poor, unsurprisingly, at around -3.

3.

ConclusionIf this sort of thing is interesting to you, I highly recommend checking out Backtrader and testing out some methods of your own.

See what strategies work better than others, test the strategies on different stocks over different timeframes, and just have fun creating and testing new strategies!Welcome – Backtraderpython backtesting trading algotrading algorithmic quant quantitative analysiswww.

backtrader.

comBacktesting – WikipediaIn a trading strategy, investment strategy, or risk modeling, backtesting seeks to estimate the performance of a…en.

wikipedia.

org.

. More details

Leave a Reply