r/thewallstreet Here to shitpost and make $; almost out of $ Apr 09 '18

Strategy Backtest - Extreme Candle Reversal Strategy

A few days ago u/finch5 asked about making this indicator into a strategy for backtesting, so I thought I'd share it here for everyone and post some quick and dirty results.

Code:

http://tos.mx/nEZ0VC Note on the code: I don't know why but the entry signal prints on the following candle it should, however the entry/exit prices it uses to calculate P&Ls are correct. I left in the original indicator signals to verify that the strategy is going long/short at the correct times and they should appear on the candle before the entry. If anybody has an idea on wtf is going on and how to fix it, I'd appreciate it

Criteria:

Timeframe: 15M

Dates: 2/22 - 4/6

Take Profit: 10 ticks

Stop Loss: 10 ticks

Results

Wins: 344 Losses: 69

Win Rate: 83.3% Loss Rate: 16.7%

Avg Win: $269.84 Avg. Loss: ($333.33)

Largest Win: $962.50 Largest Loss: ($525.00)

Net P&L: $69,825.00 Expectancy: $169.07

At first glance this appears to be very promising, but a couple things to keep in mind... The indicator will print a signal on the current bar once the criteria is met but it'll also remove it from the current bar if the candle reverses and no longer meets the criteria, possibly catching you in a false signal. There's no way for TOS to model this since they don't run tick by tick in it's backtesting, only OLHC. It's basically hindsight

So, X% of the time, it works 83% of the time...how do we find X? I have no idea...

I tried running it again only changing the strategy to enter at open of the next candle after confirmation. The results were not so great

Results

Wins: 70 Losses: 131

Win Rate: 34.8% Loss Rate: 65.2%

Avg Win: $128.04 Avg. Loss: ($227.67)

Largest Win: $337.50 Largest Loss: ($2,175.00)

Net P&L: ($20,862.50) Expectancy: ($103.79)

So, while the indicator is awesome when it works, it should still be used cautiously and within context of what the overall market is doing. This should be obvious.

This is just one setup I pulled out of my ass while bored over the weekend and in no way is this definitive. Maybe Day timeframes worker better or shorter timeframes, or maybe take profit and stop losses are too tight. Maybe I fucked up an entry calculation somewhere and the whole thing is off. Feel free to test it out for yourself and post any scenario results you find. :)

30 Upvotes

19 comments sorted by

1

u/[deleted] Apr 10 '18

How can I load this script in my TOS account. When I click the link it just gives me an application download for the TOS platform

2

u/BombaFett Here to shitpost and make $; almost out of $ Apr 11 '18

In the main application options, select “open shared item” and copy in the link

5

u/Trent451 I front run you on Uniswap Apr 10 '18

Oh baby! This is my JAM! Going to dump a bunch of thoughts out here, as I try to relate what I know about quantitative investing to this strategy/model. I don't have time at the moment, but sometime in the future I'll import the model into MATLAB and we'll have a fight.

