Wednesday 30 September 2015

Python code for the two trading rules in "Systematic Trading"

This is a brief post aimed at those who have already bought a copy of "Systematic Trading" (by the way thanks!)

As you've probably noticed I've included excel sheets here explaining the two trading rules I describe in the book - exponentially weighted moving average crossover ("EWMAC") and Carry ("Carry").

Since I also have the python code for these rules, I thought I might as well share it. You can find it here:

https://github.com/robcarver17/systematictradingexamples/blob/master/ewmac.py

and

https://github.com/robcarver17/systematictradingexamples/blob/master/carry.py

I've added these links to the 'info' page on the book's website.

If there is any other code you'd like to see let me know. I've already been asked about bootstrapping, so don't worry, that is coming.

Friday 25 September 2015

A story of poor statistical intuition


In continuing my futile quest to raise the level of debate in the quantitative investment community I thought I'd have a go at another clever and very wealthy guy, Cliff Asness, founder of giant fund AQR.

Cliff Asness. There is nothing wrong with his statistical intuition. Or his suit. Nice suit Cliff. (www.bloomberg.com)
To be precise I'm not having a go at Cliff (at some point these billionaires are going to start lawyering up) but at the authors of this specific paper:

www.aqr.com/~/media/files/papers/aqr-alternative-thinking-3q15.pdf

And, to disappoint you further, I'm not really going to 'have a go' at the authors of the paper.

I actually agree completely and wholeheartedly with the sentiments and the conclusions of the paper. I also know that the authors have done and also published other, very good, research which supports the results.

What I have a tiny little problem with is the way that the analysis is presented. More specifically it is about the interaction of a particular human weakness with a particular way of presenting data.

I should also say upfront that this is a problem by no means limited to this paper. It is endemic in the investment industry; and much worse beyond it*. It just so happens that I was sent this paper by an ex-colleague of mine very recently, and it got me a bit riled.

* Here's a great book on the subject of mis representing scientific statistical evidence which is worth reading.

So this post is not a criticism of AQR in particular; and I should reiterate that I have a huge amount of respect for their research and what they do generally.


What does the paper say


If you are too idle to read the paper it looks at the quarterly returns of a bunch of assets and style factors, conditioned on there being a terrible quarter in eithier the bond or equity markets (they also do some stuff with overlapping 12 month returns). For example if you skip to exhibit 1 it shows that for the 10 worst quarters for equities since 1972 fixed income made money in 8 out of 10.

The ex colleague of mine who sent me the paper made the following comment:

"Hello Robert,

Have you seen the paper attached yet? It is interesting that global bonds had a decent performance in the 10 worst quarters for global stocks in 1990-2014, but not the other way around... Trend following seems to have good performance when either stocks or bonds suffer."*


* I've decided to leave my friend anonymous, although he has kindly given permission for me to use this quote.

My first thought was "hmm... yes that is interesting". 

Then after a few minutes I had a second thought:

"Hang on. There are only 10 observations. Is this even statistically significant? "
This was a serious problem. More so because the authors of the paper had also highlighted a key finding, which relates to something I talked about in my last post, trend following. From the paper:

"Trend was on average profitable in all asset classes returns during these equity tail events... As noted, Trend has often performed well in the worst equity quarters... Trend has been a surprisingly good equity tail hedge for more than a century"

I stared at the numbers, but I still couldn't decide whether they were meaningful or not. The underlying problem here is that humans are rubbish at intuitively judging statistical significance - even ones like my friend and I who actually understand the concept.


A bit of small sample statistics


Before proceeding let me briefly explain my outburst on statistical significance. Those who, unlike me, saw immediately whether the results were statistically significant or not can smugly skip ahead.

If we abstract away from the specifics we can reword my friends statement as follows:

"Hypothesis 1: The average return for Bonds, conditional on poor returns for Equities, is positive."
"Hypothesis 2: The average return for Equities, conditional on poor returns for Bonds, is negative"
"Hypothesis 3: The average return for Trend, conditional on poor returns for Equities, is positive"
"Hypothesis 4: The average return for Trend, conditional on poor returns for Bonds, is positive"

The third hypothesis is also one of the main points the author's flagged up.

(By the way, and this is just a small criticism, it might have been more intuitive if the graphs in the paper had been done with Sharpe Ratio units rather than mean returns on the y axis; although do the authors quote the Sharpe Ratio's in the heading. Specifically it would probably have made sense to normalise the quoted returns by the volatility of the full sample.

However I guess what is more intuitive for me might not be to many other people; so I can live with this.)

Notice that in each of the four hypothesis we have an asset we're trying to predict returns for, and another asset that we are conditioning on. We can abstract further to avoid having to model the joint distribution in an explicit way (you can do this of course, but it would take longer to explain):

"Hypothesis 1: The average return for Bonds, in scenario X, is positive."
"Hypothesis 2: The average return for Equities, in scenario Y, is negative"
"Hypothesis 3: The average return for Trend, in scenario X, is positive" 
"Hypothesis 4: The average return for Trend, in scenario Y, is positive"

Obviously Scenario X is bad equity returns, and scenario Y is poor bond returns. The next thing we need to think about is what econometricians would call the data generating process (DGP). This isn't so much where the data is coming from, but where we pretend it's coming from.

We'll treat scenario X and scenario Y individually. Scenario X then consists of a sample of 10 returns drawn from a much larger population which we can't see. Scenario Y is another 10 returns drawn from a different population. The sample mean return for bonds in X is +3.9%; and for equities in Y is -2.7% (from exhibits 1 and 3 respectively). For Trend it's 6.4% for X, and 3% for Y.

I'm also going to assume that the underlying population is Gaussian, with some unknown mean; but with a standard deviation equal to that of the sample standard deviation*; which for bonds X is about 3% a quarter; for equities Y around 5.2% a quarter, and for Trend 7% (X) and 5.3% (Y). This is all a little unrealistic, but again it would be more complicated to do it another way, and it doesn't change the core message.

* Interestingly the full period standard deviation for bonds is 2.6%*** a quarter, equities 6.95%, and trend 5%. Risk seems to be a little higher than normal in equity crisis, but not so much when bonds are selling off.

** derived from annualised figures assuming no auto correlation between quarterly returns

Looking at my hypothesis the null I'm trying to disprove in both cases is that the true population mean return is zero (I could do it other ways, but this is simpler). So let me generate by repeated randomness the distribution of the sample mean statistic for 10 observations, given the estimated standard deviation:

