1. Succumbing to Python in the Financial Markets David Cerezo Sánchez http://cerezo.name
2. Python Advantages & Drawbacks Interactive, expressiveness: very quick prototyping Reduced development cycle: C++/Python=10:1 Time distribution in algorithmic trading (25% devising new strategies; 25% coding; 50% model fine-tuning and code maintenance): Python improvements impact 75% of development Free, nonproprietary (vs. Matlab, TradeStation,…) Multi-threading from Python 3.2! SEC mandating cashflow disclosure of ABS securities in Python Dynamic, not strongly typed (Java): errors at runtime!
3. Must-Have Python Financial Packages IbPy: Interactive Brokers Python API ultra-finance, MarWiz, pyfinancial, profitpy, QSToolKit: algorithmic trading libraries Quantlib-python: quantitative finance library NumPy, SciPy, PyIMSL: computational, scientific, numerical libraries xlrd: extract data from .xls/.xlsx files RPy2: wrapper to R, allows R function execution within Python
5. Combo Orders with IbPy # define the contract for each leg shortContract= makeOptContract(‘MSFT', '', 26, '') longContract= makeOptContract(‘AAPL', '', 350, '') # instantiate each leg shortLeg= makeComboLeg(getConId(1,shortContract), 'SELL', 1) longLeg= makeComboLeg(getConId(2,longContract), 'BUY', 1) # build a bag with these legs calendarBagContract= makeBagContract(‘MSFT', [shortLeg, longLeg]) # build order to buy 1 spread at $0.5 buyOrder= makeOrder(‘BUY', 26, 0.5) # buy! buy! buy! con.placeOrder(nextOrderId, calendarBagContract, buyOrder) # watch the messages for a bit sleep(100)
8. Fast implementation Investment Strategies “Portable Alphas from Pension Mispricing”, Journal of Portfolio Management, Summer 2006, 44-53 Pure alpha strategy 1.51% (monthly), S=0.26 Just 200 lines of Python: Heavy use of map, reduce, filter, lambda SciPy: OLS scikits.timeseries Easier to implement using RPy2 (R wrapper)
10. Substitutes vs Complements Paradox Quant/algo trading focused at human trader substitution, but… Moravec’s Paradox: Computer’s excel where humans are weak, and vice versa Vg. Advanced Chess (Computer-Augmented Chess Playing): computer chess programs allowed at human competitions Computers better at brute-force position evaluation, opening and endgame databases, transposition and refutation tables… Respect human common sense and judgment Promoted by top players: Kasparov, Anand, Topalov, … Computer-assisted Playchess.com Freestyle Chess 2005 Tournament: Amateurs+computers+better process >> specialized chess supercomputers >> grandmasters+computer+inferior process
11. Backtesting vs. Forward Testing Why do people love backtesting so much? overfitted model calibrations will always prove their strategies to have very high alpha&Sharpe ratio With hindsight, everyone’s a winner In HFT/algo/quant trading, forward testing should be the golden standard: Extremely fast changing market conditions Reverse-engineered strategies that stop working
12. Market Microstructure Don’t forget about optimal execution sizes! Or expected trading costs given trading volume and volatility!