Plotting Bitcoin Prices as an Interactive Plot with Candlesticks

Python 3: Automating Your Job Tasks Superhero Level: Data Visualization with Bokeh and Python 3
9 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€91.90
List Price:  €128.67
You save:  €36.76
£78.70
List Price:  £110.19
You save:  £31.48
CA$136.14
List Price:  CA$190.60
You save:  CA$54.46
A$149.21
List Price:  A$208.90
You save:  A$59.69
S$134.58
List Price:  S$188.42
You save:  S$53.84
HK$780.18
List Price:  HK$1,092.29
You save:  HK$312.10
CHF 90.89
List Price:  CHF 127.25
You save:  CHF 36.36
NOK kr1,073.95
List Price:  NOK kr1,503.58
You save:  NOK kr429.62
DKK kr686.39
List Price:  DKK kr960.97
You save:  DKK kr274.58
NZ$162.99
List Price:  NZ$228.20
You save:  NZ$65.20
د.إ367.26
List Price:  د.إ514.18
You save:  د.إ146.92
৳11,707.96
List Price:  ৳16,391.62
You save:  ৳4,683.65
₹8,329.22
List Price:  ₹11,661.24
You save:  ₹3,332.02
RM468.70
List Price:  RM656.20
You save:  RM187.50
₦146,985.30
List Price:  ₦205,785.30
You save:  ₦58,800
₨27,706.17
List Price:  ₨38,789.74
You save:  ₨11,083.57
฿3,624.43
List Price:  ฿5,074.35
You save:  ฿1,449.91
₺3,224.01
List Price:  ₺4,513.75
You save:  ₺1,289.73
B$510.40
List Price:  B$714.59
You save:  B$204.18
R1,807.25
List Price:  R2,530.23
You save:  R722.97
Лв179.86
List Price:  Лв251.81
You save:  Лв71.95
₩135,425.45
List Price:  ₩189,601.05
You save:  ₩54,175.60
₪370.41
List Price:  ₪518.60
You save:  ₪148.18
₱5,770.97
List Price:  ₱8,079.59
You save:  ₱2,308.62
¥15,565.94
List Price:  ¥21,792.94
You save:  ¥6,227
MX$1,660.69
List Price:  MX$2,325.03
You save:  MX$664.34
QR362.75
List Price:  QR507.87
You save:  QR145.11
P1,354.73
List Price:  P1,896.68
You save:  P541.94
KSh13,098.69
List Price:  KSh18,338.69
You save:  KSh5,240
E£4,690.53
List Price:  E£6,566.93
You save:  E£1,876.40
ብር5,727.42
List Price:  ብር8,018.62
You save:  ብር2,291.20
Kz84,733.22
List Price:  Kz118,629.90
You save:  Kz33,896.68
CLP$89,440.82
List Price:  CLP$125,220.73
You save:  CLP$35,779.90
CN¥722.24
List Price:  CN¥1,011.17
You save:  CN¥288.92
RD$5,826.59
List Price:  RD$8,157.46
You save:  RD$2,330.87
DA13,436.03
List Price:  DA18,810.98
You save:  DA5,374.95
FJ$222.79
List Price:  FJ$311.92
You save:  FJ$89.12
Q776.81
List Price:  Q1,087.56
You save:  Q310.75
GY$20,917.88
List Price:  GY$29,285.87
You save:  GY$8,367.99
ISK kr13,827.61
List Price:  ISK kr19,359.21
You save:  ISK kr5,531.60
DH990.33
List Price:  DH1,386.51
You save:  DH396.17
L1,760
List Price:  L2,464.07
You save:  L704.07
ден5,654.52
List Price:  ден7,916.55
You save:  ден2,262.03
MOP$803.51
List Price:  MOP$1,124.95
You save:  MOP$321.43
N$1,819.78
List Price:  N$2,547.77
You save:  N$727.98
C$3,674.63
List Price:  C$5,144.63
You save:  C$1,470
रु13,329.38
List Price:  रु18,661.67
You save:  रु5,332.28
S/371.45
List Price:  S/520.05
You save:  S/148.59
K388.01
List Price:  K543.23
You save:  K155.22
SAR374.96
List Price:  SAR524.96
You save:  SAR150
ZK2,538.86
List Price:  ZK3,554.51
You save:  ZK1,015.64
L457.57
List Price:  L640.62
You save:  L183.04
Kč2,271.57
List Price:  Kč3,180.29
You save:  Kč908.72
Ft35,609.88
List Price:  Ft49,855.26
You save:  Ft14,245.38
SEK kr1,071.59
List Price:  SEK kr1,500.27
You save:  SEK kr428.67
ARS$88,666.13
List Price:  ARS$124,136.13
You save:  ARS$35,470
Bs690.83
List Price:  Bs967.19
You save:  Bs276.36
COP$380,678.73
List Price:  COP$532,965.46
You save:  COP$152,286.72
₡51,169.76
List Price:  ₡71,639.71
You save:  ₡20,469.95
L2,462.78
List Price:  L3,448
You save:  L985.21
₲749,130.70
List Price:  ₲1,048,812.96
You save:  ₲299,682.25
$U3,853.49
List Price:  $U5,395.05
You save:  $U1,541.55
zł392.01
List Price:  zł548.83
You save:  zł156.82
Already have an account? Log In

Transcript

The last application in this section of the course will use the same data as in the previous application, meaning the closing prices of Bitcoin between February 20 of 2019 and march 20 of the same year, and it will generate a chart with candlesticks showing the daily price evolution. This is actually the most common type of chart used by traders and investors when evaluating price trends regarding financial assets. Now I'm going to have my code saved into a brand new Jupiter notebook. So let me create this new Python three, and I will call this notebook candlesticks. And let me paste in the code. Okay, now let's start discussing this code.

