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.

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

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

## Make your contract
stkContract = makeStkContract(sym)
## Request tick data
con.reqMktData(orderID, stkContract, '', False)
sleep(1)
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)
    sleep(.5)

##Stop receiving tick values
con.cancelMktData(orderID)

##Disconnect from TWS
con.disconnect()

Wednesday, December 19, 2012

TV is Dead

As I have yet to figure out what to do about my quantitative writings, I am back to discussing the shortfalls of  articles I find largely through Abnormal Returns. 


While still pertinent to understand, acquiring such knowledge now will not put you ahead of the game. I'm guessing this type of article is just an easy throw-away mimic article, in the vein of the "OMG the newspaper is dying!" article. Of course, it's kind of hard to write about newspapers demise anymore. Most are dead and gone, killed by up to the second free reporting. When it comes to people searching for deals and coupons in the paper, amazon and groupon (and its many clones) provide much better services--for both customer and business.

But, the newspaper sensation was fun to write about. People liked to talk about it. Now, that it is nearly dead and everyone has heard about it, I guess media outlets are moving onto the next thing. The next thing to watch die. 

You should've seen it coming ten years ago. And for the last five, online video advertising has been exploding in revenue and profit. Plus, most shows are online, and anyone with an average computer know-how can download the latest episode of anything that is not online (or ask a friend to do it for them).

The internet has replaced television for many people, and it will continue to decline. Besides habit and comfort of use, there's really no reason to access content through the television anymore. There will be even less reason to do so in ten years.

Friday, December 14, 2012

AAPL to Apple? Or APPL to orange?

I never really liked these kind of comparisons: (http://www.marketanthropology.com/2012/12/the-universal-law-of-gravatation.html). He could be right, but I don't think he's showing any data that is statistically significant. It's an analogy, alright, but not something I'd necessarily want to trade directly off of.

Thursday, September 27, 2012

SPY Intraday Mondays

I have been doing a little research into the peculiarities of certain days of the week. For instance, Monday's tend to have very little economic news released intraday and very few earnings release or reports. You might expect 2008 to have some large intraday turbulence on Mondays, given government reports released over the weekends. But this is not the case. In face, the largest down day on a Monday since 1993 has been approx -4.7% and took place during 2009. The largest up day is 4.3% and also took place during 2009.

Here's a chart displaying the return of $100,000 placed into the SPY at its open on Monday morning and taken out of the SPY at its Monday close. Rather uneventful.


Wednesday, September 26, 2012

Minutes per Price Chart

To compliment yesterday's post, here's a chart displaying the minutes spent at each price level.

(Note: This is for AAPL between 7/17/2012 and 9/21/2012)