An Introduction To Serious Algorithmic Trading
An Introduction To Serious Algorithmic Trading
An Introduction To Serious Algorithmic Trading
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?
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
6.6. Conclusions
An Introduction To
Serious Algorithmic
Trading
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.
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.
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..
For him, automated trading offered the opportunity to trade without any emotional
involvement, an opportunity that would enable him to:
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.
darwinex.com
Fast forward some time, he came up with what he thought was a great strategy using
technical analysis:
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”.
.. where tc refers to Closing time, and to refers to Opening time, of the price bar.
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.
After some time however, the strategy started losing more than it ever had in backtests.
... only to find a dramatic difference between the performance in backtesting vs live
trading.
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.
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?
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.
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.
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).
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.
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:
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.
This prevents such strategies from generalizing well to unseen data in future, to the
detriment of traders (launching them live with capital).
1. Model-focused – where the strategy fits historical data too closely, and exhibits high
variance when tested on unseen data.
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.
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.
1. Data Handling
2. Parameter Selection
3. Variable Factors
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:
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.
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.
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.
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.
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.
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).
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.
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.
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
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.
darwinex.com
minimum permissible lot size would
result in trade rejection.
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.
For example, sending 10 orders of 1 lot each is a more scalable approach than sending 1
order of 10 lots.
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.
darwinex.com
Evaluating Strategy
Performance
Several metrics exist for evaluating backtest and live trading performance.
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.
measure in finance that describes how well asset returns compensate investors for risks
undertaken.
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.
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.
darwinex.com
further information about the strategy’s actual distribution of 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.
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.
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.
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%.
Let’s now assume that the strategy does not trade for the next month.
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%
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.
darwinex.com
1. Trades very small movements,
2. Over small periods of time,
3. During low volatility hours,
4. ..but with HUGE leverage.
But the strategy will still have traded with very high leverage and hence very high 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:
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.
In summary,
Because the Sharpe Ratio depends on the underlying strategy’s risk, on its own it
cannot adequately judge a strategy’s performance.
• 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.
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.
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:
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.
darwinex.com
Simply put, Value-at-Risk is a percentile of loss, derived from:
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.
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.
Asset to trade:
EUR/USD
Direction:
Trading Logic:
Execute 1 trade per day, lasting exactly 1 hour, for 1 month, with randomly chosen directions
and starting times
Simulations:
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.
darwinex.com
6.5.1. Chart 1: Probability Density Distribution of Returns by
Leverage
Observations:
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.
After an initial, briefly slow rate of change, median return degradation accelerates to an
optimum, systematically worsening as leverage increases.
Observations:
Past a certain choice of leverage, VaR converges to -100%, ruling out high leverage as a
means nor solution to enhancing trading returns
Observations:
As the leverage increases, the probability of recovering a loss of median return in the next
month, systematically decreases.
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.
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.
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.
darwinex.com
transaction costs on strategy returns. impact by calculating:
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.
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.
darwinex.com
(e.g. mixture of assets or strategies deployed on the same account).
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.
1. A sharply dressed City trader working in a hedge fund, sat in front of his Bloomberg or
Thomson Reuters Eikon terminals, or
a doctorate in Nuclear Physics), sat in his basement trading room pouring over
mathematical models, following a staple fast food diet.
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.
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.
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.
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.
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.
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.
Backtesting
Quantitative traders can make use of statistical
tools that add another layer of validity to
backtests.
7.7. Disadvantages?
One of the most commonly voiced disadvantages
darwinex.com
discipline to master.
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.
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.