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:

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:

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


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'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("" + 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.

Monday, December 24, 2012

Simple Automated Trading System

I put together some Python code for an automated trading program for use with Interactive Brokers. It's very simple, but with a little ingenuity, could easily be expanded to include multiple contracts and various buying or selling triggers.

Sometimes it's nice to have a simple template to work from. Hope this helps someone get started with their very own automated execution program.

It uses the ibPy module and Python 2.7 (although it probably works on previous versions). I tried commenting the most important aspects.

What it does is send you a quote of the last price of the 'YM' 20130315 contract as long as that contract is between 13070 and 13100. Furthermore, (assuming you remove the '##'s) it will make a purchase if the contract is above 13083 and a sale if the contract is below 13082.

I recommend using a paper trading account while testing trade execution code. Also, the strategy within the code, if you were curious, has no quantitative backing. It's merely an example. The point is, you can add your own system in place of the one in the code.

I tried to comment on everything important in the code. If anything doesn't work or doesn't make sense, let me know! I'll try to reply quickly.

Here's the code:

from ib.ext.Contract import Contract
from ib.ext.Order import Order
from ib.opt import ibConnection, message
from time import sleep, strftime, localtime
from datetime import datetime

## Global variables
shares = 3
action = 'BUY' 
orderID = 30
last = 0
prev_last = 0
sym = 'YM'

## Contract Creation Function
def makeStkContract(sym):
    contract = Contract()
    contract.m_symbol = 'YM'
    contract.m_secType = 'FUT'
    contract.m_expiry = '20130315'
    contract.m_exchange = 'ECBOT'
    contract.m_currency = 'USD'
    return contract

## Order Creation Function
def makeStkOrder(shares,action):
    order = Order()
    order.m_minQty = shares
#    order.m_lmtPrice = limit_price
    order.m_orderType = 'MKT'
    order.m_totalQuantity = shares
    order.m_action = str(action).upper()
    return order

## Tick Handler
def my_tick_handler(msg):
    global last
    global prev_last
    #print msg
    if msg.field == 4:
        prev_last = last
        last = float(msg.price)
    print msg

con = ibConnection()
con.register(my_tick_handler, message.TickSize, message.TickPrice)

## Make your contract
stkContract = makeStkContract(sym)
## Request tick data
con.reqMktData(orderID, stkContract, '', False)
print last

## Prints last price as long as between 13070 and 13100
while (last > 13070 and last < 13100):
    print last

#### A possible execution plan. Remove '##' to initiate.
#### A paper trading account is recommended for testing and debugging trade execution code.
##    if (last > 13083):
##        action = 'BUY'
##        stkOrder = makeStkOrder(shares,action)
##        con.placeOrder(orderID,stkContract,stkOrder)
##    elif (last < 13082):
##        action = 'SELL'
##        stkOrder = makeStkOrder(shares,action)
##        con.placeOrder(orderID,stkContract,stkOrder)

##Stop receiving tick values

##Disconnect from TWS