Friday, October 14, 2011

trading strategies

here's an idea: delta hedge triple-leveraged etfs. want to have (price of underlying)/(price of hedge) as high as possible, with high vol on underlying. take advantage of the fact that leveraged etfs rebal their derivative guts daily, so their delta changes with the underlying price. hold the hedge, buy/sell the underlying to match delta, make $$ every time the underlying price bounces up, with very low risk as long as you adjust the hedge daily. if you have too much cash, buy more hedge when the price goes up instead of selling the underlying. look for 3x etfs at http://3xetf.com/ this is very similar to the way people delta hedge options, but very simple to manage and with no trader-type restrictions. another way to do it could be to hold the underlying and buy/sell the hedge. i'll need to check into liquidity/trading cost issues there. for example: long term treasuries: hedge tlt (or maybe vglt?) with sbnd. financials: hedge sef with fas. real estate: hedge schh with drv. russia: hedge ? with rusl s&p500: hedge spy, ivv with spxu or maybe the factorshares spread etfs?

Wednesday, September 14, 2011

python/c++ with microsoft visual c++

finally got a 64 bit pyd python extension working, compiled with ms visual c++ and visual studio for epd on windows. figured out that the version string in python (MSC v.1500 64 bit (AMD64)) was for visual c++ 2008 == v9.0, and the express edition only builds 32 bit. so i had to get the sdk (version 7 works with 2008) and make sure the amd64 stuff got installed with it. some notes say to install the service pack before the 64 bit stuff, but i didn't find this necessary. i ran the 'Windows SDK Configuration Tool' from the start menu, since it sounded logical, and ticked the box to link the sdk with VC 2008. not sure if that was necessary or not. one change i had to kludge manually was changing the references in C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat under the amd64 label. originally they were pointing to "%~dp0bin\amd64\vcvarsamd64.bat"; they need to be "%~dp0bin\vcvars64.bat". at that point, running vcvarsall.bat amd64 should work and a simple use of weave passes: import scipy.weave as w c = w.inline(r'printf("hi.");',verbose=2) now that the compiler, etc., are set up i can use swig and distutils to build bigger extensions (like in the swig docs), and it Just Works!

Thursday, September 1, 2011

thinking in c++

lots of versions out there, but this one seems to be the best one of volume 1 i can find. (get the non-landscape one.) don't get that version of volume 2. it is old and incomplete. the 27 dec 2003 version of volume 2 is the print version; i can't find any electronic version other than the html. i think the quality and depth is bettern than vol 1, but it's too long for me to go through right now. c++ is definitely a language that has a vast landscape to explore. i think i would need to read the 'design and evolution' book to understand the rationale and really get at how things work, and i will eventually need to read the templates book and the gang of four design patterns book to know how to use it properly. maybe the 'effective' and 'exceptional' books, too. for now i will prioritize the parts of vol 2 that i haven't read yet. 1: Exceptions: p. 53-75 9: Multiple inheritance 10: Design patterns 6: Generic algorithms 7: Generic containers 8: Runtime type identification 2: Defensive Programming 5: Templates: template programming idioms - end (p. 252 in html imported to word doc) 11: Concurrency (might skip, focuses on specific library) a few tips to remember: -- return value optimization: return a constructor call; it saves a copy-ctor and dtor -- it's ok to throw in a ctor, as long as you clean up resource allocations first -- never throw in a dtor, since the dtor could have been called due to a previous exception and c++ requires 2 unhandled exceptions to crash the program -- throw by value, catch by reference: allow the exception handling to deal with the memory management -- deal with errors and catch exceptions at the nearest handler that has enough information to deal with them

Wednesday, July 20, 2011

wireless on suspend/resume

