Project Report Format (Repaired)

You might also like

You are on page 1of 22

Identifying Candlestick Patterns using

Deep Learning
A Report Submitted to the

C.V. Raman Global University, Bhubaneswar

In partial fulfillment of the Award of the degree of

BACHELOR OF TECHNOLOGY
In
COMPUTER SCIENCE AND ENGINEERING
By
Bharat Shubham
Amritansh Soni
Anish Sahay
Rishabh Jha
Under the supervision of
Asst. Prof. Rasmi Ranjan Khansama

Department of Computer Science and Engineering


C.V. Raman Global University, Bhubaneswar
May 2021
CERTIFICATE OF ORIGINALITY
This is to certify that this major project report entitled “Identifying Candlestick Patterns using
Deep Learning”, submitted by “Bharat Shubham, Amritansh Soni, Anish Sahay, Rishabh Jha” is
partial fulfillment for the award of the degree of Bachelor of technology is a research work / web
based development ( any one as per the project chosen by the group) carried out by them
under my supervision during the period 2020-2021.The report has fulfilled all the requirements
as per regulations of the University.

Asst.Prof Rasmi Ranjan Khansama XXXXXXXXXX Dr. S.K Bisoy

Project Guide External Head, CSE


DECLARATION
This major project report is a presentation of our original research work / original web based
development. Whenever contributions of others are involved, every effort is made to indicate this
clearly. With due reference to the literature and acknowledgement of collaborative research and
discussion. The work was done under the guidance of Rasmi Ranjan Khansama. The results embodied
in this thesis have not been submitted to any other university or Institute for the award of any degree
or diploma.

Bharat Shubham (1801227157)


Amritansh Soni (1801227079)
Anish Sahay (1801227088)
Rishabh Jha (1801227388)
Department of Computer Science and Engineering
C.V. RAMAN GLOBAL UNIVERSITY
ACKNOWLEDGEMENT
We are pleased to acknowledge Rasmi Ranjan Khansama for their invaluable guidance during
the course of this project work. We extend our sincere thanks to our HOD Dr. Sukant Kishoro
Bisoy for the immeasurable support.
We are also grateful to other members of the CSE Department who co-operated with us
regarding some issues.

Bharat Shubham (1801227157)


Amritansh Soni (1801227079)
Anish Sahay (1801227088)
Rishabh Jha (1801227388)
ABSTRACT
Candlestick chart pattern is a technical tool that encapsulates the price of the asset for multiple time
frames into a single price bar. The expertized trader can predict the price trend of the asset by looking at
the pattern of some adjacent candlesticks. This project proposes the architecture for predicting the short
trend of the stocks by using the convolutional neural network and the candlestick patterns. The
experiments are conducted with a set of candlestick pattern images collected from various stocks in the
stock exchange of India. Each image captures six to twelve adjacent candlesticks. The experimental
results indicate that the proposed method can correctly predict the short trend for most stocks with
acceptable accuracy.
Contents
Certificate Of Originality ………………………………………………………………………………..i

DECLARATION …………………………………………………………………………………………ii

ACKNOWLEDGEMENTS...............................................................................................................iii

ABSTRACT .....................................................................................................................................iv

CONTENTS ……………………………………………………………………………………………..v

INTRODUCTION.............................................................................................................................1

LITRATURE SURVEY……………………………………………………………………………5

RESULT ANALYSIS……………………………………………………………………………….8

CONCLUSION AND FUTURE WORK…………………………………………………………..14

REFERENCES………………………………………………………………………………………15
CHAPTER 1
INTRODUCTION
Investment has become an integral part of our daily lives, and many people have become
accustomed to using their personal assets to generate more profit. In general, conservative
investors place their excess cash in banks, funds, or the stock market, and expect to obtain
returns on interest and dividends. In contrast, active investors prefer to use their funds to buy/sell
stocks, funds, futures, options, and so on, expecting to get higher returns. As a whole, since stock
investment can produce high profitability (although there is a risk), it is one of the most popular
investment activities. Although stock investment can bring high returns for investors, the stock
market is affected by macroeconomic and international factors, stock market volatility, as well as
governmental policies that combine to make it very difficult to achieve an assured level of
accurate stock prediction.
The main idea behind stock prediction is to analyze the available information from the stock
market in order to effectively predict future trends (stock prices), which can increase profit. The
first approach is aimed at analyzing the general economic situation, current industrial trends, and
other long-term factors. On the other hand, technical analysis focuses on the short-term stock
price (also called momentum) in the stock market.
Understanding Candlestick Charts
Candlesticks show that emotion by visually representing the size of price moves with different
colors. Traders use the candlesticks to make trading decisions based on regularly occurring
patterns that help forecast the short-term direction of the price.

