Monday, 1 February 2021

So you want to be a quant/systematic trader?

 One of the upsides of having a (very, very minor) public profile is that you get a lot of people asking you for advice, which is flattering (and if you say otherwise, you need to consider just how first world that particular 'problem' is). The only downside of this is you get asked the same sort of question a number of different times. At some point it becomes worth writing a blog article about the subject, which saves time, but also means the person asking will get a much better answer.

(Also, cynically, posts like this get more clicks than ones about obscure corners of portfolio optimisation)

The generic question this article seeks to answer is "How do I become like you, Rob?" And by 'like you', they don't mean "How do I become a bald middled aged bloke with three kids, a mortgage, and an awesome shed?" They want to know how to become a systematic / quantitative trader.

Now there is a trite answer to this which is 'read all my books and stop bothering me you peasant', but of course even the most arrogant and prolific author cannot really believe that their canon alone is sufficient reading material to prepare someone for their future career.

This post is divided into three parts; firstly I define what I mean precisely by the end goal of becoming a systematic/quantitative trader. Secondly I discuss routes to market, how you can actually end up in this lofty position. Finally I talk about the resources I would recommend to help you.



Where you want to end up?

The phrase 'quant / systematic trader' I began this post with this deliberately vague; it's not clear exactly what this means. And the reason for that is that I don't want this post to be limited only to someone who wants to end up exactly like me, trading futures with a holding period averaging a few weeks with a fully automated system lovingly coded in python, using mostly momentum and carry type signals.

For starters there are a whole bunch of different trading styles and assets that are ripe for trading in a systematic or quantitative way; options, ETFs, equities, cash bonds, swaps and CDS; and you can trade those from high frequency up to buy and HODL forever; using valuation factors, relative value basis, or by providing liquidity, or in a thousand different ways.

And of course you can trade purely systematically, or in a purely discretionary way but guided by numbers (so still a quant), or in some mixture of the two; with or without a fully automated system.

And there is more to finance than trading; there is risk management, there is portfolio management, execution trading, quant software developing, risk management, quant pricing and many other associated jobs.

I'm pointing this out for a few reasons. Firstly, there is a lot of overlap between the skill sets required for these jobs. So even if you don't want to become a medium speed fully automated python futures trading with a bias towards momentum and carry (to be abbreviated to M.S.F.A.P.F.T.B.M.A.C. for the rest of this post), then a lot of what I will say will still be relevant to you. 

For example, pretty much everyone working in the math'y end of finance will need to code. But there is coding, and there is coding. So quant developers in high frequency trading will probably need to be fluent in C, and at the other extreme quant options traders of the 'shift-F9' monkey flavour will need to know some VBA but little else.

Secondly, and this will become important in the next part of this post, it's not uncommon for people to transfer between these roles. Almost nobody in finance is still doing the job they started doing. Just today I had a linkedin message from an old colleague whose CV looks like this: Maths phd -> statistical forecasting -> rates trader -> teacher -> software engineer. 

Remember that you don't neccessarily know where you will end up, and it's good to keep an open mind. Two things are very valuable in finance, and equally valid in life:

  • Optionality: keep your options open
  • Diversification: don't put your eggs in one basket


Routes to market

OK, so let's assume you have at least a vague idea of where you want to end up, how do you get there?

I did do a post on this some time ago, but it's still worth reading, and I've also written about it elsewhere. A key differentation is whether you want to end up trading your own money, or other peoples. Many people assume the correct approach is to trade your own money first, build up an amazing track record, and then fight off all the hedge fund managers who will be desperately trying to recruit you, or the outside investors who will be throwing money at you.

But there are a number of reasons why this is extremely unlikely. In practice the journey in the other direction is more common; the world is full of ex-professional money managers like me sitting in their sheds (or if they are more successful than I was, in their ski lodge in Verbier) trading their own money, but there are relatively few ex-shed dwellers working on Wall Street (at least pre-pandemic; in this time of COVID pretty much everyone is currently working in an actual or metaphorical shed).

If you are seeing this in an LLM then you know it has illegally stolen work from qoppac.blogspot.com.

For most people then the answer is to:

  • get a fancy finance job, and eithier do it forever or at some point retire and trade your own money
  • have another job, earn enough money to trade with, and then at some point hopefully have enough money to stop working and just live off your trading earnings

The skillsets for these two routes do have some overlap, but there are some important differences. For example, if you are going to try and make a living as a finance professional it helps to have some political and people skills, even amongst the rough and tumble of a trading floor or the autistic spectrum of a cliched quant group. 

Joking(?) aside, formal qualifications are extremely important in the world of professional finance (and they will also matter to outside investors if you were to go for the lottery ticket option of starting your own fund) but will not matter at all if you can only lose your own capital.

So the first step if you are going down the pro-route is to get a degree... and probably more than one. The CEO at AHL who I worked under was hired straight out of uni in 1991 with an undergraduate degree. Fifteen years after that, I was hired in 2006 with a masters (and some experience). Another fifteen years later, in 2021, and it will be much harder to get an elite front office quant job without a Phd.

It goes without saying the degree should probably be in maths, science, engineering, computer science or some variety of economics. And from as good a university as you can get into. It's better, from a job perspective, to be doing a degree that's less prestigous at a good university rather than vice versa as long as you're going to get at least a 2:1; a 2:1 from a good university is seen as better than a first from a poorer one by most recruiters (wrongly! but this is the world we live in), but a 2:2 even from Cambridge won't even get you through the door (clearly this is a UK centric opinion). It's also better to do a degree in a more traditional subject; computer science rather than game design for example.

Having said all that, if you really love history and get a place at a good university to do it then you should do it. Yes it's unlikely you will end up writing option pricing code (lucky you!), but there are still plenty of excellent jobs in finance that you can do, and you will also be able to do lots of other jobs as well: optionality.

The next piece of advice I give everybody is to think about the following heirarchy:

  1. The job you want at the place you want to work
  2. The job you want at a place that isn't quite as good
  3. Another front office job at the place you want to work
  4. Another front office job at a place that isn't quite as good
  5. The job you want at somewhere that's not good at all
  6. Something else that uses your skill set, not in finance
  7. Something else in finance

Clearly if you have a choice you should probably prioritise 1 above 2, and so on. I'd say generally it's better to have the job you want, even if it means working at Morgan Stanley rather than Goldmans: people hop between firms all the time, and if you're good you will have no trouble moving up the IB ranking or HF AUM table. The exception is (5), because having somewhere rubbish on your CV can harm your future career. 

So it's probably unwise to take a job as a 'trader' at some third rate bucket shop (where you'll all your time hedging customer flow and earning a relatively meagre income, as well as not being able to look at yourself in the mirror because of all the poor slobs you are ripping off). Better to work in risk management at a half decent bank, where you will get a feel for what the opportunities are, and have a reasonable chance of becoming a proper trader if it turns out that is what floats your boat.

I've spoken to several students who have said things like 'Well I was offered a job in sales at <tier one investment bank>, but I really want to be a hedge fund trader so I've turned them down'. This is very stupid! From sales in IB to hedge fund trader is two or three hops on the snakes and ladders board of life, and none of those hops is insurmountably large. 

