Introductory Guide to Back Testing and Strategy Analyzer

ICE Data Services -

Intro to Back Testing and Strategy Analyzer Guide  

What is a trading system and what is it for?

(for additional information on Back Testing see also; Guide to Developing EFS Strategies)

A trading system is a set of definite rules for selling and buying the trader strictly follows. By developing and testing your own system you'll become better prepared to face the inevitable periods of losses.

A system approach to trading is an inseparable part of trading as a profession. Naturally, every professional trader has a personal trading approach or preferences as well as a personal view of risk/profit. Some prefer day trading using classic trend following approaches, some like intraday trading with numerous short trades. For some, a loss of 15% from initial capital is not a problem at all.  For others, this kind of luxury is out of all question.

All we can do is offer a minimal set of rules to base your trading system on. Then it will be up to you to decide what to consider in your personal trading plan. The main task of this article is to make users familiar with technical side of Back Testing, and how it can be applied with eSignal.


To begin building your personal approach to trading, try answering the following questions:

  • Do I prefer to follow every movement of the market or to trade long periods?
  • What part of my capital can I afford to lose if the market goes against me?
  • How much time can I devote to trading?

The answers you have for these questions formulate the opinions that help you in choosing the trading approach to use.

Then you should

  • Select liquid, tradable markets,
  • Define the tools you'll be using for technical analysis,
  • Set rules for entering and exiting the market, to correctly define and set stop loss and profit targets.

The structure of a simple trading system

Before we start discussing the meaning of EFS (eSignal Formula Script) functions and constant values, we need to understand the structure of the trading system. As we said already, a trading system is a set of rules to enter Short and Long positions. In other words, Back Testing can be described as 4 commands:

  • Enter Long
  • Exit Long
  • Enter Short
  • Exit Short

So, the whole trading strategy is made up of a set of entries and exits. Every entry and exit takes place whenever a set condition is fulfilled, a condition that is called a trading signal.

In our example we have two trading signals. One is targeted whenever the price is crossed by the moving average downward, another upward. In the first case (price is crossed by the moving average downward) we enter a Short position, in the second (price is crossed by the moving average upward) a Long position. As you can see, our system contains no exits, such a system is called a Reversive strategy (i.e. the signal to exit a Long position is the signal to enter a Short position).

Described in words, this is how our trading system looks:

  • If Price crosses above Moving Average, then Exit Short and Enter Long
  • If Price crosses below Moving Average, then Exit Long and Enter Short


eSignal Formula Script language (EFS)

The eSignal Formula Script (EFS) enables you to develop your own custom trading signals or write your own techniques using EFS for analysis. Inside an EFS file you'll find the code that powers the indicator: the formula programmed in EFS is calculated for every bar, and the values passed to the return statement in function main() are displayed on the chart. 


Programming your first EFS based “trading system”

For our example, we’ll build our trading system around equities (or stocks) traded on major Stock Exchanges. The technical analysis tool or indicator we’ll use is a simple Moving Average indicator charted within an Advanced Chart. The basic formula for the Moving Average is pre-programmed in eSignal (default = 10 day moving average on close of market), and we’ll add Buy and Sell rules to develop a simple EFS – based trading system.

There are some standard values used to build trade instructions within a trading system or strategy.

Fill Type Constants

  • Strategy.CLOSE - uses the closing price as the fill price.
  • Strategy.LIMIT - uses the StopOrLimit price as the fill price.
  • Strategy.MARKET - uses the open price as the fill price.
  • Strategy.STOP - uses the StopOrLimit price as the fill price.

Fill Bar Constants

  • Strategy.THISBAR - use the OHLC values of the current bar to determine the fill price in conjunction with the Fill Type.
  • Strategy.NEXTBAR - use the OHLC values of the next bar to determine the fill price in conjunction with the Fill Type.


LotSize Constants

  • Strategy.DEFAULT - uses the Default LotSize as the number of shares.
  • Strategy.ALL - typically used with Strategy.doSell or Strategy.doCover.
  • Specifies that all shares being held be sold/covered.


