We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 14
tana Th Ful Gude yen Py Tu Sap | 201 acu
You have free member-only story left this month. Sign up for Medium and gst an extra one
Heiken-Ashi Trading— The Full
Guide in Python.
Presenting and Back-testing the Heiken-Ashi Candlestick Technique,
Soflenkaabar (1
Jan 29-6min eadttpsufwwsightesp.com/en/educaton/tundamentals/how-to-read-a-candestick-cht
Candlestick charts are the most common form of graphical analysis. They
are an easy way to understand market psychology while having all the
quantitative information needed for the market price. In this article, we will
introduce the concept of candlesticks and then discuss another variation of
candlesticks called Heiken-Ashi. Finally, we can back-test a strategy that
uses the Heiken-Ashi chart.
Ifyou want to see how to create candlestick trading systems in Python, feel
free to have a look at the below article:
oxime mioaioheatain fugue Hak ave‘moot aaa Trg The Fld nye oy Sta
How To Create Candlestick Trading Systems in Python
Candlestick Trading: A Full Quide on Patterns and Strategies
rmediumom
Japanese Regular Candlesticks
Candlesticks are a quick way to understand OHLC data and detect patterns.
Itis very straightforward and easy to interpret. A bullish (typically green)
candle is when the market closes above its opening price. A bearish
(typically red) candle is when the market closes below its opening price.
High Pie High Pace
hsing Price ‘open Price
Moxinedin cmioanoheatai tf gucer-pyor Hak weaan as Treg Th Flue non by Stan kana Te Srp a2 acu
pn Price Cosi Pie
Let us see a full chart of candlestick to understand more how it is shown.
‘The trend is clearly bullish with some corrections seen around the red.
candles. Notice the small candles where the opening price is almost the
same as the closing price. This is called a Doji and signifies indecision and
shows a possible reversal or consolidation.‘moot ans Tare Th Fle yen oy Sta
ett
Now, that we have seen what a candlestick chart is, we can proceed with
the definition and coding of the Heiken-Ashi chart.
Heiken-Ashi Candlesticks
The Heiken-Ashi (Also called Heikin-Ashi) candlesticks seek to clean out
the picture and show a clearer trend by smoothing out the OHILC data. Here
is how to calculate the Heiken-Ashi candlesticks:
Open + High + Low + Close
Close Price =
4
Open of Previous Bar + Close of Previous Bar
Open Price =
p 2
oxime mioaioheatain fugue Hak waswanaeet tan Tha la yn iy Sota S25 an
‘And to calculate the high and low price, we take the maximum and
minimum prices of the following:
High Price = max (Open, High, Close)
Low Price = min (Open, High, Close)
‘The above formulas will smooth out the candles to give us a more defined
and clear trend.
(n
ey
+ ‘Wi A MS
EURUSD hourly chart, Regular eanlestckson the ff and Helken-Aehi candlesticks onthe right image by Author)
To code a function in Python that adds 4 new columns containing OHLC
oxime mioaioheatain fugue Hak wa‘moot aan Ay Tg The lc ner by Sten ka
Heiken-Ashi data, we can use the below code block:
def heiken ashi (Data, opening, high, low, close, where):
4 Heiken-Ashi open.
trys
for i in range(ien (Data):
Datali, wnere] ~ (Data[i - 1, opening] + pata(i - 1,
slose)) / 2
except:
ass
# Heiken-Ashi Close
For i in range(len (Data) }
Datali, waere + 3] = (Datali, opening] + Datali, high] +
Data(i, low) + Datali, closel) / 4
# Heiken-Achi High
for i in cange (Len (Data) ):
Datali, waere + 1] = sax (Datali, where], Datali, where + 3],
batati, Bighl)
# Heiken-Ashi Low
for i in range (Len (Data) )
Datali, waere + 2] = min(Datali, where], Datali, where + 31,
patati, iew1)
# To be used on an OMLC array with a few colums to spare
nny ohle_array = heixen_ashi (ny ohle-array, 0, 1, 2, 3, 4)
# The numbers signify in order: Open, High, Low, Close, then the
coluan indexed at 4 is where the first new Heiken OHIC data will be
oxime mioaioheatain fugue Hak a‘moot aaa Trg Th Flue non Oy Stan kana Te Srp a2 acu
populated (Meaning that the columns 4, 5, 6, and 7 will have a brand
yew OHLC data)
i i i
‘wid M
fhm
At, in 3 pe
asl if
LUSOCHE hourly chart, Regular candlesticks on the left and Heler-Ashi candlesticks on te right. dmage by Author)
flowy
Not
The two charts are simple a comparison between a regular candlestick chart
and a Heiken-Ashi chart, We can see the resemblance but we can also see
that the latter is smoother. We should note however that since the Heiken-
Ashi method is simply an averaging, the price shown may not be the true
market price.
kk flac | : Wh tha |
onsinedim rolnnoken-atireny iit giceroyterr BOP 8lt ee fp" “+ Mle /
arr"
‘wl
AUDCAD hourly chart, Regula candlesticks onthe let and Hetkon-Aehl candlesticks on the tght mage by Autho®)
Creating the Strategy
The basic strategy using the Heiken-Ashi charts to simply buy and sell
whenever the color of the candles changes. This should give us the
following conditions:
+ Go long (Buy) whenever the Heiken-Ashi candle changes from red
to green. Hold the position until getting a contrarian signal. We
have to be careful to initiate the position using the true market
price and not the one displayed on the Heiken-Ashi chart.
+ Go long (Buy) whenever the Heiken-Ashi candle changes from red
to green. Hold the position until getting a contrarian signal. We
eninetun container abee py OOS wef ih
gl Oy
“| oan dt
fy dt Ay Hf
L My ,‘moot aan as Treg Th Flue non ty Stan kana Te Srp an. 224 acum
‘We can code the signal function of the strategy as follows:
def signal (Data, heiken close, heiken_open, buy, sell):
for i in range (len (Data) ):
£ Datali, heiken_close] > Data[:, heiken_open] and Datali -
} hedken open}?
~patali, buy) = 1
yata[i, heiken_close] < dat
3 batal eiken._¢
1, sell
heiken_open] and Datali -
Ly heiken,¢
# The variable heiken close reft
to the Heiken closing candle
# the variable heikenopen x
‘to the Heiken opening candle
| \naiA
oxime mioaioheatain fugue Hak te‘moot aan as Treg Th Flue non ty Stan kana Te Srp an. 224 acum
(onthe right, the Helken Ashi chart on EURUSD. On the let, the signals generated on the same chert. (Image by
Author)
Now, we will see the results based on the above strategy. Intuitively, this is a
trend-following strategy. We will not use any risk management system for
this back-test.
Heiken Ashi Strategy
“Hs | Waal
oxime mioaioheatain fugue Hak sa‘moot aan as Treg Th Flue non ty Stan kana Te Srp an. 224 acum
Equity curves following the Helken-Ash strategy image by Author)
Conclusion
Why was this article written? It is certainly not a spoon-feeding method or
the way to a profitable strategy. If you follow my articles, you will notice
that I place more emphasize on how to do it instead of here it is and that I
also provide functions not full replicable code. In the financial industry, you
should combine the pieces yourself from other exogenous information and
data, only then, will you master the art of research and trading.
Lalways advise you to do the proper back-tests and understand any risks
relating to trading. For example, the above results are not very indicative as
the spread we have used is very competitive and may be considered hard to
constantly obtain in the retail trading world (but not impossible). However,
with institutional bid/ask spreads, it may be possible to lower the costs such
as that a systematic medium-frequency strategy starts being very profitable.
Moninedin nioaiokeanttein tf gucer-eytiont sa‘moot aan as Treg Th Flue non ty Stan kana Te Srp an. 224 acum
DataScience MachneLoaming —Arificiareligonce Investing Fnance
Learn more. Make Medium yours. Share your thinking.
Moninedin nioaiokeanttein tf gucer-eytiont