In python:

import numpy as np
import random as rnd
import matplotlib.pyplot as plt

stdev_dict=dict(BOND=3.0, EQUITY=5.2, TRENDX=7, TRENDY=5.3)
sample_mean_dict=dict(EQUITY=-2.7, BOND=3.9, TRENDX=6.4, TRENDY=3.0)

mean=0.0
monte_carlo=100000
sample_size=10

assetname="EQUITY"

stdev=stdev_dict[assetname]
estimate_mean=sample_mean_dict[assetname]

ans=[np.mean([rnd.gauss(mean, stdev) for x in range(sample_size)]) for unused_idx in range(monte_carlo)]
ans.sort()

if assetname in
["TRENDX", "TRENDY", "BOND"]:
    p_value=float(len([x for x in ans if x>estimate_mean]))/monte_carlo
elif assetname=="EQUITY":
    p_value=float(len([x for x in ans if x<estimate_mean]))/monte_carlo

else:
    raise Exception("unknown assetname %s" % assetname)


ax2=plt.gca()
thing=plt.hist(ans, 50)
ax2.get_yaxis().set_visible(False)
plt.title(assetname)
ax2.annotate("%.4f" % p_value, xy=(estimate_mean, 0),xytext=(estimate_mean,max(thing[0])), arrowprops=dict(facecolor='black', shrink=0.05))

plt.show()



First for equities:

Some explanation. The blue histogram shows the distribution of the sample mean, over each of the 100,000 draws of 10 returns. The arrow shows the estimate from exhibit 3, -2.7%. The number above the arrow shows what proportion of the distribution is to the left of our estimate. So there is still a 5.09% chance that average equity returns are zero, conditional on poor bond returns; and we just happen to have drawn a particularly poor set of ten returns in our data.

This result is just shy of significance, using the normal 5% criteria. We can't reject the null hypothesis.

Then for bonds:

Here we have a different story. There is almost no chance that the average return of 3.9% has come from a population with mean zero. We can reject the null. Bonds are a good hedge for equities when things get really bad.

Now for Trend, conditioned on poor equity returns:

This is very significant. Trend is a great equity hedge. Let's see if it is any good for bonds:



Not quite as good, but just creeps into being significant at the 5% level. In summary:

"Hypothesis 1: The average return for Bonds, conditional on poor returns for Equities, is positive." - we can say this is very likely to be true.

"Hypothesis 2: The average return for Equities, conditional on poor returns for Bonds, is negative" - we cannot say if this is true or not.

"Hypothesis 3: The average return for Trend, conditional on poor returns for Equities, is positive" - we can say this is quite likely to be true

"Hypothesis 4: The average return for Trend, conditional on poor returns for Bonds, is positive" - we can say this is probably true


So my friend was mostly right; 3 out of 4 is pretty good. AQR were spot on; in fact the key findings they highlighted were hypothesis 1 and 3, the most highly significant ones. What's more my own personal feelings about allocating to trend following are still justified. However it has taken a fair bit of work to prove this! 


Why we have to tell stories to explain stuff


The crux of the problem is that it's really, really hard to judge what is significant or not in small samples. Most people don't carry around an intuition about these distributions in their heads. But using small samples is quite common in papers like these. The reason is due to a flaw in the human brain, a cognitive bias, narrative fallacy. Or to put it another way we like to hear stories.

If I show you a mass of data points you will probably be thinking 'yeah fascinating. Now what Rob?'. But if I show you a nice graph as in exhibit 1 of the AQR report you'll be thinking '4Q 1987. Black Monday! Ah I remember that / I've read about that (delete depending on age)...'.

1987 crash. Yes children it's true. In the olden days traders wore suits and ties; monitors were really, really big; and the only colour they could display was green (usnews.com)

The information becomes more interesting. Clever researchers know this, and so present information in a way which makes it easier to hang a narrative off.


Why this is bad


This is bad because a story can be both unrepresentative and also statistically meaningless. If I show you a story about an aircraft crashing you are more likely to avoid flying, even if I subsequently show you some dry statistics on the relative safety of different kinds of transport.

A sample of one. (www.cnn.com)

Stories, or if you prefer small samples, can lead us to the wrong judgement*.

* I'm aware that 'you can prove anything with statistics'. However it's true to say that a rigorous analysis of a large sample set, properly presented, is always going to be more meaningful than the inferences a small sample.

Sometimes this is deliberate; as in most tabloid newspapers reporting on medical research. Sometimes it's accidental.

Of course it might be that the small sample is statistically significant, in which case we can draw a conclusion about the general population, as in the case of three out of the four hypothesis we've tested.

However if I see a paper with some small sample results in it, but no indication of significance I don't know if:

  • The authors have deliberately shown an unrepresentative and insignificant sample, and the results are wrong
  • The authors have got an unrepresentative and insignificant sample by accident, haven't realised it and the conclusions are wrong
  • The authors have got a representative sample, but not a significant one. We can't prove the conclusion either way.
  • The authors have got a significant and representative sample (the authors may, or may not realise this. I expect the AQR authors did know. These guys aren't sloppy). The authors are correct, but I have no way of knowing this.
Thus there is no way to separate out malicous intent, incompetence and good research which happens to be missing the information needed to understand the significance (as we have here). You need to tell people the values are significant. They won't work it out for themselves (even if they know how, as I do, it's far too much work to generate distributions of sample estimators for every number in every paper I read).

It's for this reason that academic papers are littered with p-values and other statistics (though that doesn't mean you can trust them entirely). I'm not saying that a 'popular finance' paper like this should be festooned with statistical confetti. But a footnote would have been nice.


Conclusion



Don't be afraid of explaining the uncertainty in estimates. Talk about it. Explain it. Let people visualise it. And if you have got significant results, shout about it.

If you're worried that this blog is going to continue in this vein (criticising the research findings of hedge fund billionaires), don't worry. Next time I'll talk about something dull and worthy, like estimating transaction costs; or I'll give you some thrilling python code to read.

But if you're only now following me in the expectation that I'll be writing a post next week about David Shaw's inability to do stochastic calculus, or Ray Dalio's insistence on assuming returns are Gaussian, then I'm sorry you will be disappointed (and if their lawyers are reading, neither of those things are true).

Tuesday 15 September 2015

So you want to be a trader?


