The TrendSpider Strategy Explorer makes it easy to backtest trading strategies. There are quite a few unique advantages over other backtesting tools on the market, so please take some time to read this blog post — even if you’re familiar with other backtesters.
The Strategy Explorer solves two related problems:
- Determining how your strategy behaved in a given market with given parameters. By understanding this behavior, it’s easier to find an appropriate market for an existing strategy or choose a sane option from a list of well-known strategies.
- Optimizing and train strategy through backtesting. This is an exploratory process. For example, you might experiment with entry conditions without worrying about exits. Our Strategy explorer offers a very powerful way for learning about price behavior for your positions.
The rest of the article focuses on a common workflow for a case when you’re having a strategy and willing to learn about its behavior.
Defining Entry Conditions
The first step is defining an Entry condition — or “when to buy”. You can define these entry conditions using the same visual editor that powers are incredible multi-factor alerts. For the purposes of this article, we will define a simple condition, such as buying when SMA20 is above SMA50 and the “4 Green Candles” pattern forms.
Defining Exit Conditions
After we’re done with Entry, we need to decide when to exit the positions — or “when to sell”.
- The most obvious option is one more “Script” (e.g. the same kind of multi-line conditions as the Entry above).
- Another option is “X Candles Passed”, which sells after given period of time (e.g. “5 candles”). This is not very useful for real trading conditions, but we’ve added it for research purpose only (e.g. when you’re willing to focus on exploring Entry condition only).
- “Take Profit” exits when your position gains more than X%.
- “Stop Loss” exits when your position loses more than X%.
You can use any combination of these conditions.
For now, we will define a simple exit condition: The SMA20 crosses down through SMA50, as well as a “Stop Loss” at -2%.
So far so good, we’ve got Entry and Exit conditions defined.
The last step is filling in the details:
- Which market do you want to test on? (e.g. AAPL? NFLX? EURUSD? Daily, or 30 min, or 10 min charts?).
- What kind of chart do you want to test on? This is out of scope of this article, but in brief, you can backtest your strategies on Raindrops or Heikin Ashi. In this example, we will simply stick to default “OHLC” option (which is the same as Bars or Japanese Candles).
- How much history you want to use when testing? That is, how far in history you would want to travel with your strategy. (e.g. 300 candles? 7,000 candles?).
At last, we can test!
Backtesting Your Strategy and Understanding the Metrics
TrendSpider Strategy Explorer tests strategies on specific markets. For example, we don’t test your strategy against 20 years of price history across various stocks. Instead, we’re giving you a way to define the underlying market, because we believe that strategies can be fine-tuned for specific markets — what works in one market might not work in another.
When you test a strategy, Strategy Explorer calculates a number of statistics and then illustrates them with charts. These charts paint a picture from different angles, and we’ll walk though all of them.
All the pictures below were generated by TrendSpider Strategy Explorer, after testing the strategy described above on MCD,D (7,000 candles).
Painting Arrows On Chart
The most illustrative way to see how a strategy behaves is painting Entries/Exits arrows on the chart (hit the “See on Chart” button to do that). This is a good way to examine particular trades, but it won’t give you much information about how your strategy behaved on a larger scale.
For example, on this chart above you can see that:
Some positions seem to exit fast and lose money. You might want to search for a way to avoid that, such as by modifying your Stop Loss level or overall Exit condition. You might think of reworking the Entry condition as well, as these entries do not look very promising.
Sometimes you exit too late, missing the highest points. You might want to revisit your Exit condition to identify better spots.
Visualizing entry and exit on your chart is a good first step for learning more about your strategy, but there are some much more powerful tools available.
Price Behavior Explorer
How Price Behavior Explorer Works
The Price Behavior Explorer illustrates how the market was behaving while you were in a position.
It answers many questions, like:
- What was happening to the market after I bought, was it growing or what?
- How many of my trades were short-living, and how many of them were longer?
You can use this chart to discover if you were buying and selling at the right time, as well as to discover ways for improving your exit conditions.
Here’s how we build it:
Each position you had has a “track”, which is essentially a piece of price action for the time period while you were in that trade.
Imagine that you had one position. For the first candle after your Entry, the price has changed by +1% (vs. the Entry price). For the second candle, the market kept rising and you made +2%. For the third candle, the price slightly retraced and you were at +1%. For the fourth candle, the market went up +3% and you exited (say, you’ve met your exit criteria of SMA crossing e.t.c.).
You can paint the track of this position like that:
Usually, your strategy has more than one trade after you backtest it. All the trades can have different length and price behavior, and here’s how the chart above could look if we added tracks of all your positions:
Pretty messy — and that’s a fairly small number of positions painted! Luckily, there’s a way to simplify the look while skill preserving most of information. We could paint an “Average Track” line, and accompany it with two lines, such as the “25th percentile” and “75th percentile”. We could also add “Maximum” and “Minimum” lines. Here’s how it will look then:
This chart is looking rather busy, but it’s incredibly useful when you master it. In a single chart, you can instantly understand if your strategy identifies the market conditions you wanted it to identify For example, if you’re building a trend-following long strategy, then you would want your Mean line (along with the whole 25%-75% cloud) being above zero all the time.
On Random Control Line
Sometimes market just goes up regardless of the strategy — it’s difficult to know if your strategy is responsible or if it’s the underlying market. The same is true for extensively declining markets and short trades. When you test your strategy, you want to know what’s the root cause for your outcome: was it the strategy doing good, or simply market going up or down?
That’s where Random Control line comes into play. Here’s how we build it.
Imagine that you’ve backtested your strategy and your strategy generated 50 trades — the longest of them being 40 candles long. In this case, Strategy Explorer picks 50 random points on the same price chart where you tested your strategy, and collects 40-candles-long tracks starting at each of them. After that, it calculates the Average Price Behavior line (exactly the same math as behind green “avg” line on the chart above) and paints it on your Price Behavior chart.
The usage is pretty straightforward: If you’re building a long-trading strategy and your Average Price Behavior line is above the Random Control line, then this means that your strategy has picked the time frames when market was rising better/faster than it did in general (for a given depth of backtesting). The Random Control line works the best when you’ve got a decent number of trades and when all of them are having approximately the same length (this means there’s no difference by a power of 10).
What Price Behavior Explorer Says
For our backtest described above, here’s a number of conclusions you could make off this particular chart:
- In general, the price was growing after your Entry moment, because Mean Change (%) line is all above the Zero line, just as most of Min/Max Change (%) Cloud. This is a good sign for a long strategy.
- 80% of your trades were shorter than 65 candles, which you can see from the ladder line of “# Of Positions”.
- While you were in positions, the market was rising in more cases than if you were buying at purely random moments of time. You can see that from your Mean Change (%) line being higher than Random Control line.
- Couple positions were extremely long (190+ candles), and they were very profitable (around +30%).
If you take a closer look on first seven candles, you’ll see that about half of the positions were losing during first four candles. Given the fact that we had a 2% stop-loss in place, this might lead to early exits.
The Price Behavior Explorer is a complex tool that provides a lot of information, so we will cover that in greater depth in a different article and proceed with the other charts for our purposes in this article.
The Performance Chart combines a bird-eye view of a whole lot of history used for your backtesting with a couple lines:
- The stock price line (gray). The number of black lines overlaying the stock price line illustrate the tracks of all positions that your strategy had. You can easily see if you were riding the trend. In this particular case, there may have been too many short-lived positions, and there were some instances of late exits (e.g. selling after the high).
- The portfolio value line (blue). The blue line shows how much you would have made using the strategy over on a comprehensive basis. In this particular base, the +80% return is significantly underperforming the buy-and-hold return of +1,700% over the same period of time (e.g. 7,000 candles).
Gainers vs. Losers
The Gainers vs. Losers chart is rather straightforward — the only unusual thing here is this set of short vertical lines.
Each strip (red and green) has a number of short vertical lines painted on it. These lines indicate the impact of a single position — the further the line stands from a previous one, the higher was the impact of this trade. In other words, if you had only three losing trades, for -1%, -1% and -3% respectively, then you would have a picture like this:
The chart gives you a sense of consistency of your positions’ outcomes.
Now back about our particular backtest, here’s how the chart looks for me:
Here’s what you can see here:
- 66.7% (46, specifically) of your trades lost your money, 33.3% (23) were profitable.
- Each losing position has lost approximately the same amount of your money.
- Most of profitable positions had negligible impact and there was a small number of positions (6) which gave huge outcome, compared to others.
The interpretation of this chart could be: Your strategy was consistent at losing money, and was spiky when it comes to making money.
Distribution of Gains and Losses
The Distribution of Gains and Losses is a powerful chart. The horizontal axis is “change (%)”, and each circle represents the outcome of one position. All the losing positions are painted red, all the winners are green.
Here’s what you can see here:
- The strategy was pretty consistent when losing money and the vast majority of losing trades ended up losing approximately 2.8%.
- Gains were very inconsistent. Average gain was +8.8%, and there’s a few outliers standing for +20% or more.
- Average return of a trade was +1.1%. This is a positive number, which is nice.
From this chart, you can tell that the strategy has failed to deliver consistent positive results. The fact that losses were consistent definitely has something to do with our Stop Loss level, so one might want to try tweaking it. For example, you might move Stop Loss level to a wild -50% , then you get a picture more along these lines:
You can see that your average return is still modest, but your loses increased. So, having a sane stop loss level seems to be a good idea for this strategy (who would image!).