New Forum

Visit the new forum at

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

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: ( 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.