I often get asked, or see people asking, how they should become a trader or fund manager? The short answer is in this diagram:

Don't worry there is a bigger version of this later


.... but that probably won't make a lot of sense unless you read the rest of this post.


The four kinds of trader

 

When I speak of a "trader" I'm going to include both those who actually do the buying and selling, as well as those who make the decisions to buy and sell, since they won't always be the same person. So fund managers are 'traders' even if they have someone else to do the actual clicking of the button (or nowadays, an execution algorithim). I'm also including systematic traders who outsource both the decision making and execution to computers, but have to design and manage the system.

Broadly speaking there are four kinds of trader:

  • Proprietary ("prop") trader (someone who trades the firms capital)
  • Institutional, buy side, portfolio manager (trades other peoples money, makes the decisions or designs the system, may or not do the execution)*
  • Institutional, sell side (eg investment bank options trader, uses bank capital but not primarily a risk taker)
  • Self funded independent trader (someone who trades their own capital)

Note that the 2nd and 3rd categories are salaried traders; they get paid a salary which is normally pretty good, plus a bonus. Prop traders normally only get a percentage of profits, though there might also be low base pay in some of the better shops. Independent traders eat everything they kill; and also stump up for losses.

I feel qualified to write this as I've worked in 3 out of these 4 jobs (portfolio manager, sell side and independent), although I have never been a proprietary trader (I do know quite a few though, so I'm not writing in complete ignorance).

An important caveat: I'm mostly ignoring the distinction between discretionary and systematic trading** here, or between asset classes. However the educational background required to be a systematic fixed income options trader is clearly different from what a discretionary spot FX trader needs; so bear that in mind. 

* in the interests of clarity I'm excluding 'execution traders', who execute buy side trades but do not make decisions as to the size or sign of the total order. This is not done for any particular reason other than making the picture above close to readable, and it's a relatively niche job; I'm not suggesting execution traders aren't real traders or anything like that... some of my best friends are execution traders...

** Systematic high frequency traders normally trade prop capital; but their career path is more similar to a systematic buy side portfolio manager so this where I categorise them.


Why do you want to be a trader?

 

This is the first question to ask yourself. The answer to this will determine if trading is really for you, and also help you decide which of the four categories you should be aiming for. Typical answers include:

  • Money (financial security)
  • Prestige (impressing people of the appropriate gender and sexual orientation at cocktail parties)
  • A deep interest in the financial markets
  • A fast paced working enviroment
  • Working with intelligent and interesting people
  • An interest in an academic subject (eg maths, computing, economics, pyschology ...) and the chance to use this in real life.
  • A high degree of autonomy 
  • Being judged on results, not subjective BS
  • Short working hours (at least compared to other finance jobs, and being say a doctor or something)

You should probably think hard about your answer. Don't say "yeah I want to work with intelligent people", if the real answer is money. This isn't a job interview. Be honest with yourself.

If you say "money", then why? Is it financial security you want, or do you want to be able to buy a flashy lifestyle and thus gain "prestige". Would you be happy as the "millionaire next door", financially secure but outwardly thrifty?

http://doesyourbaghaveholes.blogspot.co.uk/2009/07/what-cars-do-wealthy-drive.html

Or do you need to be the guy or gal turning up to a school reunion in a red Ferrari?

If you fall into the latter category I highly recommend reading Affluenza if you haven't already done so.


Which type of trading is best for me?


Now I'm going to discuss each type of trader in turn, to see how they match up to the different motivations above. Note that (a) this is all my subjective opinion and (b) things vary considerably within categories - what I'm showing is an average here. So for example whether your job is academically relevant depends more on your academic interest, and the type of trading you are doing, than on if you are on the buy side or sell side. Also working hours for salaried traders are more correlated with experience - juniors tend to do more - than with category.


First though a word about money.


Money


In general traders are paid like footballers. To be more exact there is a massively skewed or if you like pyramid distribution of income for both "professions" (although top traders earn much more than the best footballers; and the mean salaried trader probably earns more than the mean professional footballer). At the top end are the likes of Wayne Rooney* (perhaps £20 million including sponsorship deals), and the likes of George Soros, Ray Dalio and Steve Cohen (£1 billion plus). Then there are lower paid premiership players (£1 million upwards) comparable to mostly buy side and a smaller number of sell side high earners, with perhaps a handful of individual and proprietary traders (tens of millions). 

* apologies for the UK football centric figures here; feel free to transpose to your own preferred sport and country

Mr Rooney, no doubt wishing he was a hedge fund manager. http://www.theguardian.com/football/blog/2014/aug/13/wayne-rooney-manchester-united-captain-louis-van-gaal-robin-van-persie

Then we gradually descend through the lower ranks of professional footballers, at the bottom of which are bottom end league 2 players on about £30,000. Somewhere in the professional leagues are the equivalent of the average buyside and sellside traders; with average proprietary traders coming in closer to the bottom (in relative ranking, if not in actual equivalent pay).

We then have the part time and amateur footballers. Note that no footballer earns a negative number, although an amateur might end up spending a couple of thousand a year on membership, kit and travel costs. In contrast the majority, and perhaps up to 90%, of independent traders lose money. Some might be getting through five or six figures a year (though one hopes this is not for very long).

Averages are meaningless with such skewed groups. A better way of summarising is to look at ranges. First I've put a normal range which I'm reasonably confident covers about two thirds of people in each group. I've also put an extreme range, which I think 98% of people fall into (with some extremes at the upper and lower end). Apologies for putting these figures in GBP, but all the readers of this blog can probably do currency coversions in their head.
  • Proprietary trader: Normal range: £20K to £100K. Extreme range: -£10K (cost of a training courses) to £2 million.
  • Portfolio manager: Normal range: £100K to £500K. Extreme range: £50K to £10 million
  • Sell side: Normal range: £75K to £300K. Extreme range: £50K to £2 million
  • Independent trader: Normal range: £-5K to 0K. Extreme range: -£50K to £500K

Notice that some parts of the profession have a wider range than others; and that the salaried traders (sell side and portfolio manager) have higher minimums.


Proprietary trading

Success in prop trading is proportional to how many screens you have. Apparently.
Source http://cn-chillies.com/


Good for: fast paced,  judged on results

Okay for: money, prestige, interest in finance, autonomy, working hours
 
Bad for: co-workers, academic relevance

