New Forum

Visit the new forum at

Tuesday, December 30, 2014

Free Full Version of Microsoft Visual Studio 2013

It’s been a couple months since Microsoft released the professional version of Visual Studio 2013 for free under the title Community Edition. Meant for non-enterprise environments, it still has all the features of the professional version and is just as powerful.

With QuantConnect just about to open sourcing their Lean engine (sign up for a hobbyist account to support this!), it’s the perfect time to update and prepare your development environment.

VS 2013 CE is somewhat hidden away on Microsoft's site. In order to download a copy go to and scroll down to “Community 2013 with Update 4”. Click on the title of the purple bar and go to “Install Now” ( Download and install, and you’re on your way to a professional dev environment!

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
               decimal currentPercentage = (Portfolio.TotalHoldingsValue / Portfolio.TotalPortfolioValue);
               Debug("CurrentPercentage: " + currentPercentage);
               //reconfigure for 20/80 holding; but give a little room (15%)
               if (currentPercentage < (decimal).15 ){
                   decimal amtToBuy = ((((decimal).2 - (currentPercentage)) * Portfolio.TotalPortfolioValue)/data[symbol].Close);
                   amtToBuy = Math.Round(amtToBuy);
                   Order(symbol, amtToBuy);
                   Debug("Puchased " + amtToBuy + "of " + symbol);
               //reconfigure for 20/80; but give a little room (25%)
               if (currentPercentage > (decimal).25) {
                   decimal amtToSell = (((currentPercentage - (decimal).2) * Portfolio.TotalPortfolioValue)/data[symbol].Close);
                   amtToSell = Math.Round(amtToSell);
                   Debug("Sold " + amtToSell + "of " + symbol);
                   Order(symbol, -1* amtToSell);
           //we don't care if it's not end of day, so return;
           } else {


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!