Algorithmic Trading Tip - Building Risk Protection Into Your Trading by Kevin Davey Medium
Algorithmic Trading Tip - Building Risk Protection Into Your Trading by Kevin Davey Medium
Kevin Davey
90% of traders out there build strategies the wrong way. How do I know that? It shows
up in the statistics — most traders lose!
When most traders build a strategy, they have one goal in mind — maximizing profits.
That’s not necessarily terrible all by itself — after all, you need profits to make it
worthwhile.
More enlightened traders realize that the risk (drawdown) is a key measurement of a
good strategy, and incorporate that into their strategy building. Most people would not
like an algo strategy that made $50K per year if it also had $100K drawdowns. Don’t you
agree?
Hundreds of traders around the globe use the Strategy Factory® process, and that is
exactly what they do with that process: find strategies with a good balance between
profits and drawdowns.
There is another level to strategy building, one that many traders neglect — risk
protection. The need for this became apparent to many people during the coronavirus
scare of 2020. Very high volatility, quick and violent price swings, fast trends and quick
pullbacks characterized many markets, not just equities.
1 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
During crazy times like this, maybe just surviving becomes the primary goal, rather than
making money.
That is what this article is all about — techniques to reduce risk, on a strategy level. I’ll
discuss and give examples of:
I’ll be discussing the concepts, showing you some examples, providing code and
generally giving you the tools and knowledge to incorporate these “risk managers” into
your trading strategies.
Baseline Strategy
2 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
For this study, I created a simple pullback type strategy, and applied it to 120 minute
Crude Oil bars over the last 5 years.
Although this strategy makes a decent amount of money of the 5 year period, it has
significant and prolonged drawdowns. These will be useful to see if any of the risk
protection measures can improve the strategy.
The strategy itself requires a long term uptrend, and a shorter term downtrend, in order
to enter a long position. Vice versa for short positions.
It also includes an ATR based stop, with a ceiling of $3000 per contract. This type of stop
was examined in an earlier article series on stop losses.
All the code below is in Tradestation format, but here are simple “plain English”
instructions.
3 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
If the close crosses above the close “lookback” bars ago, and the close crosses below the
close .5 “lookback” bars ago, go long. Vice versa for short trades.
The stop should be a multiple of Average True Range, with a maximum amount of
$3000 per contract.
For this study, lookback was set to 10, and the ATR multiplier was set to 2.
NOTE: This strategy is for study purposes only. I am not saying this is a good
strategy to trade.
Tradestation Code
//*********************************************************
//
//www.kjtradingsystems.com
//
//*********************************************************
//
input: lookback(10),stopATR(2);
If close crosses above close[lookback] and close crosses below close[.5*lookback] then buy
next bar at market;
If close crosses below close[lookback] and close crosses above close[.5*lookback] then
4 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
var:NewStop(3000);
NewStop=StopATR*AvgTrueRange(14)*BigPointValue;
All traders like to avoid losing days, right? But experienced traders know that is not
possible. So, the next best thing is to limit the loss on really bad days. This can be a
psychological life saver, and possibly an account saver.
The version created for this study calculates the loss since midnight (chart time), and
exits all trades and prevents new trades if the loss exceeds a certain amount. This works
well for X minute based bars.
Every day, calculate the total open plus closed equity for the strategy at the last bar
before midnight. If during the next day, the current total equity minus the “midnight”
equity is less than “equitylosslimit” exit all current positions and do not take any new
trades.
Tradestation Code
//*********************************************************
5 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
//
//www.kjtradingsystems.com
//
//*********************************************************
//
input: lookback(10),stopATR(2),EquityLossLimit(1000);
var:EndDayEquity(0),CanTrade(True),CurrentEquity(0);
CurrentEquity=NetProfit+OpenPositionProfit;
EndDayEquity=CurrentEquity[1];
end;
CanTrade=True;
If close crosses above close[lookback] and close crosses below close[.5*lookback] then buy
next bar at market;
If close crosses below close[lookback] and close crosses above close[.5*lookback] then
sellshort next bar at market;
6 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
var:NewStop(3000);
NewStop=StopATR*AvgTrueRange(14)*BigPointValue;
end;
end;
If you use daily bars, you can use a stoploss statement instead:
Sell next bar at close — xxx stop; //xxx is the price where daily loss limit would be hit
Results
To see the impact this Daily Loss limit had, I varied the daily loss limit from $500 to
$10,000. At small daily losses, the performance of the strategy is definitely worse that
the baseline profit of $40K. This makes sense, since with small loss limits, the market
volatility will frequently lead to turning the system off.
7 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
From around $2000-$4000, the daily loss limit somewhat improves the performance,
although not tremendously.
So, the optimum result for the Daily Loss Limit is shown below:
Looking at the results, you might conclude that the Daily Loss Limiter is good to have.
But BEWARE! This is an optimized result. The $2500 limit is likely NOT to be the best
daily limit going forward.
Recommendation
If you want to use a Daily Loss Limit, decide on the value to use BEFORE you run the
backtest. Don’t necessarily expect a performance improvement, but instead use it
because you like the psychological comfort of limiting your daily loss. Any performance
improvement should be considered a bonus.
8 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
Have you ever had a strategy that seemed to get “stuck” in a rut of losing trade after
losing trade? Perhaps it was a counter trend strategy, constantly trying to go short in a
bull market. I’ve definitely had strategies like that.
One way to minimize the damage (again, psychological and financial) is to put a delay
on any signal after a loss. For example, once a losing trade is closed, wait 5 bars before
taking another trade. This should give you some protection from the “catching a falling
knife” syndrome — trying to trade against a trend.
After a losing trade, wait NextTradeDelay bars before taking the next trade.
Tradestation Code
//*********************************************************
//
//www.kjtradingsystems.com
//
//*********************************************************
//
input: lookback(10),stopATR(2),NextTradeDelay(1);
var:CanTrade(True);
9 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
CanTrade=True;
If close crosses above close[lookback] and close crosses below close[.5*lookback] then buy
next bar at market;
If close crosses below close[lookback] and close crosses above close[.5*lookback] then
sellshort next bar at market;
var:NewStop(3000);
NewStop=StopATR*AvgTrueRange(14)*BigPointValue;
end;
Results
For this particular strategy, putting a delay on the signal after a loss does not improve
things at all. In fact, it almost always makes performance worse, especially if the delay is
significant (10 or more bars).
10 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
Recommendation
This type of risk protection is really suited for strategies where there is a potential for
many consecutive losses. A counter trend strategy would be a good example. As with any
of these risk protection techniques, it is best to incorporate the rule in the strategy
BEFORE you backtest. Adding the rule after you see initial results is basically cheating
the backtest.
I would feel comfortable using this approach on a strategy that I knew was designed to
trade counter to the major trend.
Imagine one of your algo strategies trades Crude Oil. On Friday March 6, 2020, near the
close of the day you go long, at a price around the settlement price of 41.57. Even
though oil fell about 5 dollars a barrel that day, you feel good because all kinds of bullish
11 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
Except this weekend is different. Coronavirus becomes much more newsworthy, and
Russia and Saudi Arabia get into some kind of bizarre price war over oil. On Sunday
night, oil collapses and opens at 32.87, a drop of more than 20%, or $8,700 per contract.
Close all positions at 4 PM Eastern on Friday. Do not allow any new trades, either.
Tradestation Code
//*********************************************************
//
//www.kjtradingsystems.com
//
//*********************************************************
//
input: lookback(10),stopATR(2),FridayStoptime(1600);
var:CanTrade(True);
CanTrade=True;
12 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
If close crosses above close[lookback] and close crosses below close[.5*lookback] then buy
next bar at market;
If close crosses below close[lookback] and close crosses above close[.5*lookback] then
sellshort next bar at market;
var:NewStop(3000);
NewStop=StopATR*AvgTrueRange(14)*BigPointValue;
end;
end;
This will get you out of the market Friday afternoon, unless Friday is a shortened holiday
session. This can feel tremendous psychologically (imagine not having to worry about
positions over the weekend!), but does it really help financially?
Results
Yikes!
13 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
The max drawdown is less, and the system is in the market a lot less (which by itself is a
good thing), but you also have to give up almost 75% of profits. Clearly, for this
strategy/market, the “no weekend” trading idea is a financial loser.
Recommendation
This is a neat idea (who would not want stress free weekends?), but the benefit may be
more psychological than financial. In fact, as we see with this strategy, it might be
14 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
terrible financially.
But, as with all ideas, I recommend you test it, adding it to your strategy from the start.
Don’t create a strategy and then try to tack this on, since you’ll only accept it if
performance improves (which is cheating!).
Instead, if you like the idea, try it for a while. If you find yourself unable to create good
strategies, then this weekend requirement might be the reason, and you might want to
eliminate it.
Risk Protection #4 — If You Can’t Stand The Heat, Get Out Of The Kitchen
Volatility is a double edged sword. As traders, we need volatility to profit from price
movement. So some volatility is a good thing. But too much volatility is a bad thing. It
can mess up our algos, and trade us in and out of trades in an endless whipsaw.
Near the beginning of the Coronavirus panic in the US (March 2020) I created a
YouTube video about Market Turmoil.
I created code that would temporarily turn off strategies. Many traders who watched the
video were amazed at how well it worked.
15 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
16 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
17 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
So, at the very least, this high volatility kill switch is worth trying. Let’s see how it works
on the Crude Oil strategy.
Close all open trades, and do not allow any new trades, if the true range of the just
closed bar is greater than ATRmult times the average true range over the last 5 bars.
Tradestation Code
//*********************************************************
//
//www.kjtradingsystems.com
//
//*********************************************************
18 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
//
input: lookback(10),stopATR(2),ATRMult(1600);
var:CanTrade(True);
//switch criteria
CANTRADE=TRUE;
If close crosses above close[lookback] and close crosses below close[.5*lookback] then buy
next bar at market;
If close crosses below close[lookback] and close crosses above close[.5*lookback] then
sellshort next bar at market;
var:NewStop(3000);
NewStop=StopATR*AvgTrueRange(14)*BigPointValue;
end;
end;
Results
19 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
I optimized for different ATR multipliers, and the best case was better than the baseline
case.
But if the ATR multiplier is too small (meaning the kill switch is more active),
performance can quickly deteriorate. So, like anything else you optimize, it’s a fine line
between success and failure.
Recommendations
As with many of the other ideas presented so far, I’d treat the “kill” switch as a
psychological enhancer, rather than a monetary performance enhancer. You might not
make more profitable, but you will be on the sidelines during periods of crazy volatility.
That might be enough reason to use it.
Summary
So far, I have looked at 4 unique ideas that can reduce the risk of an algo trading
systems:
20 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
3. No Weekend Positions
These 4 ideas can be used alone, or together, depending on your objectives. Don’t expect
them to improve profit performance, but they may help improve your risk adjusted
performance. Plus, they will help you psychologically, and that alone might make them
worth doing.
As with any of the code shown in this article, the keys are:
B. Make sure to properly test and build the strategy (the Strategy Factory process is ideal
for this)
C. Optimize these as little as possible. Try to select parameter values you feel
comfortable with, rather than optimizing.
A few years ago, I got an e-mail from a very excited CTA (Commodity Trading Advisor, a
professional money manager and trader of futures). He had seen an article I had
written, and was super pumped about equity curve trading.
For those of you not familiar with equity curve trading, you use a moving average (or
other technical measure) of the trading system’s equity curve to turn it on or off (to
trade or not trade). For example, you might use a 25 period moving average of the curve
to turn the strategy on and off. Sometimes it works, sometimes it doesn’t. Some
examples of the good, bad and ugly are shown below.
21 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
A few years back, I wrote a few articles on this subject for Modern Trader Magazine
(now out of business).
You can read Part 1 here, and Part 2 here. My conclusion is that equity curve trading
generally did not work, and it can be easily manipulated into making you think it works.
Seriously, take some time and read those articles. They are worth the read. I’ll wait…
22 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
Now, back to the story. This CTA had a US bond strategy he was really excited about, and
he claimed when he added Equity Curve Trading to the mix, his strategy GREATLY
improved. So great in fact, that he thought he would “honor” me by letting me be the
first to invest with his new strategy.
Of course, I probably knew something was wrong (I’m always skeptical), so I asked him
for his spreadsheet where he did the equity curve calculations. Here is what he gave me.
23 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
It is kind of hard to tell from the figure, but if you look at his calculations, he is deciding
to take the trade AFTER the moving average has been calculated (which includes the
trade he is deciding on). In other words, he can see into the future!
24 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
I corrected his mistake, and then the equity curve trading result was worse than the
“always on” case.
25 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
I showed the CTA the correct result, he agreed his method was wrong, YET still claimed
his approach was better profit wise. I guess you can’t fix stupid…
A second pitfall is if you try enough different moving average lengths, or if you try
enough different equity curve trading approaches, you’ll find one the works — one that
really improves things. Just remember, by doing this you are optimizing!
As a result, I don’t recommend equity curve trading, but if you want to try, I would test it
on a completed strategy, try one value for a moving average (don’t optimize) and then
leave plenty of out of sample data to verify your result.
Just for kicks, I tried out a 10 bar moving average equity curve trading approach for 5
random strategies I currently trade (I stopped trading #5 a while back). To be fair, I only
applied equity curve trading to the period after strategy development (true out of
sample performance). Results are below…
26 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
27 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
2 strategies are clearly better NOT using equity curve trading ( “always on” is much
better). 1 strategy is basically the same either way. And 2 strategies (#4 and #5) are
better profit wise with “always on,” but drawdowns do decrease with equity curve
28 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
trading.
In this limited sample, maybe equity curve trading is useful for reducing drawdowns,
and might be a good idea if you can handle the reduced profit.
Your best bet is to try it yourself, and let me know what you think. I have created a
spreadsheet for you to help with equity curve trading, and it is available in the Part 3
Resource Pack.
In parts 1 and 2, I covered quite a bit to help your strategies with risk protection.
Part 1:
No Weekend Trading
Part 2:
At the bottom of this article there is a link so Tradestation users can easily download the
workspaces I used, along with all the code I used for this study.
If you don’t use Tradestation, maybe you should consider it. It would make your life
easier if you want to further my research, and students of my Strategy Factory workshop
can actually get the workshop for free with a Tradestation Rebate program I have.
I’ve also included an equity curve trading spreadsheet. Feel free to modify, just make
sure to double check your modifications — it is easy to make a mistake and “peak” at
future data!
29 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
In Part 1 of this article, I provided 4 effective ways to code risk protection into your
strategies. These techniques resonated with a lot of folks, and the consensus is “we want
more!”
So, I decided to add in 4 more risk protection techniques. These are all variations of the
original 4:
Maybe the Daily Loss Limiter is just a little too restrictive for you. Instead, you’d like to
turn off the strategy if a certain amount of loss occurs in a month.
This technique will “kill” any strategy for the rest of the month when the monthly loss
limit is hit.
Every bar, first check if today is a new month. If it is, calculate the total open plus closed
equity for the strategy at the last bar before midnight. If during the next month, the
current total equity minus the “start of month” equity is less than “equitylosslimit” exit
all current positions and do not take any new trades until the new month starts.
Tradestation Code
//*********************************************************
//
30 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
//www.kjtradingsystems.com
//
//*********************************************************
//
input: lookback(10),stopATR(2),EquityLossLimit(3000);
var:EndMonthEquity(0),CanTrade(True),CurrentEquity(0);
CurrentEquity=NetProfit+OpenPositionProfit;
EndmonthEquity=CurrentEquity[1];
end;
CanTrade=True;
If NetProfit+OpenPositionProfit-EndMonthEquity<-EquityLossLimit then
CanTrade=False;
If close crosses above close[lookback] and close crosses below close[.5*lookback] then buy
next bar at market;
If close crosses below close[lookback] and close crosses above close[.5*lookback] then
sellshort next bar at market;
31 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
var:NewStop(3000);
NewStop=StopATR*AvgTrueRange(14)*BigPointValue;
end;
end;
If you use daily bars, you can use a stoploss statement instead:
Sell next bar at close — xxx stop; //xxx is the price where monthly loss limit would be hit
Results
To see the impact this Monthly Loss limit had, I varied the loss limit from $100 to
$5,000. For this particular strategy, monthly loss limit does not have a huge impact
above $1500. Below that amount, having this limiter can be a real KILLER! Yes, you limit
the monthly loss, but at the expense of no profit!
Consider that when someone says limiting monthly loss is a great idea. It might have the
opposite effect!
32 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
So, the optimum result for the Monthly Daily Loss Limit is shown below:
Looking at the results, you might conclude that the Monthly Loss Limiter is good to
have. But BEWARE! This is an optimized result. The $1700 limit is likely NOT to be the
best monthly limit going forward.
Recommendation
If you want to use a Monthly Loss Limit, decide on the value to use BEFORE you run the
backtest. Don’t necessarily expect a performance improvement, but instead use it
because you like the psychological comfort of limiting your monthly loss. Any
performance improvement should be considered a bonus.
Risk Protection #6 (also known as #2A) — Turn Off After X Consecutive Losers
You hate losing trades. I get it — so do I. So, this risk protection turns off a strategy after
4 consecutive losers. And after it hits those 4 losers, that strategy is off forever.
33 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
Obviously, there are a few variations that I will leave to you to implement. Maybe the
number of consecutive losers is not 4, but rather 2 or 3 or 5 or 10. You should be able to
program it. Also, you can have the strategy off for a certain number of bars since the last
exit before turning it back on (hint: use barssinceexit keyword, I have it in Tradestation
code, commented out)
Tradestation Code
//*********************************************************
//
//www.kjtradingsystems.com
//Kevin Davey — Risk Protection #2A — Kill Strat After 4 Consecutive Losers
//
//*********************************************************
//
input: lookback(10),stopATR(2);
var:CanTrade(True);
CanTrade=True;
34 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
If close crosses above close[lookback] and close crosses below close[.5*lookback] then buy
next bar at market;
If close crosses below close[lookback] and close crosses above close[.5*lookback] then
sellshort next bar at market;
var:NewStop(3000);
NewStop=StopATR*AvgTrueRange(14)*BigPointValue;
end;
Results
For this particular strategy, putting a kill switch in after 4 consecutive losses does not
improve profit, but it did reduce drawdown.
35 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
Recommendation
This type of risk protection is really suited for strategies where there is a potential for
many consecutive losses. A counter trend strategy would be a good example. As with any
of these risk protection techniques, it is best to incorporate the rule in the strategy
BEFORE you backtest. Adding the rule after you see initial results is basically cheating
the backtest.
I would feel comfortable using this approach on a strategy that I knew was designed to
36 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
trade counter to the major trend. It would help keep the strategy from entering loser
after loser
Earlier I looked at not having trades open on weekends. Well, maybe you are OK with
weekend positions, and even overnight positions, but you just do not want to enter new
trades in the less liquid overnight hours (you are OK with exiting trades overnight
though).
The theory here is the overnight signals are less reliable, and prone to more slippage.
Don’t enter any new trades after 1600 and before 700 (chart time).
Tradestation Code
//*********************************************************
//
//www.kjtradingsystems.com
//
//*********************************************************
//
input: lookback(10),stopATR(2),Stoptime(1600),StartTime(700);
var:CanTrade(True);
37 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
CanTrade=True;
If close crosses above close[lookback] and close crosses below close[.5*lookback] then buy
next bar at market;
If close crosses below close[lookback] and close crosses above close[.5*lookback] then
sellshort next bar at market;
end;
var:NewStop(3000);
NewStop=StopATR*AvgTrueRange(14)*BigPointValue;
Results
Yikes!
38 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
The max drawdown is less, and the system is in the market a lot less (which by itself is a
good thing), but you also have to give up almost 75% of profits. Clearly, for this
strategy/market, the “no new trades overnight” trading idea is a financial loser.
Recommendation
This is a neat idea (who would not want stress reduced overnights?), but the benefit
may be more psychological than financial. In fact, as we see with this strategy, it might
39 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
be terrible financially.
But, as with all ideas, I recommend you test it, adding it to your strategy from the start.
Don’t create a strategy and then try to tack this on, since you’ll only accept it if
performance improves (which is cheating!).
Instead, if you like the idea, try it for a while. If you find yourself unable to create good
strategies, then this overnight requirement might be the reason, and you might want to
eliminate it.
Volatility is a double edged sword. As traders, we need volatility to profit from price
movement. So some volatility is a good thing. No volatility is a bad thing. But too much
volatility is a bad thing, too. Low volatility can mess up our algos, and trade us in and
out of trades in an endless whipsaw — or never take any trades at all.
As fate would have it, in my first book I included to Euro strategies. I had traded them
live for a while, and they were pretty good. But then in late 2014, shortly after the book’s
release, Euro volatility tanked:
Algo Trading
HelpLegal
This effectively “killed” those systems for a year or so — neither had been tested on such
a low vol environment.
40 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
So, I created code that would temporarily turn off strategies with low volatility.
Close all open trades, and do not allow any new trades, if the true range of the just closed bar is
less than ATRmult times the average true range over the last 5 bars.
Tradestation Code
//*********************************************************
//
//www.kjtradingsystems.com
//
//*********************************************************
//
input: lookback(10),stopATR(2),ATRMult(1);
var:CanTrade(True);
//switch criteria
CANTRADE=TRUE;
If close crosses above close[lookback] and close crosses below close[.5*lookback] then buy next bar at
41 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
market;
If close crosses below close[lookback] and close crosses above close[.5*lookback] then sellshort next
bar at market;
var:NewStop(3000);
NewStop=StopATR*AvgTrueRange(14)*BigPointValue;
end;
end;
Results
I optimized for different ATR multipliers, and the best case is the baseline case (meaning no
switch is best).
42 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
Recommendations
As with many of the other ideas presented so far, I’d treat the “kill” switch as a psychological
enhancer, rather than a monetary performance enhancer. You might not make more profitable,
43 of 44 6/30/2021, 12:53 PM
Algorithmic Trading Tip — Building Risk Protection Into Your Trading |... https://kjtradingsystems.medium.com/algorithmic-trading-tip-building-ri...
but you will be on the sidelines during really slow times. That might be enough reason to use it.
Summary
In Part 4, I have looked at 4 unique ideas that can reduce the risk of an algo trading systems:
2A. Kill After 4 Consecutive Losses (variation of Delay After Losing Trade)
4A. Low Volatility Kill Switch (variation of High volatility “Kill” Switch)
These 4 ideas can be used alone, or together, depending on your objectives. Don’t expect them to
improve profit performance, but they may help improve your risk adjusted performance. Plus,
they will help you psychologically, and that alone might make them worth doing.
As with any of the code shown in this article, the keys are:
B. Make sure to properly test and build the strategy (the Strategy Factory process is ideal for this)
C. Optimize these as little as possible. Try to select parameter values you feel comfortable with,
rather than optimizing.
Good Luck, thanks for reading, and let me know how these work out for you!
To access the resource package, with Tradestation code and workspaces, simply visit:
https://kjtradingsystems.com/riskstudycode.zip and https://kjtradingsystems.com
/riskstudycode2.zip.
44 of 44 6/30/2021, 12:53 PM