An Introduction To Serious Algorithmic Trading

Download as pdf or txt
Download as pdf or txt
You are on page 1of 40
At a glance
Powered by AI
Some of the key takeaways from the document are the risks of overfitting strategies when backtesting, the importance of robust risk management, and thinking about trading as portfolio management rather than individual strategies.

Some risks of overfitting discussed are that the strategy may not generalize well to new data and could be overly optimized to past conditions. This can lead to strategies that perform well on historical data but do not work going forward.

Alternatives discussed for measuring performance beyond the Sharpe Ratio include considering additional metrics like upside/downside volatility, serial correlation of returns, and value at risk.

An Introduction To

Serious Algorithmic
Trading

www.darwinex.com
darwinex.com

Table of Contents
1. A budding retail trader’s story 06
1.1. Why automated over manual trading?
1.2. His first algorithmic trading strategy
1.2.1. The rules
1.2.2. The backtest
1.2.3. The forward test
1.2.4. It doesn’t work... now what?

2. What really happened? 09


2.1. Did the strategy make sense?
2.2. Did it generalize well, or was it over-
optimized?
2.3. Why is optimization ‘non-optimal’?
2.4. Why is over-optimization dangerous?

3. Identifying Overfit Trading Strategies 12


3.1. Model-focused
3.2. Risk-focused

4. Things to keep in mind when backtesting 14


4.1. Data Handling
4.1.1. Segmenting Historical Data
4.1.1.1. Training
4.1.1.2. Validation
4.1.1.3. Testing
4.2. Selecting Length of Historical Data
4.2.1. Occam’s Razor
4.2.2. Cross-Validation & Walk-Forward Testing
4.3. Backtesting Timeframes Lower Than H1
(hourly)
4.3.1. Overfitting Risk
4.3.2. Robustness Testing

2 An Introduction To Serious Algorithmic Trading


4.4. Parameter Selection

darwinex.com
4.4.1. Using Robust Parameter Ranges
4.5. Estimating Impact of Variable Spread &
Slippage
4.6. Maintaining Stable Underlying Strategy VaR
4.7. Variable Factors
4.7.1. Correlation of Strategy Returns to Market
Volatility
4.7.2. Testing for Market Correlation
4.7.3. Optimizing Position Sizes for Scalability /
Capacity
4.7.4. Trading High Impact News

5. Things to keep in mind when backtesting 21


5.1. Commonly Used Metrics
5.1.1. What is the Sharpe Ratio?
5.1.2. Where does the Sharpe Ratio fall short?
5.1.2.1. Assumes Returns are Normally Distributed
5.1.2.2. Sensitive to Periodicity of Strategy Returns
5.1.2.3. Insensitive to the “order” of excess returns
5.1.2.4. Does not differentiate b/w Upside and
Downside Volatility
5.1.2.5. What about periods of inactivity?
5.1.2.6. Ignores Serial Correlation
5.1.2.7. Standard Deviation op does not equal Risk
5.1.2.8. It is only applicable to strategies with low
VaR (value-at-risk)
5.2. Alternatives for Performance Measurement

6. What is VaR and how does it affect you? 26


6.1. Demystifying Value-at-Risk (VaR)
6.2. VaR as a percentile of loss
6.3. The Question
6.4. The Model
6.5. The Results
6.5.1. Probability Density Distribution of Returns by
Leverage
6.5.2. Evolution of Median Return vs. Leverage
6.5.3. Evolution of VaR (95% confidence) vs. Leverage

An Introduction To Serious Algorithmic Trading 3


6.5.4. Probability of Median Recovery vs. Leverage
darwinex.com

6.6. Conclusions

7. What does the “Serious” in Serious 32


Algorithmic Trading mean?
7.1. There are two types of traders, A and B
7.2. Can individual traders compete with large
institutions?
7.2.1. Scalability & Market Impact
7.2.2. Risk Management
7.2.3. Trading Technology
7.3. What exactly is a Quant?
7.4. Why do Quants become Quants in the first
place?
7.5. Why should this interest retail traders?
7.6. Advantages of Quantitative Trading
7.6.1. Alpha is an elusive beast
7.6.2. Statistically Robust vs Regular Backtesting
7.6.3. Quantitative Trading encourages thinking in
terms of portfolios
7.7. Disadvantages?

4 An Introduction To Serious Algorithmic Trading


darwinex.com

An Introduction To
Serious Algorithmic
Trading

5 An Introduction To Serious Algorithmic Trading


darwinex.com

1. A budding retail
trader’s story

Meet Larry, a fictional character with a fictional name chosen completely at random,
whose journey represents that of the typical retail trader’s.

Larry started trading FX a few years ago.

In his quest for trading excellence, he read multiple online tutorials, followed several forums
regularly and poured through a staggering number of price charts over time.

He tried his hand at trading fundamentals, technical analysis, price action and more, both
manually as well as via commercial (and free) automated trading robots.

Each had its pros and cons.. as do most things in life.

What became clear to him very early on though, was that discretionary trading is an art in
itself, requiring not only skill but patience and mental fortitude of epic proportions.

If you’re wondering what any of this has to do with serious algorithmic trading, keep
reading..

1.1. Why automated over manual trading?

For him, automated trading offered the opportunity to trade without any emotional
involvement, an opportunity that would enable him to:

1. Focus on the logic and rationale behind his trading ideas,


2. Avoid the stress of indecision (entering too early, late or not at all),
3. Avoid other limiting factors such as human error, greed and fear.

After several unprofitable attempts at using both free and commercially available trading
robots, he decided it was time he went solo.

Going forward, he would commit to putting in the time and effort to create his very own
algorithmic trading strategy - one that he would author from scratch, using his own
experience and intellect.

And off to the proverbial drawing board he went.

6 An Introduction To Serious Algorithmic Trading


1.2. His first algorithmic trading strategy

