Categories
Forex Educational Library

Designing a Trading System (II) – The Toolbox Part 1

Introduction

The first issue of this series on trading systems finishes with a chart flow as in Fig 1, that sketches the steps needed for a good design of a trading system.

There are other ways around, of course, for example, from limited testing going directly to paper trading or small-sized trading, but this larger flow makes sure that any system that is profitable at the end of this pipe will be performing close to what’s expected.

Moreover, the designer and trader will be much more confident trading it and will be aware of what to expect in terms of returns, percent gainers, and drawdowns.

Of course, this a long process, taking months to a year, or even more to accomplish. That’s not an easy way of doing it, but, as Kevin Davey says “That’s how it’s supposed to be. Think about it for a second – if it were easy to find a strategy don’t you think others would have already found it and exploited it?”

Kevin Davey says that for him, strategy development is like a factory, a pipeline of ideas that get developed along the refining process until its output, as garbage or as a gold nut. Therefore, we need to keep our factory running all the time, filling it with new preliminary ideas.

That said, we have to build that factory, first. Throughout this article, we’ll deal with the factory building problem, therefore, we’re going to explore Fig 1 chart flow and find out what’s needed on each stage of the pipeline.

Trading ideas

Kevin Davey on his book(see reference below) gives a very good list of things to consider about idea gathering:

  • Keep an updated list of ideas. Whenever you jump on a trading idea or something that intrigues you, write it down on your list
  • Look for ideas: Ideas are around us in books, web pages, internet forums, and magazines.
  • Dumb ideas are those never tested. Absolutely everything might be a good idea. Even the craziest idea might end as a good system.
  • If you make a mistake while coding (or in your script if you aren’t coding), test it anyway. Penicillin discovery was accidental.
  • If your idea results in a bad system, try the opposite: switch buy and sell signals and see what happens. Although it doesn’t always work, it might.
  • Plan to test one to five strategies per week. With this amount of inputs, it may take six months, but you’ll end up with a pack of good trading systems.
  • Find other traders and offer to swap ideas and strategies with them. Take what others have and build strategies around those ideas.

Limited testing

Goal setting

Before going on into either a limited or an extended testing, we need to define the specific objectives our system must accomplish.

A list of the items might be:

Likes:

  • The possible markets the system is going to trade
  • The Time-frame or time-frames applicable
  • Minimum volatility to trade
  • Allowed time intervals
  • Purely automatic, Automatic entries and manual on exits or vice-versa.
  • Reward-to-risk desired interval (aiming for 1.5:1 at least is a good starting point)
  • Minimum percent of gainers we need to be comfortable with.
  • Minimum quality criteria for the system:
  • maximum allowed coefficient of variation
  • Minimum quality metrics (Sharpe, Sortino, SQN …)
  • Maximum drawdown length

Dislikes:

  • Trading on strength / Trading on weakness
  • More than X trades a day / less than X trades a day
  • More than 5 losers in a row
  • Targets too large / short

The idea

We take an idea from our list of ideas and we decide about how to develop the following details:

  • Testing and trading platform
  • Data needed
  • market direction
  • timeframe or bar size
  • trading intervals (optional)
  • Entry rules
    • Allow long and allow short signals
    • trigger long and short signals
  • Exit rules
    • Long and short stops
    • trail stops
    • volatility stops
    • Profit targets
  • Algorithm programming of decision chart flow in manual execution

Testing, optimizer, and trading platform

Developing a system by hand is possible, but it takes a lot of effort, and at least, it needs some kind of programming to perform the Monte Carlo testing, and fine-tune the position size to our particular needs and tastes.

Metatrader

Currently, the most popular system for the forex markets is Metatrader. It uses a C++ variant called MetaQuotes Language MQL. The latest version is MT-5, although MT-4 is still used by many forex brokers.

The following are the key features of the MQL-5 language:

  • C++ syntax
  • Operating speed close to that achievable with C++
  • Wide range of built-in features for creating technical indicators
  • Open-Cl support for fast parallel executions for optimization tasks without the need to write parallel code.
  • Wide variety of free code for indicators and strategies with a strong mlq4 and mlq5 community.

Python

Python is a terrific high-level programming language with tons of features and a huge number of libraries for anything you can imagine, from database to scientific, data science, statistics, and machine learning, from logistic regression to neural networks.

Python has a specific library to backtest and optimize MT4 libraries and expert advisors.