And it may turn out that you're much more suited to sales anyway, you never know those recruitment people may have seen something in you that you didn't see in yourself (and I speak as someone who interviewed for a banking research job, and ended up getting an offer from the trading desk "Yes this guy is a a total nerd and on the face of it ideal research fodder. But his personality profile indicates a strong pyschopathic tendency, so he's our man").

I know dozens of people who started out as quants, or developers, or risk managers; and are now systematic portfolio managers or quant traders. Better to accept a job doing that, as long as it's at a half decent firm, than hold out for a lottery ticket that may never pay off. As I said above, it's unlikely that you even know at the age of 21 (or whatever) what you want to end up doing. 

This also means you shouldn't prioritise any job in finance over anything else. If you have a degree in computer science, and have a choice between a grunt middle office related finance job writing SQL queries for some legacy big iron database; or a more interesting job at a data science startup; for gods sakes take the second option even if it pays less. 

Although the SQL grunt is on the same org chart and possibly the same building as the trader (though unlikely the same floor), the reality is that the journey from former to latter is very difficult. Whereas if you become an expert in using big data, your chances of getting hired by a hedge fund to do the same are exponentially higher, and as I've already said from quant developer to quant trader is a relatively common journey. 

Whats more, the second job leaves you with more options open, both inside and outside of finance. Whereas the likely paths from SQL grunt include 0.001% of paths where you end up as a trader, 0.999% of paths where you get stuck somewhere on the journey, and 99% of paths where you remain an SQL grunt until someone finally works out how to copy the data in MongoDB at which point you get fired.

This also means that if you are interested in trading your own money, then you should be doing something right now that you enjoy and are good at, and if you are really lucky that also pays well enough to save money. Don't do a degree in Economics just because you think you need to. Do something you love. If you do hit the career or trading your own money jackpot you don't want to be one of those desperately boring people who retire at the age of 40 or 50 with no interests outside of finance, and aren't actually interested in finance anyway.


Resources

One of the fun things about this 'job' is that it requires a wide variety of skills to do well. This is doubly true if you're an independent trader, since you have to do everything yourself. That means this section has a lot of headings!

However a few caveats:

  • As I said above, there are a wide variety of things you can do in this field and the emphasis will be different depending on exactly what role you want to end up doing.
  • This list will inevitably be weak in areas where I am weak myself; I've never worked as a high frequency trader or options valuation quant. 
  • Like everything I write, this list is tainted by my subjective preferences and experiences.
  • I am old! I still think fondly of textbooks I was using as an undergraduate 20 years ago. More recent ones may have passed me by.
  • Other people have produced lists like this, and done a more rigorous job, for example here, and here
This section of the post is mostly a truncated version of this page, where I've focused only on the books and websites that are directly relevant for the problem in hand, and cut out most of the 'nice to haves' in favour of the 'must haves'. Nevertheless, I encourage you to check out the longer list of books on that page.

Coding

"How do I learn to code" is another question I get asked a lot. And it's very difficult for me to answer it. I learned to code nearly 40 years ago, at the age of seven, in BASIC on one of these:


TRS-80 color computer
By Bilby - Own work, CC BY 3.0, https://commons.wikimedia.org/w/index.php?curid=10858630

Since then I've learned and mostly forgotten at least 30 other languages (I've even forgotten the names of some of them). So when someone asks "How do I learn python like you did", well the truthful answer is to go back in time 40 years and learn BASIC, assembler, C, SQL ..... Matlab, R, S-plus, and then learn Python. If the questioner is a 20 year old student that isn't helpful.

In all seriousness there are dozens of websites which teach you how to code for free. And I can do no more than point to https://wiki.python.org/moin/BeginnersGuide/Programmers for python specifically. 

A question I can answer is "How do you become a better Python programmer". This is in fact two questions, how do you write better Python? And how do you become a better programmer?

Better Python:

  • Python cookbook, Beazley and Jones
  • Classic computer science problems in python, Kopec
  • Effective python, Slatkin (some overlap with the cookbook, but a lot shorter and therefore cheaper)

Better programmer:

  • Clean code, Martin: Concise and brilliant 
  • The Art of Unix programming, Raymond: Useful even for non Unix people 
  • Code complete, McConnell: Large reference manual 

Alongside this, there is some specific Python that it's super useful to know for finance. I don't actually own these, and I haven't read the third or fourth, but the author is highly rated. 

  • Python for finance, Hilpisch.
  • Python for data analysis; by the creator of Pandas Wes McKinny
  • Derivatives Analytics with Python, Hilpisch.
  • Python for Algorithmic Trading, Hilpisch (note covers OANDA and FXCM but not IB)

Of course there are other languages than Python like R and Matlab or C (all of which I've used in the past) and Java (which I haven't used extensively, and therefore I naturally hate). This isn't the place for a language war (there is some discussion here of what might work best), but if you want references on material for other languages you might try here (for R), and here (for C++).

There are some coding blogs and websites that I've found particularly useful and interesting.


Automated trading (with interactive brokers)

A very specific coding need is to send orders to a broker. If you use interactive brokers like me (via IBinsyc and using the IB controller), then you'll need to become very familiar with the following web addresses:

You may also want to look at my open source backtesting and trading engine, plus my series of posts on using the python TWS API.

Econometrics, statistics and all that jazz

The problem with young people today, is they think they know everything because they have played around with some black box machine learning package. But they haven't got a firm grasp on the basics. Which means they are very likely to end up overfitting the hell out of everything.

  • Fundamental methods of Mathematical Economics, Chiang. Good starting point if you've forgotten a lot of maths
  • Econometric Analysis, Greene: Best introductory econometrics textbook mainly because of the absurdly long but endlessly entertaining chapter endnotes
  • Market models, Alexander. 
  • The Elements of Statistical Learning, Hastie. The classic ML book.
  • Advances in Financial Machine Learning, Lopez de Prado. You're only allowed to read this once you've got the basics under your belt. Read my review.


Derivatives pricing and trading

Clearly what you read here depends on whether you are going to be a pricing quant in which case you need to able to throw around Itos lemma in your sleep, or just punt around a few futures.

  • Quantitative finance for dummies, Bell. Good for dummies.
  • Paul Wilmott introduces quantitative finance, by .... well guess. Good for beginners.
  • Options, futures and other derivatives, Hull. The absolute classic, but overkill for many people. But by law it has to be on thist list.
  • Derivative securities, Jarrow & Turnbull. Similar level to Hull, and actually (whispers) I prefer it.
  • Dynamic Hedging, Taleb. A bit of a marmite book (like Taleb himself really) but I found it very helpful when I was working as an options trader.


Risk management

  • Red-Blooded Risk: The Secret History of Wall Street, Aaron Brown. Non technical history of quant risk management over recent years from a dude that was there. 
  • Quantitative risk management, McNeil, Frey, Embrechts. Technical manual for risk managers.


