Categories
Forex Daily Topic Forex System Design

Trading System design -Creating Your Strategy with Tradingview’s Pine Script – Part 2

In part 1 of this article series, we have created the Stochastic RSI indicator as part of our idea for a scalping strategy. Now that we have it functional, we will make the bull/bear phases and visually inspect whether it captures the turning market’s turning points.

Possible ways to create bull/bear slices

Our Stochastic RSI consists of two lines, k and d, and two trigger lines, ob and os. Therefore we can use multiple variants that may allow the creation of bull/bear price legs. Let’s consider the following 3

Variant 1 – The transition occurs at the SRSI entrance of the oversold or overbought regions.

Bull: The d-line crosses under the ob-line, which indicates it is into the overbought area
Bear: The d-line crosses over the os-line, indicating d‘s entry into the oversold area.

Code:
if crossunder (d, os)
    SRSI_Long := true
    SRSI_Short := false
else if crossover (d, ob)
    SRSI_Long := false
    SRSI_Short := true 
else
    SRSI_Long := SRSI_Long[1]
    SRSI_Short := SRSI_Short[1]

This code creates a condition SRSI_Long at the cross of d under os, which holds until d crosses over ob and reverses it, creating an SRSI_Short state. This condition is only modified by d crossing under os.
The else statement ensures the condition does not change from the previous bar.

Once we have defined the bull and bear segments, we can color-shade them to visualize them in the chart. To do it, we will use the bgcolor() function.

bgcolor(SRSI_Long ? color.green: na)
bgcolor(SRSI_Short ? color.red: na)

The first statement asks the condition of SRI-Long ( the ? sign). If true, the background color changes to green. Otherwise, no change (an). The second statement behaves similarly for SRSI_Short.

Let’s see how this piece of code behaves in the BTCUSD chart.

Variant 1 triggers the transitions too early. We see that on many occasions when the stochastic RSI enters the overbought or oversold region, it is more a signal of trend strength than a turning point.


Variant 2 – The transition occurs at D and K’s crossovers if in the overbought/oversold regions.

Bull: the k-line crosses over the d-line, if below os ( inside the oversold region. We ignore crosses in the mid-area)
Bear: the k-line crosses under the d-line, if above ob ( in the overbought area. We ignore crosses in the mid-area)

Code:

// creating the long and short conditions for case 2

if crossover(k,d) and d < os
     SRSI_Long := true
     SRSI_Short := false

else if crossunder(k,d) and d > ob
     SRSI_Long := false
     SRSI_Short := true
else
     SRSI_Long := SRSI_Long[1]
     SRSI_Short := SRSI_Short[1]
// bacground color change
bgcolor(SRSI_Long ? color.green: na) 
bgcolor(SRSI_Short ? color.red: na)

The last section for the background change is similar to Variant 1.

Let’s see how it behaves in the chart.

Variant 2 is an improvement. We see that the bull and bear phases match the actual movements of the market, although entries are still a bit early, and in some cases, it missed the right direction. It can be useful as a trigger signal, provided we can filter out the faulty signals.


Variant 3 – the transition occurs when d moved to the overbought or oversold region and, later, crosses to the mid-area.

Bull: the d-line crosses over the os-line
Bear: the d-line crosses under the ob-line.

if crossover (d, os)
    SRSI_Long := true
    SRSI_Short := false
else if crossunder (d, ob)
    SRSI_Long := false
    SRSI_Short := true 
else
    SRSI_Long := SRSI_Long[1]
    SRSI_Short := SRSI_Short[1]
// bacground color change
bgcolor(SRSI_Long ? color.green: na) 
bgcolor(SRSI_Short ? color.red: na)

 

And this is how it behaves in the chart.


Variant 3 lags the turning points slightly, but this quality makes it more robust, as, on most occasions, it’s right about the market direction. This signal, combined with the right take-profit, may create a high-probability trade strategy.

Let’s try this one. But this will be resolved in our next and last article of this series.

Stay tuned!

Categories
Forex Daily Topic Forex Risk Management

How Be Sure your Trading Strategy is a Winner?

To evaluate, the quality of a strategy is an old quest, and its answer has to do with gambling theory, although it can apply to any process in which the probability of profits is less than 100%. Of course, the first measure to know if our system is winning is when the current portfolio balance is higher than in its initial state. But that does not give very much information.

A better way might be to record winners and losers, and have a count of both so that we could apply some stats. It would be interesting to know the percentage of winners we get and how much is won on average. That also applies to losers.

We could try to find out if our results are independent of each other or they are dependent.

Finally, we could devise a way to obtain its Mathematical expectancy, which would show how profitable the strategy is.