had a problem with some ubuntu upgrades hosing my ath5k wifi after resuming from a sleep. something was messed up with the power getting turned back on, i think, and it seemed to be something that would get fixed and rebroken judging from the dmesg errors and bugposts on kernel.org. downgrading my kernel didn't fix the problem. fortunately, i found that turning it off and on with rfkill avoided the problems, so i put it into a script to do it automatically. /etc/pm/sleep.d/50_wireless:
#!/bin/sh case "${1}" in suspend) #date >> /tmp/pm.log #echo ' suspend' >> /tmp/pm.log /usr/sbin/rfkill block wlan ;; resume) #date >> /tmp/pm.log #echo ' resume' >> /tmp/pm.log /usr/sbin/rfkill unblock wlan ;; hibernate) # nothing ;; thaw) # nothing ;; esac

Thursday, July 7, 2011

cygwin-ports

cygwinports seems to be just what i was looking for to get packages that don't come with the standard set. they have rtmpdump and mplayer, and i'll check back there before trying to compile stuff again.

android sdk, avd, !*&$!$%^

tried to get android emulator working on my admittedly underpowered netbook. it works... sort of. the 3.1 platform was _really_ slow. i tried the snapshot method to make it at least boot faster; no dice. maybe 2.2 would be better, but i think what i'll really try (if i do again) is the android-x86 iso on a flash drive. or just wait -- google and/or intel seems to be coming out with x86 native android later this year.

kindle on linux

followed these instructions to get kindle for pc running on my ubuntu netbook with wine:
sudo add-apt-repository ppa:ubuntu-wine/ppa

sudo apt-get update

sudo apt-get install wine1.3
after installation, just run wine on the installed Kindle.exe in ~/.wine/drive_c/ etc.

Saturday, May 7, 2011

karnickel: macros in python

not sure when exactly a macro would be useful in python. i remember seeing such a thing in some cython code, to deal with a c++ template, though karnickel deals with the python ast so probably not useful for that. but, there it is, if the need arises.

Thursday, May 5, 2011

python embedded in gdb

version >=7 of gdb has an embedded python interpreter. here's a tutorial on it. very handy if i need to debug c or c++. i'm guessing a million new debugger guis will be built on top of this.

Tuesday, April 26, 2011

compressive sampling

just came across a new take on an old problem: compressed sensing. here's a gentle introduction to the subject. looks like youtube has some interesting lectures on this, too, especially linking compressive sensing or sampling to information theory. one thing that occurred to me is that the sparseness object expressed by the '0-norm' requires the part of the domain where the function goes to zero to do so faster than the norm parameter. otherwise, the 0^0 would be 1, just like x^0==1 for x!=0. so, something like lim x->0 exp(-1/x)^x. maybe i can think of it as an epsilon-norm, where epsilon is infinitessimal for all non-zero quantities but not exactly zero for the norm of zero.

Tuesday, March 29, 2011

trading optimization

i've been thinking about how to optimize trades for some algorithmic trading. i need to define objective functions, obviously, but how exactly? risk vs. return, but risk (for a liquid asset) depends on a portfolio state between transactions, and returns are realized upon the transaction. it just occurred to me that i can reduce it to simple shift and scaling operations on a (somewhat unknown) pdf. if i have a certain unhedged amount, x, of a risky asset, and i buy or sell to end up with a*x, then that scales the pdf f(x) to f(a*x)/a. the trade should give me a return, r, so the pdf of my total return after the trade would be something like f(a*x-r)/a. so i know what the shift and the scale are, even though i don't exactly know what f(x) is. now it's a question of whether f(a*x-r)/a is better than f(x), and i trade if it is (i.e., maximize whatever metrics to find the optimal r,a). does this answer the question of how to place limit orders away from the market prices? i'm not sure yet.

Thursday, March 17, 2011

factor shares

