Upload
others
View
7
Download
1
Embed Size (px)
Citation preview
BEATING THE MARKET WITH A SIMPLE SUPERTREND STRATEGYMATT RADTKE
MARCH 14, 2019
MATT RADTKE: BACKGROUND & EXPERIENCE
• Software development and management: 30+ years
• Trading: 12 years
• AmiBroker: 10 years
• Professional AmiBroker Researcher/Consultant: 9 years
• International client base with customers in the U.S., Canada, India, Australia,
U.K., Europe, Brazil, Indonesia
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 2
DISCLAIMER
Do not assume that the methods, techniques, or indicators presented will be
profitable or that they will not result in losses. Past results are not necessarily
indicative of future results. Examples presented are for educational purposes
only, and are not solicitations of any order to buy or sell. The author assumes no
responsibility for your trading results. There is a high degree of risk in trading.
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 3
AGENDA
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 4
AGENDA
1. AmiBroker Overview
2. Analysis: Scan, Explore, Back Test, Optimize
3. Building & Refining a Strategy
4. Robustness
a. Parameter Sensitivity
b. In Sample vs. Out of Sample testing
5. Questions & Answers
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 5
AMIBROKER OVERVIEW
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 6
AMIBROKER OVERVIEW
AmiBroker is a comprehensive technical analysis program that allows you to
quickly get started with the basics while also providing the flexibility to develop
complex trading systems.
While AmiBroker has extensive charting capabilities, it is primarily known as a
fast and powerful backtesting platform.
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 7
DATA SOURCES
AmiBroker does not directly provide any useful price data. Rather, it is a set of
tools which can be used with data from a variety of providers, including
Norgate, TIINGO, eSignal, CSI, Yahoo, Google and others.
Any data that is available in CSV format can be imported into an AmiBroker
database.
For EOD futures, forex, and US/AUS stock data, I cannot say enough good
things about Norgate Data. If you’re considering a subscription, contact me for
a discount.
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 8
ANALYSIS
SCAN, EXPLORE, BACK TEST, OPTIMIZE
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 9
ANALYSIS OVERVIEW
1. Scan: Generate Trade Signals
2. Explore: Query the Database, Calculate Indicators and Signals
3. Backtest: Simulate Actual Trades
4. Optimize: A series of backtests
a. Exhaustive
b. Smart
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 10
ANALYSIS
Before you can perform any type of analysis, you need to make selections for
each of the following:
1. Formula: Specify the AFL file containing the analysis code that you wish to
execute
2. Apply To: Will you be analyzing a single symbol, all symbols, or a filtered list
such as a watch list?
3. Range: Set the dates that you want the test results to cover
4. Settings: Long or Short? Periodicity? Pad & Align?
5. Parameters: Depending on how your script is written, you may be able to
specify input parameters without changing the AFL code.
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 11
ANALYSIS DEMO
Once you’ve defined the testing environment, you’re ready to execute one of
the four types of analysis:
1. Scan
2. Explore
3. Backtest
4. Optimize
Let’s look at how to run a backtest.
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 12
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 13
BUILDING & REFINING A STRATEGY
USING THE SUPERTREND INDICATOR TO BEAT THE MARKET
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 14
HYPOTHESES
All strategies are based on one or more hypotheses. These are the core ideas
around which the strategy is built.
For this strategy, there are two primary hypotheses:
1. NIFTY and Bank NIFTY can be profitably traded using a quantified
trend-following strategy applied to intraday bars.
2. The SuperTrend indicator is an effective way to identify the direction of
the trend.
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 15
SUPERTREND
The SuperTrend indicator is a variation on other types of volatility bands, using
a multiple of ATR to define bands above and below the current average price.
The SuperTrend line follows the lower band when the price is in an up trend (has
most recently broken the upper band), and follows the upper band when the
price is in a down trend (has most recently broken the lower band).
A more complete description as well as a function written in AFL can be found
on my web site: https://quantforhire.com/2018/08/19/supertrend-indicator/
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 16
BASE STRATEGY
We will begin with a very simple set of strategy rules which will be applied to
5-minute bars for the NIFTY index. We will trade this as a cash instrument, not
as a leveraged futures product.
• Position size will be 100% of available equity when trading one
instrument, or 50% of available equity when trading two instruments
• System will trade both Long and Short
• All entries and exits take place at the open of the bar following the entry
or exit signal
• Commissions are $0.01 per share
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 17
STRATEGY RULES
1. A Long entry signal (Buy) is generated when a 5-minute bar closes above
the SuperTrend line.
2. A Long exit signal (Sell) is generated when a 5-minute bar closes below the
SuperTrend line.
3. A Short entry signal (Short) is generated when a 5-minute bar closes below
the SuperTrend line. (Same as Long exit)
4. A Short exit signal (Cover) is generated when a 5-minute bar closes above
the SuperTrend line. (Same as Long entry)
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 18
SUPERTREND-V1.AFL
#include <SuperTrendFunction.afl>
nEntryTiming = 1;
nExitTiming = 1;
nMaxPos = Param("Max Positions", 1, 1, 10, 1);
acctMargin = 100;
pctPosSize = 100/nMaxPos * 100/acctMargin;
////////////////////////////////////////////////////
// Back Test Parameters
stLengthATR = Param("SuperTrend: ATR Length", 10, 1, 100, 1);
stWidthBands = Param("SuperTrend: Band Width", 3, 1, 20, 0.1);
////////////////////////////////////////////////////
// Set up the AB environment
SetOption("CommissionMode",3);
SetOption("CommissionAmount", 0.01);
SetOption("MaxOpenPositions",nMaxPos);
SetOption("AccountMargin",acctMargin);
SetTradeDelays(nEntryTiming,nExitTiming,nEntryTiming,nExitTiming);
SetPositionSize(pctPosSize,spsPercentOfEquity);
RoundLotSize = 1;
// Initialize all arrays so the backtest will always run
Buy = Sell = Short = Cover = 0;
BuyPrice = ShortPrice = IIf(nEntryTiming==0,C,O);
SellPrice = CoverPrice = IIf(nExitTiming==0,C,O);
SuperT = SuperTrend(stLengthATR,stWidthBands);
Buy = C > SuperT;
Sell = C < SuperT;
Short = C < SuperT;
Cover = C > SuperT;MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 19
VERSION 1 RESULTS
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 20
Statistics
All trades Long trades Short trades Buy&Hold ($NIFTY-NSE)
Initial capital 1000000 1000000 1000000 1000000
Ending capital 7371519.98 5849436.32 2522083.66 2085302.41
Net Profit 6371519.98 4849436.32 1522083.66 1085302.41
Net Profit % 637.15% 484.94% 152.21% 108.53%
Exposure % 99.87% 50.92% 48.96% 100.00%
Net Risk Adjusted Return % 637.96% 952.44% 310.90% 108.53%
Annual Return % 24.87% 21.70% 10.83% 8.51%
Risk Adjusted Return % 24.90% 42.62% 22.13% 8.51%
Max. system % drawdown -18.55% -11.23% -25.55% -28.07%
Total transaction costs 40904.12 20450.48 20453.64 3.84
OBSERVATIONS FROM VERSION 1
1. Compound Annual Return (CAR) has increased by 192%,
from 8.51% for B&H to 24.87% for the Long/Short strategy
2. Max Drawdown (MDD) has decreased by 34%,
from 28.07% for B&H to 18.55% for the Long/Short strategy
3. CAR/MDD of 1.34 is acceptable but could perhaps be improved by
avoiding overnight risk
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 21
VERSION 2 STRATEGY RULES
We will convert the strategy to a pure day-trading strategy (no overnight holds)
by adding earliest and lastest entry times, as well as a hard exit time. The
following new rules will be added to those established for Version 1:
1. Long and Short entry signals may only occur between 9:15am and
3:00pm
2. Long and Short exits signals will occur when crossing the SuperTrend line
(as before), OR at 3:15pm, OR on the second-to-last trading bar of the
day (to catch instances when the market closes early).
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 22
SUPERTREND-V2.AFL
#include <SuperTrendFunction.afl>
…
////////////////////////////////////////////////////
// Back Test Parameters
stLengthATR = Param("SuperTrend: ATR Length", 10, 1, 100, 1);
stWidthBands = Param("SuperTrend: Band Width", 3, 1, 20, 0.1);
tnEarliestEntry = ParamTime("Earliest Entry Time", "9:15 AM", 0);
tnLatestEntry = ParamTime("Latest Entry Time", "3:00 PM", 0);
tnFinalExit = ParamTime("Final Exit Time", "3:15 PM", 0);
…
tn = TimeNum();
dn = DateNum();
isEndOfDay = dn != Ref(dn,1);
SuperT = SuperTrend(stLengthATR,stWidthBands);
Buy = C > SuperT AND
tn >= tnEarliestEntry AND
tn <= tnLatestEntry;
Sell = C < SuperT OR
tn >= tnFinalExit OR
Ref(isEndOfDay,nExitTiming);
Short = C < SuperT AND
tn >= tnEarliestEntry AND
tn <= tnLatestEntry;
Cover = C > SuperT OR
tn >= tnFinalExit OR
Ref(isEndOfDay,nExitTiming);
Highlights Only
Boilerplate Removed
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 23
VERSION 2 RESULTS
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 24
Statistics - Version 2
All trades Long trades Short trades Buy&Hold ($NIFTY-NSE)
Initial capital 1000000 1000000 1000000 1000000
Ending capital 5395825.13 2118256.21 4277568.92 2085302.41
Net Profit 4395825.13 1118256.21 3277568.92 1085302.41
Net Profit % 439.58% 111.83% 327.76% 108.53%
Exposure % 95.71% 48.77% 46.94% 100.00%
Net Risk Adjusted Return % 459.29% 229.31% 698.20% 108.53%
Annual Return % 20.61% 8.70% 17.54% 8.51%
Risk Adjusted Return % 21.54% 17.85% 37.36% 8.51%
Max. system % drawdown -9.87% -17.84% -9.28% -28.07%
Total transaction costs 43308.22 22382.74 20925.48 3.84
COMPARING VERSION 2 TO VERSION 1
1. MDD has decreased by 47%, from 18.55% for v1 to 9.87% for v2.
However, MDD for Long trades actually increased from 11.23% to 17.84%,
while MDD for Short trades decreased from 25.55% to 9.28%.
2. Compound Annual Return (CAR) has decreased by 17%, from 24.87% to
20.61%. Again, the Long trades suffered (21.70% to 8.70%) from the new
rules, while the Short trades benefited (10.83% to 17.54%).
3. CAR/MDD of 2.09 for v2 is significantly better than 1.34 for v1.
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 25
VERSION 3 STRATEGY
Based on the observation that applying day-trading constraints to the strategy
helped the Short performance while hurting the Long performance, we will test
a mixed model whereby Short trade signals include time of day constraints,
while Long trade signals do not.
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 26
SUPERTREND-V3.AFL
#include <SuperTrendFunction.afl>
…
////////////////////////////////////////////////////
// Back Test Parameters
stLengthATR = Param("SuperTrend: ATR Length", 10, 1, 100, 1);
stWidthBands = Param("SuperTrend: Band Width", 3, 1, 20, 0.1);
tnEarliestEntry = ParamTime("Earliest Entry Time", "9:15 AM", 0);
tnLatestEntry = ParamTime("Latest Entry Time", "3:00 PM", 0);
tnFinalExit = ParamTime("Final Exit Time", "3:15 PM", 0);
…
tn = TimeNum();
dn = DateNum();
isEndOfDay = dn != Ref(dn,1);
SuperT = SuperTrend(stLengthATR,stWidthBands);
// Long trades can span multiple days, but Short trades will always
// exit on the day of entry
Buy = C > SuperT;
Sell = C < SuperT;
Short = C < SuperT AND
tn >= tnEarliestEntry AND
tn <= tnLatestEntry;
Cover = C > SuperT OR
tn >= tnFinalExit OR
Ref(isEndOfDay,nExitTiming);
Highlights Only
Boilerplate Removed
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 27
VERSION 3 RESULTS
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 28
Statistics - Version 3
All trades Long trades Short trades Buy&Hold ($NIFTY-NSE)
Initial capital 1000000 1000000 1000000 1000000
Ending capital 15238539 8818358.13 7420180.87 2085302.41
Net Profit 14238539 7818358.13 6420180.87 1085302.41
Net Profit % 1423.85% 781.84% 642.02% 108.53%
Exposure % 97.90% 50.93% 46.96% 100.00%
Net Risk Adjusted Return % 1454.46% 1535.08% 1367.02% 108.53%
Annual Return % 35.37% 27.38% 24.96% 8.51%
Risk Adjusted Return % 36.13% 53.76% 53.15% 8.51%
Max. system % drawdown -10.25% -15.40% -10.94% -28.07%
Total transaction costs 72438.2 32267.12 40171.08 3.84
COMPARING VERSION 3 TO VERSION 2 AND B&H
1. Compound Annual Return (CAR) has increased by nearly 72%, from 20.61%
to 35.37%. Both Long and Short CAR improved.
2. MDD has increased slightly by 4%, from 9.87% for v2 to 10.25% for v3.
3. CAR/MDD of 3.45 for v3 is a 65% improvement over 2.09 for v2.
4. Buy & Hold metrics were:
• CAR 8.51% (315% improvement)
• MDD 28.07% (63% improvement)
• CAR/MDD 0.3 (1050% improvement)
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 29
DOES IT WORK ON BANK NIFTY?IN A WORD: YES!
VERSION 3 RESULTS FOR BANK NIFTY
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 31
Statistics - Version 3 - Bank NIFTY
All trades Long trades Short trades Buy&Hold ($BANKNIFTY-NSE)
Initial capital 1000000 1000000 1000000 1000000
Ending capital 21222815.14 16373303.9 5849511.24 3005922.15
Net Profit 20222815.14 15373303.9 4849511.24 2005922.15
Net Profit % 2022.28% 1537.33% 484.95% 200.59%
Exposure % 97.79% 50.59% 47.21% 100.00%
Net Risk Adjusted Return % 2067.89% 3038.86% 1027.32% 200.59%
Annual Return % 40.45% 36.45% 21.70% 13.02%
Risk Adjusted Return % 41.36% 72.06% 45.97% 13.02%
Max. system % drawdown -26.02% -37.55% -35.49% -41.27%
Total transaction costs 49060.76 21328.5 27732.26 2.21
WHAT ABOUT TRADING NIFTY AND BANK NIFTY TOGETHER?
VERSION 3 RESULTS FOR NIFTY & BANK NIFTY
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 33
Statistics - NIFTY and Bank NIFTY
All trades Long trades Short tradesBuy&Hold
($NIFTY-NSE)
Buy&Hold
($BANKNIFTY-NSE)
Initial capital 1000000 1000000 1000000 1000000 1000000
Ending capital18365264.3
2
12345209.7
57020054.57 2085302.41 3005922.15
Net Profit17365264.3
2
11345209.7
56020054.57 1085302.41 2005922.15
Net Profit % 1736.53% 1134.52% 602.01% 108.53% 200.59%
Exposure % 97.75% 50.73% 47.02% 100.00% 100.00%
Net Risk Adjusted Return
%1776.52% 2236.55% 1280.25% 108.53% 200.59%
Annual Return % 38.21% 32.24% 24.19% 8.51% 13.02%
Risk Adjusted Return % 39.09% 63.55% 51.45% 8.51% 13.02%
Max. system %
drawdown-12.63% -23.74% -16.79% -28.07% -41.27%
Total transaction costs 65127.68 28782.6 36345.08 3.84 2.21
ROBUSTNESS
WILL YOUR STRATEGY HOLD UP IN LIVE TRADING?
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 34
PARAMETER SENSITIVITY
1. Testing a variety of indicator periods, thresholds, factors, etc. is one way to protect
yourself against curve fitting or “cherry picking”.
2. Try to test a range of parameters well above and below what you think the
optimal values are.
3. Use increments and values that make sense. A 21-day moving average corresponds
to one month of data. A 23-day moving average doesn’t correspond to much of
anything. Similarly, it would be hard to explain to your trading buddy why you’re
using RSI(12) instead of RSI(10) or RSI(15).
4. You should see good performance results from a range of parameters!
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 35
IN SAMPLE & OUT OF SAMPLE TESTING
When creating and testing any data model, including trading strategies,
it is often beneficial to develop and refine the model on one set of data,
and then to validate the model on an independent data set. These are
referred to as the In-Sample (IS) and Out-Of-Sample (OOS) data sets.
Using this approach helps to avoid curve fitting.
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 36
OUT OF SAMPLE TESTING: DATE RANGE SELECTION
One common and easy way to define an OOS data set is to segregate your
data by date range, although other methods are equally valid. The key
question then becomes which date ranges to use for the IS and OOS data
sets.
Most people prefer to use the most recent data for their OOS data set. If we
assume that the market’s behavior over the next year will be more similar to
the N years immediately prior to today than it will be to the behavior from, say,
1995-2000, then using the most recent data for OOS testing is the closest we
can come to testing against future data.
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 37
LONG/SHORT ROBUSTNESS TEST
1. We will use AmiBroker’s Optimization feature to test a range of different
parameter values for the SuperTrend indicator.
• ATR Length: 5 to 30 in increments of 5, i.e. 5, 10, 15… 30
• Band Width (ATR Multiple): 1.0 to 5.0 in increments of 0.5, i.e. 1.0, 1.5, 2.0… 5.0
• AmiBroker’s exhaustive optimization will produce 6 x 9 = 54 different strategy variations
2. Our IS test period will be from 1-Jan-2010 through 31-Dec-2015.
3. Our OOS test period will be from 1-Jan-2016 through 31-Dec-2018
4. We will use combination of CAR and CAR/MDD to select the “best” IS variation
and then see how that performs in the OOS test.
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 38
IN SAMPLE RESULTS
NIFTY & BANK NIFTY
2010 - 2015
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 39
OUT OF SAMPLE RESULTS
NIFTY & BANK NIFTY
2016 - 2018
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 40
SUMMARY
1. We have built a very simple Long/Short trend following system using the
SuperTrend indicator and a few time constraints. Simpler systems often work better
in live trading than more complex ones!
2. Using selection criteria of CAR and CAR/MDD, we were able to pick an In Sample
variation of the strategy that also performed well in Out of Sample testing.
3. Now that the selection method has been validated, a logical next step would be to
run a new IS optimization on all (or nearly all) available data, and then use the
same selection criteria to determine which variation(s) to trade.
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 41
THANKS FOR ATTENDING!
QUESTIONS?
Feel free to contact me at [email protected] or visit my website.
Information and AFL for the SuperTrend indicator is available at
https://quantforhire.com/2018/08/19/supertrend-indicator/
AFL used for the full backtest and an Excel workbook with results can be
requested by filling out the contact form at https://quantforhire.com/contact/
MATT RADTKE: BEAT THE MARKET WITH SUPERTREND 42