darwinex.com
Fast forward some time, he came up with what he thought was a great strategy using
technical analysis:

1. It involved moving average crossovers,


2. Survived a 15 year backtest on multiple currency pairs and timeframes,
3. Had a great risk/reward ratio in backtesting,
4. Fairly smooth equity curve,
5. A tolerable historical max drawdown,
6. An acceptable level of stagnation over time,
7. And survived all major financial crises during the backtest.

1.2.1. The rules

The rules of the strategy were:

1. GO LONG at the open of the next price bar, when the FAST Moving Average (MA)
with lookback period “x” crosses above the SLOW Moving Average with lookback
period “y”.

2. GO SHORT when vice versa.

In summary, the conditions necessary to trade in either direction were:

If MA(x) @ Time (tc-1) > MA(y) @ Time (tc-1): GO LONG @ Time to


If MA(x) @ Time (tc-1) < MA(y) @ Time (tc-1): GO SHORT @ Time to

.. where tc refers to Closing time, and to refers to Opening time, of the price bar.

1.2.2. The backtest

He backtested the strategy over 15 years of currency data, the strategy performing
consistently well throughout.

His hard work having apparently panned out really well, he was confident that it would
perform in similar fashion in live market conditions and launched it live.

1.2.3. The forward test

After some time however, the strategy started losing more than it ever had in backtests.

This was most unexpected...

An Introduction To Serious Algorithmic Trading 7


darwinex.com

Genuinely puzzled by this outcome, he:

1. Went through all trades executed so far with a fine-toothed comb.


2. Backtested the strategy over the same period as it had been live.

... only to find a dramatic difference between the performance in backtesting vs live
trading.

1.2.4. It doesn’t work... now what?

Perhaps the strategy was missing something.

Perhaps he needed to filter price-based signals a bit more in live trading than in
backtesting to account for live market dynamics?

He tested this theory by adding a select group of indicators for filtering out bad signals,
which had the welcome effect of eliminating several losing trades from the backtest results.

“Surely the added layer of price filters will have a positive impact on future live returns”, he
thought... and launched this revised strategy live.

Fast forward to several iterations of such indicator addition, deletion and parameter-
optimization, Larry has still yet to find a solution to the strategy simply not behaving in live
trading as it does in backtesting.

8 An Introduction To Serious Algorithmic Trading


darwinex.com
2. What really happened?

2.1. Did the strategy make sense?


Traders often focus too much attention on fine-tuning a strategy instead of understanding
why it does or does not work.

For example,

• Is there a sound hypothesis behind MA(x) > MA(y)?
• Why does it appear to work?
• What could possibly be its source of alpha?
• Could it be an optical illusion?

Is it just technical analysis and hence it must make sense if it “works”?

Ok, but the problem isn’t usually apparent when a model IS working, it’s when it is NOT (or
STOPS) working that things get tricky.

2.2. Did it generalize well, or was it over-optimized?


If a technical analysis driven strategy begins to fail, traders will often attempt to address the
problem by:

1. Fine-tuning the values of existing parameters,

e.g. Larry optimizing ‘x’ and ‘y’ by looking at which periods achieve better
performance in MA(x) > MA(y) and vice versa.

2. Adding layers of additional indicators to filter price-based signals further, in the hope
of eliminating bad signals.

3. By doing this however, they are unintentionally overfitting their trading strategies to
produce better results over a more finite set of outcomes from a MUCH larger space
of possible outcomes.

In statistical terms, a trading strategy that’s say too simple, may essentially be
demonstrating high bias.

An Introduction To Serious Algorithmic Trading 9


In order to improve its ability to generalize better to market data (and hence, ideally
darwinex.com

produce better returns), traders will perform such parameter optimization / filtering.

In the absence of a sound hypothesis that defines it, parameter optimization applied to the
technical analysis strategy above is likely to help it evolve from a:

1. High bias model (poor performance in both backtest and live trading).

2. To a High variance model (spectacular performance in backtesting, poor


performance in live trading)

Here’s what this looks like:

Image sourse: wikidocs.net

2.3. Why is optimization ‘non-optimal’?

In practice, achieving ‘optimal optimization’ is difficult in the absence of a sound hypothesis


dictating some semblance of discrete boundaries for the same.

Traders are instead forced to be subjective (based primarily on return-specific metrics)


regarding what combination of optimized parameters achieves robust outcomes over
another.

In particular, as the strategy is more closely “fit” to historical data after parameter
optimization, each backtest can possibly look better with each round of optimization.

This isn’t necessarily dangerous if the hypothesis behind the model is sound. But in
its absence, optimization is prone to leading traders into false expectations of future
performance.

When subjected to live trading conditions, the odds of an optimized strategy failing are
therefore usually high.

10 An Introduction To Serious Algorithmic Trading


darwinex.com
2.4. Why is over-optimization dangerous?
By optimizing to any degree above a certain baseline, any trading strategy becomes
increasingly more closely fit to the historical data it is modeled on, and is hence unable to
generalize well to new market data.

In summary, by not having a crystal clear understanding of the algorithm to begin with, the
odds of rectifying a strategy when it begins to fail for any reason, are stacked against the
trader.

Let’s discuss this in a bit more detail in the following two section:

1. Identifying overfit trading strategies,


2. Things to keep in mind when backtesting.

An Introduction To Serious Algorithmic Trading 11


3.
darwinex.com

Identifying Overfit
Trading Strategies

Overfit trading strategies typically perform well in backtesting environments, creating the
illusion that they exploit the market inefficiency being targeted, really well.

However, when deployed in a live trading environment, their performance is


disproportionately different to what was observed in backtesting, due primarily to their
modeling historical data too closely.

This prevents such strategies from generalizing well to unseen data in future, to the
detriment of traders (launching them live with capital).

We can generalize them into two main categories:

1. Model-focused – where the strategy fits historical data too closely, and exhibits high
variance when tested on unseen data.

