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
€93.41
List Price:  €130.79
You save:  €37.37
£79.90
List Price:  £111.86
You save:  £31.96
CA$137.16
List Price:  CA$192.03
You save:  CA$54.87
A$153.08
List Price:  A$214.33
You save:  A$61.24
S$135.90
List Price:  S$190.27
You save:  S$54.36
HK$781.66
List Price:  HK$1,094.36
You save:  HK$312.69
CHF 91.23
List Price:  CHF 127.73
You save:  CHF 36.49
NOK kr1,107.76
List Price:  NOK kr1,550.92
You save:  NOK kr443.15
DKK kr696.86
List Price:  DKK kr975.63
You save:  DKK kr278.77
NZ$168.60
List Price:  NZ$236.05
You save:  NZ$67.44
د.إ367.24
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,972.60
List Price:  ৳15,362.09
You save:  ৳4,389.48
₹8,346.11
List Price:  ₹11,684.89
You save:  ₹3,338.77
RM475.54
List Price:  RM665.78
You save:  RM190.23
₦123,487.65
List Price:  ₦172,887.65
You save:  ₦49,400
₨27,846.85
List Price:  ₨38,986.71
You save:  ₨11,139.85
฿3,694.58
List Price:  ฿5,172.56
You save:  ฿1,477.98
₺3,236.41
List Price:  ₺4,531.11
You save:  ₺1,294.69
B$519.35
List Price:  B$727.11
You save:  B$207.76
R1,861
List Price:  R2,605.48
You save:  R744.47
Лв182.73
List Price:  Лв255.83
You save:  Лв73.10
₩137,535.16
List Price:  ₩192,554.72
You save:  ₩55,019.56
₪374.71
List Price:  ₪524.61
You save:  ₪149.90
₱5,763.02
List Price:  ₱8,068.46
You save:  ₱2,305.43
¥15,525.12
List Price:  ¥21,735.79
You save:  ¥6,210.67
MX$1,695.96
List Price:  MX$2,374.42
You save:  MX$678.45
QR364.20
List Price:  QR509.89
You save:  QR145.69
P1,420.18
List Price:  P1,988.32
You save:  P568.13
KSh13,372.99
List Price:  KSh18,722.73
You save:  KSh5,349.73
E£4,798.26
List Price:  E£6,717.75
You save:  E£1,919.49
ብር5,719.92
List Price:  ብር8,008.12
You save:  ብር2,288.20
Kz83,641.63
List Price:  Kz117,101.63
You save:  Kz33,460
CLP$96,028.39
List Price:  CLP$134,443.59
You save:  CLP$38,415.20
CN¥723.89
List Price:  CN¥1,013.48
You save:  CN¥289.58
RD$5,819.58
List Price:  RD$8,147.64
You save:  RD$2,328.06
DA13,455.35
List Price:  DA18,838.03
You save:  DA5,382.68
FJ$226.17
List Price:  FJ$316.65
You save:  FJ$90.48
Q777.12
List Price:  Q1,088.01
You save:  Q310.88
GY$20,929.88
List Price:  GY$29,302.67
You save:  GY$8,372.79
ISK kr14,022.59
List Price:  ISK kr19,632.19
You save:  ISK kr5,609.60
DH1,011.06
List Price:  DH1,415.53
You save:  DH404.46
L1,768.33
List Price:  L2,475.73
You save:  L707.40
ден5,750.59
List Price:  ден8,051.06
You save:  ден2,300.46
MOP$805.69
List Price:  MOP$1,128
You save:  MOP$322.31
N$1,863.42
List Price:  N$2,608.86
You save:  N$745.44
C$3,672.13
List Price:  C$5,141.13
You save:  C$1,469
रु13,425.24
List Price:  रु18,795.88
You save:  रु5,370.63
S/376.35
List Price:  S/526.91
You save:  S/150.55
K385.89
List Price:  K540.26
You save:  K154.37
SAR375.01
List Price:  SAR525.03
You save:  SAR150.02
ZK2,677.10
List Price:  ZK3,748.04
You save:  ZK1,070.94
L464.84
List Price:  L650.79
You save:  L185.95
Kč2,347.91
List Price:  Kč3,287.17
You save:  Kč939.26
Ft36,370.18
List Price:  Ft50,919.71
You save:  Ft14,549.52
SEK kr1,092.76
List Price:  SEK kr1,529.91
You save:  SEK kr437.15
ARS$87,614.14
List Price:  ARS$122,663.30
You save:  ARS$35,049.16
Bs690.85
List Price:  Bs967.22
You save:  Bs276.36
COP$390,329.27
List Price:  COP$546,476.60
You save:  COP$156,147.32
₡51,017.42
List Price:  ₡71,426.43
You save:  ₡20,409.01
L2,476.75
List Price:  L3,467.55
You save:  L990.79
₲749,236.02
List Price:  ₲1,048,960.40
You save:  ₲299,724.38
$U3,833.15
List Price:  $U5,366.56
You save:  $U1,533.41
zł404.64
List Price:  zł566.52
You save:  zł161.87
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.