loading...
Cover image for Algorithmic Trading: backtesting an intraday scalping strategy

Algorithmic Trading: backtesting an intraday scalping strategy

michrodz profile image Mich Rodz Updated on ・4 min read

This is the third article of a series I’ve been writing. If interested you can start here.

What is scalping?

In the trading world, “scalping” refers to taking many small profits as quickly as possible instead of using a “buy and hold’ strategy and wait a longer period of time to make a — hopefully — bigger profit.

Alt Notice the green and red dots during a trading day (purchases and sells, respectively). The title of $1031 refers to a 9.6% profit over a $1000 initial simulated investment, after about 70 days (3 1/2 trading months).

Investopedia defines a scalper as someone that

intends to take as many small profits as possible, without letting them evaporate. This is the opposite of the “let your profits run” mindset, which attempts to optimize positive trading results by increasing the size of winning

As mentioned in my first article, the use of margin might be useful to further increase the profits (or losses) made with a scalping strategy.

Doing manual scalping might be a time-intensive and stressful activity since you have to keep track of all the charts for the stocks you care about. And buy/sell many times at the right moments.

Imagine doing all submitting all those BUY and SELL orders as seen in the figure above. Doing that manually, with many stocks, is virtually impossible.

A computer can do this hard work for you. Faster and better.

Let a computer do the hard work for you

Algo Trading is a very handy and simple way to exploit the scalping strategy.

With Algo Trading, you can do a simple script that buys or sells a stock when certain conditions are met. This happens as frequently as you want: many times a day, a minute or whatever.

The idea is to build a script that buys and sells as many times as possible, making a small profit as often as possible.

Scalping is just a general mindset of what we want to do. But we still need some indicator or signal of when to buy or sell.

The concept of “mean reversion”

Alt The price fluctuates around the average.

In case it’s not already clear by the image above, Investopedia defines “mean reversion” as:

Mean reversion is a theory used in finance that suggests that asset prices and historical returns eventually will revert to the long-run mean or average level of the entire dataset.

This means that the price of a stock will, in general, tend to “revert” to its average price.

That is, if a price suddenly hikes we would expect it to return to a lower value. Also, if a price suddenly drops we would expect it to return to a higher value.

We can use this idea to have buy signals. If the price of a stock crosses the average from a lower to a higher price, we might decide to Buy.

The scalping strategy suggests selling as soon as the price of the stock is higher than the purchase price.

Backtesting a scalping strategy

In my last article, Algo Trading: backtesting your algorithm, I posted some sample code on how to download one year of data.

Also how we could backtest a simple “buy and hold” strategy over that year of data.

Now, I’m going to share a script that allows you to backtest a scalping algorithmic trading algorithm.

The idea of the script is to:

  • Loop over all days of a given year.
  • Calculate an n-point moving average of the close price.
  • For every day, loop over the minute-by-minute data.
  • If the open price for a given minute changes from being below the average to above the average we simulate a Buy.
  • We sell as soon as the open price is greater than the buy_price.

If you already downloaded the data as shown in my previous article, the script copied below should work out of the box.

With 2016 data, using AAPL it performs almost 3x over a Buy and Hold strategy! From 9.9% to 30.2%.

AAPL - market change: 1.099
AAPL - wallet change: 1.302

If you also download the data for FTEC and V and run the script using

SYMBOLS = ["AAPL", "V", "FTEC"]

you would also obtain good profits over a Buy and Hold strategy:

AAPL - market change %: 1.099
AAPL - wallet change %: 1.302
AAPL - num_transactions: 454
...
V - market change %: 1.032
V - wallet change %: 1.049
V - num_transactions: 290
...
FTEC - market change %: 1.145
FTEC - wallet change %: 1.298
FTEC - num_transactions: 390

Check out the script

But again, as said in my first article, this same algorithm underperforms using data from 2019! : /

Also, I haven’t tested for a wider range of symbols.

I'll do that in a next article.

I also want to do a rolling profit analysis over this strategy with more stocks. My goal will be to determine if there’s a moment common to most stocks were the market started growing so much in 2019.

Any ideas? I'd be glad to read your comments on ways to improve this.

Posted on by:

michrodz profile

Mich Rodz

@michrodz

Python. DevOps. Tech enthusiast.

Discussion

markdown guide