2. Risk-focused – where the performance of a weak model is compensated for with an


unrealistic, loss averse risk management rationale.

3.1. Model-focused
In the former (model-focused), a trading strategy will typically perform very well in
backtesting, but either stagnate for lengthy periods of time (or fail entirely) in live testing.

Overfit Trading Strategies (model-focused)


From both a trader and
investor’s perspective, such
strategies are easy to identify
visually:

Returns will at some point


reach a point of inflexion
where they no longer
appear similar to historical
performance.

12 An Introduction To Serious Algorithmic Trading


3.2. Risk-focused

darwinex.com
In the latter (risk-focused), a trading strategy will demonstrate smooth, very consistent
returns in backtesting as well as in live testing for a period of time.

This makes such strategies far more dangerous than the former as they are difficult to
identify from looking at just returns charts.

Overfit Trading Strategies (risk-focused)

Before & After (risk-focused)

An Introduction To Serious Algorithmic Trading 13


4.
darwinex.com

Things to keep in mind


when backtesting

Simulating a strategy’s historical


performance correctly, increases the
probability of it generalizing well to unseen
market data in future.

As such, it’s important that all backtesting be


conducted robustly, careful attention being
paid to any factors that could contribute
towards historical and live performance
being markedly different.

Let’s organize some best practices into 3


main categories:

1. Data Handling
2. Parameter Selection
3. Variable Factors

4.1. Data Handling


In this first section, we discuss how to treat data used in MetaTrader 4 backtesting.

4.1.1. Segmenting Historical Data


A common misconception in MetaTrader backtesting is to associate a strategy’s robustness
with how well it performs on the largest possible amount of historical data available to the
trader.

A strategy that performs remarkably well on the entire dataset is at risk of being overfit
(high variance).

Conversely, a strategy that has been back-tested on too small a portion of historical data is
likely to encounter high bias.
In both cases, the likelihood of the trading strategy generalizing well to unseen
market data is poor at best.

Therefore, it is important at the very least, that any backtesting be conducted using
independent training, validation and test sets:

14 An Introduction To Serious Algorithmic Trading


4.1.1.1. Training

darwinex.com
The data segment used for training the strategy parameters on (or in other
words, checking for decent performance in backtesting).

4.1.1.2. Validation
The first unseen data segment used to test how the same parameters chosen in
training, perform on unseen data. Passing this phase adds confidence that the
strategy is robust.

Discrepancies in this phase also permit revision of parameters before finally


testing on the last batch of test data.

4.1.1.2. Testing
If a strategy passes both training and validation phases satisfactorily, running a
final simulation on this last “held out” segment of data adds another layer of
confidence that the trading strategy either exhibits robustness or is prone to
excess stagnation or failure in live trading.

Training, Validation, Testing Samples

Care should be taken however to not bias the outcome by modifying any parameters
during the test sample phase.

If the trading strategy passed in-sample (training), validation, but not testing, it is advisable
to go back to the drawing board for any modifications required, and then re-run both in-
sample and validation tests again.

4.2. Selecting Length of Historical Data


The amount of historical data required for a backtesting exercise is directly proportional to
the complexity of the trading strategy being tested.
In other words, the more complex (large number of training parameters) the strategy is, the
more data required to ascertain the validity of its underlying hypothesis.

An Introduction To Serious Algorithmic Trading 15


Occam’s Razor – Simple vs. Complex Trading Strategy
darwinex.com

4.2.1. Occam’s Razor


In line with the Occam’s Razor principle, when optimizing parameters to estimate the
amount of data required for a backtest, it is generally best to favour simpler over more
complex strategies if their output performance is not dramatically different (for better or
worse).

4.2.2. Cross-Validation & Walk-Forward Optimization


Cross-Validation is an approach applicable to many fields including trading, and involves
training an algorithm over a portion of data and validating the optimized parameters on
another portion of data.

Walk-forward optimization is one application of cross-validation in financial trading. The


method enables traders to estimate the best combination of values to assign to a strategy’s
parameters.

It involves iteratively optimizing parameters over sequential windows of in-sample data


and testing over the remaining (non-windowed) data, results from all windows being used
at the end of the test to ascertain the best parameter values.

If the amount of data available is not as large as is optimal for backtesting, traders
may consider a walk forward optimization to make better use of smaller data samples.

4.3. Backtesting Timeframes Lower Than H1 (hourly)

Trading strategies that operate below hourly (H1) timeframes, are prone to
experiencing excess divergence between their simulated and live trading performance.
This behaviour is primarily due to a considerable amount of noisy data in timeframes
below hourly.

4.3.1. Overfitting Risk

If a strategy models on data over small timeframes, traders must be mindful of overfitting
risk (particularly in complex strategies with a large number of parameters).

16 An Introduction To Serious Algorithmic Trading


4.3.2. Robustness Testing

darwinex.com
This is a practice where a trading strategy is subjected to a series of tests to ascertain
whether its backtest exhibits realistic outcomes and isn’t overfit to the data it was
performed on.

Example Chart: Monte Carlo Simulation / Optimization of Trading Strategies

Monte Carlo simulation is an example of robustness testing, where several variations are
made to a strategy’s input parameters and the strategy backtested iteratively over all
combinations of inputs that result from these variations.

This can quickly estimate how closely (or not) a strategy could perform in live trading
vs. backtesting.

4.4. Parameter Selection


This section discusses some best practices in terms of risk management and strategy
optimization.

4.4.1. Using Robust Parameter Ranges

Regardless of the number of parameters in a trading strategy, it’s important that traders
choose parameter values that do not fit the underlying data too closely.

For example, it is a more robust practice to use an Exponential Moving Average (EMA)
period of 50 instead of 49 or 47, if the resulting backtest performances are slightly different

An Introduction To Serious Algorithmic Trading 17


but both positive in both cases.
darwinex.com

Similarly, during any genetic or walk-forward optimization, it is good practice to choose


