1
00:00:00,990 --> 00:00:01,340
Great.

2
00:00:01,340 --> 00:00:06,870
So what we're going to do in this lecture is we'll start building this graph

3
00:00:07,000 --> 00:00:08,550
out of this data.

4
00:00:09,080 --> 00:00:14,760
So this graph was generated out of 10 days of stock market data.

5
00:00:15,080 --> 00:00:20,630
So it's not a great representation to make any buying decisions because you have few data so you don't

6
00:00:20,630 --> 00:00:22,030
really see the trend here.

7
00:00:22,130 --> 00:00:23,320
But if you have more data,

8
00:00:23,330 --> 00:00:30,530
so if we increase this range here you'll get a bigger picture here with lots of candlesticks and everything

9
00:00:30,530 --> 00:00:31,840
will make more sense.

10
00:00:32,070 --> 00:00:37,100
So the script that we'll write will work with any number of data.

11
00:00:37,280 --> 00:00:38,330
This is what we have

12
00:00:38,330 --> 00:00:40,570
so far so let me go

13
00:00:40,570 --> 00:00:45,210
shift J and shift m to merge the cells.

14
00:00:45,380 --> 00:00:47,030
So just some housekeeping there.

15
00:00:47,300 --> 00:00:53,800
Now my notebook is more organized so we're importing the data from the pandas data reader import

16
00:00:53,850 --> 00:01:01,360
date time and we also need to import now the required modules from Bokeh.

17
00:01:01,490 --> 00:01:04,030
So we said we'll be using glyphs.

18
00:01:04,160 --> 00:01:13,570
So we will be using maybe a rectangle for these figures and then a segment for these vertical lines.

19
00:01:13,640 --> 00:01:20,000
So these are available from the bokeh.plotting interface.

20
00:01:20,030 --> 00:01:24,470
So from bokeh.plotting we need,

21
00:01:25,160 --> 00:01:35,030
we need to figure object show methods and the out file method as well to output the HTML

22
00:01:35,030 --> 00:01:43,210
file with a graph init, and then we read the data frame from the data reader method of the data module

23
00:01:43,220 --> 00:01:45,990
of the pandas data reader library.

24
00:01:46,160 --> 00:01:48,830
So for now we can leave it like this.

25
00:01:48,830 --> 00:01:57,410
But then later we can grammatically change the name parameter here so that we can quickly pass

26
00:01:58,090 --> 00:02:01,360
various kinds of company symbol here.

27
00:02:01,640 --> 00:02:08,240
So in this case we have Google or we can simply pass other symbols and then get the chart for any

28
00:02:08,660 --> 00:02:09,710
company.

29
00:02:09,960 --> 00:02:12,370
Let me delete this variable here.

30
00:02:12,740 --> 00:02:17,770
So we have a clean sell there and we go to the next cell.

31
00:02:17,840 --> 00:02:25,400
We got an invalid syntax there when I executed the cell from bokeh.plotting, yeah you can see here that the

32
00:02:25,400 --> 00:02:31,240
arrow is pointing you to this part of the code.

33
00:02:31,310 --> 00:02:34,380
So in this line you have passed something wrong.

34
00:02:34,400 --> 00:02:42,260
So from bokeh.plotting you should import figure and then the comma and show the output etc.

35
00:02:43,010 --> 00:02:46,910
So delete these cells.

36
00:02:47,190 --> 00:02:48,080
And here we go.

37
00:02:48,140 --> 00:02:58,510
Now what we can do first is well you need to think about building the structure or your plot.

38
00:02:58,780 --> 00:03:02,410
So that means you need to establish a figure object.

39
00:03:02,590 --> 00:03:07,700
And this is we have, we will have a spatial X-axis here.

40
00:03:07,900 --> 00:03:10,680
So that should be a date time axis.

41
00:03:10,690 --> 00:03:21,670
So what we can do is we store the figure object in a p variable figure and then let's say the x

42
00:03:22,240 --> 00:03:29,220
axis type parameter equals to the date time string.

43
00:03:29,870 --> 00:03:35,510
Let's give the plot a width of 1000 and a height of

44
00:03:39,050 --> 00:03:39,600
300.

45
00:03:42,950 --> 00:03:43,910
Than pass a title there.

46
00:03:46,990 --> 00:03:51,980
Let's say candlestick chart.

47
00:03:53,500 --> 00:04:02,500
So at this point we should have an empty plot area with these dimensions and now we should think about

48
00:04:02,530 --> 00:04:05,590
how we plot these boxes.

49
00:04:05,590 --> 00:04:13,060
So let's think about the boxes first and there may be a couple of ways to generate these boxes.

50
00:04:13,060 --> 00:04:18,980
The first that I think of is the quad method that generates quadrants.

51
00:04:19,090 --> 00:04:26,980
So for the quadrant method, for the quad method you'd have to pass a left border for the rectangle and

52
00:04:26,980 --> 00:04:33,480
then a right border and then the bottom and the top limits as well.

53
00:04:33,490 --> 00:04:44,710
So for left border you would have to pass you know here is the data frame. Let's print that out.

54
00:04:44,770 --> 00:04:54,780
So for the left border you'd have to pass the start date. So we have a date time axis here and then

55
00:04:55,120 --> 00:05:01,850
generally these candlesticks are the width of these boxes is equal to 12 hours.

56
00:05:01,900 --> 00:05:04,020
You can also build wider rectangle there.

57
00:05:04,600 --> 00:05:07,300
But this is an optimal representation.

58
00:05:07,300 --> 00:05:10,020
So experience say is this is better.