A candlestick chart is a style of financial chart used to describe price movements of a security,
derivative, or currency. Each "candlestick" typically shows one day, thus a one-month chart may
show the 20 trading days as 20 candlesticks. Candlestick charts can also be built using intervals
shorter or longer than one day. It is similar to a bar chart in that each candlestick represents all
four important pieces of information for that day: open and close in the thick body; high and low
in the “candle wick”. Being densely packed with information, it tends to represent trading
patterns over short periods of time, often a few days or a few trading sessions.

FIGURE: 01

Candlestick chart of EUR/USD currency pair on daily timeframe in Meta-Trader 5 trading platform.

Candlestick charts are most often used in technical analysis of equity and currency price patterns.
They are visually similar to box plots, though box plots show different information.

Candlestick Components

Just like a bar chart, a daily candlestick shows the market's open, high, low, and close price for
the day. The candlestick has a wide part, which is called the "real body." This real body
represents the price range between the open and close of that day's trading. When the real body is
filled in or black, it means the close was lower than the open. If the real body is empty, it
means the close was higher than the open.
FIGURE: 02

Traders can alter these colors in their trading platform. For example, a down candle is often
shaded red instead of black, and up candles are often shaded green instead of white.

Description

The area between the open and the close is called the real body, price excursions above and
below the real body are shadows (also called wicks). Wicks illustrate the highest and lowest
traded prices of an asset during the time interval represented. The body illustrates the opening
and closing trades. The price range is the distance between the top of the upper shadow and the
bottom of the lower shadow moved through during the time frame of the candlestick. The range
is calculated by subtracting the low price from the high price.

If the asset closed higher than it opened, the body is hollow or unfilled, with the opening price at
the bottom of the body and the closing price at the top. If the asset closed lower than it opened,
the body is solid or filled, with the opening price at the top and the closing price at the bottom.
Thus, the color of the candle represents the price movement relative to the prior period's close
and the "fill" (solid or hollow) of the candle represents the price direction of the period in
isolation (solid for a higher open and lower close; hollow for a lower open and a higher close). A
black (or red) candle represents a price action with a lower closing price than the prior candle's
close. A white (or green) candle represents a higher closing price than the prior candle's close. In
practice, any color can be assigned to rising or falling price candles. A candlestick need not have
either a body or a wick. Generally, the longer the body of the candle, the more intense the
trading.

Candlesticks can also show the current price as they're forming, whether the price moved up or
down over the time phrase and the price range of the asset covered in that time. Rather than
using the open, high, low, and close values for a given time interval, candlesticks can also be
constructed using the open, high, low, and close of a specified volume. In modern charting
software, volume can be incorporated into candlestick charts by increasing or decreasing
candlesticks width according to the relative volume for a given time period.
Usage

Candlestick charts are a visual aid for decision making in stock, foreign exchange, commodity,
and option trading. Looking at a candlestick, one can identify an asset’s opening and closing
prices, highs and lows, and overall range for a specific time frame. Candlestick charts serve as a
cornerstone of technical analysis. For example, when the bar is white and high relative to other
time periods, it means buyers are very bullish. The opposite is true for a black bar. A candlestick
pattern is a particular sequence of candlesticks on a candlestick chart, which is mainly used to
identify trends.
CHAPTER 2
LITERATURE SURVEY
I. Predicting Stock Price Trend Using Candlestick Chart Blending
Technique
The author “Yoshihisa Udagawa” deals with a technical analysis for stock price
predictions using candlestick charts. The stock prices are apt to show no directional
movements when there is no significant news, resulting in generating a series of noisy
candlesticks. We propose a blending algorithm that combines candlesticks sharing
certain price ranges into one candlestick to eliminate the noisy candlesticks. The
paper discusses statistical measures on candlesticks to produce appropriate blended
candlestick charts for the prediction. The experimental results on the Nikkei-225
stock average show that the blended candlesticks are successful in offering
information for short-term stock price predictions. The performance of the proposed
algorithm is measured showing that it can blend daily candlesticks of 25 years within
two seconds.

II. Stock Prediction by Searching for Similarities in Candlestick Charts