ib is offering its customers commision-free trades on factorshares spread etfs. this looks really interesting: diy hedge fund with asset classes. trying to figure out how the funds operate, based on the holdings info from their website... each of the 5 has basically $3m in treasuries, $2m in cash, and a thousand in a treasury fund. each one then takes a position in nearest future contract (long and short) for the two spread assets, for 2x the nav/share * 100k shares/unit * 2 units (currently). the treasuries are probably to offset the time discount on the futures contracts, and that and the cash are for margin, obviously. the fund is probably for a little bit of liquidity during the daily rebalance. i'm guessing the nav comes from the $5m risk free + net market value of the futures. the nav/share doesn't exactly match the price, probably given the constraints of the contract sizes and maybe the lower liquidity of the etf at the moment. the futures holdings are updated daily, to restore dollar neutrality (same forward contract dollar amounts). for the current number of shares and risk-free holdings, the funds basically started at $25/share. the s&p e minis are apparently for lots of 50. the 30yr tbill futures are for lots of 100. the 5 etfs are s&p/tbill, tbill/s&p, s&p/usd, oil/s&p, gold/s&p, where each is bull/bear. 2x leverage on each leg, for a 4x total leverage (but still just 2x on the spread). right now the volume is pretty light, around 10k/day, since they just lauched a few weeks ago. i wonder if there is any arbitrage opportunity for these, knowing their methodology. especially for s&p/tbill vs. tbill/s&p. does the rebalance accumulate anything based on independent underlying price movements? no, because all the money gained from selling an advancing future is put into the one that declined, and the same thing will happen at the end of the next day. but if i rebalance between these, it will. i'm just not sure if it's any better than just rebalancing among 2x single asset funds, unless you think they're anticorrelated instead of just uncorrelated. maybe it has the advantage of not needing to rebalance with cash, like single funds would. one thing maybe i can try is to arb the s&p/tbill vs. tbill/s&p like this: right now fse has 139 emini and -71 tbill, and fsa has -177 emini and 91 tbill. closing for fse was $22.57 and fsa was 28.18. if i could buy 177/139 fse it would cost 28.74, or 91/71 would cost 28.92. so if i had some of each, i could have sold fse and bought fsa near the end of the day, eg sell 9 fse for 203.13 and buy 7 fsa for 197.26. that would be selling a share of 1251 emini, -639 tbill and buying a share of -1239, 637. so the net would be 42, -2. nav calculation time is based on the first of the futures contracts to settle: s&b/tbond 3pm (ET) tbond/s&p 3pm s&p/usd 3pm oil/s&p 2:30pm gold/s&p 1:30pm the nav must depend on the price of the futures and what exposure they can be rebalanced to. futures prices might not be as easy to come by, so maybe i could compare to other leveraged etfs like sso, sds, dgl (or iau/gld (not leveraged) or dgp/dzz (monthly, not daily)), dbo (or dig?, uso, oil), tlt (not leveraged), tbt (or pst? no), udn (not leveraged) (or uup, not leveraged and bull instead of bear but much higher volume) (these only use dx contracts, not front month). this wouldn't necessarily be perfect since nav can deviate from price, but it should be close for the heavily traded ones. here's a good ref list for leveraged etfs

Saturday, March 12, 2011

aptitude tests

seems like a lot of companies are using tests by their hr department to screen applicants. shl is one that is mentioned a lot. this one was referred to as having good practice tests.

Friday, March 11, 2011

audio + video

figured out how to get an alternate audio into a video file: # take mp3 out of audio flv ffmpeg -i altAudio.flv -acodec copy altAudio.mp3 # take the video only out of the video mencoder -ni -ovc copy -nosound -o noAudio.mp4 origVideo.mp4 # put the audio and video together mencoder -ovc copy -oac copy -audiofile altAudio.mp3 -o out.avi noAudio.mp4 might not need both the ffmpeg step and -ni on the video extraction, but at least one of them is necessary. i tried without both and the video played at double speed while the audio was normal speed in the out.avi. might also be able to use a different format out with -of. not sure if avi transcodes it. EDIT: tried without -ni, still works fine. might need to adjust delay with - and +. also, i found that rtmpdump has very handy -e and -o options. i dropped the -A option with -e and it seems to work (kind of), not sure if that is absolutely necessary. now i'm trying to get mencoder on a file to let me playback faster. tried -vf filmdint and filmdint=io=2:1, and it doesn't hurt but doesn't seem to be essential. so far, best results are with just -fps 30000/1001 -ofps 20000/1001 and speed it up with ']'. -oac copy doesn't work; have to use something else like pcm (uncompressed) or mp3lame. the original audio is 48000Hz aac, which i can encode with -oac lavc -lavcopts acodec=aac, but that makes encoding really slow and messes up a/v sync, even when i try to compensate by changing -srate. so far this version looks promising, but the avi has no idx: mencoder -ovc copy -oac mp3lame -fps 30000/1001 -ofps 20000/1001 -o out.avi infile.mp4 could try -of mpeg or lavf, with a lavf format=mp4 for example, since the original is mp4 and plays pretty well (except when i speed it up too much). ok, this one seems to work pretty well: mencoder -ovc copy -oac mp3lame -fps 30000/1001 -ofps 20000/1001 -of lavf -lavfopts format=mp4 -o out.mp4 infile.mp4