59
00:05:10,240 --> 00:05:13,600
So 12 hours and then you have a 12 hour gap.

60
00:05:13,600 --> 00:05:22,420
So 24 hours span for the day for the first day and then the next day is here, 12 hour width for the

61
00:05:22,420 --> 00:05:23,010
rectangle.

62
00:05:23,020 --> 00:05:26,300
This happens to be a very narrow one.

63
00:05:26,560 --> 00:05:34,960
And then 12 hour gap, and then the third day, 12 hour gap forth day, and then we have here, we have, that would

64
00:05:34,960 --> 00:05:37,290
be 48 hours.

65
00:05:37,300 --> 00:05:42,450
So we have a weekend here with 48 hours and then the next day and so on.

66
00:05:42,610 --> 00:05:53,640
So the quadrants, and for the bottom and the top limits you would have to pass the opening and closing values

67
00:05:53,650 --> 00:05:57,090
so the price in dollars and here we need to do

68
00:05:57,290 --> 00:05:57,970
a trick, so...

69
00:06:02,600 --> 00:06:10,130
I explained to you that when the closing price is higher than the opening price we would have like a grey rectangle

70
00:06:10,390 --> 00:06:18,110
let's say with one color, but in another other cases like in the third row here where the closing price

71
00:06:18,110 --> 00:06:22,080
is less than the opening price we would have a red rectangle.

72
00:06:22,250 --> 00:06:29,480
That means we need to actually build two different sets of glyphs for the boxes.

73
00:06:29,480 --> 00:06:32,170
So we're still talking about the boxes here.

74
00:06:32,210 --> 00:06:43,080
So that means you would have something like p.quad and then p.quad again, and then for the first functions,

75
00:06:43,130 --> 00:06:50,660
so for the first set of quadrants you'd have to pass only those data frame rows that satisfy the

76
00:06:50,660 --> 00:06:54,830
condition that the closing price is higher then the opening price.

77
00:06:54,830 --> 00:07:02,430
So we would have to write something like, you know, let me delete this do this, let me create a cell there.

78
00:07:02,540 --> 00:07:09,550
So df, first of all df.index is how you select the date range.

79
00:07:09,680 --> 00:07:13,580
So date is listed as an index of the data frame here.

80
00:07:14,180 --> 00:07:18,340
These are simple columns open, high, low, close, volume, and adjusted close.

81
00:07:18,530 --> 00:07:27,600
But this is an index, so to access that you see df.index. To access open, the open column

82
00:07:27,830 --> 00:07:32,030
you can do df.open and you get those values.

83
00:07:32,450 --> 00:07:43,650
So how about filtering only the rows of the data frame where close is higher than open?

84
00:07:43,910 --> 00:07:50,570
Well with df.index you select the entire data range.

85
00:07:50,630 --> 00:08:00,500
So we need only those cells with close higher than open that means out of this range you need to make

86
00:08:00,730 --> 00:08:07,880
another subselection which would be like the df.

87
00:08:07,910 --> 00:08:09,070
close

88
00:08:10,010 --> 00:08:13,070
is higher than df.open.

89
00:08:13,070 --> 00:08:16,940
So this is like a condition that is applied to this range.

90
00:08:16,970 --> 00:08:23,430
Now if I execute this I get only three, four values.

91
00:08:23,780 --> 00:08:28,040
So the first value is first of March.

92
00:08:28,040 --> 00:08:37,520
So this one here, yeah here is close is higher than open here, yep, so close is greater than open at this.

93
00:08:38,040 --> 00:08:46,080
And then we have open is higher here and 8. 8th of March is close is higher

94
00:08:46,120 --> 00:08:52,410
then opening, yeah. Here as well and here too.

95
00:08:52,430 --> 00:08:53,880
Great.

96
00:08:53,900 --> 00:08:55,430
So what does this mean then?

97
00:08:55,720 --> 00:09:04,420
Well, this means that when you pass here the coordinates for the left limit of your quadrants you'd

98
00:09:04,430 --> 00:09:08,380
have to pass this expression.

99
00:09:08,560 --> 00:09:13,210
So that's more or less how you build this chart using quadrants.

100
00:09:13,220 --> 00:09:21,670
Now I'd like actually to use rectangles to build the chart. I've tried both quadrants and rectangles and I think

101
00:09:22,120 --> 00:09:29,140
rectangles give you a better default zoom of the overall graph.

102
00:09:29,170 --> 00:09:34,460
So when I build a graph with quadrants you have some area here,

103
00:09:34,480 --> 00:09:40,920
so some glyphs will be cut off by the axes, but you can solve that by setting the default zoom.

104
00:09:41,050 --> 00:09:42,750
So that's not a problem.

105
00:09:43,570 --> 00:09:47,460
And so I'd ask you to actually use quadrants after

106
00:09:47,550 --> 00:09:50,370
I built this with rectangles.

107
00:09:50,920 --> 00:09:52,200
So as an exercise.

108
00:09:52,360 --> 00:09:56,190
But for building this for I'll use rectangles.

109
00:09:56,290 --> 00:10:01,880
So now you know how to filter these data and we will be using these filters.

110
00:10:01,960 --> 00:10:08,290
So for rectangles as well because with rectangles we also need to build two sets of rectangles so p.rect

111
00:10:08,290 --> 00:10:11,000
p.rect here as well.

112
00:10:11,460 --> 00:10:13,970
And so let's do that thing the next lecture.

113
00:10:14,230 --> 00:10:17,630
Let's keep this lecture short and I'll talk to you later.