I'm going to skim over the operations and lines of code we've already discussed and tested throughout the previous lectures and just focus on the new concepts that this application brings to the table. First of all, as always, we are importing the modules and tools that we need in our application. Nothing fancy here. Next, we are using the same web page we used earlier to load the necessary data to a panda's data frame using the read HTML methods. From this data frame, we also need to convert the date format to a format that is more easy to work with. And we are doing this using the date time method once again, applied on the date column of our data frame, nothing new thus far.

The next thing I did is I renamed two of the columns in our data frame using the Rename method to remove the asterisk symbol from their name. This can and will generate issues when running the application. So it's better to have clean names for our columns. Also, notice here that I used the in place equals true argument for each of these lines. lines in order to make these changes in place and modify the data frame itself. So let me show you how our data frame looks like after performing these operations.

And I'm going to use the plain old Python interpreter for this demonstration. So this is it import pandas. Now I'm going to copy and paste this. Okay, first of all, looking at our code, let me copy and paste the following lines as well. So we can see the result of these operations. Also the renaming operations, the second one as well.

Okay, now let's see the F. Okay, so now we have cleaned names for the open and close columns, as well as the correct date time format inside the date column. Great. Now let's take a look at how the final candlestick chart should look like. So I'm going to hit Ctrl plus enter. So now Notice that we have great candles as well as red candles. A great candle means that the closing price of bitcoin was higher than the opening price that day.

A red candle is exactly the opposite, meaning that the closing price was lower than the opening price of that day. Also notice that each of these so called candles has these weeks or tails or shadows sticking out at the top and on the bottom of each candle. The upper shadow over candle signals the highest price that was achieved that day, whilst the lower shadow signals the lowest price recorded during that day. And we have both of these prices in our data frame table as well on the high and low columns. Now going back to our code, in order to draw each candle on the chart and also determine the color of that candle based on the daily prices. We have to come pair the opening and closing price.

And that is exactly what we are doing on these two lines of code. Actually, let me show this to you in the Python interpreter again, we already have the data frame loaded. So I'll just copy and paste these lines of code, and then we are going to see the results. So this is the first one. And also the second line right here. Let's see type of ink.

And also let's see ink, and also the other variable as well. Okay, so notice that these two data sets are actually pandas series objects, as you can see right here, and by looking at the values returned by these objects, we can see that we either have true or false for each of the days in the table. Based on these two evaluations, we will be able to generate the correct candlestick down below in the code On the next line, we are also setting the width of each candlestick, considering the fact that each candlestick represents a whole day of Bitcoin trading, and that the x axis is of type date time, the bulk a website uses the value of half a day in milliseconds, as you can see right here to set the width of each candlestick in their example, if we would use 24 hours instead of 12 right here, then the candles would have no space in between them and would make the chart look pretty sloppy.

Actually, let's try this and see what it looks like. So let me replace 12 with 24 hours and let's hit Ctrl Enter. Yeah, I like the old look better, to be honest. Okay, so I'm going to close this and I'm going to also undo this change and have a half a day for the width of the candles. On the next two lines of code. We are performing some operation Now that we've already used and tested in the previous videos, we're specifying the interactive tools for the plot.

And then we are creating the figure object, also passing some arguments to customize the look and feel of the plot. Next, we are setting an attribute that we haven't used thus far in this section, and that is major label orientation equals pi divided by four. This attribute sets the orientation of the x axis labels to 45 degrees instead of the plain old horizontal or vertical display. And you can notice the setting being applied on the x axis of our chart. And then setting the color intensity of the grid lines is done using the grid line alpha attribute, which we've already seen. Keep in mind that a smaller alpha will make the grid lines almost invisible, whilst a large alpha value will make them stand out next time It's time to draw the vertical bars representing the candlesticks using the V bar method applied on the figure object.

And also the upper and lower shadows of each candlestick using the segment method applied on the same figure object. So first of all using the segment method, we are providing the coordinates for the highest and lowest prices of the same date. Basically, this method takes four arguments. Let me write this in the interpreter so you can see it better. And I'm going to bring this Okay, that's better. So we have segment of x zero, y zero, x one and y one.

In our case, x zero and x one are the same, and that is the date stated using the DF dot date attribute. As you can see in the code, why does the y zero coordinate is represented Buy the highest price of the day df dot high and the white one coordinate is the lowest price of the day, which is df dot low. Also we are setting the color of these weeks to black. Next what I did is I called the V bar method twice once for the case when the closing price is higher than the opening price, and once for when the opposite is true. In each of these cases, we must pass several arguments to the V bar method. First we have the x axis value, which is the date of each candlestick.

Then we have the width of that candlestick W. Then we must also add the two coordinates of the vertical bar on the y axis meaning the opening and closing prices. And of course the color to use in each case to differentiate between the two cases. Optionally, you can also set the border color of each vertical bar, I set it to black Finally, as always, we are generating an HTML file to save the output, and also the show function to display the resulting figure to the screen. So if we run our code once again, we have this beautiful interactive candlestick chart. Congratulations, you reached the end of this section. I hope you enjoyed it, and I'm convinced it's going to come in handy someday.

Of course, we barely scratched the surface in this section when it comes to data visualization. The bulk a module is very comprehensive and allows you to do way more than we've managed to cover throughout these videos, and it definitely deserves its own course. Having that said, Thank you again for watching and I'll see you in the next one. Bye.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.