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



8 comments:

  1. "My logic is that the market does not know what my position is or has been", I partially disagree with this. Market or some counter parties, should have known where ppl start opening or accumulating positions on those price regions on that side, of course, not one specific strategies positions.

    ReplyDelete
    Replies
    1. Perhaps. But I doubt the market noticed the single contract of Eurodollar I sold this morning, unless they're very attentive indeed.

      Delete
  2. Moritz is sort of right about your exit for a trade being a function of what happens in the future and you're sort of right about future price movement not being a function of what has happened in the past but you are both fundamentally wrong in the overall scheme of things.

    So you can tinker all you like but it's all academic and borderline pointless when you are fundamentally wrong about how the object your testing works.

    ReplyDelete
    Replies
    1. Academic and pointless is exactly what I'm shooting for on this blog. Good to know I hit the target spot on.

      Delete
  3. Moritz is sort of right about your exit for a trade being a function of what happens in the future and you're sort of right about future price movement not being a function of what has happened in the past but you are both fundamentally wrong in the overall scheme of things.

    So you can tinker all you like but it's all academic and borderline pointless when you are fundamentally wrong about how the object your testing works.

    ReplyDelete
  4. Sorry, future price movement not being a function of your 'accumulated position'. There is a difference.

    ReplyDelete