Wednesday, March 2, 2011

national review online audio

outloudopinion.com has apparently been posting audio files of select nro articles for at least a year now, much to my surprise. even have a convenient rss feed.

Wednesday, February 23, 2011

REMINISCENCES OF A STOCK OPERATOR

by Edwin LeFevre. recommended by more than one trader, to give an accurate feel of what being a trader is really like. one trader i talked to described trading as a spiritual experience, that you have to do a gut check every day when you have that much on the line. amazon has a nice annotated copy available that gives good historical background information and explanation of some of the things that are harder to understand now. but gutenberg has the original up.

Wednesday, February 16, 2011

pybtex

if i ever need to mess around with beastly bst (bibtex style) files, i won't. i'll switch to pybtex, which looks like it's a lot more fun to use.

Tuesday, February 1, 2011

quant topics i should learn

looked through a big glossy from wilmott's certificate in quantitative finance. saw some terms that i wasn't sure i knew about. so here's a list, so i can make sure i learn about them and i don't need to take any of their classes. fokker-planck and kolmogorov the radon-nikodym derivative girsanov's theorem yield, duration, and convexity stochastic and spot-rate models affine stochastic models heath, jarrow and morton reduced-form model and the hazard rate structural default models cds pricing, market approach synthetic cdo pricing risk of default, structural and reduced form copulas brownian bridge (monte carlo?) sobol' (quasi monte carlo?) crank-nicolson black-litterman (portfolio management) levy copulas (cdo pricing) fixed income: bgm, black 76 variance gamma vg le'vy stochastic monetary policy models for interest rate derivatives gram-schmitdt process

how i became a quant

very good book to get a feel for the kind of personalities in the quant world and peeks into the various places that quants work. also small glimpses into the types of problems quants work on. neil chriss has some interesting refs for using binomial and trinomial tree to match price/volatility of all available options simultaneously for european and american options. also, some other work he did on optimization of portfolio transition/liquidation combining transaction cost/liquidity with risk of holding a position too long. probably worth a look. peter j\"{a}ckel makes some interesting comments about (not surprisingly) the monte carlo method with low-discrepancy sequences in finance, in particular about getting good results with sobol' numbers in high dimensions. andrew weisman, big shot at merrill lynch, gives some warnings about the silicon ceiling: don't appear too nerdy or academic or people will think you can't make decisions. other aspects of the culture are more like a dog pack than a meritocracy. also, information-free performance enhancements: smoothing, selling volatility, and doubling up. use cvar to help with nonsymmetric hedge fund returns, resampled optimization to deal with difficult error estimation. risk metrics are ordinal, not cardinal, quantities. chapter 23 has an interesting reference to market microstructure -- using supply and demand curves on the level of bid/ask spread and probabilities of tick movements. also, a ref to a publisher that prints classic biographicals on turn-of-the-century market operators.

Saturday, January 29, 2011

cape -- cyclically adjusted price/earnings