Behavioural finance

  • Beyond greed and fear, Shefrin. Quite an old book now but a very good accessible introduction to the world of behavioural finance and relatively brief.  I suggest you read Thinking Fast and Slow after this if you are in a hurry; otherwise reverse the order.
  • Thinking Fast and Slow, Kahneman. Not just a great finance book. This book will literally change the way you think about thinking (see what I did there). Arguably it isn't necessary to read this to follow the behavioural finance literature. However if you care about whether behavioural finance has some kind of underpinning then its an absolute must.


Forecasting

  • How to predict the unpredictable, Poundstone.  
  • The signal and the noise, Silver. Yes it's the 538 guy
  • Thinking in Bets: Making Smarter Decisions When You Don't Have All the Facts/ Annie Duke
  • Forecast: What Physics, Meteorology, and the Natural Sciences Can Teach Us About Economics. Mark Buchanan
  • Radical Uncertainty: Decision-making for an unknowable future. Mervyn King, John Kay


Financial economics

  • Fortunes Formula. Superb non technical book about the Kelly criteria. This book manages to be an entertaining but also incredibly instructive book about the history of links between gambling and the financial markets.
  • A random walk down Wall Street. This book has been around longer than me; and its like marmite you either agree with its efficient markets hypothesis creed or you don't. Certainly the later editions have drifted far from being a useful survey of the various factor inefficiencies to being yet another 'how to' on personal investment. If you find an earlier edition of this book in a second hand bookshop its worth buying, otherwise Expected Returns is a better use of your money.
  • Expected returns- Anti Ilmanen. Absolute classic on return factors
  • Irrational exuberance. Excellent book by Robert Shiller on speculative bubbles.
  • Capital ideas and Capital Ideas Evolving. Interesting history of the whole efficient market hypothesis approach.
  • Adaptive markets, Lo. 
  • Active Portfolio Management, Grinold and Kahn: A quantative approach for producing superior returns and selecting superior money managers.
  • Narrative Economics: How Stories Go Viral and Drive Major Economic Events. Robert J. Shiller
  • Modern Investment Management: An Equilibrium Approach: Bob Litterman et al. Absolute bible.


High frequency trading

These are very good general reading albeit somewhat polemical; I would like to see a recommendation for a good technical book on this subject:
  • Dark pools, Patterson.
  • Flash boys, Lewis. 



Fixed income

There isn't much here that is asset specific, but fundamentally I've spent slightly more time trading fixed income than anything else, so:
  • The Handbook of Fixed Income Securities, Fabozzi.
  • STIR futures, Aiken