Python-Metatrader can also be bridged to Meta Trader using ZeroMQ free software distributed messaging. (http://zeromq.org/intro:read-the-manual)

https://www.youtube.com/watch?v=GGOajzvl860

Python enables the implementation of different kinds of strategies, compared to those developed by typical technical analysis, although there are technical analysis libraries in Python. As an example, Python makes it easy to develop statistically-based strategies, for example, stats-based pivot points.

The implementation of Monte Carlo permutation routines takes less than 4 lines of code, And Python has a lot of machine learning functionality, including parallel GPU-enabled neural network libraries, such as TensorFlow, a terrific deep learning neural platform by Google. https://www.tensorflow.org

There exists a large community of quants developing stats-based strategies in Python, and lots of free code to start from, so ZeroMQ is a serious alternative to direct design in MQL.

One of the most popular Python packages is the Anaconda distribution. Anaconda incorporates Jupyter Notebooks, an interactive Python web-based environment that allows development of Python apps as if it were a notebook (Fig. 3). Anaconda includes, also, an integrated software development IDE called Spyder (fig. 4)

Anaconda distribution may be found at https://www.anaconda.com/download/

Data

Forex historical data bars can be acquired for free from the broker for major pairs crosses, and exotic pairs, with more than 5 years of one minute, via MT’s the historical data center (F2). Starting from the minute bars, other timeframes can be recreated easily

Tick and sub-minute data aren’t available for free, so we should consider if the idea needs data resolutions beyond the minute.

Forex traders are fortunate in that their data is already continuous through time. Futures trading needs to combine several contracts on a continuous contract because futures contracts expire every three months.

The problem is that the expiring contract prices are slightly different from the starting prices of the new contract because future prices are affected by interest rates since its value is computed taking into account the cost of the money, and this cost varies as the distance to its expiration approaches.

If you need to build your own continuous contract, there are several ways. The simplest is just to put them side by side and let the gap appear, but, obviously, this is wrong, so the second easier way is to back-adjust the price in the old contracts, by subtracting the distance in points from the current contract’s starting price to the ending price of the latest expiring contract. That’s ok, but you may end up with negative prices if you are doing that a lot of times. The other problem with this method is that you lose the actual historical price values and, even worse, the original percent variations.

The best method, in my opinion, is to convert prices to ratios using the formula:

Price change = Closei – closei-1 / closei-1

Then go back to the first price of the current contract and perform the conversion back from there on the historical data series.

Data Cleansing

Historical data are rarely perfect. Spurious prices are more common than most think. Also, it may be desirable to get rid of price spikes that, although correct, are so unusual that they should be ignored by our system. Therefore, it may be desirable to include a data cleansing routine that takes away unwanted large spikes.

A way to do that is to check each bar in the price history and mark as erroneous any bar if the ratio of its close to the prior close is less than a specified fraction, or greater than the specified fraction’s reciprocal. All marked erroneous dates won’t be used to compute any variable, indicator or target, or they can be filled with the mean values of the two neighboring bars.

Data normalization

The common way to design a trading system does not involve any price normalization or adjustment, besides what is needed to create a continuous contract in the futures markets.

There are two kinds of technical studies, those whose actual value at one bar is of key importance in and of itself, and those whose importance is based on their current value relative to recent values. As examples of the first category, we may consider PSAR, ATR, Moving averages, linear regression, and pivot points.  Examples of the second category are stochastics, Williams %R, MACD, and RSI.

The reason to adjust the current value of an indicator to recent values is to force the maximum possible degree of stationarity on it. Stationarity is a very desirable statistical property that improves the predicting accuracy of a technical study.

There are two types of price adjustment: Centering and scaling. Centering subtracts the historical median from the indicator. Scaling divides the indicator by its interquartile range.

Centering

There are several ways to achieve centering. One of them is to apply a detrending filter to the historical price series. The other way is to subtract the median of some bar quantity, for example, 100 to 200 bars.

Scaling

Sometimes centering the variable may destroy important information, but, we may want to compensate for shifting volatility. It may happen that a volatility value that is  “large” in one period might be “medium” or even “short” in another period, thus, we may want to divide the value of the variable by a measure of its recent value range. The interquartile range is an ideal measure of variation because it’s not affected by outliers as a classical standard deviation would be.

The formula to do that is:

Scaled_value = 100 * CDF [ 0.25 * X/(P75 – P25) ] -50

Where CDF is the standard normal Cumulative Density Function

X is the unscaled current value

P75 and P25 are, respectively, the 75  and 25 percentile of the historical values of the indicator.

Spectral Dilation

John Ehlers coined the term “spectral dilation” to signal the effect of the fractal nature of the markets and its profound effect on almost all technical indicators.

The solution he proposes to get rid of this effect is a roofing filter.  A roofing filter is composed of a high pass filter that only lets frequency components whose periods are shorter than 48 bars pass in. The output of the roofing filter is passed through a smoother filter that passes components whose periods are longer than 10 bars.

Market direction

We must decide whether we are going to trade both directions indistinctly or if we should define a permission rule for every direction.

It seems straightforward the need to define a trend-following rule, but it’s not. Sometimes that rule doesn’t help to improve performance. On the contrary, it forbids perfectly valid entry signals and it hurts profits and other system metrics.

To assess the efficacy of a market trending signal, the right way is to test it after having tested the main entry signal. We should weight any possible improvement, but focused return on account, not merely in an increase in total profits.

Timeframe or bar size

Many traders don’t pay attention to this variable, although it plays an important role in the performance of a system. Timeframe length is really an important issue when developing a trading system because it will define a lot of things:

  • The length of the timeframe is inversely proportional to the number of trade opportunities available on a given period.
  • Length is proportional to the time it takes to close a trade.
  • It’s also connected to the mean achievable profit. An hourly bar would offer a channel width much wider than a 5-minute bar. Since trading costs are a fixed amount for every trade (spread + commissions) timeframe length is proportional to the ratio Rewards/costs.
  • It directly connects with risk. A longer timeframe needs longer stops, so we should lower our position size for the same monetary risk

The forex three basic categories are:

  • Midterm: from 2-hour bars to a couple of days. It may be used with swing trading or similar techniques.
  • Short term: From 15 minutes to 1-hour bars.
  • Very short term: from seconds to 15 min-bars.

Time frames of popular strategies:

  • Scalping: It’s a very short-term strategy. From seconds to a few minutes to complete a trade. It usually takes less than 5 bars to complete a trade.
  • Day trading: from very short-term to short-term. From minutes, up to 1-hour bars, although the most popular are 5, 10 and 15 minute-bars. It takes from 3-5 minutes up to hours to complete a trade. Open trades are closed before traders stop their trading session.
  • Range trading: This type of strategy doesn’t rely on a time frame but on a range breakout. So, its length depends on the range size. A small range takes less to be crossed through, while a large range may take 30 minutes or more. The most useful range will be that one that transition on average every 3-6 minutes.

It is not advisable to choose very short time frames. There, the market noise is very high and the timing for entries and exits is much more critical. The most critical parameter of a trading system is profitability with low variance. Profit objectives can be easily achieved using proper position size, but not over-trading in shorter time frames. Those very short time frames are only good for your broker.

General rules for entries

Once we have an entry rule we need translating it into a computer language. Computer languages are a formal and unequivocal description of a set of rules to do a task. If we need to test our idea in 5 years of one-minute data we surely will need an advanced trading platform such as MT5 or MT4 at least. But, even if we aren’t going to do that we still need to formalize our rules.

To do that a good solution is to implement our system in some pseudo code. This is, even, advisable as a first step before programming it to real code. Python users are very fortunate because Python code matches perfectly pseudo code. An example of pseudo code might be:

# Pseudo code for a simple 2-MA strategy using the slope instead of the crossover.

Inputs:

minRR = 1.5

PeriodLong = 15

PeriodSHort = 5

NN = 1

StopLong = MinLow(Low, 10) - 3 pips # it takes the low point of the last 10 bars - 3 pips

StopShort = MaxHi(High,10) + 3 pips # it takes the max of the last 5 bat
TargetLong = MaxHi(High, 20) -3 pips # Target is max of the last 20 bars – 3 pips
TagetShort = MinLow(Low, 10) + 3 pips # Target is the min of the last 10 bars + 3 pips
# Go long if both moving averages are pointing up

if Long_avg[0] > Long_avg[1]  and Short_avg[0] > Short_avg[1]:  
         GoLong = True
         GoSort = False
         RR = (TargetLong –Price) / (Price-StopLong)

# Go short if both moving averages are pointing down

if Long_avg[0] < Long_avg[1]  and Short_avg[0] < Short_avg[1]:  
         GoLong = False
         GoSort = True
         RR = (Price - TargetShort) / (StopShort - Price)

If Golong and RR>minRR:
         Buy NN contracts at Price, limit

If Goshort and RR >minRR:
         Sell Short NN contracts at Price Limit

# Stops and targets

If myposition > 0:
         Sell at StopLong
         Sell at TargetLong

If myposition < 0:
         Buy to cover at StopShort
         Buy at TargetShort

Bullet points for creating good entries:

  • KISS principle applies: The “Keep it simple stupid” principle devised by the US Navy. If you can’t explain it in simple terms you’ll have a problem while converting it to rules or code.
  • Limit the parameter number: The higher the number the higher the probability of overfitting. If you have just 3 entry parameters, then with exit and filter parameters you’ll end up with 8 to 10 that need to be optimized. Try to limit that to no more than two.
  • Think differently. For example, MA crossovers have been used extensively. If you want to try them, think on how to innovate using them, for example, using MA’s against MA crossover users by fading the signal as a kind of scalping and look what you get.
  • Use a single rule first, test it and continue adding another one and observe its effect in performance, so you’ll know if it works or is junk.

General comments on exits

Exits seem to be the poor relative in the trading family. Most people pay little attention to exits. They seem to believe that a good entry is all that matters. But I’ll tell you something: Exits can turn a lousy entry strategy into a decent or good system, and the contrary applies: it can turn a good entry strategy into a loser.

Stop loss define the risk: The distance from entry to stop-loss together with the size of our position are the variables needed to compute the monetary risk of a trade.

Profit target to entry define the reward, and with reward and risk, we can define our reward to risk ratio. This ratio and the average percent of winners is all we need to make a rational decision to pull or not to pull the trigger on a particular trade.

If for example, our system’s average percent winners are 50%, and the risk is two times bigger than the expected reward, it’s foolish to enter that trade. We’d require that the reward was at least a bit bigger than the risk to have an edge.

The usual exit methods are:

  • Technical-based stops: Stops below supports on long entries and above resistance on short entries
  • MAE Stops. Maximum adverse execution is a concept devised by John Sweeney. The main idea is: If our entries have an edge, then there will be a behavior on good trades different to bad trades. Then if we compute the sweet spot where the good trade almost never reaches, this is the right place to set our stop (In following articles, we will develop on this idea).
  • Break-even stops: At some point when the trade is giving profits, many traders move the stop loss to break-even. This is psychologically appealing, but it may hurt profits. Moving the stops to break-even should be based on statistically sound price levels that, not on gut feeling, and should be based upon its goodness, not to make us more comfortable.
  • Trail stops: As the trade develops in our favor, the stop-loss is raised/lowered to a new level. Trail stops may be linear or parabolic.
  • Profit Targets. Profit targets really are not stops. Exits on targets are usually accomplished using limit orders. As with the MAE stops, we should test the best placement for profits statistically instead of fixed money targets.

 

©Forex.Academy


References:

Building Winning Algorithmic Trading Systems, Kevin J. Davey

Computer Analysis of the Futures Markets, Charles LeBeau, George Lucas

Statistically sound Machine Learning for Algorithmic Trading of Financial Instruments, Aronson and Masters

 ©Forex.Academy
Categories
Forex Educational Library

Forex Designing a Trading System (I) – Introduction to Systematic Trading

Trade and money

Trade is a concept that began with the advent of the Homo Sapien, some 20K years back. People, back then, traded their spare hunting pieces for new arrows, spears, or something else he had no time to make himself because he was hunting mammoths.

So, the first questions about what a fair deal was, and, also, how to measure and count things, began.

Trade and agriculture were significant factors that drove the Cro-Magnon men from the caves into civilization.  Everyone started specializing in what they were good at, and people had time to think and test new ideas because they didn’t need to spend time hunting.

Trade brought accounting, the concept of numbers and, finally, mathematics. Ancient account methods were discovered more than 10k years ago by Sumerians in Mesopotamia (the place between two rivers). Also, Babylonians and Egyptians gave value to accounting and measuring the results of their work and trade activities.

Sumerians were the first civilization where agricultural surpluses were big enough that many people could be freed from agricultural work, so, new professions arose, such merchants, home builders, book-keepers, priests, and artists.

The oldest Sumerian writings were records of transactions between buyers and sellers. Money started being used in Mesopotamia as early as 5,000 B.C.  in the form of silver rings. Silver coins were used in Mesopotamia and Egypt as early as 2,600 B.C.

Accounting and money are interlinked. Money was (and still is) the standard way to define the value of products and services, accounting is the method to keep track of earnings, loses and costs and evaluate the use of resources and time.

Currency trading is nothing but a refinement of the concept when several types of currencies are available in an interlinked civilization. Currency trading is the way to search the fair value of currency in relation to other currencies. To traders, accounting is the way to measure the properties and value of their trading system.

Automated versus discretionary

There are several reasons why we’ll need an automated trading system. First of all, people always trade using a system, even when they think they don’t. People trade their beliefs about the market, so their system is their beliefs.

The problem with a system based on just beliefs is that, usually, greed and fear contaminate those beliefs, and, thus, the resulting system behaves like a random system with a handicap against the trader.

Facts

Economic information translates gradually to price changes. Future events aren’t instantly discounted on price.  This is the reason for the existence of trends.

Leverage produces instability in the markets because participants don’t have infinite resources to hold to a losing position. That’s one reason for the cyclic nature of all markets and their fat tail probabilistic density distribution of returns.

There is a segmentation of participants by their risk-taking potential, objectives, and time-frames

The market reacts to new strategies. A new strategy has diminishing returns as it spreads between market participants.

The Market forgets at a long timescale. The success rate of market participants is less than 10%, so there is a high turnover rate. A new generation of traders rarely learn the lessons of the previous generation.

There is no short-term link between price and value.

The market as a noisy structure

All these facts make the markets chaotic, with a fractal-like structure of price paths, a place with millions of traders trading their beliefs, but everyone with a different timeframe and expectations.

This is ok, as no trade is possible if all market participants have the same viewpoint, but the result of hundreds of thousands of beliefs and viewpoints is that the market is as noisy as a random coin flip, as we observe in Fig. 1, reproducing three paths with 200 coin-flip bets, that closely resembles the paths of a currency or futures market.

Contradictory strategies may be both profitable or both losers

On my article about trading using bands, profitable trading VII, I’ve described two totally opposed systems: one was a Donchian breakout system, and the other was The Turtle Soup plus one. Both made money and both traded opposite to each other. That shows that opposing beliefs produce profitable systems.  Trading is not a competition to decide who’s right and who’s wrong. Trading is a business, and the goal of a trading system is to make money, not being right. In fact, none of these two systems were right even 50% of the time, but both were profitable.

Some time ago I developed a mechanical system and was so wrong that I saw almost a continuous downward equity curve. Nice! I thought. This system is so bad that if I switched it from buy to sell and vice-versa it might result in a great system!

Wrong! The reverse system was almost equally bad. That’s the nature of the markets. Nothing is easy or straightforward.

Too much freedom is dangerous

The marketplace is an open environment where a trader can freely choose entry time, direction, the size of her trade and, finally when to exit. No barrier nor rule forces any constraint on a trader. Such freedom to act is the difference between trading and gambling, but it’s a burden to discretionary traders, especially new to this profession because they tend to fall victim to their biases.

The main biases that a novel trader suffers are two: The need to be right and the belief in the law of small numbers. These two biases combined are the main culprits for the trader’s bias to take profits short and let losses run. The need to be right is also the culprit for the trader’s inclination to prefer high-frequency of winners instead of high-expectancy systems. For more on this read my article Trading, a different perspective.

To counteract this unwanted behavior, we’d need to restrict the trader’s freedom by forcing strict entry and exit rules that guarantee a proper discipline and ensure the expected reward to risk, and, at the same time, avoiding emotionally driven trades.

Discretionary versus systematic

The probability of a discretionary trader to succeed is minimal, mainly because, without a set of rules to enter and to exit, the trader immediately fall into the trap of the law of small numbers and starts doubting his system, usually with a substantial loss, as a consequence of considerable leverage.

Most successful traders develop and improve a trading strategy using discipline and objectivity, but this cannot be qualified as a systematic system because its rules are based on their beliefs about the state of the market, their mental state, and other unquantifiable factors.

A systematic trading approach must, conceptually, include:

  • Entry and exit rules should be objective, reproducible, and solely based on their inputs.
  • The strategy must be applied with discipline, without emotional bias.

Basically, a systematic strategy is a model of the behavior of one or several markets. This model defines the decision-making process based on the inputs, without emotional or belief content.

Trading can only be successful using a systematic strategy. If a trader does not follow one, trades will not be executed consistently, and there will be the tendency to second-guess signals, take late entries and premature exits.

Personal adaptation

Developing our own trading system not only gives us confidence in its results, but we’d be able to adapt it to our personal preferences and temperament. We don’t like suits that don’t fit well. A system is like a suit. A perfectly good system for one trader might be impossible to trade for another trader. For instance, a trader might not be comfortable trading on strength while another one cannot trade on weakness. A trader may hate the small losses produced by tight stops, so he favors a system with wide stops. That’s why we need to adapt the system to fit us.

The need to measure and keep records

Measurements allow distinguishing good systems from bad ones. There are several parameters worth measuring, that, later, will be dealt with, but the final objective of measurements is to determine if the trading idea behind the system is worthwhile or useless.

Measurements allow finding where the trading system is weak and optimizing the inadequate parameter to a better value. For instance, we might observe that the system experiences sporadic large losses or that it faces too many whipsaws. Measurements allow searching the sweet spot where stops do its duty to protect our capital while preserving most of the profitable trades.

When the system is already trading live, we might use measurements to adapt it to the current conditions of the market, for example to an increase or decrease of volatility. Measurements help us detect when a system no longer performs as designed, by analyzing the current statistical performance against its original or reference.

Discretionary strategies allow measurements, as well, and, sometimes help to improve a particular parameter, as well, particularly stop-loss position, but, how can this help with entries or exits if they are discretionary or emotionally driven?

Measurements, finally, will help us assess proper risk management and position sizing, based on our objectives and the statistical properties of the developed system. This concept will be developed later on, but my previous article Trading, a different perspective, mentioned above, deals with this theme.

Information

In the discretionary trading style the forex information is categorized into the following areas:

  1. Macroeconomic
  2. Political
  3. Asset-class specific
  4. News driven
  5. Price and volume
  6. Order flow or liquidity driven

In the systematic trading style the information is taken from Price and volume, although lately, some systems are also using sentiment analysis, by scanning the social networks (especially Twitter) and news, with machine learning algorithms. Order flow is left to systems designed for institutional trading because retail forex participants don’t have this information.

In this context, systematic trading simplifies information gathering, by focusing mostly on price and its derivatives: averages and technical studies.

Key Features of a sound system

The essential features a system must accomplish are:

Profitability: The model is profitable in a diversified basket of markets and conditions. To guarantee its profitability over time, we should add another feature: Simplicity. Simple rules tend to be more robust than a large pack of rules. With the later, often, we get extremely good back-tested results but this outcome is the result of overfitting, therefore, future results tend to be dismal.

Quality: the model should show a statistical distribution of returns differentiated from a random system. There are several ways to measure this. The Sharpe Ratio and the Sortino Ratio are two of them. Basically, quality measures a ratio between returns and a measure of the variation of those returns, usually this variation is the standard deviation of returns.

Risk Management and position sizing algorithms: Models are executed using position sizing and risk management algorithms, adapted to the objectives and risk tastes of the trader.

It is desirable that the algorithm for entries and exits be separated from risk management and position sizing.

Sharpe Ratio (SR) and other measures of quality

Sharpe Ratio

Sharpe ratio is a measure of the quality of a system, and is the standard way for the computation of risk-adjusted returns.

SR =ER/ STD(R)

SR = Sharpe ratio

R = Annualized percent returns

ER = Excess returns = R – Risk-free rate

STD = Standard deviation

Sortino Ratio

The Sortino Ratio is a variation of the Sharpe Ratio that seeks to measure only the “bad” volatility. Thus, the divisor is STD(R-) where R- is linked solely to the negative returns. That way the index doesn’t punish possible large positive deviations common in trend following strategies.

Sortino Ratio = ER/SRD(R-)

Coefficient of variation(CV)

The coefficient of variation is the ratio of the standard deviation of the expected returns (E), which is the mean of returns, divided by E. It’s a measure of how smooth is the equity curve. The smaller the value, the smoother the equity curve.

CV = STD(E)/E

SQN

The inverse of CV multiplied by the square root of trades is another measure of the quality of the system. It’s a measure of how good it is in comparison with a random system.

SQ = √N x E/STD(E)

Another, very similar measure of quality comes from Van K. Tharp’s SQN:

SQN = 10*E/STD(E), if the number of samples is more than 100 and

SQN = SQ if the number of samples is less than 100.

The capped SQ value allows comparing performance when the number of trades differs between systems.

Calmar Ratio

CR = R% /Max Drawdown%

It typically measures the ratio over a three year period but can be used on any period, and it’s mental peace index. How stressing the system is, compared to its returns.

CR shrinks as position size grows, so it can be a measure of position oversize if it goes below 5

Defining the parts of the problem

To finally produce a good trading system we need to identify, first, the elements of the problem, and at each one of them find the best solutions available. There are many solutions. There’s no question of right or wrong. The optimal solution is the one that fit us best.

1.    Identify the tradable markets and its features

The forex market is composed of seven major pairs and 16 crosses. The trader should decide about the composition of his trading basket in a way to minimize the correlation of the currently opened trades.

Liquidity is an important factor too. Especially noteworthy is to detect and avoid the hours when liquidity is low, and define a minimum liquidity to accept a trade.

Volatility is also necessary information that needs to be quantified. Too much volatility on a system designed in less volatile conditions may fail miserably. Especial attention to stop placement if they don’t get automatically adjusted based on the current volatility or price ranges.

We should be careful with news-driven volatility. We must decide if we trade that kind of volatility or not, and, if yes, how to fit that into our system.

2.    Identifying the market condition

A trading signal works for a defined market condition. A trend following signal to buy does not work in sideways or down trending markets.

Identify regression to a mean. Usually, mean-reverting conditions happen in short timeframes, due to the spread of trading robots, liquidity availability, and profit taking. Regression to the mean markets merits special entry and exit methods, using some kind of bands or channels, for instance.

Identify oversold and overbought: it is crucial to detect overbought and oversold conditions to avoid trades when it’s too late to get in.

Finally, we must find a way to include all this information in the form of a set-up or trading filter.

3.    The concept and yourself

There are lots of ways to trade a market, but not every one of them will fit all traders. An example of a tough system for myself would be the Donchian breakout system. A robust and simple trend following system, but I know I wouldn’t be comfortable with 35% winners because I know that this means 12% chance of having a streak of 5 losers.

Therefore, to know what fits you, you should try to know yourself and your available time for trading.

Conceptually there are two kinds of players: Those who need frequent small gains and accepts sporadic substantial losses (premium sellers) and those who are willing to pay insurance for disasters, taking periodic small losses in search of large gains (premium buyers).

A premium seeker prefers a hit-and-run style of trading: scalping, mean-reverting, swing trading, support- resistance plays, and similar strategies. A premium seeker has a negatively skewed return distribution, as in Fig. 2:

A premium buyer is a trend follower, an early loss taker, a lottery ticket buyer, a scientist experimenting in search of the cancer cure. He is willing to be wrong several times in a row, looking for the big success: When he finds a trend, he jumps on it with no stops. If proven right, he adds to the position, pyramiding, as soon as new profits allow it. A premium buyer has a positively skewed return distribution, as in Fig. 3:

We notice that the most psychologically pleasing style comes from premium-sellers. The problem with this trading style is that it is not insured for the black-swan-type of risks. He is the risk insurer!

The premium buyer is like a businessperson. A person who is willing to assume the cost of the business for a proper reward. His problem is that he must endure continuous streaks of small loses.

An early loss taker is against the crowd instinct to take profits early, a habit with a negative expectancy, so it pays extra returns to be contrarian.

There is a mixed style where reward to risk is analyzed and optimized. It uses stop protection, while the percentage of winners is enhanced using profit targets.

The main concept for a sound system, in my opinion, is to make sure our distribution of returns has a positive skew. That can be accomplished if we make sure our system has a mean reward-to-risk ratio higher than 1:1, preferably greater than 2:1 but never less than 1:1, and by setting profit targets in tune with the market movements – placing them near resistance or support, and using trail stops.

4.    The law of active management

The Fundamental Law of Active Management was developed by Grinold and Kahn to measure the value of active management, expressed by the information ratio, using only two variables: Manager skill IC and the number of independent investment opportunities N.

IR = IC x √N

If two managers have the same investment skills but one has a more dynamic management, meaning N is higher than the other manager, its return will outperform the less dynamic manager.

This formula can be used in trading strategies, as well:  On two equally smart strategies, the one with more frequent trading will outperform the other. There’s a limitation, though, that this formula doesn’t address: The cost of doing business is more significant the higher the frequency of trading.

5.    Timeframes: Fast vs Slow

The trader’s available daily time has to be considered too. Does the trader have time to be on the screen the whole day or are they busy during work hours, hence, only able to dedicate just a couple of hours at noon to trading.

Unless the trader is willing to use a fully automated system, the time available to them dictates the possible timeframes. A trader who’s busy all day cannot trade signals that show on 1-hour timeframes but is instead forced to focus on swing trading signals that can be analyzed at noon. A full-time trader has all available time-frames at their disposal.

Well summarize here the classification made by Robert Carver on his book Systematic Trading:

·      Very Slow (average holding period: months)

Very slow systems tend to behave like buy and hold portfolios. Trading rules tend to include mean reversion to very long equilibrium such as relative value equity portfolios, that buy on weakness and sell on strength.

As a result of the law of active management, returns from dynamic trading diminish, the lower the trading frequency. Therefore, at large holding periods, the return tends to be poor, unless the skill at timing the market were top notch.

·      Medium (average period hours to days)

The law of active management gives us a clue that this timeframe gets more attractive results than a longer time-frame.

It’s adequate to part-time traders that can do swing trading, working in the evening, searching for signals to be used in hourly and daily charts. From a Forex perspective, these medium-speed timeframes are less crowded with traders. Therefore, strategies may work better than shorter timeframes.

·      Fast( from microseconds to one day)

The Sharpe ratios could be very high in these timeframes, an important portion of the raw returns ought to be spent on costs (commissions and spreads).

6.    Risk

Risk is a broad concept. There are several kinds of risk. The first type of risk is the trade risk. The risk you assume on a particular trade. That’s the monetary distance from entry to your stop loss multiplied by the number of contracts bought or sold.  It’s easy to assess and measure.

The second type of risk is the Potential drawdown a system may experience. This kind of risk is dependent on position size and the percent losers of the system, therefore, we can estimate it with certain accuracy.

Risk can be defined as the variability of results. It’s a statistical value that measures the mean value of the distance between results, and the mean of results and is called standard deviation. The point is that market volatility is shifting and, further, it’s different from asset to asset.

If a trader has a basket of tradable markets, there might happen that one asset is responsible for 60% of the overall volatility on her portfolio, because the position size in that particular asset is higher, compared with others or because its volatility is much higher.

The best way to reduce the overall risk is through diversification and volatility standardization.

Diversification:

To reduce overall risk, there is just one solution: To trade a basket of uncorrelated markets and systems, with risk-adjusted position sizing, so no single market holds a significant portion of the total risk.

Below is the equation of the risk of an n-asset portfolio when there’s no correlation:

𝜎 =√ ( w1 𝜎12 + w2 𝜎22 + … + wn 𝜎n2)

where 𝜎I is the risk on an asset, and wi is the weight of that asset on the basket.

Let’s assume that we hold a basket of equal risk-adjusted positions in 5 uncorrelated markets with a total risk of $10. Therefore, we have a $2 risk exposure on each market, and the correlated total risk would have been 10. But, if the assets were totally uncorrelated, the expected combined risk would be computed using the above equation, then:

Risk =√ (5 x 22) = √20 = 4.47

So, for the same total market exposure, we have lowered our risk by more than half.

Volatility standardisation

Volatility standardization is a powerful idea: It is the adjustment of the position size of different assets, so they have the same expected risk.

This allows having a balanced portfolio where each component has a similar risk. It means, also, that the same trading rule can be applied to different markets if applied with the same standardized risk.

Leverage

The forex industry is attractive for its huge amount of allowed leverage. A trader is allowed to control up to one million euros with a modest 10,000 euro account. That is heaven and hell at the same time. If a trader doesn’t know how to control her risk, he’s surely overtrading.

I recommend reading my small article on position size, but for the sake of clarity, let’s do an exercise.

Let’s compute the maximum dollar risk on a $10,000 account and a maximum tolerable drawdown of 20%, assuming we wanted to withstand 8 consecutive losses.

According to this, we will assume a streak of eight consecutive losses, or 8R.

20% x $10,000 = $2,000 this is our maximum allowed drawdown, and will be distributed over 8 trades, so:

8R = $2,000 = $250, therefore:

Our maximum allowed risk on any trade would be $250 or 2.5% of our running account.

By the way, that value is a bit high. I’d recommend new traders starting with no more than 0.5% risk while beginning with a new system. It’s better to pay less while learning.

The second part of the equation is to compute how many contracts to buy on a particular entry signal.

The risk on one unit is a direct calculation of the difference in points, ticks, pips or cents from entry point to the stop loss multiplied by the minimum allowed lot.

 

Consider, for example, the risk of a micro-lot of the EUR/USD pair in the following short entry:

 

Size of a micro-lot: 1,000 units

           Entry point: 1.19344

              Stop loss: 1.19621

 

We see that the distance from entry to stop loss is 0.00277

Then, the monetary risk for one micro-lot: 0.00277 * 1,000 = € 2.77
Therefore, the proper position size is €250 /€2.77= 90 micro-lots, or 9 mini-lots

Using this concept, we can standardize our position size according to individual risk. For instance, if the unit risk in the previous example were $5 instead, the position size would be:

PS = €250/5 => 50 micro-lots.

That way risk is constant and independent of the distance from entry to stop.

Finally, it’s better to use a percentage of the running capital instead of a fixed euro amount, because, that way, our risk is constantly adapted to our current portfolio size.

7.    The profitability rule

A trading system is profitable over a period if the amount won is higher than the amount lost:

∑Won -∑Lost >0    (1)

The average winning trade is the sum won divided by the number of winning traders Nw.

W =∑Won / N (2)

The average losing trade is then:

L =∑Lost / NL,  (3)  where NL is the number of losing trades.

Thus, equation (1) becomes:

WNwLNL, > 0    (4)

The number of losing trades is the total number of trades minus the winning trades:

NL = N – Nw     (5)

Therefore, substituting (5) and dividing by N, equation (4) becomes:

WNw / N – L(N-Nw) / N > 0     (6)

If we define P = Nw / N,  then (N-Nw) / N = 1-P,  and  (6) becomes:

WP– L(1-P) > 0   ->    W/L x P – (1-P) > 0    (7)

Finally, if we define a Reward to risk ratio as Rwr = W/L  Then we get

P > 1 / (1+ Rwr)     (8)

Equation 8 is the formula that tells the trader the minimum percent winners required on a system to be profitable if its mean reward to risk ratio is Rwr.

Of course, we could solve the problem of the minimum Rwr required on a system with percent winners greater than P.

Rwr  > (1-P)/P     (9)

8.    Parts of a trading system

In upcoming articles, we’ll be discussing all parts of a trading system extensively. Here we are just sketching a skeleton on which to build a successful system.

A trading system is composed of at least of a rule to enter the market and a rule to exit, but it may include the following:

  • A setup rule: A rule defines under which conditions a trade is allowed, for example, a trend following rule.
  • A filter rule: A filter to forbid entries under certain conditions, for example, when there is low volume, or high volatility, the overbought or oversold conditions are reached.
  • An entry rule, defined with price action, moving averages, MACD, Bollinger Bands and so on.
  • A stop-loss, to limit losses in case the trade goes wrong. Optionally a trailing stop.
  • A profit target: Profit target may be monetary, percent, based on supports or resistances, on the touch of a moving average or any other.
  • A position sizing rule. As mentioned before, it should make sure the risk is evenly and correctly set.
  • Optionally, A re-entry rule. The rule decides a re-entry if the stopped trade turns again on the original trade direction.

9.    Chart flow of the development of a trading system

In the next chapters of this series, we will develop on every aspect sketched in this introductory article.

 

 


References:

Professional Automated Trading, Eugene A. Durenard

Systematic Trading, Robert Carver

Profitability and Systematic Trading, Michael Harris

Computer Analysis of the Futures Markets, Charles LeBeau, George Lucas

Building Winning Algorithmic Trading Systems, Kevin J. Davey