robust ranges of parameter values that typically increment in steps of 5 or 10, as opposed
to 1 or 2.

Robust range selection in this manner does carry the risk of introducing high bias into the
algorithm.

Therefore, it is also important for traders to test how using robust parameter values
impact the overall performance or ability of the strategy to generalize well to unseen data.

4.5. Estimating Impact of Variable Spread & Slippage

Measuring Variable Spread / Slippage Is Important

Whatever trading platform or tools


traders use, it’s important that they
have (or can
be configured to) the ability to
backtest with variable spread and
slippage.

Traders can run such simulations on


their backtest results in a few ways,
three of which are:

1. By importing backtest results


into a spreadsheet application
(e.g. MS Excel), modifying
Open/Close prices of trades
with randomly generated
values between a sensible
range (e.g. between 0.1 and 2.0 or 5.0) and recalculating P/L.

2. By importing backtest results into a statistical computing environment such as R, GNU


Octave or MATLAB and doing the same as in point (1) above.

3. Using 3rd-party commercial tools.

4.6. Maintaining Stable Underlying Strategy VaR


Making use of dynamic stop loss and take profit targets , proportionate to available
account equity, allows for realistic backtest outcomes, and aids risk stability.

In trading strategies that increase or decrease position sizes by available equity, it is


particularly important that minimum/maximum lot sizes be restricted to sensible
ranges for the following reasons:

18 An Introduction To Serious Algorithmic Trading


1. A lot size dropping below the

darwinex.com
minimum permissible lot size would
result in trade rejection.

2. Lot sizes scaling too large in


single trades (e.g. to 100 lots or
above) would introduce capacity
constraints during live trading.
Resolving this could possibly require
fractional position sizing to address,
depending on factors such as
time of day, strategy capacity and
available liquidity.

3. Such constraints would not be


visible in a MetaTrader backtest,
and without being factored in
during strategy development, Dynamic Position Sizing / Liquidity Considerations
possibly create the illusion of an
extremely profitable strategy with
no liquidity considerations holding
it back.
4.7. Variable Factors
In this final section, we discuss some considerations that trading strategies can benefit from
during the design and post-backtest phases.

4.7.1. Correlation of Strategy Returns to Market Volatility


Comparing the correlation of a trading strategy’s returns to those of its underlying
assets, reveals the dependency of the strategy on the same.

A strategy with a low dependency on the assets it trades is less likely to demonstrate
unexpected behaviours during periods of decline or sudden turbulence in underlying
assets, as opposed to one that relies heavily on underlying asset returns.

4.7.2. Tip: Testing for Market Correlation


To analyze a strategy for Market Correlation as well as 11 other investment attributes,
traders are encouraged to upload backtests to the Darwinex platform, review scores and
visualize the evolution of their strategies for each attribute.

4.7.3. Optimizing Position Sizes for Scalability / Capacity


One of the ways in which a strategy’s overall investment capacity can be improved, is
by implementing fractional order sizing as opposed to fixed, singular lot sizing.

For example, sending 10 orders of 1 lot each is a more scalable approach than sending 1
order of 10 lots.

An Introduction To Serious Algorithmic Trading 19


darwinex.com

4.7.4. Trading High Impact News


Strategies that trade during high impact news events are impacted negatively by sudden
swings in variable slippage and spreads at the time of the event.

One way to reduce (if not eliminate) such negative impact is by having the strategy
disable trading at predefined times or intervals corresponding to such events.

20 An Introduction To Serious Algorithmic Trading


5.

darwinex.com
Evaluating Strategy
Performance

Several metrics exist for evaluating backtest and live trading performance.

5.1. Commonly Used Metrics


1. No. of Trades
2. Win Ratio (%)
3. Average Win
4. Average Loss
5. Best Trade
6. Worst Trade
7. Expectancy
8. Sharpe Ratio
9. Profit Factor
10. Standard Deviation
11. Arithmetic Average Holding Period (AHPR)
12. Geometric Average Holding Period (GHPR)

In particular, the Sharpe Ratio has long been used by traders as the de facto standard for
evaluating trading strategies.

However, it isn’t without its shortcomings, and is quite possibly misleading if not understood
correctly for what it really represents.

Since traders refer to this metric quite often, let’s discuss why it’s a non-optimal tool for
measuring the performance of a trading strategy.

For your convenience, the remainder of this discussion is organized as follows:

1. What is the Sharpe Ratio and what is it used for?


2. Where and how does the Sharpe Ratio fall short?
3. Alternatives for performance measurement.

5.1.1. What is the Sharpe Ratio?


Developed in 1966 by Nobel prize winner William Forsyth Sharpe , the Sharpe Ratio is
a measure of the excess return of a portfolio or trading strategy relative to its underlying
risk.

An Introduction To Serious Algorithmic Trading 21


Originally termed the “reward-to-variability ratio”, it is commonly used as a risk/return
darwinex.com

measure in finance that describes how well asset returns compensate investors for risks
undertaken.

Mathematically, the Sharpe Ratio can be expressed as:


Rp Rf
Sharpe Ratio (p) = ­
σp
where:
Rp = Expected excess return of investment or trading strategy
Rf = Risk−free rate of return
σp = Standard deviation of excess returns

Note:
For the remainder of this discussion, the risk-free rate (R f ) is assumed to be 0.00,
i.e. 100% of the portfolio is assumed to be invested in risky assets.

While the Sharpe Ratio is certainly a decent measure to form initial impressions of
risk/reward, the remainder of this post aims to demonstrate why – on its own –
it is not adequate for evaluating a trading strategy’s performance, and hence
unsuitable for guiding investment decisions.

5.1.2. Where does the Sharpe Ratio fall short?

The following is a comprehensive (but by no means exhaustive) list of problems with


the Sharpe Ratio, in the context of evaluating trading strategy performance.

5.1.2.1. Assumes Returns are Normally Distributed