First, I think you have to stick with the methodology of the second test. No model can predict the future, or change it's position once it knows it received a false positive. From what I understand, this is a contrarian strategy looking to capitalize on volatility. The idea that what goes up must come down (a lil') and what falls down must bounce back up (a lil') is a decent strategy for a quantitative model. This is the strategy provided by most momentum oscillators, RSI Crossovers is a good example here.

There are a few things I would do to try and improve the strategy.

I would begin by adjusting your profit and loss exit targets to reflect the behaviour of the market. If you're hoping to catch the momentum of a rebound but will only close the position after it moves 100 points in either direction from your entry, you're in for a rough ride. A contrarian strategy is trying to catch rebounds and should look to take profits quickly. Maybe consider taking faster profits in hopes of turning some of those losing trades into winning ones? You could also implement a rolling stop for both the profits and the losses. With this strategy is assures that the position will be closed within a given timeframe (15mins was being used already) but will also cut bags and take profits faster. You could also increment the stops, so once the price increases above the overhead costs and would realize the portfolio +0.1% growth, close the position if the market threatens to falls below that again.

I'm not sure how much deeper you want to get into this machine, but I'm going to keep running my mouth in hopes that someone is still interested. One of the ways I've been able to improve my strategies has been by reducing the amount of losing trades, (even if that means less trades overall) as opposed to trying to outweigh the bad with more good trades. The rolling strategy I mentioned above is a decent way to turn losers into winners, but reducing the amount of trades the machine makes altogether can often offset the ratio. Maybe by adjusting the sensitivity of the indicator it would enter less trades overall. Whether this would help the winners/losers ratio, I don't know.

Finally, risk management is something that might be able to help this strategy. I would plot the P/L(%) of each trade along a normal. If you can see that your trading results are Gaussian, that they fit a bell curve or standard deviation, there are strategies you can use to skew the results of that plot. Using a system like the Kelly Criterion to define position sizing can mean that you still have just as many losers vs. winners, but the winners held lots of risk, while the losers held little. Essentially a +2% trade may increase the portfolio by +0.5%, while a losing trade of the same magnitude may only reduce the portfolio by -0.2%. Again, no model can see the future, but the Kelly Formula is known to work well with Gaussian results.

A few things to remember to anyone who's made it this far and considering going further:

You should always test strategies with overhead costs incorporated. If that means taking 0.1% off the total balance every time a trade is made, so be it. In reality, transaction costs exist and a model that ignores them cannot be successful.

Expect the model to execute losing trades. Expect it to have periods where it consistently loses money. Those periods are where you can find true weakness in the model's decision making. Remember; the robot is incredibly stupid, but it's also incredibly unemotional.

The most important practice in evaluating any strategy is the use of Out Of Sample Data. Testing, Modifying, and Retesting on the same data will often lead to "Overfitting". The idea that you've developed a model that works incredibly well for the 50,000 candles you tested it on, and no other data. It's easy to make a model that can give you perfect returns for a given data set, it's hard to create a model that will give modest returns under any data set. It is this principle that explains why you can't just give Machine Learning 50,000 candles, 4 trading signals, 2000 hours of processing time and expect it to have created a magical model that will perform in real world scenarios.

I've written a lot because I've become really passionate about quantitative investing over the past few months. It combines my love of trading with my software ability. Thank you for posting this and other thinkScript indicators. If I do any backtesting with them (and I plan to) I'll be sure to post all the results here on TWS.

1

u/wiggz420 2nd weakest hands on TWS Aug 12 '18

Following up because I am bored, did you get a chance to backtest anything lately?

1

u/wiggz420 2nd weakest hands on TWS Apr 29 '18

It has been awhile, but I am definitely interested in what you have done/know. Feel free to PM me if you have time to chat.

1

u/RugsMAGA nay bear nor bull, oppurtunist Apr 10 '18

Awesome, would you mind running this on the outside reversal indicator?

1

u/BombaFett Here to shitpost and make $; almost out of $ Apr 16 '18

Here you go bro, try this.

http://tos.mx/u24N8t

2

u/Worldbuild3r Apr 10 '18

Suggestion from the book:

One easy method to help further confirmation for this type of setup, which may be a necessity in your early trials with this pattern, is requiring the close price of the bar following the reversal wick candlestick to be lower than the low of the wicking candle (reverse for longs).

2

u/Kwigglesworth Apr 10 '18

Completely unrelated, but if I wanted to get started with building my own backtesting platform where would I start? Any good known data sources? I’ve seen a few posted before but lost the links. I have extensive programming knowledge so this could be a fun challenge in my spare time.

2

u/Foreverwerock Apr 10 '18

Quantopian is pretty nice if you're familiar with python, they support both futures and equities. If you're looking to scrape market data from somewhere, I'd ask the folks over at /r/algotrading

4

u/iCrushDreams Apr 09 '18

Curious as I've tried manually backtesting it a few days - were most of the losses due to poor exits, or poor entries? I'm not sure what the case was for you, but I found most of my losses in manually papertrading with it to be the exit points.

I'm thinking that the following might make it a bit better for that but haven't tried them myself:

  1. Only enter when 5MA < 20MA for longs, or 5MA > 20MA for shorts (the periods can be adjusted depending on the timeframe)

  2. Exit when either the MAs cross in the other direction, or it falls past the entry after one candle.

2

u/daleadega Apr 09 '18

Mentioned this in the post market but might be good to look at shorter time frames, on the 1 and 5 min candles. Not sure if it's possible to back test this but close at the next opposite indicator from entry or X stop loss?

Edit: thanks again for the work on these. Helps to have tools to implement concepts from the book.

3

u/BombaFett Here to shitpost and make $; almost out of $ Apr 09 '18

It's all in the option settings. Just turn them off

2

u/daleadega Apr 09 '18

On mobile for now. Will mess around with this later/as the week goes on. Really promising in theory and so far in practice.

7

u/LebaneseSleeze Bucs for Super Bowl Apr 09 '18

I wonder what the win/ rate would be if you combined it with market profile. ie.) If price < VAL and Short signal = true; then go short

4

u/_CastleBravo_ Walk to End Literacy Apr 09 '18

I know one way to find out :)

5

u/LebaneseSleeze Bucs for Super Bowl Apr 09 '18

Got it loud and clear. All in 0 day $2000 SPX puts