Tuesday, December 9, 2014

Barbell Strategy Backtest

QuantConnect is working towards the support they need to open source their code. To promote their open source campaign, I’ll be converting a simple version of the barbell strategy into a QuantConnect backtest. If you want to run the code without reading anymore, feel free to sign up at QuantConnect and clone my code! And if you enjoy QC, consider $10/mo a bargain for contributing to the next generation of backtesting. I personally can't wait to start looking at their code :)


Onto the article!


The following is a simple take on the idea presented over at “Don’t Fear the Bear” (DFB from now on). Basically, we’re going to place approximately 20% of our portfolio in a high risk instrument and place the other 80% into a low risk instrument. DFB suggested XIV as the high risk instrument and cash as the low risk instrument. We’ll be doing the same.


I suggest you clone my backtest on QC and play around with the dates, symbols and various other values to get an idea of how the code works. If you want to generate the backtest code from scratch (i.e. not clone mine), you can clone QC’s “Basic Template.” All of the barbell strategy code was derived from a “Basic Template” clone.

We’ll be using the following line to tell QC which symbol we want to trade:


public string symbol = "XIV";

Now, wherever you see “MSFT” replace it with the word symbol. Do not use quotation marks. For example:
AddSecurity(SecurityType.Equity, "MSFT", Resolution.Minute);
would become:
AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);


What this replacement does is inserts whatever value we have symbol set to in each location. This will make it easier to change the symbol value if you ever wanted to. Instead of updating each of the locations, you can just update the line public string symbol = “XIV”; to public string symbol = “SPY”; and the backtester will know you want to use “SPY” instead of “XIV”.

Changing the amount within the setCash(); method call determines how much money you want the strategy to be tested with. I set mine to 100000. This doesn’t have to be changed, but it gives you an idea of how to change this value. Try starting with other values, if you’d like.


Next is the code that does contains all of the logic for our system. Two slashes tell the backtester to ignore those lines. They are called comments, and I’ve left you with several to give you an idea of what is going on.


Continue on after the code for more commentary.

======= Portion of Main.cs ===========


//only get daily close
           if (data[symbol].Time.ToString().Contains("3:59:00")) {
//prints out to console window
               Debug(data[symbol].Time.ToString());
               
               decimal currentPercentage = (Portfolio.TotalHoldingsValue / Portfolio.TotalPortfolioValue);
               Debug("CurrentPercentage: " + currentPercentage);
               
               //reconfigure for 20/80 holding; but give a little room (15%)
               if (currentPercentage < (decimal).15 ){
                   //buy
                   decimal amtToBuy = ((((decimal).2 - (currentPercentage)) * Portfolio.TotalPortfolioValue)/data[symbol].Close);
                   amtToBuy = Math.Round(amtToBuy);
                   Order(symbol, amtToBuy);
                   Debug("Puchased " + amtToBuy + "of " + symbol);
                   return;
               }
               
               //reconfigure for 20/80; but give a little room (25%)
               if (currentPercentage > (decimal).25) {
                   //sell
                   decimal amtToSell = (((currentPercentage - (decimal).2) * Portfolio.TotalPortfolioValue)/data[symbol].Close);
                   amtToSell = Math.Round(amtToSell);
                   Debug("Sold " + amtToSell + "of " + symbol);
                   Order(symbol, -1* amtToSell);
                   return;
               }
           //we don't care if it's not end of day, so return;
           } else {
               return;
           }

==================

The first thing you’ll notice is that we’re only looking at minute bars that come from the end of day. That’s what the “3:59:00” check is for. This can be achieved several ways, but this way is fairly easy to understand. If it’s 3:59 PM we can go ahead and continue with our logic.


Anywhere you see “Debug();” is an easy method to print to QC’s console. Very helpful when you’re trying to figure out what is going on with your code!


Next we’ll check the percentage of our holdings that our in XIV. We want this to be 20% at the start. And, we want to rebalance this using some sort of logic. I have chosen to rebalance when XIV gets to 15 or 25% of holdings. You can play with this number. If you’re feeling adventurous, try to rebalance only at the beginning of the month. :)


You’ll see amtToBuy and amtToSell, which are both variables holding the value created by the statements that follow. All I’m asking is for the backtesting program to determine how many shares (rounded to an integer) I need to buy or sell to bring myself back inline with our 20% goal. Nothing too complicated, although I do have to use the (decimal) cast in order to force .2 from float into decimal. Decimal is what’s used typically in the backtesting program (it’s good for money related activities like stocks).


After we find out how much we want to buy or sell, we can put in an order. This is as easy as using the Order(); method; which, is really, really easy. Just make sure your sell amount is negative. That is how QC knows the order is a sell.


I hope the backtest code is otherwise fairly clear: data[symbol].Close gives the price of the symbol you are following. And, Portfolio.TotalHoldingsValue gives the value in dollars of your the symbol you own. Portfolio.TotalPortfolioValue lets you know how much your entire portfolio (cash included) is worth.


The outcome of the backtest isn't amazing, but it is a good start to your backtesting portfolio. Feel free to post questions and other backtests in the comments!


Also, please consider supporting QuantConnect’s drive to open source its code. Becoming a QC member is a small price for access to an amazing product! Just remember, the more people who have access and can work on it, the better the code will become!

Saturday, November 22, 2014

Unlimited Free Backtests with Large Tick Data Source

There are other services out there that allow free backtests. However, QuantConnect is the only service I know of that allows unlimited free backtests with tick data. Let me say that again: they have tick (i.e. time and sales) data going back to 1998--something around 4TB of data. And, if you prefer, or want to utilize it along side the tick data, they also have minute and second data.