Outcomes and probability statements

No trader is able to know in advance the result of the next trade. However, we could estimate the probability of it to be positive.

A probability statement is a figure between zero and one specifying the odds of the event to happen. In simple terms,

Probability = odds+ / ( odds+  +  odds – )

On a fair coin toss game: odds of heads (against, to one) = 1:1

probability Fair coin toss = 1/(1+1)

= 0.5

Probability of getting a Six on a dice:

odds = 5:1 – five against to one

Probability of a Six = 1/( 1+5) = 0.16666

We can also convert the probability into odds (against, to one) of occurring:

Odds = (1/ Probability) -1

As an example, let’s take the coin-toss game:

Odds of a head = 1/0.5 -1 = 2-1 =1:1

That is very handy. Suppose you have a system on which the probability of a winner is 66 percent. What are the odds of a loser?

System winners= 0.66 so -> System losers = 0.34

loser odds = 1/0.34 – 1 = 2 -> about 2:1.

That means, on average, there is one loser for every two winners, which means one loser every three trades.

Independent vs. Dependent processes

There are two categories of random processes: Independent and dependent.

A process is independent when the outcome of the previous events do not condition the odds of the coming one. For example, a coin toss or a dice throwing are independent processes. The result of the next event does not depend on previous outcomes.

A dependent process is one where the next outcome’s probability is affected by prior events. For example, Blackjack is a dependent process, because when cards are played, the rest of the deck his modified, so it modifies the odds of the next card being taken out.

This seems a tedious matter, but it has a lot of implications for trading. Bear with me.

What if we acknowledge our trades are independent from each other?

If we consider that our trades are independent, then we should be aware that the previous results do not affect the next trade, since there is no influence between each trade.

What if we know our system shows dependency?

If we know that our system’s results are dependent, we could make decisions on the position size directed to improve its profitability.

As an example, let’s suppose there is a very high probability that our system gets a winner after a loser, and also a loser after a winner. Then we could increase our trade size every time we get a loser, and, also, reduce or just paper-trade after a win.

Proving there is dependency on a strategy or system is very difficult to achieve. The best course of action is to assume there is none.

Assuming there is no dependency, then it is not right to modify the trade size after a loser such as martingale systems do since there is no way to know when the losing streak will end. Also, there is no use in trading different sizes after a winning or losing trade. We must split the decision-making process from trade-size decisions.

Mathematical expectancy

The mathematical expectancy is also known as the player’s edge. For events that have a unique outcome

ME = (1+A)*P-1

where P is the probability of winning, and A is the amount won.

If there are several amounts and probabilities then

ME = Sum ( Pi * Ai)

The last formula is suitable to be applied to analytical software or spreadsheet, but for an approximation of what a system can deliver, the first basic formula will be ok. Simply set

A = average profit and

P = percent winners.

As an example, let’s compute the mathematical expectancy of a system that produces 40% winners and wins 2x its risk.

ME = (1+2)*0.4 -1

ME = 3*0.4 -1

ME = 0.2

That means the system can produce 20 cents for every dollar risked on average on every trade.

Setting Profit Goals and Risk

Using this information, we can set profit goals. For instance, if we know the strategy delivers a mean of 3 trades every day – 60 monthly trades- The trader can expect, on average, to earn (60 * 0.2)R, or  12* R, being R his average risk.

If the trader set a goal of earning $6,000 monthly he can compute R easily

12*R = $6,000

R= $6000/12 = $500.

That means if the trader wants a monthly average of $6,000, he should risk $500 on every trade.

Final Words

On this article, we have seen the power of simple math statements, used to help us define the basic properties of our trading system, and then use these properties to assess the potential profitability of the strategy and, finally, create a simple plan with monthly dollar goals and its associated trade risk.

 

Categories
Forex Educational Library

Risk, Reward, and Profitability

The Nature of Risk and Opportunity

Trading literature is filled with vast amounts of information about market knowledge: fundamentals, Central Banks, events, economic developments and technical analysis. This information is believed necessary to provide the trader with the right information to improve their trading decisions.

On the other hand, the trader believes that success is linked to that knowledge and that a trade is good because the right piece of knowledge has been used, and a bad trade was wrong because the trader made a mistake or didn’t accurately analyse the trading set-up.

The focus in this kind of information leads most traders to think that entries are the most significant aspect of the trading profession, and they use most of their time to get “correct” entries. The other consequence is that novice traders prefer systems with high percent winners over other systems, without more in-depth analysis about other aspects.