By making use of standard deviation –


a measure that assumes returns to be
normally distributed – the Sharpe Ratio
cannot adequately quantify behaviours
observed in real world trading.

In reality, rarely are trading strategy


returns concentrated within fixed standard
deviations around mean returns.

In fact, most strategies will demonstrate


non-zero skewness and kurtosis, whereas a
normality assumption will hypothesize that skewness is zero and underestimate tail
risk.

Therefore, if using the Sharpe Ratio, a trading strategy experiencing, e.g. a period of
unexpected, very exceptional performance may lead to a significant deviation from
normality, acutely biasing the performance evaluation and misleading investment
decisions.

22 An Introduction To Serious Algorithmic Trading


Over and above the assumption of normality, the Sharpe Ratio doesn’t give any

darwinex.com
further information about the strategy’s actual distribution of returns.

5.1.2.2. Sensitive to Periodicity of Strategy Returns

Since the only component of the Sharpe Ratio‘s denominator is σp , varying the
periodicity over the same time interval will affect the output Sharpe Ratio.

This is due to the fact that excess return over the same time interval will stay the
same, but the standard deviation of returns over different periods (e.g. daily, weekly,
monthly, etc) will be different.

In terms of performance evaluation, this is a risk since a more favourable Sharpe Ratio
on one time frame, may possibly enforce a preference for that time frame and hence
mislead an investment decision.

5.1.2.3. Insensitive to the “order” of excess returns

Standard deviation as a measure, does not consider the order of data it is presented.
Two trading strategies with wildly different paths to the same excess return, could
theoretically have the same standard deviation over the same time interval.

Using the Sharpe Ratio, it wouldn’t be possible to adequately evaluate which of the two
is a more risk-effcient candidate for investment. Indeed, upon visual inspection of such
strategies, the investor may have excluded one or the other regardless of its Sharpe Ratio.

5.1.2.4. Does not differentiate b/w Upside and Downside Volatility


Once again, owing to standard deviation
σ p being the Sharpe Ratio’s denominator,
large fluctuations in excess returns (even
when they’re positive) can effectively
lower the Sharpe Ratio. This is due to the
fact that both

1) an unusually large gain, and


2) a similar-sized drawdown

can potentially increase the value of the standard deviation more than that of the excess
returns, over the same time interval, effectively penalizing both good and bad performers.

This issue was addressed to an extent with the development of the Sortino Ratio,
which takes only downside deviation (let’s call it σdownside) into consideration. It is a
modification to the Sharpe Ratio, the approach penalizing “bad volatility” instead of
all volatility.

However, many of the problems with the Sharpe Ratio as discussed in this post also
apply to similar variations.

An Introduction To Serious Algorithmic Trading 23


5.1.2.5. What about periods of inactivity?
darwinex.com

By design (excess return over standard deviation), a strategy’s Sharpe Ratio will decline
over periods of trading inactivity despite no positions (i.e. zero return) being taken during
that time.

To make this concrete, let’s assume a strategy so far has 12 months of non-zero returns:

1.5%, 2.0%, -1.0%, 1.2%, 3.5%, -2.5%, 0.2%, 4.1%, 1.5%, 2.0%, -1.0%, 1.2%.

Therefore, assuming Rf = 0.00,


Expected excess return Rp = 0.01058333
Standard Deviation σp = 0.01889905
Sharpe Ratio = 0.01058333–0.000.01889905 = 0.5599928

Let’s now assume that the strategy does not trade for the next month.

The new returns series is now:

1.5%, 2.0%, -1.0%, 1.2%, 3.5%, -2.5%, 0.2%, 4.1%, 1.5%, 2.0%, -1.0%, 1.2%, 0.00%

Once again, assuming Rf = 0.00,


Revised expected excess return Rp = 0.009769231
Revised Standard Deviation σp = 0.018331
Revised Sharpe Ratio = 0.009769231–0.000.018331 = 0.5329349

What we’ve just observed is the Sharpe Ratio penalizing trading inactivity, the Sharpe Ratio
declining by 4.83% without the strategy taking any trading decisions over the last month.

This tendency therefore renders it non-optimal as a performance measure.

5.1.2.6. Ignores Serial Correlation

Serial correlation describes the relationship


between individual returns over successive
intervals of time.

When present, it can smooth strategy returns,


consequently biasing standard deviation
calculations of the time interval under study.

In such situations, there is a risk of the Sharpe


Ratio being considerably OVERSTATED in
the case of positive serial correlation, and
UNDERSTATED in the case of negative serial
correlation.

5.1.2.7. Standard Deviation σp does not equal Risk

This is perhaps best explained using the following scenario.

24 An Introduction To Serious Algorithmic Trading


Let’s suppose a strategy:

darwinex.com
1. Trades very small movements,
2. Over small periods of time,
3. During low volatility hours,
4. ..but with HUGE leverage.

If the market barely moves, the standard deviation σp will be minimal.

But the strategy will still have traded with very high leverage and hence very high risk.

5.1.2.8. It is only applicable to strategies with low VaR (value-at-risk)

The Sharpe Ratio can really only be applied to trading strategies operating with low to
mid-sized VaR, e.g. lower than 20% to 30% at maximum.
Let’s suppose a trader:

1. Has an amazing Win Ratio of 100:1


2. But risks all his capital on every single trade

Given these dynamics, it is inevitable that the trader will lose all his capital at some point.

The Sharpe Ratio up until this strategy’s inevitable demise would have been quite
spectacular.

More alarmingly though, it could have mislead investors using it as a performance


measure, to make ill-fated investment decisions.
However, if this same trader risked only 10% of his capit al per trade (i.e. lower strategy
VaR), he would likely make consistently spectacular returns without self-combusting.

At Darwinex, we measure Return/Risk (since inception) for DARWINS – a metric similar to