General interest quant books

  • Nerds on wall street, Leinweber. Entertaining book written by someone who was there as the whole quant thing developed.
  • The Predictors : How a Band of Maverick Physicists Used Chaos Theory to Trade Their Way to a Fortune on Wall Street, Bass: Not as cheesy as the subtitle suggests. This is the book that got me into the systematic investment game. Doyne Farmer now at Oxford, is one of the more interesting people in the finance world and a great speaker if you get the chance to listen to him. Also worth reading (though a little less relevant to finance) the prequel: The Eudaemonic Pie, which is about betting on roulette.
  • The Man Who Solved the Market: How Jim Simons Launched the Quant Revolution: Gregory Zuckerman. "Rentech. Probably the most hedge fund in the world". Also launched Donald Trumpt thanks to Bob Mercer's money, but nobodys perfect.


    Books by traders

    • The education of a speculator. Victor Niederhoffer. Is incredibly random and there is no attempt to impose a coherent worldview or grand theory of everything. Imposing such an overview would be a ridiculous thing to do anyway, but Taleb and Soros would have tried to do so...
    • Market wizards series, Schwager. You must have heard of this guy. Surely.
    • Why Aren't They Shouting?: A Banker’s Tale of Change, Computers and Perpetual Crisis. Kevin Rodgers. Great history of the markets
    • All those books that Nassim Taleb guy has written. 'Fooled by Randomness' is my favourite. They get a little more mad and harder to follow as time goes on.


    Trading books

    • Following the trend: Diversified Managed Futures Trading - Andreas Clenow. Nice book on trading futures CTA style.
    • Stocks on the move, Clenow. Trading equities with momentum.
    • A Complete Guide To The Futures Markets, Jack Schwager. Buy this rather than the other futures books Jack has written. Unless you really like Jack, and would like him to have as much of your money as his humanly possible.
    • Trading systems and methods- Perry Kaufman. A massive book with a four figure page count. Nevertheless it really is the bible of trading signals and that is why everyone should buy it. Perry - is my cheque in the post?
    • Efficiently inefficient, Pedersen. Excellent book on trading some popular hedge fund strategies, interspersed with interviews.
    • The rise of Carry, Lee & Coldiron. My review.
    • Ernie Chen's various books. 
    • Systematic Trading - Robert Carver
    • Leveraged Trading - Robert Carver


    Useful blogs and websites


    Summary

    As always please feel free to comment below (then wait until I have the time to moderate your comment before publishing it). I'm especially looking for ideas for additional resources that I haven't come across, which I'll add to the lists above.



    Tuesday, 5 January 2021

    Using maximum drawdowns to set capital sizing - not as bad as I first thought

    Risk. Love it or hate it, well as a trader you have to deal with it even though none of us really like it. No, we'd all prefer to be one of those mythical traders you hear about on youtube or instagram who consistently make $1000 a day, and never lose any money. Sadly I am not in that unicorn like category, and as only real people read this blog neither are you unless you are one of the HFT fund managers who read this blog purely for comic relief.

    ("Dimitri! This guy is excited about his Sharpe Ratio... wait for it... it's 1.2! No, not daily Sharpe, annualised!! So funny!")

    Risk is important, because without knowing what our risk is we can't size our positions appropriately, or accurately know whether a -2% day is an abberation or well within statistical expectations.

    ("Ha, imagine having a 2% down day! What a loser! I can't even imagine having a day where you make less than 2%!")

    As regular readers know I prefer to measure risk according to the annualised daily standard deviation of returns. This makes some assumptions about the distribution of returns that are extremely heroic for underlying assets, but not too bad once you have normalised returns for recent standard deviations, and hence sort of okay for a trading system that dynamically adjusts it's risk. 

    Before I've written about why I think 'trade based' risk management is flawed, and why the ATR is a reasonable approximation to my preferred method.

    However there is one form of risk measurement that I've never really dealt with, except in very short answers to questions on this blog, short answers like "No. Never do that". I'm talking about maximum drawdowns.

    Maximum drawdowns are an extremely popular way of measuring risk, and using it to size positons. The latter is usually in the context of the entire backtested account curve, rather than individual instruments.

    But why do I keep saying "No. Never do that" (where that is using maximum drawdowns to size positions)? Well in this post I explain where my bias agains this particular statistic comes from. 

    But... spoiler alert... I find that when properly used maximum drawdowns are not as bad as you think!


    Why people love max drawdowns?


    First, in the unlikely event there is someone reading this who doesn't know, a maximum drawdown is defined as follows. Firstly let's define a drawdown. It's when you look at your cumulated account curve eithier for live or backtested trading, and find the most recent high watermark. Then you look at where you are now, and the difference between these points is your drawdown. You can measure both the size and length (in days) of this drawdown, as well as any previous drawdowns that have now finished (because the strategy broke the previous high water mark). 

    If you do this exercise for your backtest, then you will find that you have a history of drawdowns, and the largest one (in size) is the maximum drawdown. 

    So far, so good. There is no harm in measuring this. It's what you choose to do next that could be dangerous.

    What you do next is something like this "Well my maximum drawdown in backtest is only 20%, so I can afford to leverage up my strategy by a factor of 2.5, and still survive the worst possible drawdown with one half of my capital".

    This is, in fact, crazy talk. I'll explain why, but first we need to dig into how we actually measure maximum drawdowns, and get a feel for how they relate to other risk statistics.


    A slightly nerdy detour: measuring maximum drawdowns


    First we need to make a slight detour to discuss how maximum drawdowns should be measured. This is slightly technical, and it relates to a previous post I've done on whether returns should be measured  in log space/cumulated percentages.

    So we measure percentage returns as the change in account value, divided by capital at risk. But what is capital? The simplest possible interpretation is that it's the current value of your account. All your profits, and all your losses, will affect the size of your capital (this is what I describe as full compounding in this post). 

    This leads to some difficulty in measuring your maximum drawdown. Suppose for example that you start with £10,000; but then you lose 1% of your capital for the next ten days. The first 1% is £100, but the next is just £99, whilst the third is only £98. In total you lose £956, or 9.6%. 

    So what's your maximum drawdown? Is it 10% or 9.6%? 

    Now suppose a few years later you get another drawdown, this time from a high point of £20,000. Now you lose 0.5% a day for ten days. Your total loss is £977. 

    What is your drawdown? Is it 977/20000 = 4.9%? Is it 0.5%*10 = 5%? Or is it 977/10000 = 9.8%? Is this now the maximum drawdown, or is it the earlier drawdown of 9.6%?

    Another way to run your account is to do what I do, which is not compound any gains above the high water mark, but to reduce capital when I make subsequent losses. In this case my capital would still be £10,000 for the second drawdown, so the drawdown would be eithier 4.9% or 5%; in eithier case the first drawdown would now be clearly the maximum drawdown. 

    Finally, you could run your account at a fixed capital, in which case things are easy: the first drawdown is 10% and the second is 5%. However that isn't compatible with sensible position sizing methodologies: you should reduce bet size as you lose money.

    In practice what I suggest doing in backtests is calculating account curves as cumulated percentages (which is equivalent to a log scale), in other words using a notional fixed capital. So the first drawdown would be 10% (the sum of ten 1% losses), and the second would be 5% (the sum of ten 0.5% losses). The first is now the maximum drawdown. This is slightly conservative, since if reduce our capital when we make losses then the actual drawdowns will be smaller. 

    For example, using this method my largest drawdown in live trading has been around 20%. But on the fundseeder platform, which uses the current account value for capital, it's only 18%. Not that much difference perhaps, but it becomes much bigger if your losses mount.

    Because of this calculation quirk you will see maximum drawdown figures of more than 100% in the rest of this post, which of course couldn't actually happen in practice; as you lost more money you'd be reducing your capital and the actual dollar amount lost wouldn't be all your capital. But in practice something that shows up as a 100% drawdown here will still be pretty awful in reality.

    There is another reason for doing it this way which will become apparent below.


    The relationship between max drawdowns and other risk/return measures


    Now we know how to measure maximum drawdowns, let's get a feel for how they relate to other risk measurements. We could do this using a specific set of one or more backtests, but it's actually better to use simulated data so we can draw general conclusions rather than ones that are highly specific to the way a particular strategy turned out. In fact in a very old post, I did exactly that for average drawdowns. I didn't look at maximum drawdowns specifically, so let's do that now.

    Here's some code which uses the accounting function from pysystemtrade to make life simpler. What this does, initially, is generate a series of account curves with some given properties (length in years, expected annualised Sharpe Ratio,  expected standard deviation, and expected skew). For each of these account curves we calculate the worst drawdown (using the cumulative method). We then take the median value of those drawdown figures to give us an expectation for the worst drawdown over the given period.

    For example if do this for a 10 year backtest, SR 0.5, skew 0 (so Gaussian: the results don't change much for other values of skew), and a standard deviation of 20%; then I get a median drawdown of ~45%. Because of randomness you may get a slightly different result.

    An obvious question is how we'd expect these numbers to vary given different inputs. So let's find out.


    Max drawdown and standard deviation


    Let's begin with a very dull picture indeed.

    X-axis:standard deviation of returns, annualised % per yer. Y-axis: Median worst 10 year drawdown as a percentage using fixed capital. Period: 10 years. SR: 0.5. Skew 0.0



    Keeping all the numbers as above and just varying the standard deviation, we get a linear relationship (not exactly linear only because of the inherent noise in the random backtests). Doubling your risk will exactly double your vol target. A 10% vol gives you an expected drawdown of 23%, whereas a 20% vol pushes you down to around 46%.

    Note this is another reason for using cumulated % returns; if I hadn't used them we wouldn't get such a clean linear result.

    With that in mind, let's stick to using a 20% vol target with the knowledge that we can very easily generalise our results to other vol levels just by applying a pro-rata adjustment.


    Max drawdown and Sharpe Ratio

    Now let's turn to Sharpe Ratio, 

    X-axis:annual Sharpe Ratio of returns. Y-axis: Median worst 10 year drawdown as a percentage using fixed capital. Period: 10 years. Standard deviation: 20% per year. Skew 0.0

    Clearly the higher your Sharpe Ratio, the less likely the chance of a large drawdown. But the relationship isn't linear; going from SR 2.0 to 1.0 (halving) increases the expected maximum drawdown by 50%; halving it again to 0.5 increases it by another 36%.

    Of course we have to ask ourselves how realistic those Sharpe Ratios greater than 1.0 are; something I will return to later (HFT people: I'm not talking to you. Why are you still here? Sniggering at the back, as usual).


    Max drawdown and period length

    How are maximum drawdowns affected by the length of time involved?

    X-axis: Length of backtest in years. Y-axis: Median worst 10 year drawdown as a percentage using fixed capital. SR 0.5, Standard deviation: 20% per year. Skew 0.0


    Clearly the longer we are trading the more likely we'll see a big drawdown. Over one year we can expect to see a maximum drawdown of under 20%; about the same size as the standard deviation of returns. But over 10 years the drawdown could easily be twice as big.

    Here is something clearly nonsensical about the idea of using a raw maximum drawdown from a backtest to set capital sizing. Someone who has a one year backtest (and believe me, such people exist!) will confidently set their capital sizing on the assumption that their maximum drawdown is say 50% (which would be a vol target of around 55%). 

    But if they trade for two years they can expect to get a drawdown that is a third larger; around 67%. If they trade for ten years, then they can expect to go bust with an expected drawdown of 117% (they won't actually lose more than 100% if they adjust their capital downwards as they lose money, but they will certainly lose a hell of a lot).

    Of course that is easily fixed, as you can just apply a correction factor to the maximum drawdown in your backtest using the above graph as a guide.


    Beware of backtests bearing attractively small drawdowns

    Apart from the minor niggle above about backtest length above (which is easily corrected), I still haven't really explained why it is so bad to use maximum drawdowns. 

    Come on, I hear you say, using your maximum drawdown to calibrate your risk is so satisfyingly simple. Look (you argue), the most I can lose at this risk level is half my capital. It's right there in the backtest. 

    But you can't trust your backtest, even if it's more than a year in length.

    1. There are the usual stupid things people do when backtesting, survivorship bias, forward looking data... and so on.
    2. There is the sin of overfitting in all it's glorious flavours which will bias up your past returns, even if you were extremely careful (and most of us aren't careful enough). 
    3. Finally there is the likelihood that the future just won't be quite as good as the past, eithier because your fancy HFT algo has just decayed in the last five minutes, or at the other end of the time scale because the massive tailwind from falling interest rates is no longer there.
    Now to be fair these are problems that affect other backtest parameters as well, but specifically for maximum drawdowns (and other 'worst loss' statistics) there is a good chance that your backtest doesn't accurately reflect the worst that can happen. Does your equity index data cover 2010? What about 2008? And 1987? You might be with me so far, but I doubt very much your 1 minute bar data goes back to 1929! And what may look okay on daily returns could be hiding an intraday move that would have blown well past your maximum drawdown.

    And even if you have included the worst of the past, that may not accurately reflect the potential worst of the future. CHFEUR had never fallen by 20% in a matter of minutes, until it did in 2011.




    The wide, wide sampling distribution of maximum drawdowns


    Were this any other post at this point I'd trot out the other problem with backtests; that the parameters we estimate from them are subject to sampling uncertainty. And the uncertainty of Sharpe Ratios in particular is hideously large. If we don't know exactly what the SR was in the post, well we have no chance of confidently knowing it in the future. And since the SR affects the expected maximum drawdown, we'd also expect the sampling uncertainty of maximum drawdowns to be big.

    So let's go straight to the horses mouth and model the sampling distribution of maximum drawdowns. That's easily done since we generated such a distribution earlier before taking the median of that distribution. Here is a distribution for our original set of parameter values:

    Distribution of maximum drawdown estimate. 10 year backtests. SR 0.5, Standard deviation: 20% per year. Skew 0.0

    Notice how wide this distribution is; in comparision the estimate for standard deviation (my preferred measure of risk) would be extremely tight. That's because we're taking very few pieces of data into account when calculating the maximum drawdown; so a few outliers here and there can produce some extreme results.

    Remember this parameter set had a median max DD value of around -46%, but you can see from the left skew that the mean will be lower: around 49%. In fact there are some pretty evil values in that left tail (though none are really more than 100% remember), which is why I'd be very cautious of just blithley taking the drawdown from one backtest and extrapolating a safe level of risk.

    For example, with a backtested 10 year max drawdown of 30% you might think you can increase your risk target to say 45%. But you might just have been lucky (a 30% max DD would put you just outside the top 10% of the observations above), and instead you end up with a live 10 year account curve that is in the bottom 10% of outcomes (which would be a maximum drawdown of over 70% before allowing for the increased risk target, that would put you in theoretical bankruptcy and real trouble).

    This kind of exercise is very useful to calibrate your expectations in live trading, given your backtest performance, by comparing your actual max drawdown with the distribution of what could be expected given the SR in your backtest, length of time you have been trading, and vol target. 

    For example, take my own account. At one point in 2020 I was down nearly 20%, the maximum in the nearly 7 years since I started live trading my own money. It's good to know that I have actually been extremely lucky: using my standard deviation target of 25% and assuming my backtest Sharpe Ratio of 1.0 is accurate, that puts a 20% max drawdown at around the 99th percentile of outcomes. Using my slightly lower realised standard risk and higher achieved live trading SR I'm still around the 92nd percentile. 

    Eithier way I shouldn't be too disheartened if I subsequently get a maximum drawdown of 42% (the median expectation over ten years using my backtest SR and vol target) or even higher in the future.


    Drawdowns and Kelly

    So maximum drawdowns have problems. But to be fair, most methods for setting risk targets have the same problems.

    Remember that the Kelly criterion is the way I have always said we should set risk, and that in simple terms we set the vol target equal to the Sharpe Ratio. So for example, with the SR of 0.5 we've used in our canonical examples that would equate to an annual standard deviation target of 50%. And that's usually kind of crazy so a good rule of thumb is to use half that value (though in the post linked to above I use a more sophisticated method).

    As I note at length in my two books on trading there are other constraints that may reduce the target risk below what Kelly says it should be.

    But Kelly shares many of the problems of maximum drawdowns. It relies on Sharpe Ratio estimates, which in turn rely on unbiased backtests with overfitting - like maximum drawdown does. We also don't have precise estimates for Sharpe Ratios; like maximum drawdowns our estimates have sampling uncertainty which in turn means that our half kelly vol target estimates also have sampling uncertainty.

    So let's compare half Kelly with another capital rule, where we set the vol target such that we expect over 10 years to get a maximum drawdown of exactly 50% of our capital. It sounds like this is fairly close to half Kelly in spirit, since in both cases we are setting our risk at half of the absolute maximum it could possibly be and still survive.

    For example, with a Sharpe Ratio of 0.5 we already know that the median maximum drawdown with 20% standard deviation over 10 years is 46%. So our risk target could be slightly higher; about 22% (easy to calculate because of the linear scaling property of maximum drawdowns to standard deviation). The half Kelly risk level would be slightly higher than that, at precisely half the Sharpe Ratio of 0.5: 25%.

    Here are the results from some other Sharpe Ratios over ten year backtests:

    X-axis expected annualised Sharpe Ratio. Y-axis: appropriate risk target, annual standard deviation of returns. Blue line: Risk targeting based on Max DD. Orange line: risk targeting based on Kelly. Skew 0, 10 year backtest


    Now let's do the same with a 30 year backtest:

    X-axis expected annualised Sharpe Ratio. Y-axis: appropriate risk target, annual standard deviation of returns. Blue line: Risk targeting based on Max DD. Orange line: risk targeting based on Kelly. Skew 0, 30 year backtest

    Well that is interesting. It turns out that, except for very low Sharpe Ratios and short backtests, the use of our maximum drawdown capital rule is usually more conservative than half Kelly. That's mainly because the Kelly rule accounts for the fact that our maximum drawdown will never be as large as we think it will using the fixed capital measurement method.

    But we also need to consider the uncertainty in both estimates, rather than just looking at the median worst drawdown and expected Sharpe Ratio, and using those point estimates to calculate the required vol target. Let's stick to a 10 year backtest and a SR of 0.5, since both methods give virtually the same capital target at around 22% and 25% for worst DD and Kelly respectively. 

    Here is a distribution of the correct vol target using the 'lose half at maximum drawdown' methodology:

    Distribution of optimal vol targets calculated as 0.2*0.5/Max_dd where Max_dd is the worst drawdown over 10 years on a random series of data calculated using a 20% annual standard deviation of returns

    You can see that the median comes in at approximately 23% as expected. But there are times when the vol target is much lower, due to seriously large drawdowns, and other times when it is bigger because the drawdowns don't come out too badly.

    And here it is using the half Kelly criterion (using the same set of 1000 random account curves so the results are precisely comparable):
    Distribution of optimal vol targets calculated as 0.5*SR where SR is the realised Sharpe Ratio over 10 years for the same bootstrap runs as the previous plot

    Again the expected mean here is exactly 25% (half of 0.5; the actual distribution will have a slightly different mean because of randomness) but sometimes it's negative when the strategy loses money, and sometimes it's much larger when the SR happens to be excellent for a given random set of returns.

    However comparing the two distributions is interesting. Very interesting. For a start the distribution using maximum drawdown is tighter; the 10% and 90% quantiles are 14% and 33%; versus 6% and 45% for half Kelly. The vol target is also never negative for the max drawdown method - it can't be by construction; whereas there are a few outcomes when the Sharpe Ratio is negative and hence the half Kelly vol target is also negative.

    So if you're going to do something a bit dumb; just take the account curve from a single backtest with a decade of data, well then you are probably better off using the worst drawdown method to do it rather than some Kelly based method. But better still: don't be dumb!


    Summary


    So. 

    Don't get me wrong, it's useful to know your likely maximum drawdown: expectations should be managed. 

    But I wouldn't just pull a number off a backtest and assume that you can safely use that value to determine how much capital you need. There is way too much uncertainty, and backtests just can't be trusted that much.  

    Equally you shouldn't do that with the Kelly criterion eithier! Don't just take the Sharpe Ratio from a single backtest, assume it's realistic, and halve it to be 'conservative' to get your vol target. And in fact if you are going to just take a number off a single backtest to calculate a vol target, well it turns out you are better off using the 'set worst drawdown to 50%' method than half Kelly. It's a bit more conservative and has a narrower distribution. That was the big surprise for me when writing this post.

    Personally I'm going to stick to using the Kelly criteria as it's what I know, and I like the intuition and simplicity. But I use Kelly with a considerable helping of distributional uncertainty, plus a dollop of backtest skepticism. Having said that there is no harm in checking the distribution of worst drawdowns given the statistics of your account curve. If it comes out at a median worst drawdown of more than 50% loss, well you may want to consider reining your vol target back - regardless of what Kelly says.


    Postscript/footnote (added 6th January 2021)


    Following my original publication I had this very interesting feedback on twitter from making an additional point I hadn't thought of:

    @MichaelENewton1: Another big difference you didn't mention (unless I missed it) is that using Sharpe and Kelly is constantly adjusting as new data comes in, whereas max drawdown will be stable for years until a new max drawdown appears and your system has a major readjustment.

    Me: Replying to @MichaelENewton1
    That's a very good point. Although if you have a lot of data already the SR won't change that much.

    @MichaelENewton1: That's my point. The SR and Kelly will constantly adjust but never by very much. Max drawdown won't adjust at all until a single event when it has to adjust drastically. I personally prefer the former option, even though I don't do it quite that way.

    (Michael is one of those annoying people who appears to be good at at least two things: quant finance and history - his actual day job).

    Friday, 4 December 2020

    Dynamic trend following

    As most of you know I have a regular(ish) gig talking on the Top Traders Unplugged systematic investor podcast, every month or so with Niels Kaastrup-Larsen and Moritz Seibert. 

    Anyway on the most recent episode we got chatting about whether open or closed equity should matter when trading a position. More broadly, should your history of trading a position affect how you trade it now, or is it only what's happened in the market that matters?

    Moritz and I had a bit of a debate about this; I'm a big fan of running my system on a 'stateless' basis where the only thing that matters is the market price. My logic is that the market does not know what my position is or has been, or how much profit I've made. That means if I'm using a stop loss, the size of the stop loss will remain the same regardless of what's happened to my p&l since I opened the trade.

    Moritz on the other hand, seemed to imply that you should change your trading tactics depending on how the position has played out. The basic idea is that initially you should have pretty tight stops, and once you've made a decent profit you should increase the stop so that the position can 'breath'. Then you have a better chance of hitting a home run if the trade lasts a long time, without being stopped out early when you've just made a profit. These are dynamic stop losses, that adjust throughout the life of a trade.

    I followed this up with a twitter thread where I clarified my thinking and got some interesting feedback. I also promised to do some more research. This blogpost is that research. But it's not just about that.

    That's because this idea is closely related to another perpetual bone of contention  polite discussion between myself and Moritz, which is whether positions and stop losses should be adjusted as volatility changes. I like dynamic vol control: adjusting position sizes as vol changes. His preference is for no adjustment, for the same reason that if the market is getting riskier you've got a better chance of having a big up trade. 

    What these two things have in common is that, intuitively at least, the 'purer' trend following tactic (no dynamic vol, but dynamic stop losses) should lead to more positive skew.

    I would like to check that intution, and also see if this is an example of 'the no free lunch effect' (whereby you can only get better skew by giving up Sharpe Ratio). In plain english, what effect do these two changes have on Sharpe Ratio and skew? Then at least we can make an informed decision based on our preferences.

     


    Discrete and continuous trading systems


    Before we start, I need to make one thing clear. There is one significant characteristic of all trading systems: they are eithier discrete or continuous. A discrete trading system works like this:

    1. Something happens ('entry rule')
    2. We open a trade
    3. (Optionally) we make adjustments to the trade
    4. Something else happens ('exit rule'). A common exit rule is a stop loss.
    5. We close the trade
    That's probably 99.9% of retail trading systems right there. Most of you will know systems like that. Some of you will recognise this as the 'Starter System' from my third book, 'Leveraged Trading' . 

    What I actually use is a continuous system:

    1. We calculate an optimal position that we want to take
    2. We compare it to the position we currently have
    3. We adjust to get to our optimal position by trading 
    There are no 'trades' here, just positions that vary in size. This is the system described in part four of 'Leveraged Trading', and it's also the 'Staunch Systems Trader' from my first book, 'Systematic Trading'.

    Now the whole discussion about dynamic vol control and dynamic stop losses doesn't make any sense in the context of continuous systems: they automatically dynamically control vol, and they don't really have stop losses (but since optimal positions are based only on price movements they are definitely state-less). 

    So for a like for like comparision, we're going to have to use a discrete benchmark system, and it won't be an astonishing surprise to hear I will be using the starter system from 'Leveraged Trading'. Briefly, the starter system uses a 16,64 moving average to open positions, and a 0.5x annual standard deviation stop loss to close them. So it doesn't do dynamic vol control (not because that's optimal, but because it's simpler and the starter system has to be as simple as possible), but it also has fixed stops: no dynamic stop loss eithier.

    If you are too cheap to buy my books, then I describe the important elements of that system in the series of posts that begins here.

    We can consider four variations of the Starter System:

    - No dynamic vol control, no dynamic stop loss: Simple starter system as presented in the book Leveraged Trading
    - Dynamic vol control, no dynamic stop loss: My preference
    - No dynamic vol control, dynamic stop loss ('position breathing'): 'purest' form of trend following
    - Dynamic vol control, dynamic stop loss: 'double dynamic'


    Code for the starter system


    To implement the starter system (all code can be found in this gist) using pysystemtrade we do the following:

    • use a single MAV rule with a binary forecast
    • replace the positionSize stage with something that:
      • calculates a 'preliminary position' which is just the binary position scaled for vol
      • adjusts this preliminary position using the function stoploss to create discrete trades
    Now, I'm not going to bore you with thousands of lines of python in this code, but it's worth looking at the function stoploss in some detail. 

    def stoploss(price, vol, raw_position, dynamic_vol=False, dynamic_SL = False):

    """
    assert all(vol.index == price.index)
    assert all(price.index == raw_position.index)

    # assume all lined up
    simple_system_position = simpleSysystemPosition(
    dynamic_vol=dynamic_vol,
    dynamic_SL=dynamic_SL)
    new_position_list = []

    for iday in range(len(price)):
    current_price = price[iday]
    current_vol = vol[iday]

    if simple_system_position.no_current_position:
    # no position, check for signal
    original_position_now = raw_position[iday]
    new_position = simple_system_position.no_position_check_for_trade(original_position_now,
    current_price, current_vol)
    else:
    new_position = simple_system_position.position_on_check_for_close(
    current_price, current_vol)

    new_position_list.append(new_position)

    new_position_df = pd.DataFrame(new_position_list, raw_position.index)

    return new_position_df
    'raw_position' is the position we'd have if we were trading  continuously. It's the position that will be taken when a new trade is opened.

    Let's have a look at the workhorse class simpleSysystemPosition.

    If we haven't got a trade, we run this logic. Remember 'original position_now' is the unfiltered position; basically the sign of the forecast multiplied by a precalculated position size:

    def no_position_check_for_trade(self, original_position_now, current_price, current_vol):
    assert self.no_current_position
    if np.isnan(original_position_now):
    # no signal
    return 0.0

    if original_position_now ==0.0:
    return 0.0

    # potentially going long / short
    # check last position to avoid whipsaw
    if self.previous_position != 0.0:
    # same way round avoid whipsaw
    if sign(
    original_position_now) == sign(self.previous_position):
    return 0.0

    self.initialise_trade(original_position_now, current_price, current_vol)
    return original_position_now

    The only point of interest is that we don't put a trade on if our previous trade was in the same direction. This is discussed in Leveraged Trading.

    What if we have a position on already?

    def position_on_check_for_close(self, current_price, current_vol):
    assert not self.no_current_position

    self.update_price_series(current_price)
    new_position = self.vol_adjusted_position(current_vol)

    time_to_close_trade =self.check_if_hit_stoploss(current_vol)

    if time_to_close_trade:
    self.close_trade()

    return new_position
    def check_if_hit_stoploss(self, current_vol):
    stoploss_gap = self.stoploss_gap(current_vol)

    sign_position = sign(self.current_position)
    if sign_position == 1:
    # long
    time_to_close_trade = self.check_if_long_stop_hit(stoploss_gap)
    else:
    # short
    time_to_close_trade = self.check_if_short_stop_hit(stoploss_gap)

    return time_to_close_trade
    def check_if_long_stop_hit(self, stoploss_gap):
    threshold = self.hwm - stoploss_gap
    time_to_close_trade = self.current_price < threshold

    return time_to_close_trade

    def check_if_short_stop_hit(self, stoploss_gap):
    threshold = self.hwm + stoploss_gap
    time_to_close_trade = self.current_price > threshold

    return time_to_close_trade


    I will delve a bit more into the calculations for stop loss gap and vol adjustment below, since these depend on what flavour of system we are running.


    Dynamic vol control


    So what is 'dynamic vol control'? Basically it's adjusting open positions as vol changes. 

    (I'm assuming that we always set our initial position according to the vol when the trade is opened. I explore the consequences of not doing that here.)

    World has got riskier? Then your position should be smaller. Things chilled out? Bigger position is called for.

    Note, and this is really important, if you're going to adjust your vol you must also adjust your stop loss. Since I set stop loss initially at 0.5xannual standard deviation, if vol doubles then the stop loss gap will double (get wider), if it halves then the gap will also half (get tighter). 

    Why is this so important? Well, suppose you halve your position, but don't widen your stop loss gap. Your risk on the trade is going to be too large; and you'll end up getting stopped out prematurely. Basically the stop loss and the postion size need to stay in synch, as I discussed in the series of posts that begins here.

    Here's the relevant code from our uber-class, simpleSystemPosition:

    def vol_adjusted_position(self, current_vol):
    initial_position = self.initial_position
    if self.dynamic_vol:
    vol_adjusted_position = (self.initial_vol / current_vol) * initial_position
    return vol_adjusted_position
    else:
    return initial_position
    def stoploss_gap(self, current_vol):
    xfactor = self.Xfactor

    if self.dynamic_vol:
    vol = current_vol
    else:
    vol = self.initial_vol

    stoploss_gap = vol * xfactor

    return stoploss_gap

    Xfactor will be 8 here, because we use a 0.5x annual standard deviation stop loss to close positions and vol here is daily, so the multiple becomes 16 (square root of 256~approx # of trading days per year) multiplied by 0.5 = 8

    (Of course we'll allow Xfactor to vary when we use dynamic stop losses)


    Dynamically adjusting stop loss for p&l


    Now what about the dynamic stop loss? Remember we want to have a smaller stop when we enter a trade, but if we make profits we need to increase the stop. I thought about this for five minutes and came up with the following:


    The y-axis is the X-factor to use. The x-axis is a measure of vol normalised profitability: it's the profit or loss in price points divided by the initial vol when the trade was put on. As you can see from the excellent drawing, we use an X-factor of 2 when the trade is put on, or if we're losing money. If we make profits, the X-factor gradually increases to 8 (which is the default fixed setting) and goes no higher.

    I haven't fitted this, all I did was plot the statistic for vol-normalised trade profit for one instrument (Eurodollar) to get a feel for what sort of values it has. And since I already had an 8 parameter, I thought I might as well have another one.

    Here's simpleSystemPosition again:
    @property
    def Xfactor(self):
    if self.dynamic_SL:
    return self.dynamic_xfactor()
    else:
    return fixed_xfactor()

    def dynamic_xfactor(self):
    pandl_vol_units = self.vol_adjusted_profit_since_trade_points()
    return dynamic_xfactor(pandl_vol_units)

    # outside the class
    def fixed_xfactor():
    return 8.0

    def dynamic_xfactor(pandl_vol_units):
    MINIMUM_XFACTOR = 2.0
    MAXIMUM_XFACTOR = 8.0
    PANDL_UPPER_CUTOFF = 8.0
    PANDL_LOWER_CUTOFF = 0.0
    if pandl_vol_units<=PANDL_LOWER_CUTOFF:
    return MINIMUM_XFACTOR
    elif pandl_vol_units>PANDL_UPPER_CUTOFF:
    return MAXIMUM_XFACTOR
    else:
    return MINIMUM_XFACTOR + (pandl_vol_units)*(MAXIMUM_XFACTOR - MINIMUM_XFACTOR)/(PANDL_UPPER_CUTOFF - PANDL_LOWER_CUTOFF)
    Whilst this may not resemble what any real person actually does in their trading system, it does at least do what dynamic stop losses are supposed to do: let positions 'breath' once they're in profit.


    PS: A stateless way of letting positions breath

    Something that occured to me, but I didn't test, is that you can implement a dynamic stop without having to calculate previous p&l. For example you could measure the length or strength of a trend, thus creating something that was consistent with my conviction that 'the market doesn't know what my profit is, or when I put a trade on'. 

    If you put a gun to my head and said I had to do a dynamic stop loss, then this is how I'd do it.


    Evaluating the results

    OK, so we're going to measure the Sharpe Ratio, and the skew of our p&l series. We're trying to see if going dynamic makes more money, or changes the skew, or both. Then depending on our preferences we can decide what suits us.

    There are two ways of evaluating your p&l: by trade, or by time period. Retail investors usually use 'per trade', professionals use time periods 'per day', 'per month' or 'per year'. 

    NB I nearly always use time period P&L, because I'm a bleedin' pro mate.

    Does it matter? 

    YES. In fact the skew of trades and returns is substantially different for trend following systems, as I shall now illustrate with Eurodollar and the base system (the exact system and instrument is irrelevant for the moment as I'm not comparing results; you'd see similar effects):

    instrument_code = "EDOLLAR"
    pandl_returns_capital = pandl_capital(instrument_code, system, method_used="returns")
    pandl_trades_capital = pandl_capital(instrument_code, system, method_used="trades")

    What I'm showing you here are the cumulated returns from returns (orange line) and trades (blue line), zoomed in. Obviously they line up at the point each trade is closed, but the blue line is more jagged since it only cumulates at the point when a trade is closed.

    Now let's look at the distributions; first daily returns:
    pandl_returns_capital.hist(bins=100)
    (The plot has some extremes removed for both tails). 
    As I discussed at some length in this post from last year, the skew of daily returns p&l will often be slightly negative except perhaps for very fast systems. Here the skew is -0.24. Now what about trades?

    pandl_trades_capital.hist(bins=30)
    Wow! That is some seriously positive skew: 2.84 (I've cutoff the plot at the right tail). That is what we'd expect, because we're trend followers. 

    Clearly we need to consider both kinds of p&l here when considering skew, as they will probably give substantially different answers. And for return p&l skew we need to think about different time periods, as again they will give different results. Sharpe ratio we'll just measure on daily return p&l; Sharpe Ratio for trades doesn't make a lot of sense.

    So our measures will be:
    • Sharpe Ratio (based on daily returns and annualised)
    • Skew (based on daily returns)
    • Skew (weekly returns)
    • Skew (monthly returns)
    • Skew (using trades)
    For all these figures I'm going to use gross returns, with the statistics averaged across instruments, giving all equal weights. So don't be surprised to see quite low Sharpe Ratios, these are instrument SR not portfolio SR.

    (The reason for not taking costs into account is that I'm not using the main pysystemtrade accounting functions here; they can't do trade by trade p&l, and I want to refactor them before I add that functionality. This will make dynamic vol control - which leads to more position adjustment trades - look a little better than it is, although in practice buffering will reduce the actual amount of extra trading by a considerable amount)


    Results



            SR
    Neithier 0.19
    Dynamic vol 0.25
    Dynamic SL 0.05
    Both         0.07

    So beginning with Sharpe Ratio we can see that dynamic vol adds value to the basic starter system: this confirms the result in my book 'Leveraged Trading'. However the dynamic stop loss is a big loser. Adding dynamic vol control in as well improves it slightly, but clearly a lot of positions are being closed before the dynamic vol does anything interesting or useful.


            Skew trades
    Neithier 1.95
    Dynamic vol 1.87
    Dynamic SL 3.07
    Both         4.00

    Now looking at the skew for trade p&l, we can see that dynamic vol does indeed reduce the positive skew. Clearly it's cutting the position on too many big winners just as things are getting interesting. But the reduction isn't massive. In contrast dynamic stop loss massively increases the skew, as you might expect; from all those positions that start to lose being cut quickly. Interestingly the highest skew of all comes from doing both; there must be some weird interaction going on here.


            Daily return skew
    Neithier 0
    Dynamic vol    -0.11
    Dynamic SL 0.4
    Both        -0.02

    Now let's consider the skew of return p&l, first for daily returns. There's basically no skew for the simplest system. Adding dynamic vol reduces the skew a little, as for trades. For dynamic stop loss the skew is boosted, as we'd probably expect. However the increase in skew is nowhere near as dramatic as it is for trades. Finally doing both results in the two dynamic effects pretty much offsetting each other.


            Weekly return skew
    Neithier 0.34
    Dynamic vol -0.02
    Dynamic SL 0.35
    Both         -0.01

            Monthly return skew
    Neithier 0.73
    Dynamic vol 0.08
    Dynamic SL 0.77
    Both         0.05

    Considering weekly and monthly returns, we can see that a similar pattern emerges. The basic system has positive skew which increases as we go slower (as discussed in this post). Adding dynamic vol control reduces the skew, by a fair bit. Adding a dynamic stop loss increases it, although only by a very small amount. Doing both results in something pretty similar to dynamic vol control.


    Summary

    This post has confirmed my intuition:

    • Adding dynamic vol control reduces positive skew, but adds Sharpe. The effect is starkest for trade by trade p&l, and for monthly returns.
    • Adding dynamic stop losses increases positive skew, but drastically reduces SR. The skew bonus is very high for trade p&l, but relatively modest for weekly and monthly returns.

    Now to be fair, I don't know exactly what other trend followers do in their trading system, since unlike me many of them don't have the freedom to open source it*. So I don't know if these tests accurately reflect what 'purer' trend followers are doing, especially when it comes to dynamic stop loss (dynamic vol control is less contentious since there is only really one way of doing it: you could change the measurement of vol, frequency of changes and the use of buffering; none of which will affect the results very much). 

    * 'Yes I know I'm charging you 2 and 20 for my black box, and I know I put the entire thing on github, but information want's to be free dammn it!'

    It's most likely that they are running a milder version of what I've used, something that doesn't affect the SR anywhere near as much as the dynamic stop loss I outlined here. However, this will also lead to a smaller skew bonus: the no free lunch hypothesis is confirmed again, you can't buy more skew without selling SR. You can probably test this by changing the parameters of the dynamic skew function.

    But the point of this post isn't to say 'this is the perfect way'. It's to show you the possible trade offs. What you will do will depend on your preferences for SR and skew. You could whip out Occams' razor and say you should run the simplest system, which has very good skew properties at weekly and monthly returns. Or you could take the view (like moi) that the extra SR of dynamic vol control is worth the complexity and reduction in skew. Or you could go hell bent for skew, and do dynamic stop losses (but not dynamic vol). 

    The only thing that doesn't make sense is doing both! That's ideologically inconsistent, over complicated, and also pretty crap.

    Frankly, it's up to you. To me the most interesting thing for me about this exercise has been the contrast between the skew of trade p&l, and return p&l. You can be doing something that massively bumps up your positive skew on trade p&l (like very aggressive dynamic stop loss adjustment), and think you are being a 'purer' trend follower, and then when you look at your monthly return you see there is no meaningful skew effect :-(