The aim of stock prediction is to effectively predict future stock market trends (or
stock prices), which can lead to increased profit. One major stock analysis method is
the use of candlestick charts. However, candlestick chart analysis has usually been
based on the utilization of numerical formulas. There has been no work taking
advantage of an image processing technique to directly analyze the visual content of
the candlestick charts for stock prediction. Therefore, in this study we apply the
concept of image retrieval to extract seven different wavelet-based texture features
from candlestick charts. Then, similar historical candlestick charts are retrieved based
on different texture features related to the query chart, and the “future” stock
movements of the retrieved charts are used for stock prediction. To assess the
applicability of this approach to stock prediction, two datasets are used, containing 5-
year and 10-year training and testing sets, collected from the Dow Jones Industrial
Average Index (INDU) for the period between 1990 and 2009. Moreover, two
datasets (2010 and 2011) are used to further validate the proposed approach. The
experimental results show that visual content extraction and similarity matching of
candlestick charts is a new and useful analytical method for stock prediction. More
specifically, we found that the extracted feature vectors of 30, 90, and 120, the
number of textual features extracted from the candlestick charts in the BMP format,
are more suitable for predicting stock movements, while the 90 feature vector offers
the best performance for predicting short- and medium-term stock movements. That
is, using the 90 feature vector provides the lowest MAPE (3.031%) and Theil’s U
(1.988%) rates in the twenty-year dataset, and the best MAPE (2.625%, 2.945%) and
Theil’s U (1.622%, 1.972%) rates in the two validation datasets (2010 and 2011).

III. Short-Term Stock Market Prediction Based on Candlestick Pattern


Analysis
This study will focus on 10 different candlestick patterns, five bullish reversal pattern
(end of negative trend) and five bearish (end of positive trend) reversal patterns. The
bearish candlestick patterns are Evening Star, Bearish Harami, Bearish Engulfing,
Gravestone Doji Top and Hanging Man. The bullish candlestick patterns are Morning
Star, Bullish Harami, Bullish Engulfing, Gravestonr Doji Bottom and Hammer. These
patterns were chosen based on an assumption that these ten patterns are the most
common in the market. This assumption is built on previous research mentioned in
the background section, where these ten patterns were widely used and researched.
Below are formulated mathematical definitions of these patterns. The definitions of
the patterns below have been taken from Steve Nisons book Japanese Candlestick
Charting Techniques. There he defines them with words and pictures. For the purpose
of this thesis they have been translated to formulas that can be used in an algorithm.

IV. Predicting Short Trend of Stocks by Using Convolutional Neural


Network and Candlestick Patterns
Candlestick chart pattern is a technical tool that encapsulates the price of the asset for
multiple time frames into a single price bar. The expertized trader can predict the
price trend of the asset by looking at the pattern of some adjacent candlesticks. This
paper proposes the architecture for predicting the short trend of the stocks by using
the convolutional neural network and the candlestick patterns. The experiments are
conducted with a set of candlestick pattern images collected from various stocks in
the stock exchange of Thailand (SET). Each image captures six to twelve adjacent
candlesticks. The experimental results indicate that the proposed method can
correctly predict the short trend for most stocks with acceptable accuracy. In addition,
the proposed architecture achieves better accuracy and training time than that of the
well-known architecture, ResNet-18.
V. Candlestick Analysis based Short Term Prediction of Stock Price
Fluctuation using SOM-CBR
Stock market analysis and prediction has been one of the widely studied and most
interesting time series analysis problems till date. Many researchers have employed
many different models, some of them are linear statistic based while some nonlinear
regression, rule, ANN, GA and Fuzzy logic based. In this paper we have proposed a
novel model that tries to predict short term price fluctuation, using Candlestick
Analysis. This is a proven technique used for short term prediction of stock price
fluctuation and market timing since many years. Our approach has been hybrid that
combines Self Organizing Map with Case Based Reasoning to identify profitable
patterns (candlestick) and predicting stock price fluctuation based on the pattern
consequences.

VI. On the effectiveness of candlestick chart analysis for the Brazilian


