Master Python through building real-world applications (Part 9)

Let’s do it right away.

Make money, money, make money.

But also remember this,An investment in knowledge pays the best interest.

— Benjamin FranklinUnderstanding Candlestick ChartUsing Candlestick — RobinhoodCandlestick chart is a very efficient way of looking at all the columns we saw earlier, that too at once.

As you can see, the green candle says the price of the stock increased today while red candle indicates that it decreased, with high and low indicated in the same candle.

Here is a good article for a better understanding of candlestick charts.

Just in case you need that.

Building Candlestick BodyI believe we are on the same page.

We know what candlestick charts are and we know how to download the stock market data.

Now, we can build the body of candlestick charts using the rect method of Bokeh library.

But before that, we have to create a figure in which our plot will show.

It’s more like an outer boundary.

To do that, we will use figure method of Bokeh library.

It will simply generate an empty box in which your chart will show later on.

Moving towards the rect method, there are four mandatory parameters you need to pass in order to create a complete rectangle.

First two are, X-coordinate and Y-coordinate of the center point of your rectangle (it’s by default, weird but important).

And the last two are Width and Height.

Let’s look at the code first.

Now let me explain each parameter used in rect method so that you know how our first candlestick chart is prepared.

X-axis — As discussed, we need the center point of our rectangle, so for that, we need x and y-axis of that point.

For x-axis, we are taking df.

index as it contains dates.

Y-axis — For y-axis, we are taking the (open+close)/2 for the same date so now we have the center point of our rectangle.

Width — For width, we are taking hours as a parameter.

A day is of 24 hours.

We will take 6 hours on both sides so the body of our rectangle would be of 12 hours then 12 hours gap and then the new candle.

Basically, we are spacing in terms of time and not in terms of space.

Bad pun.

I know.

Height — The absolute difference between open and close would be the height of the bodyYou have reached the first milestone.

You now know everything you need in order to create your first candlestick chart on data from your favorite company.

My chart for TESLA looks something like this, what about you?Not bad for us.

But for Tesla, uh-oh.

Making the chart visually appealingNo one wants to watch all blue candles, doesn’t make any sense.

That’s why, just for this one, we will go with the flow.

Green and red candles for the price up and price down respectively.

Also, I have created separate functions for all the parameters in the rect method and later created a new column for each and stored data in it so it is easy to understand how it’s all working.

You can see the updated data frame below the code and the updated chart right below that.

This is how the new data looks like.

Wayyy better, ain’t it?Building Candlestick SegmentsWe have a visually appealing chart and the body of the candle, but we are still left with the highs and lows.

Just like all our lives, these candles too have highs and lows.

Look at line 41 and you’ll know how to handle it, it’s that easy.

What if it’s that simple in life too?.We wish.

But hey, look, there we have it.

A perfect candlestick chart for 10 days.

How about we go long, for like 3 months or so?.Wondering how it will look?.Me too.

Let’s feed our curiosity.

The chart you see below is created using these 60 lines of code (I’ve added comments so it's not even 60).

Ain’t it amazing?And that’s that.

Looks beautiful.

EndnotesAs we all know, we learn from visualizations far better than we learn from raw data.

Building visualizations from data are really rewarding and with help of external libraries like Bokeh, Python’s visualization game is stronger than ever.

In this post, you learned about stock market data, how to download it, what are candlestick charts and how to create them with help of Bokeh library.

If you have any doubt regarding anything from this article, feel free to reach out via Twitter, Email or Linkedin.

Happy Learning.


. More details

Leave a Reply