i saw an interesting quote from page 3 of the money pull-out section of the january 8, 2011 issue of the financial times. 'eleven reasons to worry - but two reasons to invest' by merryn somerset webb. i thought the identification of the 'only really reliable long-term indicator' was interesting. i wonder if the assumption that predicted returns can come from either price adjustments as well as earnings depends on a dividend-paying stock. 'as societe generale's albert edwards points out, on a cyclically adjusted price/earnings (cape) ratio, the us market remains seriously overvalued. so, unless this time really is different (and i'd bet a good deal that it is not), we can expect to revert to mean at some point. let's not forget, as edwards put it, that while the history of the last 130 years or so has been both remarkable and appalling -- "the deaths of empires, the birth of nations, periods of deregulation, periods of re-regulation, world wars, revolutions, plagues and huge technological and medical advances -- "none of these events mattered from the perspective of value" the long run average cape remained much the same. ... i'm not worried about valuation in the short term. the cape is -- so far -- just about the only really reliable long-term indicator of stock market returns we have. but it is rarely much use to anyone in the shorter term. som, while we need to watch it, we don't need to panic every time it flashes at us.'

Tuesday, January 25, 2011

rotation, quaternions, etc.

i always have to look this stuff up when i need it, so i found a great ref here. from the abstract: We present the three main mathematical constructs used to represent the attitude of a rigid body in three- dimensional space. These are (1) the rotation matrix, (2) a triple of Euler angles, and (3) the unit quaternion. To these we add a fourth, the rotation vector, which has many of the bene¯ts of both Euler angles and quaternions, but neither the singularities of the former, nor the quadratic constraint of the latter. There are several other subsidiary representations, such as Cayley-Klein parameters and the axis-angle representation, whose relations to the three main representations are also described. Our exposition is catered to those who seek a thorough and uni¯ed reference on the whole subject; detailed derivations of some results are not presented. Keywords{Euler angles, quaternion, Euler-Rodrigues parameters, Cayley-Klein parameters, rotation matrix, di- rection cosine matrix, transformation matrix, Cardan angles, Tait-Bryan angles, nautical angles, rotation vector, orientation, attitude, roll, pitch, yaw, bank, heading, spin, nutation, precession, Slerp 1

Wednesday, January 19, 2011

fifty years in wall street

a highly recommended book on the history of us finance and some of the big names and players during the latter half of the 19th century is out of copyright and available for free online. it's a heft tome (~1000 pages) but you can get plain text, pdf with scanned images, or djvu files here or here. the plain text looks like it was ocr and is pretty good quality, though might cause some problems with tts.

Thursday, January 6, 2011

windows python in cygwin

finally solved a problem (or found a workaround, at least) for something that had bothered me for a while: when i tried to use windows python (not cygwin python, which worked fine) in a xterm, it seemed not to be connected to stdout, stderr, and stdin. neither the interpreter nor the debugger prompt would show up, and nothing happened when i used print or sys.stdout.write. the mysterious thing was it would work from a non-x cygwin shell. but i needed mouse action on the desktop and screen (which uses a text-based x windows server) remotely. turns out the problem is how cygwin interfaces a non-cygwin console app from the terminal. it talks to it through pipes rather than with a real pty, and the issues there are deep and woolly. so all these windows programs are buffering in the pipe, not realizing how impatient i'm getting on the other end. fortunately, python has an easy workaround. the -i option makes it assume interactivity, skipping the tty check. i can use it on the cli or #! shebang, and now it's working. only problem is it drops me into an interpreter when the script finishes, so i have to type quit() (c-d, c-z, c-c are all ignored). ref here

parallel, numpy, shared memory,...

trying to figure out how to do parallel processing efficiently with python, and numpy in particular. i want something simple, closely related to the original serial code (sorry, mpi, you're not welcome here). parallelpython holds some promise, dodging the gil by starting separate interpreters and piping pickles back and forth. similar to pyro, and it looks pretty seemless between smp vs. cluster. unfortunately, pp does not provide for any shared mem so big data (even read only) must be copied (and pickled!) on smp. multiprocessing is now built in to 2.6 and backported as far as 2.4 or 2.3. doesn't handle remote processes, though the pp/pyro-type pickle server (manager) interfaces with inet ports. i think it basically forks the process to make the worker processes, so you get less overhead (os service vs. cranking up a new python). and there's no need to feed it modules or any other globals; these get copied on the fork. it has some capability to share memory, though i think these are only kinda raw ctype buffers. (i think all of this is similar to the approach posh used, though more generally for user-defined types -- high quality hackery but unmaintained since 2003.) apparently some people have coaxed numpy into using these ctype arrays to make np arrays sit in shared memory land, with views available to the children. (maybe using this sort of thing.) the approach got an attaboy from the big man himself, travis oliphant, but (in the same dir) sturla has a sharedmem module written later (cleaned up and posted here) that looks like it makes lower level sys calls to create shared memory space manually. does that mean the multiprocessing shm is unsatisfactory? the paper does warn that it's a moving target, and the scipy cookbook indicates the same thing: 'this page was obsolete as multiprocessing's internals have changed.' epd has a webinar coming up promising to demo multiprocessing with large arrays, so maybe i should see what they do. anyway, if i do use this for parallel stuff, this blog post might be useful. here's another page that looks very useful for multiprocessing. fabric looks interesting, too, though more geared toward sysadmin stuff. maybe similar to posh in some ways.

financial times rss

FT offers a dizzying array of separate rss feeds for its articles, but i don't think all of them end up in the print newspaper. i'd like to get the articles that the editors have deemed most important, without having to pay the £2/paper to get them. so... i bought a copy and tried to find where the articles in the paper show up in rss. and here they are, in roughly the order of importance given at the end of 'all you need to know about the city': lex column lex main 2nd (companies and markets) section companies: companies main, uk (companies|uk, though there's a lot that's not in the paper here), uk smaller companies (companies|uk) markets: markets main (markets section, last page) 1st section management: management main (business life) comment: comment main, opinion, analysis (comment & analysis) world: world main, europe, asia-pacific, africa, us, uk business, uk economy looks like google reader might be a convenient way to combine all the streams into one and maybe even keep track of which have been read already. and i was hoping the mobile version (http://www.google.com/reader/m) would simplify the scraping to whittle it down around the body text. but it doesn't always work; sometimes it only takes the first paragraph or two and scraps the rest, probably because it catches a break before a table or image. so i think i'll have to navigate through to the original page and scrape from there. EDIT: i guess the people at ft are smart enough to make it easy for me. they post links for their print edition (and us, europe, middle east, and asia editions). only thing i didn't find on that page was the 'money' special pullout from the weekend edition. i think most of those articles were in the 'personal finance' section of the website. and i discovered that each html page for a section has an rss icon link in the upper right, so it's easy to snag stuff once i know where they are on the website. going through the sections, i found they went roughly in order with pretty close, though not exact, correspondence to the articles in print. here are the sections on the website and the page numbers of articles listed under them, to give you an idea of the density: front page: 1,1 must read national news: 2,2,2,3,3,4,4,4,4,4,4 skip 1/2 to 2/3 world: 5,5,5,5,6,6,6,6,6,6,6,6,7,7,7,7,8,8,8 good read comment & analysis: ,9,10,10,10,10,11,11,11,11 skip some of these, though the latter ones are really good letters: 10,10,10,10,10 skip all of these! life & arts: (pull-out) 1,2,2,2,2,3,4,4,5,5,5,6,7,6,6,7,8,9,19,10,10,11,11,11,12,12,13,13,14,14,20,20,17,17,17,17,17,17,17,17,17,17,17 a lot of things skipped between 14 ad 17, but i would skip this whole section. ft magazine: (pull-out) 15,54,12,7,10,8,44,44,47,43,43,46,46,52,53,51,50,49,48 i would skip almost all of this house & home: (pull-out) 1,2,2,3,6,7,7,8 i would skip practically all of this section lex: 24,24,24,24 must read companies: 12,12,?,13,13,12,14,14,14,14,14,15,15,15,14,15,16,16,16,16,?,17,17,?,17 good read, especially toward the end markets: 22,22,?,23,23,23,23,24 must read