stock market
Several techniques have been developed in pursuit of understanding the behavior of
the financial market, in an attempt to predict the asset pricing behavior. The
candlestick chart created in the 18th century is one of these techniques. In 2006, Greg
Morris conducted a study on the effectiveness of this technique for the U.S. capital
market. However, no similar work was done for the Brazilian market. In this paper,
the behavior of part of the Brazilian capital market was studied using sixteen
candlestick patterns. We considered the data series of ten stocks between 2005 and
2009, totaling approximately 40% of Ibovespa (São Paulo Stock Exchange Index)
turnover. The frequency of confirmation of each pattern was measured along seven
exchange sessions after occurrence of such pattern, and results were compared to
those presented by Morris. Additionally, adjustments of the observed proportions of
hits were tested for their statistical significance. Results found in the frequently
analysis showed a discrepancy in relation to Morris’s study. Likewise, in statistical
analysis few patterns have confirmed the behavior expected of them. In at least one
case the trend expressed by data, although significant, was contrary to the original
interpretation of the pattern. Therefore, direct application of patterns developed for
other markets, times or actions is not recommended. Such results do not allow for an
affirmation that candlestick patterns have the power to predict future behavior of
stocks traded in the Ibovespa stock market.
CHAPTER 3
RESULT ANALYSIS
ResNet
A residual neural network (ResNet) is an artificial neural network (ANN) of a kind that builds on
constructs known from pyramidal cells in the cerebral cortex. Residual neural networks do this
by utilizing skip connections, or shortcuts to jump over some layers. Typical ResNet models are
implemented with double- or triple- layer skips that contain nonlinearities (ReLU) and batch
normalization in between. An additional weight matrix may be used to learn the skip weights;
these models are known as Highway Nets. Models with several parallel skips are referred to as
DenseNets. In the context of residual neural networks, a non-residual network may be described
as a plain network.

Need For ResNet

Mostly in order to solve a complex problem, we stack some additional layers in the Deep Neural
Networks which results in improved accuracy and performance. The intuition behind adding more layers
is that these layers progressively learn more complex features. For example, in case of recognizing
images, the first layer may learn to detect edges, the second layer may learn to identify textures and
similarly the third layer can learn to detect objects and so on. But it has been found that there is a
maximum threshold for depth with the traditional Convolutional neural network model.

Residual Block

This problem of training very deep networks has been alleviated with the introduction of ResNet
or residual networks and these Resnets are made up from Residual Blocks.

FIGURE: 03 - Residual Block


The very first thing we notice to be different is that there is a direct connection which skips some layers
(may vary in different models) in between. This connection is called ’skip connection’ and is the core of
residual blocks. Due to this skip connection, the output of the layer is not the same now. Without using
this skip connection, the input ‘x’ gets multiplied by the weights of the layer followed by adding a bias
term.

ResNet Architecture
ResNet network uses a 34-layer plain network architecture inspired by VGG-19 in which then
the shortcut connection is added. These shortcut connections then convert the architecture into
the residual network as shown in the figure below:

FIGURE 04: Architecture of ResNet


Overview
Briefly, we will be executing the following steps:

 Collecting the daily historical stock data for multiple stocks using the python library
nsepy.
 Converting the data into Candlestick charts.
 Labelling the Candlestick charts as having an “Up” or “Down” movement based on
percentage movement (of close price) in a particular direction.
 Training a Deep Learning Model on the data.
 Getting the areas of interest (based on the values of activations)and visualizing them
using heat maps.

The libraries we will be requiring as we move ahead:

 nsepy
 pandas
 numpy
 matplotlib
 fastai

NSEpy

NSEpy is a library to extract historical and realtime data from NSE’s website. This Library aims
to keep the API very simple.

Python is a great tool for data analysis along with the scipy stack and the main objective of
NSEpy is to provide analysis ready data-series for use with scipy stack. NSEpy can seamlessly
integrate with Technical Analysis library (Acronymed TA-Lib, includes 200 indicators like
MACD, RSI). This library would serve as a basic building block for automatic/semi-automatic
algorithm trading systems or backtesting systems for Indian markets.

Pandas

Pandas is a software library written for the Python programming language for data
manipulation and analysis. In particular, it offers data structures and operations for manipulating
numerical tables and time series. It is free software released under the three-clause BSD
license. The name is derived from the term "panel data", an econometrics term for data sets
that include observations over multiple time periods for the same individuals. Its name is a play
on the phrase "Python data analysis" itself.

NumPy
NumPy is a Python library used for working with arrays. It also has functions for working in
domain of linear algebra, Fourier transform, and matrices.

In Python we have lists that serve the purpose of arrays, but they are slow to process. NumPy
aims to provide an array object that is up to 50x faster than traditional Python lists. The array
object in NumPy is called ndarray, it provides a lot of supporting functions that make working
with ndarray very easy. Arrays are very frequently used in data science, where speed and
resources are very important.

Matplotlib