The reality is that the market is characterized by its randomness; and that trading, as opposed to gambling, is not a closed game. Trading is open in its entry, length, and exit, which gives room for uncountable ways to define its rules. Therefore, the trader’s final equity is a combination of the probability of a positive outcome – frequency of success- and the outcome’s pay-off, or magnitude.

This latest variable, the reward-to-risk ratio of a system, technically called “the pay-off” but commonly called risk-reward ratio, is only marginally discussed in many trading books, but it deserves a closer in-depth study because it’s critical for the ultimate profitability of any trading system.

To help you see what I mean, Figure 1 shows a game with 10% percent winners that is highly profitable, because it holds a 20:1 risk-reward ratio.

A losing game is also possible to achieve with 90% winners:

So, as we see, just the percentage winners tell us nothing about a trading strategy. We need to specify both parameters to assess the ultimate behaviour of a system.

The equation of profitability

Let’s call Rr the mean risk-reward of a system.  If we call W the average winning trade and L the average losing trade then Rr is computed as follows:

Rr = W/L

If we call minimum P the percent winners needed to achieve profitability, then the equation that defines if a system is profitable in relation to a determined reward-risk ratio Rr is:

P > 1 / (1 +Rr) (1)

Starting from equation (1) we can also get the equation that defines the reward-risk needed to achieve profitability if we define percent winners P:

Rr > (1-P) / P (2)

If we use one of these formulas on a spreadsheet we will get a table like this one:

When we look at this table, we can see that, if the reward is 0.5, a trader would need two out of three winning trades just to break-even, while they would require only one winner every three trades in the case of a 2:1 payoff, and just one winner every four trades if the mean reward is three times its risk.

The lessons learned from analysing these equations are:

Let’s call nxR the opportunity of a trade, where R is the risk and n is the multiplier of R that defines the opportunity. Then we can observe that:

  1. If you spot an nxR opportunity, you could fail, on average, n-1 times and still be profitable.
  2. A higher nxR protects your account against a drop in the percent of gainers
  3. You don’t need to predict the price to make money because you can be profitable with 10% winners or less.
  4. As a corollary to 3, the real money comes from exits, not entries.
  5. The search for higher R-multiples with decent winning chances is the primary goal when designing a trading system.

A high Rr ratio is a kind of protection against a potential decline in the percentage of winning trades. Therefore, we should make sure our strategies acquire this kind of protection. Finally, we must avoid Rr’s under 1.0, since it requires higher than 50% winners, and that’s not easy to attain when we combine the usual entries with stop-loss protection.

One key idea by Dr. Van K. Tharp is the concept of the low-risk idea. As in business, in trading, a low-risk idea is a good opportunity with moderate cost and high reward, with a reasonable probability to succeed. By using this concept, we get rid of one of the main troubles of a trader: the belief that we need to predict the market to be successful.

As we stated in point 3 of lessons learned: we don’t need to predict. You’ll be perfectly well served with 20% winners if your risk reward is high enough. We just need to use our time to find low-risk opportunities with the proper risk-reward.

We can find a low-risk opportunity, just by price location as in figure 3. Here we employ of a triple bottom, inferred by three dojis, as a fair chance of a possible price turn, and we define our entry above the high of the latest doji, to let the market confirm our trade.  Rr is 3.71 from entry to target, so we need just one out of four similar opportunities for our strategy to be profitable.

Finally, we should use Rr as a way to filter out the trades of a system that don’t meet our criteria of what a low-risk trade is.

If, for instance, you’re using a moving average crossover as your trading strategy, by just filtering out the low profitable trades you will stop trading when price enters choppy channels.

Conclusions:

  • Risk-reward is the parameter that allows the assessment of the opportunity value of a trade.
  • The higher the opportunity, the less the frequency of winners we need to be profitable.
  • Therefore, we can assess an opportunity just by its intrinsic value, regardless of other factors.
  • That frees us from seeking accurate entries and set the focus on trade setup and follow-up.
  • We just need to use the familiar market concepts, for instance, support and resistance, to design a robust trading system, by filtering out all trades that don’t comply with the risk-reward figure.
  • Trading becomes the search for low-risk opportunities, instead of trying to forecast the market.

Appendix:

Example of Rr Calculation:

As we observe in Fig 3, the risk is defined by the distance between the entry price and the stop loss level, and the reward is the distance between the projected target level defined by the distance from the Take profit level to the entry price:

Risk = Entry price– Stop loss

Reward = Take profit – Entry price.

Rr = Reward / Risk

In this case,

Entry price  = 1.19355

Stop loss = 1.19259

Take profit = 1.19712

Therefore,

Risk = 1.19355 -1.19259 = 0.00096

Reward = 1.19712 – 1.19355 = 0.00357