the Sharpe Ratio – because a DARWIN’s risk is capped at 10% VaR, for which such a ratio is
practical and adequately applicable.

In summary,
Because the Sharpe Ratio depends on the underlying strategy’s risk, on its own it
cannot adequately judge a strategy’s performance.

5.2. Alternatives for Performance Measurement

If the objective is to measure trading strategy performance, it makes more sense to


estimate:

• A trading strategy’s performance compared to random strategies trading with the same
risk profile.

• How much risk a strategy can handle compared to random strategies trading the same
asset profile.

An Introduction To Serious Algorithmic Trading 25


6.
darwinex.com

What is VaR and how does


it affect you?

One of the most common mistakes traders make is assuming that excess leverage can
enhance returns from high performance / low risk trading strategies.

While this is indeed true to some extent, it is equally true for the opposite scenario where it
can be the sole reason for complete trading account annihilation.

Contrary to popular belief among retail trading masses, poor trading strategies don’t kill
accounts, operating at unsustainably high VaR does.

We’ve studied this extensively at Darwinex Labs and quantitatively demonstrated the long-
term impact of incremental leverage on trading strategy performance.

Particular emphasis in our work was laid on simulating a trader’s choice of leverage and
the associated likelihood of successful recovery in the advent of a loss.

By the end of this discussion, you will hopefully develop a better understanding of how
leverage affects traders’ chances of survival in the financial markets and be better
equipped to decide what leverage you consider most optimal, depending on your risk
appetite.

Before delving into the details of how we conducted this research, let’s first simplify the
concept of Value-at-Risk (VaR) a little for the benefit of all our readers.

6.1. Demystifying Value-at-Risk (VaR)

VaR is based on the probability distribution of returns from an asset, portfolio and/or
trading strategy.

One of the main premises behind the concept is that all liquid financial instruments exhibit:

• Varying degrees of uncertainty…


• .. due to varying degrees of market risk…
• …. from various sources of risk, depending on the instrument(s) in question

It’s this uncertainty that can be modeled with appropriately chosen probability distributions.

To keep things straightforward for the purpose of this discussion, bearing the
following two concepts in mind will serve you well.

26 An Introduction To Serious Algorithmic Trading


6.2. VaR as a percentile of loss

darwinex.com
Simply put, Value-at-Risk is a percentile of loss, derived from:

A model – the part used to calculate the VaR.


A measure – what we infer from the output of the model.

The model is used to calculate a return accompanied by a probability of the asset or


portfolio losing more than this return over a given time horizon. i.e. it is a statistical measure
of the losses possible when taking on market exposure.

For example,
A VaR model applied to a trading strategy may output “Monthly VaR (95%) =
-21.0%”, which translates to “there is a 5% chance of this trading strategy losing
more than -21.0% in any given month”.

With that in mind, let’s now discuss the specifics of the research itself.

An Introduction To Serious Algorithmic Trading 27


6.3. The Question
darwinex.com

Beyond what VaR does loss recovery become statistically improbable?

The answer to this question isn’t at all straightforward to decipher quantitatively.

Indeed as you’ll see shortly, though our findings gave us conclusive evidence that using
excess leverage is not in the best interests of a trader, we weren’t able to pinpoint an exact
or tight range to answer this question satisfactorily.

We therefore leave it up to readers to form their own conclusions based on this study’s
findings.

6.4. The Model


To calculate the VaR and other descriptive statistics, we needed to construct a probability
distribution of returns for each level of leverage we wanted to test.

To generate these returns, we first created the following trading strategy:

Asset to trade:

EUR/USD

Direction:

Randomly trade LONG or SHORT

Trading Logic:

Execute 1 trade per day, lasting exactly 1 hour, for 1 month, with randomly chosen directions
and starting times

Simulations:

100,000 (repeat trading logic this many times)

Leverage:

1:1 to 400:1 (repeat trading logic for each leverage level in this range, in steps of 10).

Logic dictates that the higher the leverage used for trading, the greater the risk of not being
able to recover the account in the advent of a large loss, as a result of running out of the
capital required to maintain that leverage assuming capital in the account is the trader’s
only available capital.

Running the algorithm above produced the following results, attesting to the validity of this
assumption.

28 An Introduction To Serious Algorithmic Trading


6.5. The Results

darwinex.com
6.5.1. Chart 1: Probability Density Distribution of Returns by
Leverage

Observations:

As leverage increases, all 3 of the following metrics worsen considerably:

1. Monthly VaR (95% confidence)


2. Median Return
3. Probability of recovering loss of median return – P(Recovery)

This behaviour presents itself in the chart above, in the form of systematic shifting to the left
of each distribution with higher leverage than the prior.

6.5.2. Chart 2: Evolution of Median Return vs. Leverage

An Introduction To Serious Algorithmic Trading 29


Observations:
darwinex.com

After an initial, briefly slow rate of change, median return degradation accelerates to an
optimum, systematically worsening as leverage increases.

6.5.3. Chart 3: Evolution of VaR (95% confidence) vs. Leverage

Observations:

Past a certain choice of leverage, VaR converges to -100%, ruling out high leverage as a
means nor solution to enhancing trading returns

6.5.4. Chart 4: Probability of Median Recovery vs. Leverage

Observations:

As the leverage increases, the probability of recovering a loss of median return in the next
month, systematically decreases.

30 An Introduction To Serious Algorithmic Trading


6.6. Conclusions darwinex.com

The findings of this study further reinforce some of the assertions made earlier in this post,
as well as conclusions made in Part I of this series:

1. Trading beyond a certain VaR makes it statistically improbable that the trader will be
able to recover a loss of median return in the next month.

2. It makes more sense for traders to employ stable, moderate to low VaR and leverage
investor capital instead.

3. Traders stand to benefit from analyzing their trading strategies in this manner,
in order to take preemptive action should their strategies be at risk of statistically
irrecoverable loss.

An Introduction To Serious Algorithmic Trading 31