Being a prop trader means it's all about your p&l. Nothing else matters. You're only job is to trade the firms money. No staying late at the office to score political points. If you're an adrenalin junkie, there's no better enviroment. If you're not then working with hyper competitive people for several hours a day might be a little draining. Also forget about using your econometrics phd. It's just like playing a computer game.

I have never been a prop trader, and wouldn't ever want to be. But I do know some poor deranged souls who are.

Portfolio manager

John Paulson. Hugely successful hedge fund manager. So good, he can trade with his back to the desk.
www.businessinsider.com

Good for: money, prestige, interest in finance, co-workers, academic relevance, judged on results

Okay for:fast paced, autonomy, working hours

Bad for: I can't think of anything. Which is why this is the most sought after kind of trading job.


Oh yes, to be a fund manager, especially at a hedge fund collecting 2 and 20 rather than long only getting a mere 0.5 and 0. The best of these jobs are very pleasant indeed, and very, very, very, well paid. There is often the chance to spend lots of time thinking interesting thoughts, and little doing boring stuff. The only people you have to answer to (unless you've made enough money) are outside investors, and annoying risk managers.

Anyone with any sense would prefer this to any other kind of trading job, which is why they are insanely hard to get.

The author's last real job was working as a hedge fund portfolio manager, specifically at a systematic CTA.


Sell side

Tom Hayes, LIBOR fixer extraordinare. Currently the most famous sell side trader in the world. But perhaps not the best role model.
www.telegraph.co.uk

Good for: money, prestige, interest in finance, fast paced

Okay for: academic relevance, co-workers, judged on results

Bad for: autonomy, working hours


This is what most people think of when they think about traders: some guy in a bank barking 'buy! sell!' down a phone whilst snorting a line of coke as an exotic go go dancer shines his shoes with their naked derriere.

Now for reality.  Nobody has used an actual phone on a trading floor since 2006; it's all done electronically or on Bloomberg IM unless you are doing some insider trading or LIBOR rigging. Also coke and go go dancers may have been de riguer on the trading floors of the 1980's but no longer.

Also being a bank trader is nowhere near as interesting as it was pre 2008. Regulation and compliance mean that proprietary risk taking (rather than just hedging customer flow) is almost absent from banks. As a result although base salaries have risen top bank traders are now very much the poorer cousins of their buy side colleagues (though minimum pay is higher, and there is less variability). Plus the hours in banks were always longer than the buy side; that at least hasn't changed.

The author spent a year and a half working as an investment bank trader, and hated every minute.


Independent

Navinder Sarao. Alleged to have caused the flash crash (complete nonsense IMHO - Free #NavSarao). Currently the most famous  independent trader in the world.
cnbc.com

Good for: autonomy, working hours, judged on results,

Okay for: interest in finance, academic relevance, fast paced (but varies)

Bad for: co-workers, prestige

Terrible for: money

The main downside of trading independently is it's the only job in this post where you can get paid a negative amount; and indeed most people who try it manage to do that. And you don't get to work with interesting and intelligent co-workers, just some bozo who sits at home all day, probably just wearing his* boxer shorts (that's you)**. And it isn't that prestigous; since anyone in the know will realise (a) you probably don't make any money and (b) you're just some bozo who sits at home all day in his boxer shorts.

* if you don't wear boxer shorts, put the name of your favorite trading clothing here
** you could of course join a trading arcade; but that just means you'll be hanging around with other bozo's, though hopefully wearing slightly more.

The upside is you can, depending on your trading style, spend very little time on actually trading and lots about thinking interesting thoughts about the financial markets (though you're going to be a little out of the loop compared to those working in the febrile atmosphere of a real trading floor). You can use your obscure phd in neural networks to predict prices if that is your wont (though be warned, it probably won't work). And you never have to take any s*** from anybody. Except your wife, asking why you don't have a proper job anymore, and if you're home all the time anyway; perhaps you could do some chores?

In the interests of full disclosure the author of this post is currently an independent trader.


The map


We're now in a position to return to the map.



The various 'trader' boxes should now make sense. Within the buyside and sellside I've included additional boxes for the front and back office. I've also broken out 'unprofitable' prop and independent traders; these are a stop on the journey rather than the final destination.

The lines on the map show possible routes between boxes. Very thick lines are routes that are straightforward and heavily travelled; thinner lines less so (though this is all relative). So for example it's very easy to become an unprofitable independent trader; but very hard to go from independent trader to fund manager.

If a line isn't there it means I think it highly improbable that those trajectories will happen, though of course they are not impossible.

The lines are also coloured. Each colour represents a different route through the trading "profession", which I'll discuss next.

This is clearly a simplification; and I'll talk through some of the missing nuances below.

The main routes


Lone ranger (red)

It's very easy to become an independent trader. You just need some money and a brokerage account. A few clicks later and hey presto! You're a trader.

It's much, much harder for someone who is self taught, with no relevant experience and probably low starting capital, to become a profitable independent trader; one whom can give up all other work and live comfortably on the net profits from their business; with enough capital in reserve to smooth over the inevitable bad months and years that even brilliant traders will have.

A successful independent trader might want to become  a prop trader, and leverage their skills off a larger capital base. However the business model of most prop shops assumes that you trade pretty frequently to provide flow to brokers for hich the shop receives a kickback. It's very hard to be profitable if you trade a lot independently; surviving independents are most likely to be too slow to interest prop shops.

Even harder is for an independent to launch their own hedge fund. Despite this I often see people saying "I have traded for 6 months and made money, how do I start my own hedge fund". There are some new iniatives that might make this easier. But be warned this route is the one with the lowest chance of success.

Prop bandit (yellow)


It's just as easy to become a prop trader. All you need is some money, which you usually have to hand over to the prop shop for 'training'. There are some more reputable shops which don't require this up front payment, but obviously they are more selective with who they pick.

Not everyone who gets in to the training course will subsequently be chosen to trade the firms capital; and only a proportion of those will last for long enough to make a reasonable steady income.

A good prop trader can probably go independent quite easily; although if they are intraday traders they may need to stay within the fold of a prop shop to keep access to the low commission rates needed to succeed in this arena.

It's slightly easier for a good prop trader to become a fund manager than it is for an independent trader; larger amounts of capital and more trading equate to a verifiable track record. Nevertheless again this is a route which has a low probability of success.



Backroom boy (blue and green)


With many apologies to female wannabe traders, but the alliteration here is too good to miss. In the olden days it was relatively common for people to begin in the mail room (this guy didn't even start there, the mail room was a promotion for him) and work their way up to CEO. Exchange floor traders would frequently start as clerks and runners.

Sidney Weinberg, telling everyone for the like the millionth time that he started as a janitors assistant, and how young people today don't even know they are born.... https://en.wikipedia.org/wiki/Sidney_Weinberg


A modern day version of this parable is to begin in a back office role (or middle office), work hard, impress the right people and hoist yourself up to a front office role. This is do-able, but not easy. You're also going to be a few years behind the cohort who start out in the front office.

You'll also need the right qualifications; if you don't start without them you'll need to go back to school to get them. The reason this strategy makes sense is that it's easier to get a back office role than to go straight into the front office. It is however still a gamble and not a path that many people travel. If you can get straight into the front office (next route I discuss) you clearly should.

There isn't much difference between the buy and sell side; except in general it's harder to get into the buy side from scratch. You can also make a parallel move from sell side to buy side; with the reverse move being slightly easier.


Fronting up (cyan and yellow)

This is probably the most common route into trading; and hence probably the most straightforward.

If you have the right qualifications, land a few good internships, and impress at an interview; then you can go straight into the front office. From here the trading desk is much, much closer. Of course there are fine variations within the front office; some front office jobs are more relevant than others, and the journey to the trading desk will be easier depending on your starting point and the kind of trading you wish to do.

Economists and strategists will fit neatly into global macro type roles, equity (bond) analysts will obviously find it easy to move to equity (bond) trading, whereas quant analysts are a more natural fit for options trading and systematic trading. Good technologists with the right experience can also move into systematic portfolio management roles. Moving from risk management into trading is harder, but I know of people that have moved between those two roles comfortably*.

* If I ruled the world all traders would have to work as junior risk analysts for 2 years before buying a single thing. And all senior risk managers would have to have at least 5 years trading experience.

If you're in an M&A or sales role then it's harder to see how a move into trading would make sense, but then these are worlds I know almost nothing about so I can't really comment.

Again it's harder to get into the buy side, and you can move from sell to buy side; with the reverse move being slightly easier.


Overachiever (dark blue and purple)


A very small number of people will go straight on to jobs on a sell side trading desk (although initially it's likely to be very junior, with relatively small budgets). An even smaller number of people do the same on the buy side.

Clearly this is the dream ticket, but you shouldn't pin your hopes on it. It really is very difficult. You should have a plan B in case this doesn't work out, like another front office job.

You can move from sell to buy side; with the reverse move being slightly easier, yeah yeah you know this by now...

There are some lines that might seem odd on the graph; from sell side trader and portfolio manager back up to independent trader. Well it's pretty simple; these are very well paid jobs. Which means trading independently is possible; and you're probably going to have the skills to do it pretty well (although the move is slightly harder for sell side traders who may find their trading strategies don't work so well when not embedded within the customer flow of a banks market making).


Final advice


If you really want to be a trader the easiest route is as follows:

  • Go to a really good university and get good qualifications
  • Get a sell side front office role that will put you near the relevant trading desk
  • Move to the relevant sell side trading desk.
  • If desired a move over to buy side portfolio management can follow
  • At this point if you're any good and you don't blow your bank account on frivolous pursuits you'll end up with enough money to trade independently, or spend the rest of your life at the beach if that is your thing.

Finally, if you're curious, and in the interests of full disclosure, below is the route I took:*


* I had a 2.5 year break from finance when moving from the sell side, to buy side fund manager...

Make of that, what you will.

Best of luck to you all in whatever career you decide to pursue.


Monday 14 September 2015

"Systematic Trading" is released today


Today is the big day. My book Systematic Trading - A unique new method for designing trading and investing systems is officially released (although some people who ordered the ebook may already be reading it).

Here is a picture of my new "baby":



Even in this age of technology there is something cool about having produced an actual physical book, and I'm dead chuffed (note to non-native english speakers - this means terribly pleased). But don't just take my word for it, at least one other person thought it was good (apart from my mother, who thought it was amazing, even though if I'm being honest she may not have grasped some of the finer points):

"A remarkable look inside systematic trading never seen before, spanning the range from small to institutional traders. This isn't only for algorithmic traders, it's valuable for anyone needing a structure - which is all of us. Carver explains how to properly test, apply constant risk, size positions and portfolios, and my favorite, his "no rule" trading rule, all explained with scenarios. Reading this will benefit all traders."

Perry Kaufman, author of Trading Systems and Methods, 5th Edition (Wiley, 2013)


Anyway, you can find out more about the book here:

http://www.systematictrading.org/


Or if you already know you want a copy then you can buy it here:

http://www.harriman-house.com/book/view/4598/trading/robert-carver/systematic-trading/


If you'd like to hear more then I will be talking about my book in various places (both actual and virtual) over the next few months. Keep an eye on this page for more details.

It only remains for me to thank anyone who has already ordered the book, or will do so in the future, and to hope that you enjoy it.

Wednesday 2 September 2015

Systems building - Checks and balances


Driverless cars are, apparently, very close to commercial reality. I don't know about you but there is something pretty scary about a computer being completely in control of a complex process, which could have catastrophic consequences if it went wrong.

Ah it was nothing. You should have seen the other guy...  (From autospies.com)

That might seem a strange attitude. After all I run a fully automated systematic trading strategy. True if it goes wrong the consequences are 'just' losing money, rather than death and dismemberment; but I am pretty comfortable with letting the computer trade my money for me. Partly might be because I wrote the software myself; but given I am a competent amateur rather than a professional programmer this shouldn't be that reassuring.

No - what gives me comfort is having information about my system. Because I have a series of ways to automatically monitor my system; to understand if it's behaving properly and if anything is going wrong.

This post will tell you how I keep a close eye on my automated trading computer. It is the sixth, and final post, in a series on the nuts and bolts of building systematic trading systems. Warning: there are a few python and linux commands sprinkled here. However you don't need to be familiar with these environments to follow the post.


I like driving in my car


Let's return to the car analogy. In my car I have a dashboard with various lights and dials. If I had a nicer car, I'd probably have a built in touch screen interface as well.

Now that is what I call a dashboard ... (knightrideronline.com)

Leaving the specifics aside these various components can be classified as one of:

  • Indication lights. Binary (on/off), and which show if something is happening or not. Example - Turn signal / indicator light.
  • Warning lights. Binary (on/off), and show if something is wrong if lit. Example - low fuel warning.
  • Informative dials and displays.  Variable, show to what degree something is happening. Example - speedometer.
  • Interactive reporting. Can be interrogated to give different information. Example - satellite navigation or in car computer touchscreen.
I'll use the same keywords here to classify the different types of monitors and reports I generate. Note that a warning light ought always to be off, but an indication light could 'legally' be on, or off. If you saw your turn signal blinking when you hadn't selected it that would be a problem.

 Note that with all diagnostics the actual reporting component might be broken. So you can get false errors when all is well, or a report that all is well when it really isn't (those with some statistical training might think about type 1 and type 2 errors: usually I'd prefer to get a false error, than miss a real error).

This means there is a flaw in having warning lights. If a warning light isn't on, then it might indicate there is no problem. Or it could indicate the warning light itself isn't working, but there is an underlying problem. It's perhaps better to have a light which is green if there is no problem, then goes red when there is.

That's the analogy; so how do we actually do this in practice? We need some way of getting the underlying raw information, and then we need to report it to the user.


Sources of raw information


There are three different underlying sources of information that can be used for system status reporting: logs/echoes, diagnostics and existing data.

Logs and echoes


The most basic information you can use are echoes and logs. Echoes are my name for the trace output from a process (everything that is printed to screen, or stdout if you want to be pedantic). So my unix crontab* begins like this:

run@bilbo ~/echos $ crontab -l
#
# Daily sample FX Prices
#
0 6  * * 1-5 $HOME/workspace/systematic_engine/syscontrol/scripts/samplefx  LIVE >> $HOME/echos/samplefxpricesLIVE 2>&1


* Note for non unix users - a crontab is a schedule of processes that get kicked off on a regular basis. It's one reason why linux / unix is better for automated processes.

The part after >> means that the trace output from calling the script samplefx LIVE will be dumped into the file samplefxpricesLIVE. This is the 'echo' file for this process. Good things about echo files are that they are simple, and that without any explicit error handling they'll always dump the trace of an error failure when it happens. Bad things are that they include everything that a process writes to stdout, and unless you truncate them regularly they just get larger every day; so it can be hard to find stuff.

Logging is a bit more sophisticated. It requires software to say 'I want to log this please' rather than just printing.


run@bilbo ~/logs $ cat fx.log
2015-08-31 06:00:04 INFO    : fx           : sample all fx running  (sample_all_fx)
2015-08-31 06:00:04 INFO    : fx           : Running in AUTO mode (sample_all_fx)
2015-08-31 06:00:04 INFO    : fx           : sampling USD (sample_all_fx)
2015-08-31 06:00:04 INFO    : fx           : sampling AUD (sample_all_fx)
2015-08-31 06:00:04 INFO    : fx           : sampling AUD: getting quandl price (sample_all_fx)
2015-08-31 06:00:11 INFO    : fx           : Added fxprice AUD 2015-08-31 00:00:00 0.716025 ok (add_price_row)


In my system this is produced my simple commands like this:

    log=logger()
    log.info("sample all fx running ")
    log.info("Running in %s mode" % entrymode)


My code:
  • Outputs everything in a nice way, with time stamps, and padding so it's easy to read
  • Handles different levels of log message differently - information, warning, error or critical error.
  • Recognises which module is writing the log, and which parent process it's come from (using the python function inspect.stack)
  • Directs messages to the appropriate log file, depending on what level of error we have and the parent process.
  • Sends me an email when a critical error is logged.

It's based on the python logging package. This package also allows a new log file each day, and the automatic archiving of old log files.

Note that both logs and echoes aren't much use for understanding what is going on. You can do basic monitoring by running tail logfilename -f in a unix terminal, which will show you a live updating file (and is all you can really do if people ask to see your 'system in action'), but there is nothing here like the dashboard of a car.

"Can I see your trading system?". What people expect to see.... (kevinonthestreet.com)

.... and what people actually get.

They also aren't much use as a data layer for a reporting application, since a big unstructured text file isn't the best way to store retrievable data.Normally you'd only go to these files if something had gone badly wrong, and you needed very detailed information about what happened.

Diagnostics


Unlike the free text of logs and echoes; diagnostics are a structured and usually quantified record of what a process has been up to. So rather than logging "Just got an ask price of 124.2 for US 10 year bonds" I'd store the value 124.2 in a place specifically for that purpose.

A diagnostic record is a value (or sometimes text string), for a given date, and for a given set of characteristics, eg if I store the ask price for 10 year bonds, then I could get the time series of such values with a python command like diagnostic(system="prices", instrument="US10", value="ask_price").read()

If you're interested I store my diagnostics in a mixture of database and hdf files, with the read/write methods figuring out what to write and where to read from based on the type of the value being stored.

As with logs/echoes I will rarely look at the naked raw data. But they are used as the data layer for reporting. About two thirds of the information that the 'user facing' reports that I discuss below ultimately comes from diagnostics.


Existing system data


Here I am referring to the data I actually use in my system, eg prices, intermediate calculations, positions and trades. This isn't data specifically stored for diagnostic purposes; it's needed anyway. About one third of the information in my reports is just pulled from existing databases. Since generating and storing diagnostics is costly (both in terms of processing power and disk space), it's better to use data you already have if you can.


Reporting

This isn't my trading dashboard. But it's cool though, isn't it? (dashboardspy.wordpress.com

No, I don't have a fancy GUI that displays an actual trading dashboard (although my former employees, AHL, have an amazing one displayed on giant touchscreens in their offices if you can talk your way into them); instead I have a mixture of:

  • text reports that are emailed to me each day, 
  • a series of scripts that I can run manually to give me different kinds of information, again text based; 
  • and an interactive method for visually reporting ad-hoc diagnostics.


Reports


All my reports are produced daily, and I produce two versions. The first is emailed to me, and contains only problems (the warning lights). The second is stored on disk, and is more verbose.

I can also run all my reports interactively on an ad hoc basis, perhaps with different arguments; so rather than getting just todays trades I could get them for the last two weeks. 

Monitor report


I run two kinds of reports that give indication and warning type information.

The first report is my monitor report. This is a very simple report that:

  • Check's when a particular process last happened (from explicit diagnostics for this purpose) or particular kind of data was last written (by examining the appropriate database tables).
  • Compares this to when we expected it to happen. Daily events are expected to happen.... yes every week day; other processes during market hours.
  • Reports anything that is delayed, where the definition of delayed for me is 12 hours (fine for a relatively low frequency).
A typical emailed monitor report might look like this:

Ran monitor report at 2015-08-31 12:08:11
 Following are delayed more than 12 hours


prices-fx                 HKD          504.0     2015-08-10 00:00:00



\ ***************  FINISHED  ******************



This is telling me that I have a delay on FX rates for HKD of 504 hours; more than the 12 hour delay cutoff (this is an extreme example as I've been on holiday - a wet camping holiday if you must know).

This is a picture of me on my holiday, whilst HKD was moving slightly more than usual. Well it's not actually me. But you get the idea. (http://www.bbc.co.uk/)

I'd then need to find the underlying cause; which could involve looking at other diagnostics, or log files (in this case it's because the FX rate - normally virtually fixed - moved more than expected, which requires a manual intervention on my part).

The full verbose version looks like this:

run@bilbo ~/reports $ cat monitor_report.txt | more
Monitor report at 2015-08-31 12:08:11


 prices-fx                HKD          504.0   2015-08-10 00:00:00
last_beat-samplerunner    PLAT         1.3     2015-08-28 18:31:50
last_beat-signalsrunner   PLAT         1.3     2015-08-28 18:31:53
prices-optimal            PLAT         1.3     2015-08-28 18:32:59
prices-adj                EDOLLAR      1.3     2015-08-28 18:36:16
last_beat-samplerunner    EDOLLAR      1.3     2015-08-28 18:36:34
last_beat-signalsrunner   EDOLLAR      1.3     2015-08-28 18:36:39
prices-optimal            EDOLLAR      1.3     2015-08-28 18:37:49
prices-adj                WHEAT        1.2     2015-08-28 18:43:48
last_beat-samplerunner    WHEAT        1.2     2015-08-28 18:43:56
last_beat-signalsrunner   WHEAT        1.2     2015-08-28 18:43:59
prices-optimal            WHEAT        1.1     2015-08-28 18:45:11 


.... and so on. Clearly everything is running smoothly. The delays of just over an hour are misleading; for simplicity I assume that everything 'should' have got a price exactly when the market 'closed' (and for these US markets, that is my system closing time of 8pm local time).

That is why I use a 12 hour threshold - I am happy if my system runs once a day for a given instrument. A different tolerance for delays would make sense for other people; or for those whose system always gets a price for a given instrument at specific times each day.


Reconcile report

 

My second report for indication and warning information is the reconcile report. This runs a series of cross checks to see if the various system states are properly aligned. This won't make any sense until I explain it.

An emailed reconcile report would look like this:

Reconcile report issues at 2015-08-31 13:00:03


 No issues with optimal vs actual positions

IB and my positions perfectly matched

IB and my fills perfectly matched

Roll status with action needed

       code                              msg  near_expiry_days  position price_contract  relative_volume rollstatus                 suggest
2   LIVECOW           Roll positions ongoing                 0        -1         201510         0.004920      ALLOW                CONTINUE
7      BOBL         Roll complete *ROLL ADJ*                 7         0         201509         0.012277      ALLOW                ROLL_ADJ


 Run rollinstrument as needed

Missing forecasts

Missing last value for  KR10 cryer
Missing last value for  KR3 cryer
Missing last value for  LIVECOW rushton
Missing last value for  KR10 rushton
Missing last value for  KR3 rushton
Missing last value for  BOBL rushton
Missing last value for  BTP rushton
Missing last value for  BUND rushton
Missing last value for  OAT rushton
Missing last value for  SHATZ rushton
Missing last value for  AEX rushton
Missing last value for  CAC rushton
Missing last value for  SMI rushton
Missing last value for  EUROSTX rushton


no instruments at limit

No stopped process


No instruments with trade controls
 

 
No instruments overrides


No issues with positions (locks, other contract trading, in forward when not rolling)



\ ***************  FINISHED  ******************



Clearly what we will want to see is lots of smiley faces (yes I really do have them in the report). These are the 'green' warning lights I mentioned in my original explanation of the dashboard metaphor. If there are is a missing smiley faces then I might need to investigate further.

Let me quickly explain each of the sections of the report:

  • Are the positions I have (according to my position database) in line with the optimal: what I want to have given current market prices and my trading rules?
  • Are the positions I have (according to my position database) in line with what my broker is reporting to me?
  • Are the fills I have for today (according to my trades database) in line with what my broker is reporting to me?
  • Are any of my futures contracts in a state where I need to think about rolling them forward?
  •  Am I missing the last forecast for a given trading rule variation (here shown with internal code names) and instrument? This might be for legitimate reasons, but I need to know about it.
  • Have I exceeded my self imposed trading limits - maximum number of contracts per day; or position limits - maximum number of contracts to hold?
  • Have I manually stopped any of my processes from running? This might be a valid thing to do, but I don't want to forget about this and leave it turned off.
  • Are any of my instruments in a trading state other than "OK TO TRADE"? Again I might want this, but I musn't forget I've done it.
  • Have I applied an override; eg cut positions by half in this instrument, to a given instrument?
  • Are any of my instruments locked (meaning a trade is taking place)
  • Do I hold positions in something other than the current, or next contract I want to roll into?
  • Do I hold positions in the next contract, when I am not in the right rolling state?

There is also a longer report on disk, but I won't reproduce it here. Again clearly there are some things here that are quite specific to a futures system, but you should be able to put together a checklist for your own system which reflects your own concerns.

 

Risk report


The remainder of my reports are for informative purposes. Probably the most important of these is my risk report. This is a complex report, so I'll go through section by section.

The first section shows instruments where 'stuff has happened' - eithier in the market, or my system, or both. It's those instruments where closer attention might be warranted if the system has started behaving weirdly.

In the first table I show the instruments for which my forecast has changed the most today. I also show the daily price change, normalised for volatility, and the current size of the forecast. If my intuition suggests that the forecast change is large, or unwarranted given market movements, I will delve deeper.

The next table shows instruments for which the absolute volatility normalised daily price change is bigger than 1. Again I also show the forecast, and the change in forecast. I have similar tables looking at changes in prices, and forecasts, over a 2 week horizon (given the speed of my system they are likely to be more meaningful).

Section two focuses on my biggest expected risks.

I list all instruments for which my forecast is larger than 15 (where the expected average absolute value of my forecasts is 10). I show the actual positions, and optimal positions, I have in each instrument and show all the numbers used to calculate them, such as instrument weight and diversification multipler.

Obligatory book plug: This part of the blog will make a little more sense if you read part three of "Systematic Trading".

I also have a table with all the instruments for which the monetary value of my risk exceeds a given amount. I compare the expected risk I'd have if I could hold fractional futures contracts with what I actually have given discrete integer positions.I compare actual versus expected risk (using 2 weeks of returns for the former) for the instruments with the largest risk.

Section three of the report looks at total portfolio risk. I calculate this in various ways, with different assumptions for volatility and correlations. I also include statistics on my cash holdings here (so I can see if I need to do any spot currency transactions).

The final section looks at my return, volatility, and Sharpe Ratio over various periods (from 2 weeks up to one year).

As usual the emailed version includes only the biggest positions and risks; a more verbose version gives values for every single instrument.


Trades report

 

This is simply a list of all the trades I've executed, and my calculation of execution costs - what I could have paid if I'd been able to execute at the mid, what I would have paid if I'd crossed the spread immediately, and what I actually paid (the difference between the latter two due to my simple execution algo). It also shows the time delay between getting a price, calculating the resulting optimal position, issuing the order, and getting filled.

Any trades with large costs (even if they're positive) or long delays might warrant further investigation.

The default emailed report will be of just todays trades (this is one occasion where there is no fuller verbose version), but I can also interactively run the report for a longer period.



Profit and loss report

 

You can easily imagine what this report is for. Read my detailed post on accounting data to see how I use various sources to work out p&l numbers. I default to running a p&l for a single day, at the close, but I can also get it for different periods interactively. The emailed report contains the highlights (biggest profits/losses), whilst the verbose version has every single position I've held during the reporting period.

Very large p&l would pique my interest, but I'd probably look at the risk report to see where it came from (an unusually risky position, or a very large move in price).

 

Scripts


Here is a list of the scripts I can run in a unix terminal to get various nuggets of useful information when required (with various additional arguments). The emphasis here is on small pieces of code to do one particular specific job. Note that most of these diagnostic functions are also used when I generate the emailed reports (the underlying code can be run eithier by a script and display output; or it can return the raw data to the reporting 'engine').

  • displaylimits: What are my positions, and trades, for each instrument and how do these compare to my self imposed limits on those?    
  • rollstatus: Present eithier a summary of futures roll status, or a detailed report for an individual instrument.     

  • displaydiag: Display the diagnostic values for a given set of characteristics.  
  • lastbeat: Show when each process was last run for each instrument  
  • orderdiags: Display all the diagnostics generated by my execution algo for a particular order.     
  • displayforecasts: Show a pandas data frame of the forecasts I have for each instrument.
  • missingforecasts: Print a list of instruments and trading rule variations for which the last forecast value was missing.

  • showorder: Give me information about a particular trade.
  • displaytickets: Show the orders (both filled and unfilled) for the last N days
  • recfills: Compare the fills data from my database, and from the broker      

  • displaypositions: Show a pandas data frame of the positions (summed up to instrument level).  
  • lastposition: Show me the current position in each futures contract
  • lastopt: Last optimal position for each instrument (the position my system wants to take)  
  • displayopt:          Show a pandas data frame of the optimal positions for each instrument.     
  • optvspos: Compare my optimal positions to what I actually have.   
  • recpositions: Compare the positions from my database, and from my broker.       

  • displayfx: Show me the time series of FX rates for a particular currency    
  • lastfx: Show me the last FX rates for all currencies.   
  • displayvolume: Show a time series of volume for a particular instrument and futures contract.   
  • displayraw: Show me the time series of raw futures prices for a given futures contract.      
  •  displayadj: Show a time series of prices, after futures back adjustment  
  • lastadj: Give me the last futures back adjusted price for each instrument  

  • findIB: Find the instrument which has a given brokers identity code.          
  •  showcontract: Give me all the static information about a particular futures contract.  
  • showinstrument: Give me all the static information about a particular instrument.

   

  

Interactive diagnostics


A picture is worth a thousand words. This is why your car dashboard has a little picture of a petrol pump as a fuel warning light, rather than LED lights spelling out "NEARLY OUT OF FUEL!". To find out what is causing some behaviour, like a particular trade, will often involve looking at a dozen or more individual diagnostic time series.

So I've written some stuff in python to allow me to understand visually what has been going on. Let me give you a quick demo. I usually don't run this on a trading machine to avoid loading it up or the remote chance of a file lock. The diagnostic data is copied every day to my NAS, and then pulled down to my laptop.

This is in a python session:

from syssignals.diags import diagsBlob
from matplotlib.pyplot import show
from sysdiag.picture_subfuncs.display import plot_it

data=diagsBlob("LIVE", "VIX")

This is quick since the class loads in diagnostics 'on demand'. To load all the diagnostics up for a given instrument would take a while, so it doesn't get something unless it has to. Now we're actually going to plot something; when we run this there will be a delay whilst the diagnostics that are needed are 'sucked' in.

ans=data.show_how_combined_signal_calc_dict()
plot_it(ans)


Example of a multiple plot. The dataBlob method returns a dict of pandas dataframes. plot_it is just a function to plot these.       

data.show_position_and_threshold().plot()
show()

Example of a single plot. The dataBlob method returns a single pandas dataframe, which can be plotted as normal.



The end


As it's the end of the series, what better picture to conclude than one of a tombstone? Or in this case a gravestone, apparently (forextradingstrategies4u.com) Warning: The author of this post does not endorse the use of candlestick analysis, and would go as far to say that it is at best scientifically unproven, and at worst complete nonsense.

Yes, this really is the final post in my series on the nuts and bolts of building systematic trading systems.

The first five posts are:

http://qoppac.blogspot.co.uk/2015/04/system-building-data-capture.html
http://qoppac.blogspot.co.uk/2015/05/systems-building-futures-rolling.html
http://qoppac.blogspot.co.uk/2015/06/systems-building-deciding-positions.html
http://qoppac.blogspot.co.uk/2015/07/systems-building-execution.html


If you need more help on coding up an automated system in python on the Interactive Brokers platform, check out this series of posts. If you want advice on designing trading systems then you really ought to buy, and read, my book.