QuantConnect even allows you to pull in the backtest data using their easy to use Rest API, or utilizing their (currently in beta) Visual Studio plugin. If you decide to utilize the backtesting portion of the Rest API more than 5 times a day, it’s a relatively cheap upgrade to unlimited api backtests: 19.95. You can always switch back to their Web IDE for continued unlimited backtesting.

Plus, even better, Visual Studio 2013 Community Edition has just been released. It has all the features of the Professional Edition for use with small teams and for personal use. As mentioned above, QuantConnect has a plugin (currently in beta, and open sourced on github) that allows you to easily develop strategies directly from Visual Studio. Given Visual Studio is easily one of the best IDEs on Windows, this is beyond a blessing; and, definitely worth going along for the “beta” plugin ride. This is not available through other services that typically require API access through the browser. Although, I won't forget to mention that QC also has a nice browser-based IDE.

At a bare minimum, I would recommend signing up for free and trying out their web IDE. They use C# which is considerably more powerful than some of the languages used on other backtesting sites. This power comes in handy when you’re dealing with many, many data points and complex algos. It’ll keep things snappy while data is massaged across a number of cloud servers, allowing you to have your data points crunched in no time. And, if you’re new to C#, QC also has good documentation to help you along.

There are also many innate features (e.g. ability to have multiple files in a project) that you will come to appreciate as you delve into their system. They are adding things and upgrading frequently. I continue to expect more in the coming weeks and months. They have good documentation and are easy (and free) to sign up with. Here's another link to their site: QuantConnect.com.

Please, feel free to post any backtests you run in the comments; I’d be interested to see!

Thursday, November 20, 2014

Gödel's Market on Github

There are some snippets of useful code on this blog that could transform into even more useful code if placed in a more conducive environment. I've created a Github account for blog related code here: https://github.com/godelsmarket.

Right now I've only had the time to put up a repository for the IB Historical Data Downloader. I had to clean up the code formatting a little bit and transfer over the article's content to a readme to make things a bit clearer.

I'd really like to get all the code from the site transferred over. Then I would like to make it more user-friendly so that individuals who do not have as much experience with programming can get their feet wet. That's the goal :)

If you have any questions on how to make a piece of code do something more interesting than it does, please ask. These are basic starting points. I love to help, but I'd rather not just give all the answers right up front. For one, it's no fun that way (check out Project Euler and tell me, with a straight face, that it would be fun to have the answers given to you...). Second, you'll learn far more trying to develop your own program, which will give you more confidence to create more complicated things. Third, if you want it all done for you, I'm available for hire. :)

Now, back to the data.

Sunday, June 15, 2014

Average Return by Trading Day of Month


Often people will say that the first of the month tends to be positive. By the above analysis (SPY from 07/01/2000 to 06/13/2014), that has--on average--been the case. You could also say go long for the 9th trading day of the month and short for the 14th. And if there are 23 trading days in the month, go long the close of the day before--well, if it happens to be in August.

Good luck and good trading. Please do not use the above for trading. Prepare your own analyses (the above could be flawed) and do in-depth investigations. I am not giving financial advice. I'm merely playing with numbers.

Saturday, June 14, 2014

Regeneration

It's been more than a year since I last posted on this site, and a busy year for me. I've been trading regularly over the last four months or so, and have decided to get back into the blogging world.

To start with, I've updated the links on the right to better reflect the state of the blogosphere, with blogs no longer posting placed into the "Of Interest, Archived" category.

More to come.

Tuesday, February 5, 2013

EURUSD Average Monthly Gain

I've been a little interested in foreign exchange pairs recently. They have a different flavor of trading from equities or derivatives. In any case, I ran the following test on data going back to 1990 November 1998. It looks at the opening of the month compared to the month's close (log changes).

I highlighted some of the more interesting months. Looks like December has a nice strong long bias to it; much more consistent direction than any of the other months at 80% positive. Of course, I wouldn't trade any of this by itself.

EDIT: (new chart using IMF.org's data found here. Assumes purchase at first day of listed month and sale at first day of following month. This data goes back to November 1998 rather than 1990. Many thanks to Sanz Prophet for pointing at the mistake! Currently getting my hands on some better forex data! :D)

The above is not meant to be trading advice. Furthermore, past results are not necessarily indicative of future returns. And, as seen in the earlier version of this post, there is no guarantee that the data is accurate.

Tuesday, January 29, 2013

Weather's Effect on Stock Prices: Part One

Yesterday, I thought it would be neat to check out weather's affect on stock prices. So, I looked around for some historical temperature data and found Weather Underground, Inc. Despite their name, they do provide weather information for above ground locations. They also, luckily for us, provide historical data in easy to download CSV format.

So, I wrote this nifty Python script to help us download them.
import urllib

month = 1
year = 2000

while year < 2013:
    while month < 13:
        urllib.urlretrieve("http://www.wunderground.com/history/airport/KNYC/" + str(year) + "/" + str(month) + "/1/MonthlyHistory.html?req_city=NA&req_state=NA&req_statename=NA&format=1", "csvfiles/" + str(year) + "-" + str(month) + ".csv")
        month = month + 1
    month = 1
    year = year + 1

That should download data back to 2000. I checked on their data for 1990 and they didn't seem to have it. Furthermore, I'm not entirely sure of the accuracy of this data.

This concludes Part 1. Yes, no analysis yet; but, half the fun of analyzing data is getting your hands on it in a format that you can process. Feel free to upload this data into a SQL database. I'll provide some data on the market's tendencies before and after snow, rain, temperature correlation, etc, in a few days.