Rr = 0.00357 / 0.00096

Rr = 3.7187

©Forex.Academy

Categories
Forex Educational Library

Designing a Trading System (III) – The Toolbox Part 2

Trading simulator

The trading simulator is the part of the trading platform that takes the programmed rules of a trading system and computes the simulated paper-trades along a time interval. The reason for its existence is the speed, efficiency, and accuracy with which it can perform thousands of computations.

Forex traders usually get MT4 for free when opening a trading account, and MT4 includes what it calls a “strategy tester” which is accessible by clicking a magnifying glass button or Ctrl-R.

Simulator outputs

All trading simulators generate outputs containing a considerable amount of information about the performance of the system on a particular market. Data about gross profit, net profit, maximum drawdown, percent winners, mean and max profit and loss, mean reward to risk, return on account, etc. should be present in a general report.

In well-designed simulators, the output is presented as a several page report, with text and graphics depicting relevant information of the system.

Summary reports

An example of a summary report of the MT4 Strategy Tester is shown in Fig. 1.

MetaTrader has a summary report of average quality, but since it’s widely used let’s discuss its main components. This summary report shows the minimum needed to realize if our strategy is worthwhile or junk. The main parameters are shown below.

  • Initial deposit: The amount of initial paper money account. It only matters as the initial reference for testing.
  • Total net profit: The difference between “Gross profit” and “Gross loss”
  • Gross profit: The sum of all profits on profitable trades
  • Gross loss: the sum of all losses on unprofitable trades
  • Profit factor: The percent ratio between the gross profit and the gross loss: A fundamental metric.
  • Expected payoff: The monetary expectancy of the system. The average monetary value of a trade. One of the most important values. It should be greater than zero for a positive expectancy.
  • Absolute drawdown: The largest loss that is below the initial deposit amount.
  • Maximal drawdown: The largest drawdown took from a local maximum of the balance. It’s essential, because an otherwise good strategy might be useless if it has several 50%+ drawdowns. You need to set your mark on the maximum allowed level you’re able to tolerate.

Even if the system’s results are below your mark, it might be optimum to perform Monte Carlo permutations (preferably more than 10.000) to study the probability of those drawdowns closely. Please be aware also that this value changes with position size, so it will increase as you increase your position and, consequently your risk.

  • Relative drawdown: The maximum percent loss relative to the account balance at the local maximum. The same as above but percentwise.
  • Total trades: Total number of trades. It is important to get at least 100 trades to get a statistically good approximation. This value is of interest, also, when comparing systems. When multiplied by the expected payoff it should return the total net profit value.
  • Short positions (won %): The number and percent profitable on short positions. It shows how good the system is in short positions. You should watch if there is an asymmetry when compared to long positions and analyze why this might occur.
  • Long positions (won %): The number and percent winners on long positions.
  • Profit trades (% of total): The total number and percent of profitable trades. Although technically it’s not that important to get high values on this parameter, as long as there is a good profit factor, this value psychologically is important. For example, many trend-following systems have no more than 35% winners. Therefore, you should decide if you’re able to accept just one winner out of three or you’re more comfortable with higher values at the expense of less reward-to-risk ratios on trades.

Percent profits allow us to compute the probability of a winning streak, and, when multiplied by the average profit it shows the likelihood of a monetary streak.

The probability of a winning streak of length n  is  the %Profit to the power of n:

Probability of an n-Winning Streak:

PWn  = %Profit n

Then, the expected profit on an n-streak of winners is

Expected profit_S on = PWn * average profit trade.

Below the probability curve of an n run-up streak in a system with 58% profits.

 

  • Loss trades (% of total): The number and percent of unprofitable trades. This value is computed subtracting 1- %Profits. Besides the psychological effect on traders, it allows us to directly compute the probability of a losing streak in the same way as in the winner streak case

Probability of an n-Losing streak 

PLn = %Lossn

We should remember that

%Loss = 1 – %Gain

Therefore, the probability of a loss on a 45% gainers system is 55%.

And the expected loss on that streak will be:

                        Expected loss_S = PLn * Average loss trade

Below the distribution on a system with 48% losers

 

Thus, using these two metrics, %Profit and %Loss, we can build a distribution curve of run-ups and drawdowns, and build a graph, as an alternative to a full Monte Carlo simulation, so we could get a more in-depth insight of what to expect of the system in term of run-ups and drawdowns.

Above, the distributions of run-ups and drawdowns of the same system, normalized to the risk taken called R, that matches the average loss.

These figures were performed by a simple algorithmic computation using Python and it’s plotting library matplotlib.

