A Simple Mean Reversion Stock Trading Script in C#

Photo by Oscar Nord on Unsplash

Python is not the only language

In the past, I’ve published stories on Medium showing how to write algorithms that trade stocks based on company fundamentals and how to run a technical analysis day trading algorithm in the cloud. Both of those articles assumed that:

  • Python was the language the reader wanted to use.
  • You had access to an Alpaca brokerage account and could therefore use Polygon’s premium data feed.

This meant that those outside the US were out of luck, as you need to be a US citizen to trade with Alpaca. In this article, though, I’ll show an example implementation of a new C# trading script. Because it uses their free paper-trading API, anyone can run it. You‘ll be able to test it out in their paper trading environment, whether or not you have money in an account with them.

Using the Paper Trading API with a C# Script

To get access to Alpaca’s free paper trading API, sign up at Alpaca’s website. Once you’ve done that, you should have access to the dashboard, where you can watch the performance and contents of your portfolio change as your algorithms run. For now, you’ll just need to hit the “Generate Keys” button to get started.

The Code

Once you’ve got your API keys, we can go ahead and use them to get our code running. I’ll post the code first, then explain what it’s doing below.

We’ll be using the C# SDK for Alpaca’s trade API, which you can install from NuGet. (The SDK is hosted on GitHub and is open source, so feel free to take a look at the underlying code for yourself.)

To connect to the API, we simply create a new REST client, giving it our keys and the API URL. (All of these pieces of info can be taken from the Alpaca dashboard, and you should fill in the REPLACEMEs near the top of the file with your own keys.) Using the REST client, we check Alpaca’s clock and calendar API endpoints to figure out when the market will open and when we’re getting too near to close. (We don’t want to hold the position overnight, so we liquidate the position before close to be safe.)

Trade Logic of this Algorithm

The trading logic of the algorithm is simple. It looks at data for one symbol — set here to SPY, as an example — and each minute, it checks on its current price and its average price over the last 20 minutes. If the current price is higher than the average, it doesn’t want to hold any position. However, if the current price is lower than average, it wants to allocate a certain percentage of your portfolio to holding shares. It’s following the economic theory of mean reversion, assuming that when the price is below the average, it’s more likely to come back up.

The “scale” variable at the top determines how much of your portfolio goes into the position. You can see exactly how it factors in with this chunk of code:

Decimal avg = bars.Average(item => item.Close);                Decimal currentPrice = bars.Last().Close;
Decimal diff = avg - currentPrice;
// ...
Decimal portfolioShare = diff / currentPrice * scale;

The scale is by default set to 200, and if you follow the code above, you’ll see that means that a change of .5% from the 20 minute average would cause 100% of your portfolio to be invested.

I encourage you to play around with the scale and different symbols, and see if you can find a combination that works for you. SPY is used as an example because it has a high trading volume and does not tend to move too dramatically during market hours. I encourage you to try different symbols and scales and see if you can find an edge on any stocks with this approach. If you’d like to augment the code, too, you might practice by extending the algorithm to check the EMA as well and factor that indicator into its purchasing decisions.

Later, I’ll post another article showing how we can use Polygon’s premium data feed to improve this script. If you’re interested in giving that version a try, you’ll need a brokerage account with Alpaca. With a live trading account, you’ll be ready to give the other version a try as well as apply this code to your own trading ideas.

Technology and services are offered by AlpacaDB, Inc. Brokerage services are provided by Alpaca Securities LLC (alpaca.markets), member FINRA/SIPC. Alpaca Securities LLC is a wholly-owned subsidiary of AlpacaDB, Inc.

You can find us @AlpacaHQ, if you use twitter.

Follow Automation Generation, a Medium’s publication created for developers/makers in trading and fintech.