Strategy Functions

  • Strategy.doLong (Description, Fill Type, Fill Bar, LotSize, StopOrLimit)
    Returns true if filled, false if not filled.
  • Strategy.doSell (Description, Fill Type, Fill Bar, LotSize, StopOrLimit)
    Returns true if filled, false if not filled.
  • Strategy.doCover (Description, Fill Type, Fill Bar, LotSize, StopOrLimit)
    Returns true if filled, false if not filled.
  • Strategy.doShort (Description, Fill Type, Fill Bar, LotSize, StopOrLimit)
    Returns true if filled, false if not filled.
  • Strategy.isInTrade ()
    Returns true if currently in a trade (long or short). Otherwise false.
  • Strategy.isLong ()
    Returns true if currently long. Otherwise false.
  • Strategy.isShort ()
    Returns true if currently long. Otherwise, false.
  • Strategy.setStop (dStop)
    Set a stop order. This will close out the active position if the stop order
    price is reached or exceeded.
  • Strategy.clearStop () Clear (remove) the current stop order.
  • Strategy.getPositionSize () Returns the number of shares currently held. This number is negative if short, positive if long. E.g.: -100 is short 100 shares. 100 is long 100 shares.
  • Strategy.getDefaultLotSize () Returns the default lot size as set by the user before starting the Back Test.

A simple EFS-based trading system - Moving Average Crossover Strategy

This trading system is based on the moving average concept. A Moving Average is an indicator that shows the average value of a security's price over a period of time. When calculating a moving average, a mathematical analysis of the security's average value over a predetermined time period is made. As the security's price changes, its average price moves up or down. Our Back Test formula consists of a Long position if the price crosses the moving average upwardss and a Short position, if the price crosses the moving average downwards.

eSignal includes a Formula Editor (Tools-->EFS menu) that you can use to modify existing formulas or create new EFS-based trading system formulas to be used with the Strategy Analyzer.

When you open a formula, it is displayed in a Formula Editor window.  All of the formula components are color coded so that you can identify them more easily. You can view the formula in this window, make your changes, check a formula’s syntax to make sure there are not any errors, change Formula Editor properties, and use the Formula Editor toolbar to help you work with a formula.


So, let's create a file in the Formulas > Back Testing folder named, my_first_strategy.efs.

1. Copy the EFS code below:

2. open the EFS Editor from Tools on the Main Toolbar and paste it into the Editor window.

3. Save As “my_first_strategy.efs” in the Back Testing folder (@ Program Files\eSignal\Formulas\Back Testing).

(Note:  The following code uses EFS2, which requires eSignal Version 7.9 or later.)

var study = null;
function preMain() {
  setPriceStudy(true);
  setStudyTitle("Back Test Moving Avg");
  setCursorLabelName("MA");
}
function main() {
  if (study == null) study = sma(10);
  var v = study.getValue(0);
  if (v == null) return;
  if (close(0) >= v) {
    if (!Strategy.isLong()) {
      Strategy.doLong("Crossing Up", Strategy.CLOSE, Strategy.THISBAR);
    }
  } else {
    if (!Strategy.isShort()) {
      Strategy.doShort("Crossing Down", Strategy.CLOSE, Strategy.THISBAR);
    }
  }
  if (Strategy.isLong()) {
    setBarBgColor(Color.green);
  } else if (Strategy.isShort()) {
    setBarBgColor(Color.red);
  }
  return v;
}

4. Open an Advanced Chart and apply the formula.  Right-click on the chart and select the my_first_strategy.efs fromula from the "Formulas" menu. 

5. Then right-click in the Advanced Chart again and select Tools-->Back Testing.

6. In the Back Testing Dialog box select the Symbol, Interval and Time Template desired (the Symbol, Interval and Time Template will default to the active Advanced Chart that you have open in eSignal).

7. Set the formula to be back tested by selecting the Browse button to the right of the "Formula" field and select the “my_first_strategy.efs” from the Back Testing Folder.

8. Enter "test" as the file name for the Back Test report in the Output File field.  Back test report files are saved to the eSignal\BtOut\ folder.


9. Click on “Test” and the eSignal Strategy Analyzer Report will come up in a new window.


Note: Debugging - You should check whether your trading system works the way you expect it to work. Debugging is the process of finding and correcting logic errors. Most errors in scripting are caught when compiling by the eSignal Formula Editor. The Formula Output Window displays information about all the syntax errors you made and indicates where the errors were made.  However, logic errors do not violate syntax rules and cannot be caught by the formula engine.  If a formula is not performing as you are expecting, use the debugPrintln() function in various locations of your formula code to track down possible logic errors.

Strategy Analyzer Report

Analysis of a trading system with the eSignal Strategy Analyzer

With the eSignal Strategy Analyzer you have the unique possibility of seeing the advantages and disadvantages of your strategy, changing the way you buy and sell, improving trading robustness and gaining faith in your trading performance. The eSignal Strategy Analyzer contains 6 sections accessible by clicking on tabs in the top part of the report window. Trading system performance is analyzed from different approaches, each accessible from its tab. Overall, the report provides more than 250 values useful for strategy performance analysis with multiple modes of graphical presentations included.  In the settings dialog box the user is able to create personal settings for displaying the values of the report and analyzing the strategy.