7.
darwinex.com

What does the “Serious” in


Serious Algorithmic Trading mean?

For most good traders:

1. One single backtest over the same chronologically ordered time series is never
enough.

2. Considerable amounts of time are spent understanding the core hypothesis behind a
trading strategy before any backtesting or development of any kind.

To arrive at some definition of “serious”, we’ll need to go through some key differences ex-
hibited by serious traders vs others.

7.1. There are two types of traders, A and B

Type A Type B

Formulates hypothesis for trading strategy Spots patterns that appear to repeat them-
from observations in data. selves but doesn’t understand why.

Constructs a model for testing the logic of Gathers as much asset data as possible and
the hypothesis before backtesting. backtests the discovered patterns.

Identifies whether the model is applicable Repeats the backtesting exercise above
to more than one asset. If it is, attempts are across multiple assets.
made to establish why.
If results are promising, i.e. performance
It is important that there be clear reasoning is consistent across multiple assets, this is
behind the initial hypothesis being often mistaken for “robustness”.
applicable to any given dataset.

For example, why EUR/USD and not GBP/


USD?
If reasoning is sound, the next step is to A range of fixed spreads are used in back-
create a prototype strategy and backtest testing. Without 3rd-party tools it is not pos-
across all assets identified earlier. sible to simulate the effects of e.g. variable
Spreads, commissions, simulated variable slippage in DMA environments.
slippage and swaps are all included in
If backtest results are very good, it is

32 An Introduction To Serious Algorithmic Trading


the backtest for simulating the impact of common for Type B traders to estimate this

darwinex.com
transaction costs on strategy returns. impact by calculating:

Impact (per trade) = Expectancy - Possible


Slippage (per trade)
Type A traders are not limited by the Type B traders are limited by the available
available functionality of trading platforms. functionality of their trading platform’s
programming language, e.g. MQL for
E.g. they will use programming languages MetaTrader.
such as Python, C/C++ and R to develop
strategies that can benefit from a larger There is hence a greater risk of being
base of available functionalities (statistical unable to properly validate a trading
risk analysis, machine learning, etc.) strategy’s performance in the absence of
the technology required to do so.
At the very least, Type A traders will conduct Limited by the trading platform’s available
Monte Carlo simulations to establish a functionality, Type B traders will often not
range of sensible VaR (Value-at-Risk) to test strategies to assess metrics such as VaR.
operate with.
Instead, metrics such as maximum
This mitigates the risk of assuming, e.g. that drawdown are considered important for
a strategy’s maximum drawdown over the deciding what level of leverage to use in live
entire length of the backtest may permit trading.
taking advantage of excess leverage for
enhancing performance.

Again, libraries available in programming


environments like Python and R help
achieve this easily.
When working with portfolios of assets, Limited by the available functionality of the
Type A traders will at the very least measure
trading platform’s programming language,
periodic correlation between them, in an Type B traders run the risk of missing critical
attempt to reduce the risk of being over- points such as inter-asset correlation,
exposed without adequate diversification. giving performance more importance
over the very real risk of being caught in
Again, this can usually only be achieved in situations where inter-asset correlation can
external tools and programming languages exacerbate losses.
such as Python and R.

7.2. Can individual traders compete with large institutions?

Regardless of Type A or B, nor considering skill level and experience, a trader will at one
point or another, question how practical it is to compete with large institutions in the same
markets.

After all, it is “big money” at the end of the day that moves these markets.

The reality is that institutions face several regulatory, technological, structural and capital
constraints that individual traders don’t.

An Introduction To Serious Algorithmic Trading 33


It’s these very constraints that also lead to institutional funds often exhibiting a degree of
darwinex.com

predictability that is visible to – and can potentially be exploited by – individual traders.

The answer to the question is therefore YES.

Let’s examine why in the contexts of:

1. Scalability & Market Impact,


2. Risk Management, and
3. Trading Technology.

7.2.1. Scalability & Market Impact

Individual traders can design and execute trading strategies that target small inefficiencies
in the market.

Such inefficiencies often have capacities up to a fairly limited amount of capital before they
lose their profitability.

This limited capital support usually ranges from a few hundred thousand to a few
million dollars, and is therefore of little to no interest to institutional funds trading with a
substantially larger capital base.

Furthermore, individual trader capitalization is much lower than that of institutions. This is
an advantage as retail trading activity in highly liquid markets cannot therefore create any
substantial market impact.

7.2.2. Risk Management

Without middle or compliance offices enforcing


industry standards and regulatory oversight,
individual traders have the option to model their
own risk management techniques as they deem
fit, promoting flexibility that can indeed contribute
to generating excess returns.

However, while this may certainly be an


advantage for some experienced traders, it is a
double-edged sword.

At no risk of being “overruled”, individual traders are at greater risk of exercising


“nonoptimal” risk management decisions, often leading to negative outcomes, e.g.
accounts blowing up due to excessive use of leverage or aggressive risk management that
wouldn’t otherwise have been permitted in an institutional setting.

Furthermore, with no enforcement of industry best practices and risk management


oversight, individual traders often find themselves modeling risk at the execution level (e.g.

34 An Introduction To Serious Algorithmic Trading


stop losses and take profits), without much consideration given to risk at the portfolio level

darwinex.com
(e.g. mixture of assets or strategies deployed on the same account).

7.2.3. Trading Technology

Institutional traders do not enjoy the same flexibility as retail traders, in terms of their choice
of technology for trading and strategy development.

Retail traders can choose from a large selection of servers, hardware, trading platforms,
programming languages and toolkits, without corporate IT policy or a predetermined list of
“permitted systems” affecting their technology preferences.

The only disadvantage that accompanies this flexibility though, is that it can get quite
expensive (relative to a retail trader’s available funding) to purchase hardware, software
and server subscriptions.

These costs must therefore be paid for by traders themselves, whereas in institutions they
would most likely be mitigated by management fees charged.