Matplotlib is a collection of functions that make matplotlib work like MATLAB. Each pyplot
function makes some change to a figure: e.g., creates a figure, creates a plotting area in a figure,
plots some lines in a plotting area, decorates the plot with labels, etc.

In matplotlib various states are preserved across function calls, so that it keeps track of things
like the current figure and plotting area, and the plotting functions are directed to the current axes
(please note that "axes" here and in most places in the documentation refers to the axes part of a
figure and not the strict mathematical term for more than one axis).

Fastai

Fastai is a deep learning library which provides practitioners with high-level components that
can quickly and easily provide state-of-the-art results in standard deep learning domains, and
provides researchers with low-level components that can be mixed and matched to build new
approaches. It aims to do both things without substantial compromises in ease of use, flexibility,
or performance. This is possible thanks to a carefully layered architecture, which expresses
common underlying patterns of many deep learning and data processing techniques in terms of
decoupled abstractions. These abstractions can be expressed concisely and clearly by leveraging
the dynamism of the underlying Python language and the flexibility of the PyTorch library.
Fastai includes:

 A new type dispatch system for Python along with a semantic type hierarchy for tensors
 A GPU-optimized computer vision library which can be extended in pure Python
 An optimizer which refactors out the common functionality of modern optimizers into
two basic pieces, allowing optimization algorithms to be implemented in 4-5 lines of
code
 A novel 2-way callback system that can access any part of the data, model, or optimizer
and change it at any point during training
 A new data block API
In this section we are completing the following steps:

 Obtaining the historic stock data (for the past two years) using the “obtain_data”
function.
 Determine whether a particular chart should be classified into “Up” or “Down” based on
movement in the upcoming five days.
 Create a Candlestick chart for a time period of 20 days.
 Save the Candlestick chart to the respective folder (Up/Down).

FIGURE 05: UP And Down Candlestick Charts

Now, we are setting up the data such that we can use it train a deep learning model. We are
completing the following tasks:

 Setting the batch size for training the model.


 Splitting the data into training and validation datasets.
 Applying augmentations on the data (to reduce overfitting).

Let’s train the model –

We trained a deep learning model (having an accuracy of nearly 62 % on a validation set) above.
A more detailed overview of what we did is as follows:

 Used transfer learning to train a pre-trained neural network on our charts using an
appropriate learning rate.
 Increased the size of our images (for the earlier training cycles it was 224 by 224 and
then we increased it to 352 by 352)
 Used transfer learning again to train the previous network on the new larger images using
an appropriate learning rate.

And we got an accuracy of nearly 62 % on our validation dataset.

FIGURE 06: Time for each Epoch while training


CHAPTER 4
CONCLUSION AND FUTURE WORK
Firstly, Obtaining the historic stock data (for the past two years) using the “obtain_data”
function. Determine whether a particular chart should be classified into “Up” or “Down”
based on movement in the upcoming five days.
Then we created a Candlestick chart for a time period of 20 days.
We saved the Candlestick chart to the respective folder (Up/Down). We trained a deep
learning model. Used transfer learning to train a pre-trained neural network on our charts
using an appropriate learning rate. We have increased the size of our images (for the earlier
training cycles it was 224 by 224 and then we increased it to 352 by 352). We have generated
a candlestick chart of various stocks of large capital Indian Stock. We trained a deep learning
model. With the help of transfer learning we trained a pre-trained neural network on our
charts using an appropriate learning rate.

Future Work

 We will be creating heat maps highlighting areas which were of interest to the neural net
using the activations. 
 We will improve the accuracy by implementing it with different models like GoogleNet,
AccordNet and Microsoft Cognitive toolkit.

 We will switch to GPU system for better training performance.


REFERENCES

[1]  Mr M.M Goswami, Prof C.K Bhensdadia, Prof A.P Ganatra  “ Candlestick Analysis based
short term prediction of stock price Fluctuations using SOM - CBR “ Faculty of Technology,
D.D University- Nadiad

[2] Filip Martinsson and Ivan Liljeqvist “ Short Term Stock Market Prediction Based on
Candlestick Pattern Analysis. “ Skokan for Datavetenskap och Kommunikation.

[3] Razik Batliwala, Muddassir Khan “ Stock Market Prediction using Candlestick chart”
IT,Padhmbhusham vasantdada patil pratisthan’s college of Engineering, Sion Mumbai.

[4] -  Kietikul Jearanaitanakij, “ Predicticting short trend of stock by using CNN and candlestick
pattern”- Faculty of Engineering, King Mongkut’s Institute of technology, Ladkrahang. 

You might also like