Total performance is most easily evaluated from the Strategy Analysis tab, which is, at the same time, hardly a sole measurement of strategy value.  To get a complete picture of strategy performance, take care to analyze the Strategy Analysis along with the other sections.

 
The next two tabs, Trades and Trades Analysis, show information on the trades. The Trades tab represents each trade as a multi-column sheet.  The Trade Analysis tab focuses on separate trades to estimate the overall strategy performance, with general trade analysis included.

The Periodical Analysis tab serves to broaden the analysis available from the Strategy and Trade Analysis tabs. Strategy performance is evaluated as related to periods of time (annual, monthly or daily) to evaluate consistency.

 

A user-friendly toolkit for graphic presentation and handling is included in the Graphs tab. The Graphs tab adds powerful visual display capabilities to the eSignal Strategy Analyzer analysis and evaluation.

The eSignal Strategy Analyzer is equipped with a powerful Help feature. To see Help information on any value you're interested in, just point your mouse and left-click when a question sign appears. Use this features when reading a report to save you time and help you understand reports yourself without having to consult the Help section.

There can be no "general" way to read and interpret eSignal Strategy Analyzer Reports. The system approach to trading is an inseparable part of trading as a profession. Naturally, every professional trader has a personal trading approach, personal preferences and a personal view of risk/profit.

Some prefer day trading using classic trend-following approaches.  Some like intraday trading with numerous short trades.  For some, a loss of 15% from initial capital is not a problem at all.  For others, this kind of luxury is out of the question.

And, surely, there is not a single statistical value that tells us if a trading system is good or bad.  It all largely depends on personal preferences, but it's possible to point out a number of important issues that are worth some attention when analyzing the eSignal Strategy Analyzer Report.  In other words, no matter what your trading approach is, there are definite borders for the values to lie within.

The most common error of strategy analysis is ignoring the relative values, such as the Max Strategy Drawdown (%) or Return on Account. Usually the first one that catches a beginner's attention is Total Net Profit. That’s natural because it's the overall dollar profit or loss achieved by the trading strategy during the test period. But, Total Net Profit is not such an important values, because it shows only the absolute profit or loss value. You may have a large Total Net Profit, but, all the same, suffer unacceptable losses while trading.

So, the prime values are Gross Loss and Max Strategy Drawdown. Gross Loss of a strategy is most important, yet is often overlooked. It should be noted that net profit increases, not only when gross profit improves, but also when gross loss is reduced. Analyzing and working over losing trades is an extremely important part of trading strategy analysis.

As to the Max Strategy Drawdown and Max Strategy Drawdown (%) values, they show the largest equity dip that took place during the test period. The largest equity dip is the biggest difference between an equity high and a subsequent equity low. The Max Strategy Drawdown value forms the Account Size Required (i.e. the amount of money you must have on the account to start trading the strategy).

Only after analyzing Max Strategy Drawdown and determining the Account Size Required, can we make an adequate evaluation of the Total Net Profit. This is accomplished using the Return on Account value, the sum of money you would make compared to the sum of money required to trade the strategy, after considering the margin and margin calls. This value is calculated by dividing the Total Net Profit by the Account Size Required.

One can give many more indications about important value in the eSignal Strategy Analyzer. For instance, Total # of Trades must be greater than 30 to make sure that the results are statistically valid. Even if your have tested your strategy on 25 years of history data, if your system didn't take at least 30 trades, results won't be convincing.

Largest Winning Trade isn't the most demonstrative value. But it's wise to look at how the size of the Largest Winning Trade relates to the Avg. Trade (win & loss) (i.e. whether the largest trade really is an Outlier Trade. Outlier trades are those that exceed the average trade by a significant value (plus or minus three (3) standard deviations). Considering this, it's wiser to pay attention to the Select Net Profit, than the more usual Total Net Profit, because Select Net Profit is the modified Net Profit (with all outlying trades, both positive and negative, removed). The final value thus indicates the net profit for standard trades.

Max Consec. Losers may also turn out to be a most important value -- as a measure of psychological stress, you'll have to go through when trading this system. Knowing the maximum number of possible values you avoid panic when consecutive losses really occur.

Ratio Avg Win/Avg Loss is the value that must be greater than 1 (break-even). Surely, anything greater than 5 is great results, but even a value of 2 profits can be good, if other values are within good bounds.

We've listed just the main values worthy of attention. Really, not a single value is useless - - every one serves a purpose, informing the user of the trading system's performance. For more detailed information on every value, use the Hints system.