7.3. What exactly is a Quant?


Many individual traders will often hear the term “Quant” and may visualize either:

1. A sharply dressed City trader working in a hedge fund, sat in front of his Bloomberg or
Thomson Reuters Eikon terminals, or

An Introduction To Serious Algorithmic Trading 35


2. A stocky chap wearing a Batman t-shirt, with telescopes for specs (and possibly
darwinex.com

a doctorate in Nuclear Physics), sat in his basement trading room pouring over
mathematical models, following a staple fast food diet.

For the avoidance of doubt:


you do not need a university degree to become a Quantitative Trader - though
having one does come in extremely handy, IF you were paying attention in
class!:-)

Now, both these individuals above likely followed a similar academic (or independent
learning) path to their current disposition.

It’s even possible that the City trader was once a frequent visitor to his own basement
trading room, before being spotted by a financial recruiter and beamed up to the 39th
floor.

This prompts a few more questions.

7.4. Why do Quants become Quants in the first place?

There are numerous advantages to quantitative trading, advantages that not only reduce
the financial impact of discretionary decision-making going wrong, but also help measure
performance in a manner that permits efficient future learning.

36 An Introduction To Serious Algorithmic Trading


For the purpose of distinction,

darwinex.com
it must be noted that unlike algorithmic trading that employs technical analysis,
the quantitative approach to algorithmic trading involves primarily Time Series
Analysis, Bayesian Statistics and Machine Learning.

R&D involving these disciplines often leads to more effective methods of generating
uncorrelated risk-adjusted returns, all too often ignored by the retail fundamental trader or
technical analyst.

Those who recognize their advantages, naturally progress down a path to becoming
Quants.

7.5. Why should this interest retail traders?


The world of quantitative trading extends far beyond that of technical moving average
crossovers, candlestick patterns, martingales, grids, and off-the-shelf trading systems
celebrating their latest and greatest “trading secrets” that resulted in their most
sensationally overfitted backtests to date.

It does so because the markets are a much more complex phenomenon than we think, and
undergo several micro-structure and regime changes frequently.

Indeed one could go as far as to say that the market is a stochastic process in and of itself.

Generating uncorrelated risk-adjusted returns in as seemingly stochastic an environment as


this, is therefore a mammoth task.

Why more retail traders haven’t then embraced quantitative methods (that enable the
discovery and subsequent exploitation of inefficiencies in stochastic processes) can only be
attributed to a lack of awareness and approachable learning resources.

7.6. Advantages of Quantitative Trading


7.6.1. Alpha is an elusive beast
Alpha is difficult to discover, gradually loses its profitability as more capital is allocated to
its exploitation, and inevitably transitions from a source of risk-adjusted profitability to a
source of risk.

Quantitative methods allow traders to not only discover and exploit alpha, but to do so in a
manner that maximises its profitability horizon and minimizes risk of decay.

Quantitative analysis also facilitates the creation of statistical testing frameworks that can
forecast risk of alpha decay in advance.

An Introduction To Serious Algorithmic Trading 37


7.6.2. Statistically Robust vs Regular
darwinex.com

Backtesting
Quantitative traders can make use of statistical
tools that add another layer of validity to
backtests.

In simple terms, if you’re currently examining


a backtest and taking its results with a “grain
of salt”, substitute the latter with some rigorous
quantitative testing and you have a much more
credible (or not) backtest on your hands.

7.6.3. Quantitative Trading


encourages thinking in terms of
portfolios.
Common behaviours among a large segment
of individual traders include deploying a single
trading strategy on a single asset, or deploying the
same strategy on a mix of assets, without careful
consideration given to weighted allocation as
would be the case in an institutional setting.

In the former’s case, the trader has put all his/her


eggs in one basket.

In the latter’s case, the trader has employed logic


that goes something like “if it’s a robust strategy, it
will perform well in all markets, under all market
conditions.”

Unfortunately, this logic is prone to leading traders


into intractable situations where the lack of an
appropriate risk-adjusted allocation matrix may
lead to unexplained underperformance.

Quantitative analysis allows traders to construct


portfolios of assets or strategies (or both), such
as to maximize their aggregate profitability while
minimizing their aggregate risk. This is simply not
possible to achieve without quantitative methods
being employed.

7.7. Disadvantages?
One of the most commonly voiced disadvantages

38 An Introduction To Serious Algorithmic Trading


of Quantitative Trading is that it’s a complex

darwinex.com
discipline to master.

So is learning to drive a car for the very first time.


The right instructor can be the difference between
a confident future driver and a jittery one.

Quantitative trading is no different.

Today there are several digital learning platforms


that greatly accelerate learning from the comfort
of your own home.

Complex disciplines such as quantitative


trading have been made considerably more
approachable and accessible to everyone
interested.

For example,
for less than the price of a daily cup of
coffee, people are now able to enroll
in full-fledged, instructor-led and
internationally accepted digital courses in
machine learning, artificial intelligence,
computational finance, risk management
and more.

The fruits of a Quant’s labour are far sweeter than


one can imagine. You will be well served by visiting
the Darwin Exchange Leaderboard for inspiration.

In the mood for more?


Traders on our community forum have put
together a great list of recommended books
for everyone from the novice enthusiast to the
experienced trader!

Here's the link.

An Introduction To Serious Algorithmic Trading 39


www.darwinex.com

Forex and CFDs are leveraged products. They may not be a suitable investment for you as they carry a high degree
of risk to your capital and you can lose more than your initial deposits. Please make sure you understand all the risks
involved. The Darwinex® trademark and the www.darwinex.com domain are owned by Tradeslide Trading Tech Limited,
a company duly authorised and regulated by the Financial Conduct Authority (FCA) in the United Kingdom with FRN
586466. Our Company number is 08061368 and our registered office is Acre House, 11-15 William Road, London NW1 3ER,
UK.

You might also like