Let’s say that, in this case, our average loss is 500 €. And we are using a system on which, according to fig 2d, there is a 2.5% likelihood of hitting a 5xR drawdown. Therefore, if we have a 10,000 € account, there is a 2,5% chance that it will reach a 2500 € loss, or 25% drawdown. If we increase our risk to 1,000 € per trade, we’ll end up with 50% drawdown. Moreover, if we don’t like a 25% max drawdown, we should reduce the size of our trades to set the risk according to the max drawdown we are willing to accept.

As we see, this kind of analysis is much richer than a mere maximum drawdown measure, because we know the actual probability of a drawdown length and its monetary size, and is linked to the maximum allowed risk.

  • Largest profit trade: The largest profitable trade, we need to analyze large trades and evaluate if they are accidental outliers. We should evaluate their contribution to the profit curve. If for example, our profit balance is due to a small number of random outliers, and the rest of the profitable trades are just scratching the break-even mark we should be cautious about the future profitability of the system.
  • Largest loss trade: The largest losing trade. Largest losing trades give us hints about the positioning of our stop-loss levels. If we get sporadic but large losses, we need to check if we have bad historical data or if we suffer from gaps or spikes that needed to be corrected.
  • Average profit trade: The result of dividing the total profit by the number of profitable trades. An important metric to be used in conjunction with the statistical method described above.
  • Average loss trade: The result of dividing the total loss by the Nr. of losing trades. The average loss is a description of out mean risk per trade. We must be aware of the implications of that figure. We need to be prepared for more than 5 consecutive losses, and its associated risk, as previously discussed.
  • Maximum consecutive wins (profit expressed as money): the longest streak of profitable trades and its total profit.
  • Maximum consecutive losses (loss expressed as money): The longest streak of unprofitable trades and its total loss. As on the previous point, these values are better analyzed using the statistical method described above.

Other convenient parameters might have been handy (but not included):

  • Standard deviation of the expected payoff: This value is only computable by exporting the results list and performing the computation on a spreadsheet or Python notebook.
  • Win/loss ratio: The ratio of the mean winner to the mean loser, easily computed since those two values are shown.
  • Sharpe ratio or similar quality metric: Computed from Expected payoff and its Standard deviation.
  • T-statistics, that may help determine if the system has some statistical validity or it’s close to a zero-mean random system. It’s also important to know if the system delivers positive or negative skewed results.

These statistics can be obtained by saving the strategy report and using Excel to compute them, or using Python. In both cases, we need to build a small script that takes a bit of effort the first time but will reward us with a continuous stream of subtle details that no simulator shows.

The optimization procedure on the MT4 strategy tester uses a complete back-tested approach. There is no out of sample testing, so to avoid false expectations, it might be good to perform the optimization procedure using only a chunk of the historical data available and, after optimizing, running the optimized system in another chunk of the data series to get out of sample results.

Graph

This tab shows the equity balance graphic on a back-test (in this case a free EA: Headstrong Free, after optimizing it from 2011 to 2013). Below, the optimized EA behavior in out-of-sample data. Close to a random system.

 

 

Trade by trade reports

The “results” tab shows a trade by trade report organized by date and time. That report presents the time, type, size, price profit and balance of every operation. Of course, open actions don’t show a profit.

By right-clicking on any part of the report, you can save it on file for future use or further analysis using Excel or Python.

Data burnt

One major issue with data testing is that if we test on all the data available we “burn” it. That means that any posterior retest will be a bit more curve-fit. The issue is that while we go from a fair system to a great one, we introduce a small change here and there, and when back-testing that variation, we select the best performers and discard others. As the number of back-tests increases using the same data, a hidden curve fitting is emerging.

Theoretically one should perform a test using a data set and, after a change on a parameter, make a new test using another set, but, in practice, we end up testing all our strategy variants on a single dataset. That’s the reason we need to be cautious.

So, what’s the best way to use our data so it won’t burn too much? Kevin J. Davey says he uses just a portion of all the data he has, large enough to get statistically valid results. He also says he makes a random selection of the portion to be used. That way he uses new data after a change in parameters and makes sure his historical database is used minimally.

In the next issue, we’ll deal with entry evaluation. Let’s remember that we still are in the “limited testing” stage. The idea of entry evaluation is to assess the validity of an idea as an entry signal. That we’ll discuss, as said, in the next article of this series.

 


References:

Building Winning Algorithmic Trading Systems, Kevin J. Davey

Encyclopedia of Trading Strategies, Jeffrey Owen Katz, and Donna L. McCormick

 Graphs were done using custom Python 3 software and also, from MT4, trading platform.

 ©Forex.Academy
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