19th Ave New York, NY 95822, USA

How To Find The Best Candlestick Patterns With Amibroker

LOREM IPSUM DOLOR SIT AMET

Japanese candlestick patterns are useful because they allow traders to quickly visualise price action in the market. There are many different patterns and many different opinions on their effectiveness.

In this article we will look at a trading system that attempts to dynamically select the best performing candlestick pattern from the previous six months and trade that pattern for the next month. Amibroker formula will also be provided.

System Rules

The idea of this trading strategy is to find the most profitable candlestick pattern over a six month period (in-sample) and then trade that pattern for the next month (out-of-sample).

We then step forward a month and repeat the process so that we are always selecting the most profitable candlestick pattern from the prior six month period.

The idea is that we are attempting to stay in sync with the market and only trade the best price action pattern.

Example

For example, let’s say we backtest Apple stock between 1-January 2000 and 1-July 2000 and we find that the best performing candlestick was the hammer.

We will then select the hammer as our buy rule and run the backtest between 1-July 2000 and 1-August 2000 (one month on).

After this is done, we will shift the in-sample one month forward and repeat the process.

So we move the in-sample backtest forward to 1-February 2000 – 1-August 2000. Then we backtest the best performing candlestick from that period on the next out-of-sample segment. Which would be between 1-August 2000 – 1-September 2000.

This process is also known as walk-forward optimisation and it is a good method to counter the problem of curve fitting.

Amibroker Formula

For this test, we will be optimising on four of the most popular candlestick patterns; bullish engulfing, bearish engulfing, piercing line and hammer.

We will use VarGet and the Optimize function in Amibroker to cycle through each of the candlestick patterns during the in-sample. The walk forward function will be used to process the simulation using profit factor as our target.

The formulas for the individual candlestick patterns is taken from Candlestick Analysis For Professional Traders where I analyse the performance of over 25 patterns.

The code we will use for this test is shown next:

//For educational purposes. Full Disclaimer applies.
//Initial version derived from https://www.mail-archive.com/amibroker@yahoogroups.com/msg38981.html h/t & thanks to gucytribe
SetOption("InitialEquity",100000);
SetOption("CommissionMode",3);
SetOption("CommissionAmount",0.01);
SetOption("AllowSameBarExit", 1);
SetPositionSize( -100, spsPercentOfEquity );
SetTradeDelays(1,0,1,1)
BuyPrice = O;
SellPrice = C;

// Candles

O1 = Ref(O,-1);
H1 = Ref(H,-1);
L1 = Ref(L,-1);
C1 = Ref(C,-1);
Downtrend = L < Ref(LLV(L,10),-1);

BullishEngulfing = (C1<O1) AND (C>O) AND (O<C1) AND (C>O1);
BearishEngulfing = (C1>O1) AND (C<O) AND (O>C1) AND (C<O1);
PiercingLine = (C1<O1) AND (((O1+C1)/2)<C) AND (O<C) AND (O<C1) AND (C<O1);
Hammer = (((H-L)>2*(O-C)) AND ((C-L)/(H-L)>0.66) AND ((O-L)/(H-L)>0.66)) AND Downtrend;

// Entry/exit rules

S1 = BullishEngulfing;
S2 = BearishEngulfing;
S3 = PiercingLine;
S4 = Hammer;

SetupIdentifier = Optimize("Setup",1,1,5,1);
SetupType = VarGet("S" + SetupIdentifier);

Buy = SetupType;
Sell = 1;

//End

Backtest – SPY

In order to test this trading idea we will run a walk forward optimization on SPY between 1/2000 – 1/2017. SPY is a highly liquid ETF that tracks the S&P 500 so is ideal for our analysis.

Entries will be placed on the open following the candlestick pattern and exited on the same day close. Commissions will be set at $0.01 per share and we will use a fully invested starting capital of $100,000.

Backtest Results

The following statistics and equity curve show the concatenated results from the walk forward test:

  • Net Profit: $18,225.97
  • Annual Return: 1.01%
  • Maximum Drawdown: -5.43%
  • Risk Adjusted Return: 38.25%
  • CAR/MDD: 0.19
  • Win Rate: 56.14%
  • Profit Factor: 1.47
  • # Trades: 114
  • Exposure: 2.65%

Equity curve for dynamic candlestick pattern selection system

As you can see, the simulation was profitable and risk-adjusted returns were not too bad at 38.25%. However, the total return is not good and most of the strong performance came between 2000-2005.

The next chart shows how the candlestick pattern changed throughout the simulation:

best candlestick pattern by month

The first few months the best performing pattern was the bearish engulfing pattern. It then changed to bullish engulfing before changing again to the hammer. Piercing line was a not a strong pattern and not used until 2003.

Backtest – E-Mini S&P 500

We will now go through the same process but this time we will test the E-Mini S&P 500 futures contract.

Once again, we will run a walk forward optimisation between 1/2000 – 1/2017 and hold trades for one bar. Commissions will be set at $20 round trip and position size will be one contract. We will also use a fixed stop loss of ATR(1)*3.

Backtest Results

The following stats and equity curve show the results of the walk forward this time on the E-Mini S&P 500 future:

  • Net Profit: $6925
  • Annual Return: 0.40%
  • Maximum Drawdown: -9.25%
  • Risk Adjusted Return: 196.66%
  • CAR/MDD: 0.04
  • Win Rate: 53.53%
  • Profit Factor: 1.16
  • # Trades: 170
  • Exposure: 0.21%

candlestick pattern selection for E-Mini S&P 500 contract

As you can see, the simulation was profitable overall and produced a net profit of $6925. Risk adjusted returns were OK and exposure was very low.

Everything else was mediocre and you wouldn’t want to trade this without significant improvements.

Thoughts & Observations

In this article we created a trading strategy that dynamically selects the best candlestick pattern from the previous six months and trades it the following month.

We saw that when applied to the SPY ETF and E-Mini S&P 500 future, the results were profitable but not desirable.

I must tell you that I spent a fair amount of time on this. I tested quite a few ideas and different markets without much success and I got the feeling I was wasting my time.

A big problem here is getting a large enough sample size to make the results reliable. Some candlestick patterns are quite rare on daily charts and this is why we only used the four most common patterns.

It might be better to apply this strategy to intraday data instead. With intraday data we will be able to gather a lot more signals and increase the sample size.

Overall, the results were not very good but the idea (dynamic selection) could still be very useful.

Instead of optimising candlestick patterns, we could cycle through different trade setups, different indicators, correlation values etc.

There are numerous possibilities that can be looked at and incorporated using the Amibroker formula shown above. Try it out for yourself!

 

Simulations in this article created with Amibroker using data from Norgate Investor Services.


Comments (3)

I’ve suspected that you can’t use candlesticks alone. An annual return of 0.4% is small, so we have to find other filters.

Joe . . .
Appreciate your examples and descriptions.

For the effort involved so far I have found SectorSurfer hard to better.

I am a subscriber t it at http://www.Sumgrowth.com.
The basic platform allows a buy list of 12 symbols (or strategies) holding one position on a six month walk forward approach.

Send me 12 symbols and I’ll send you back the result. Display time frames are from one month to some ten plus years.

By grace,

Buck

rbuckgray@gmail.com

Ok. Thanks for your comment.

Leave a comment