Can You Turn Failure into Success?

Have You Ever Wondered If You Just Reversed the Logic?

You have been there before. What you thought was a great trading idea turns out to be a big flop. We have all developed these types of algorithms. Then it hits you, just flip the logic and in turn the equity curve. Hold your horses! First off you have to make sure it’s not just the execution costs that is hammering the equity curve into oblivion. When testing a fresh trading idea, it is best to keep execution costs to zero. This way if your idea is a good one, but is simply backward, then you have a chance of creating something good out of something bad. I was playing around with a mean reversion day trading algorithm (on the @ES.D – day session of the mini S&P 500) that created the following equity curve.  Remember to read the disclaimer concerning hypothetical performance before proceeding reading the rest of this blog.  It is located under the DISCLAIMER – REAMDE! tab.  By reading the rest of this blog post it implies that you understand the limitations of hypothetical back testing and simulated analysis.

The pandemic created a strong mean reversion environment. In the initial stage of this research, I did not set the executions costs – they defaulted to zero. My idea was to buy below the open after the market moved down from the high of the day a certain percentage of price. Since I was going to be buying as the market was moving down, I was willing to use a wide stop to see if I could hold on to the falling knife. Short entries were just the opposite -sell short above the open after the market rallied a certain percentage of price. I wanted to enter on a hiccup. Once the market moved down a certain range from the high of the day, I wanted to enter on a stop at the high of the prior bar. I figured if the price penetrated the high of the prior five-minute bar in a down move, then it would signal an eventual rotation in the market. Again, I was just throwing pasta against the wall to see what would stick. I even came up with a really neat name for the algorithm the Rubber Band system – stretch just far enough and the market is bound to slam back. Well, there wasn’t any pasta sticking. Or was there? If I flipped the equity curve 180 degrees, then I would have a darned good strategy. All it would take is to reverse the signals, sell short when I was buying and buy when I was selling short. Instead of a mean reversion scheme, this would turn into a momentum-based strategy.

Here are the original rules.

maxCloseMinusOpen = maxList(close - todaysOpen,maxCloseMinusOpen);
maxOpenMinusClose = maxList(todaysOpen - close,maxOpenMinusClose);

if c < todaysOpen and todaysOpen-c = maxOpenMinusClose and
(maxCloseMinusOpen + maxOpenMinusClose)/c >= stretchPercent Then
canBuy = True;
if c > todaysOpen and c- todaysOpen = maxCloseMinusOpen and
(maxCloseMinusOpen + maxOpenMinusClose)/c >= stretchPercent Then
canShort = True;
Guts of the complete failure.

Here I measure the maximum distance from the highest close above the open and the lowest close below the open.  The distance between the two points is the range between the highest and lowest closing price of the current day.  If the close is less than today’s open, and the range between the extremes of the highest close and lowest close of the trading day is greater than stretchPercent, then an order directive to buy the next bar at the current bar’s high is issued.  The order is alive until it is filled, or the day expires.  Selling short uses the same calculations but requires the close of the current bar to be above the open.   The stretchPercent was set to 1 percent and the protective stop was set to a wide $2,000.  As you can see from the equity curve, this plan did not work except for the time span of the pandemic.  Could you optimize the strategy and make it a winning system.  Definitely.  But the 1 percent and $2000 stop seemed very logical to me.  Since we are comparing the range of the data to a fixed price of the data, then we don’t need to worry about the continuous contract distortion.  Maybe we would have to, if the market price was straddling zero.  Anyways, here is a strategy using the same entry technique, but reversed, with some intelligent trade filtering.  I figured a profit objective might be beneficial, because the stop was hit several times during the original test.

$2K was hit often!
Using some trade filtering and stop loss and profit objective on the reversal of the original strategy.

If you like the following code, make sure you check out my books at Amazon.com.  This type of code is used the Hi-Res and Day-Trading editions of the Easing_Into_Easylanguage series.

input: stretchPercent(0.01),stopLoss(1000),takeProfit(1000),
dontTradeBefore(930),dontTradeBeforeOffset(5),
dontTradeAfter(1500),dontTradeAfterOffset(5),
rangeCompressionPercent(0.75);

vars: buysToday(0),shortsToday(0),mp(0),atr(0),canBuy(False),canShort(False),canTrade(False);
vars: todaysOpen(0),maxCloseMinusOpen(0),maxOpenMinusClose(0);
if t = sessionStartTime(0,1)+barInterval Then
Begin
todaysOpen = open;
maxCloseMinusOpen = 0;
maxOpenMinusClose = 0;
buysToday = 0;
shortsToday = 0;
canTrade = False;
atr = avgTrueRange(20) of data2;
if trueRange of data2 < atr * rangeCompressionPercent Then
canTrade = True;
canBuy = False;
canShort = False;

end;

mp = marketPosition;

if mp = 1 and mp <> mp[1] then buysToday +=1;
if mp =-1 and mp <> mp[1] then shortsToday +=1;

maxCloseMinusOpen = maxList(close - todaysOpen,maxCloseMinusOpen);
maxOpenMinusClose = maxList(todaysOpen - close,maxOpenMinusClose);

if c < todaysOpen and todaysOpen-c = maxOpenMinusClose and
(maxCloseMinusOpen + maxOpenMinusClose)/c >= stretchPercent Then
canShort = True;
if c > todaysOpen and c- todaysOpen = maxCloseMinusOpen and
(maxCloseMinusOpen + maxOpenMinusClose)/c >= stretchPercent Then
canBuy = True;


if canTrade and t >= calcTime(dontTradeBefore,dontTradeBeforeOffset) and
t < calcTime(dontTradeAfter,dontTradeAfterOffset) and t < sessionEndTime(0,1) Then
begin
if shortsToday = 0 and canShort = True Then
sellshort next bar at l stop;
if buysToday = 0 and canBuy = True Then
buy next bar at h stop;
end;


setExitOnClose;
setStopLoss(stopLoss);
setProfitTarget(takeProfit);
The anti Rubber Band Strategy

Trade filtering was obtained by limiting the duration during the trading day that a trade could take place.  It’s usually wise to wait a few minutes after the open and a few minutes prior to the close to issue trade directives.  Also, range compression of the prior day seems to help in many cases.  Or at least not range expansion.   I only allow one long entry or one short or both during the trading day – two entries only!  Read the code and let me know if you have any questions.  This is a good framework for other areas of research.  Limiting entries using the mp variable is a neat technique that you can use elsewhere.

And as always let me know if you see any bugs in the code.  Like Donnie Knuth says, “Beware of bugs in the above code; I have only proved it correct, not tried it!”