Back to Index

Building a PlotLy $GME Chart in Python


Chapters

0:0
1:0 Simple Moving Average
3:56 Time Series Intraday Api
5:36 The Candlestick Format
24:48 Indicators
28:37 Moving Average
36:29 Add Candlesticks
43:11 Unbalanced Volume
56:44 Update the Layout
58:39 Confirming Trade
60:11 Shaking Oscillator

Transcript

Hi, and welcome to the video. We're going to do something a little bit different today, and we're going to build a Python project from scratch. And what I want to do is build a Plotly chart and feed stock information into this chart from the Alpha Vantage API, which is very similar to the Yahoo Finance API, as far as I am aware.

I haven't used either of them that much, to be honest. But they seem to have pretty good documentation, and it's a little bit different, and I just want to explore using this API rather than Yahoo Finance, which is like the de facto standard, almost. So after we give some stock information to this chart, we want to actually chart it and plot a few interesting indicators.

So you can see on the screen, I've gone for a few different indicators here. I haven't really seen any of these because I'm not a finance person, so I don't claim to know what all of this means exactly. Obviously, the simple moving average is a moving average of the stock price over time, so that one is quite simple.

And I just want to include that in there so that we can see that we're actually doing things right and we're plotting these indicators correctly. And then we have this thing called the onBalanceVolume, which measures positive and negative volume flow. So the reason I'm including all this volume and balancing momentum-based indicators is at the moment in the stock market, we have the big Wall Street versus Wall Street bets.

And I want to use this opportunity to just build a cool project that at least gives a little bit of information on what is actually happening in terms of the actual stock market. Now, a lot of these are traditional indicators. They've been around for a few years, so I don't know if they're actually going to be useful or not because, obviously, the market conditions at the moment are not normal.

But it'll be interesting to see. And then we have this balance of power, which is the market strength of buyers versus sellers. So again, pretty relevant. Rate of change, maybe we'll include this. I'm not sure, this one called Arun, which looks quite interesting, actually. So if we go on here, you can see that it essentially gives us this.

So we get a downtrend and uptrend indicator line. And whenever these cross over, that's typically when you see an uptrend or downtrend. And I think that'll be quite interesting to see as well. And so this is based on the time between highs and lows in a specific time period.

So what you're doing here is measuring the lowest point, and that gives you the points, the edges, or the peeps and troughs of this indicator here. So I think that'll be pretty interesting. And then we have this Chaikin oscillator. And I thought this would also be quite interesting, because this is tracking the strength of price movements and the underlying buying and selling pressure to estimate demand.

So that should also be pretty interesting. So let's go ahead, and we're going to start coding this up. So the first thing we need to do is figure out how to use the Alpha Vantage API. So basically, all the data that we want, we can get from here. So the main part of that is going to be this time series intraday API.

So we'll just go ahead and figure this out. OK, so if we just take this, let's figure out the format that data is actually going to come in with. OK, so we have our interval, which we included here, symbol. We're going to change that to GME. And then we have this time series, which is in the dictionary.

And inside here, we have our open high, low, close, and volume as well, which is interesting. So OK, let's pull that together. So first, I do want to have the candlestick format. So I came up with this. So candlestick is this, whereas if we were just tracking the average, it would look like this.

So first, we're just going to do this with Blightly. And then we'll figure out how to build the candlestick. So we're going to go ahead and try and build that. So for now, we're just going to use this data. And we'll get into a data frame. So import pandas and just clean it up a little bit.

OK, so I'm going to initialize the data frame first. And then we'll just loop through each of these entries. We'll have the date and time in one column, the open, high, low, close, and volume in other columns. And we'll just append each one of these as we work through.

So here, we're just going to print out each of the date time keys. Because here, we need to actually access this in order to get into our actual values here. So just print this out. And then we need to also add this. There we go. So these five minute intervals, and this is what we're going to work with.

Let's just make things a little bit easier to read. , and now we're going to do here, we're going to do here, we're going to do here, we're going to do here. Let's just make things a little bit easier to read. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns. And now, we're going to do here is append a dictionary, which is going to contain each one of these columns.

Let's do that. Let's go with the open for now. OK. So you can see that it's not showing anything. And this is an issue with one of the Plotly extensions. OK. So we just need to install those. It's not difficult, though, from what I remember. Yeah, this is Plotly extension.

Yeah. So it's the JupyterLab extension. So that's checking what we have. OK. So I need these two, JupyterLab extension-- JupyterLab Plotly and Plotly widget. So let's go and install those two. I realize maybe this is too small for you to see. So these two are going to do. Although these are the uninstallable, we're going to install them.

All right. OK. OK. So then we need to install the other one as well. Then we might need to restart Jupyter, but let's see if it works. I'm also going to need a JupyterLab build as well, just in case I need it. I don't think I will, but let's try.

OK. So let's go back to our lab. And let's just refresh and try. Let's see if it works. If not, we'll just restart. Yeah, OK. So let's just restart it quickly. And let's try all this again. Here we go. So it's working now. So yeah, that's pretty cool. Although, what is this?

We have-- yeah, that's weird. Do we need to-- let me check what these are. I assume these must be strings. They're not actual numbers. Yeah, so we need to convert these into numbers as well. OK, so we can do that with the two numeric function. And then just pass the columns that we want in there.

I'm not sure if we can do all of them at once or if we have to do one at a time. Let's try all. OK, yeah, so just one at a time. Let's just take this list. I'm also not sure if it does it in place or not as well.

Let's just have a quick look. OK, so I think it does not do it in place. Let's see. OK, so-- OK, so no, it doesn't. All right, there we go. So now it's a float. This should work now. There we go. So that looks pretty cool. And we have this sort of interactive-- interactivity with the chart, which-- this is why I wanted to use Plotly over, like, Matplotlib.

I think this is definitely something we want to have in the chart. So we have that now. Before we do the candlesticks, let's just try putting on a few of the indicators and see how that looks and see if they're straightforward or not. So it is simple moving average.

So let's add that in. Add that in. OK, so let's just clean this API requesting up a little bit. OK, so that is our API. And then we just want to change these. We'll just use it like this instead. OK, so we have SMA. We want GME weekly interval.

No, we want-- so five minutes. Oh, no, we don't. We want-- so it's a moving average. So yeah, let's go weekly. And see how that looks. Time period, this is like 10 days. What do we have here? We don't have a time period. So we do want to adjust that a little bit.

So maybe we can-- let me zoom in a little bit here. Probably can't see anything. So the function interval, so yeah, we have a few. Let's go like a 60 minute, because then we can actually see that on the time period that we're already looking at. Number of data points.

But OK, let's change this to a 60 minute moving average. So no, we don't want to change that one. We change this one. And add in the API key as well. OK, let's just see what we get. OK, cool. So we have this-- OK, we have this technical analysis.

Interval, OK. So we want to pulling data out of here. OK, cool. So I wonder if you can add multiple functions. I don't know. But let's stick with this. And we're basically going to do the same thing again, where we take these. But this time, we are getting our moving average.

So technical analysis. Let's put this in. And all we want here is the date time and then this SMA value. So technical analysis, I think that's fine. So now we just change this to SMA. I think that should be OK. Let's see. OK, let's put that in here so I don't do that again.

And I just overwrote all of that. I mean, I need to change this. OK. OK, that looks cool. So we will also need to do this again, where we convert it to numeric. Let's just add SMA head here. Then let's plot this. And let's just see what we get.

OK. So we get a lot more data here. So we need to restrict that to only within what we have here. But at the same time, I think it would be cool if we got a little more data than what we have. But let's just keep it dynamic for now.

So we just take the minimum date time that we have within DF. And then we'll just apply that to all of our technical indicators so that we're only pulling it from then onwards. And we could probably get it from the API. But let's just filter the data frame. Yeah, because here, I'm not sure they do actually give you a date time limit that you can just extract from that point.

So yeah, I think we need to do it from the data frame anyway. So after we do all of this, let's also get the min date time. That would just be DF date time dot min. We also need to re-extract all of that because I overwrote it earlier. Okay, and then let's check our date time.

It should be something reasonable. So yeah, I think that makes sense. It's like 12 hours before. No, about eight hours before this point. So that's fine. So eight hours of data in there. So when we do this, all we need to do is just filter out anything that is below that date time.

So all we do is SMA equals SMA, where the date time is greater than or equal to minDT. So let's just check. So we have the length of SMA here. So we've got 560 rows. And then if we do this, we filter it down to nine, which is more reasonable.

So we have, I think we had around eight hours in there. So nine hours maybe. And we're taking it in 60 minute intervals. So that makes sense. And then if we do that, cool. So we get our SMA indicator here. So let's plot those both together and see how it looks.

Okay, so apparently you need to also add fig, add scatter. And then you add mode lines onto the end to add another line. So maybe I didn't see this. Okay, so line is supposed to be lines, I think. Okay, cool. So that looks good. We have our SMA and our actual plot.

So that seems pretty straightforward. But now we actually want to maybe add candlesticks into this because that would be pretty cool in my opinion. No idea how we can do that in Plotly. So let's try and figure it out. It actually looks really simple. So you need to use the graph objects instead of express.

Let's try and add that then. So if it's really that easy, then this -- oh, it's super easy. Oh, wow. Okay, so that's our candlestick. That's really cool. I don't really know if you want this bit, but I suppose it's kind of cool. Let's just go with it for now.

So we have that and then we also want to add scatter again to add the SMA. Okay, that's pretty cool. So now we just want to add in a few of those other indicators. So what do we have? We have this OnBalanceVolume, BalanceOfPower, RateOfChange, Arun, and Trikin. So let's do this OnBalanceVolume one first.

I'm going to add in a couple of functions to actually do things as well. So like here, every time that we want to convert these to numeric, I think we want to convert anything that's just not called date/time. So we can just add that as a function. Let's do toNumeric and then just add our DataFrame.

Then we just go for col in df.columns. We only want to do this if col is not equal to date/time. So every other column we do want to convert to numeric. And just return that. Okay, so instead of needing to write it out every time, we can just write toNumeric when we have our data.

Okay. So here we're getting the time series data. We also want to convert to numeric here. We can leave it as df. Okay, and let's just test that, see how it is. Okay, looks good. So we can remove that. And then we are getting our SMA data. So let's just move that into OneBox as well.

And this, we just want SMA and SMA. Okay. Let's move this up to the top as well. I want to add in a few more here. So we have -- which one is it? It's the onBalance, I think. Yeah, the onBalance volume, let's do that one. And we're probably going to do something similar here again, right?

So let's go to our documentation and let's get the onBalance. Obv. So we have this. So paste that there. Do something pretty similar again. So we have the API key, which is going to be our key. The interval. I should probably check what that actually means for this. So this one is probably the one that I was most interested in seeing.

It's like a -- it measures the positive and negative flow with the intention of identifying where you've got a load of sort of big investors putting a lot of money into it. Or sorry, when you've got a lot of smaller investors putting a lot of money into it, so basically the volume is higher.

Because obviously the retail investors are putting a lot more bets on, but they're smaller buys or sells. So that I imagine would be how this works. Although, you know, I've just read this very briefly before, so it could be completely wrong. But it sounds pretty interesting from reading this.

So let's just try and implement it. I'm going to go with shorter time period than what it's suggesting here. Because I don't know if we will see anything with that. Or the shorter interval, sorry. And then GME. Okay, so let's put this in. No need to see what we actually get here.

Okay, so we have this technical analysis. And it's basically the same as before when we had the SMA. So we could probably write a function that does that for us. But let's just do it like this for now. So we just change this to ABV. That's a pretty easy change.

Okay. So let's try that. I think this one should be pretty interesting to see. And I don't know, to be honest, we're going to need to put these on another chart. Because obviously that isn't going to work. I imagine there isn't an easy way of doing it. But let's just check quickly.

Yeah. Okay. So we should probably have a look at how people normally do this. Okay, and let's try and stick with the graph objects. So I don't know if this is going to give us -- yeah, this just gives us express. Okay. So we're actually just going to use a scatter.

Okay, cool. So we want something like this. We go scatter. And then we add X and Y. Okay, so we just swapped out ABV. Okay, cool. So we have this. Interesting. So this is our unbalanced value. Is that right? Unbalanced volume, sorry. So I wonder if this would make more sense because it's volume to have it as a bar chart at the bottom.

Yeah, that would be interesting, I think. Okay. So we have subplots here. We have space very similar to -- okay, perfect. So that looks kind of good. But we want steps, so I think it's the same as far as I know. Yeah. The only thing that I'm not doing here is matching on the X-axis.

But again, that shouldn't really be an issue for us because we do actually have -- we have already matched on the X-axis. So let's give that a go. So we want subplots, make. And we append trace this time instead. So this time we're going to append trace. I don't know if we can add scatter.

Okay, no, so we need to -- yeah, I don't know if we should have really been doing that anyway because it needs to be more like this. And then I suppose we can add mode here. Or maybe not, let's not for now. Let's just append trace. And we do that for SMA as well.

Let's see how that goes. I just want two rows. And we've already imported this. So let's -- okay, so we have data, Y. Okay. And we also need to specify the row and the column as well. Okay, so this is going to be row -- which one? Yeah, this one's going to be row two.

This one here will be row one, col1. And we just need to remove the data part that we added in here. So I suppose -- yeah. So append trace. And we just remove that. Okay. So yeah, it's pretty messy. Okay, so it's because we have this. So I kind of want to just get rid of that for now.

So I don't know what it is, like an excited slider maybe. Okay, so this is kind of what we need, I think. Yeah. So we just need to update the layout. And just made this false. And I think -- let's put this up to here. Type date, okay, this should work.

Let's see. Okay, cool. So we have -- so now we have this. What is it most useful? How do you normally -- let's have a look at how this unbalance volume is normally plotted. Okay. So this is the unbalance volume here. So it's just like another line chart underneath the other line chart.

So it's basically what we have already. This is interesting. So here's like a confirming trade when they're both in the same direction. Okay, so this is what we want to look at here. When the price and OBV are making higher peaks and higher troughs, upward trend is likely to continue.

So when they're both going up, when they're making lower and lower, there's a downward trend. If the OBV is rising, accumulation may be taking place. So if we have a look at this. So you can see they're both going down. So that's like, okay, the downward trend is likely to continue.

And here they're both going up. So upward trend is likely to continue. And then they're just leveling out here. So it's not really anything of interest. But I suppose here it's kind of balanced. And we see the momentum sort of pushing up again just before it rises. So maybe that is what we're sort of looking for.

Either way, it's pretty interesting to actually look at. And then I also want to probably have a look at this shaking osculator as well. So it looks pretty interesting, I think. So if we go back into the API documentation. So I think it's this one. Yeah. So it's the Chaikin AD line.

Okay, so this is measuring big investors, I think. So when you're looking for where the big investors are putting their money, this is supposed to be an indicator of it, as far as I understand. So it examines the strength of price moves and the underlying buying and selling pressure to try and measure the demand.

Then divergence between the price and the oscillator is indicated as most frequent single and flags a short-term reverse on price. So that'd be interesting to see if we can maybe find that happening around here or around here. So let's figure that one out as well. Okay. So this time, we're just replacing the function with AD interval, this one's doing daily.

So maybe this is the sort of thing we want to be looking at more frequently. So you have the key, GME. And I think that's pretty much what we want. So let's put this down to, let's go 60 minutes again. And then we can maybe change that. Okay, so let's see what we get.

So this is Chaikin AD. Okay, cool. So let's pull that through. So I'm going to use this code again. So replace this. I'm just going to call it AD or just call it Chad, Chaikin AD. Okay. And what was it? Just Chaikin AD, right? Yeah. So that's what we want.

Let's replace this with Chaikin AD again. And add Chad down here as well. Okay. So I think, so this bit we need, what do we want this bit to be? So just Chaikin AD. Okay. We've got two numeric, so that all looks pretty good to me. Let's add that in.

So obviously that's going to need another row. So let's just add that in. Add Chad. And we'll put this at the bottom. We need to add another row here. Let's see how that looks. So, okay, cool. Doesn't look bad. It's interesting. So the point was that when they diverge, it's an indication of a sort of a movement change.

And that indicates a reversal in price. So I don't know, I suppose here is like the only bit where it kind of doesn't follow the price because the price is pretty level. In fact, it's even going down here, right? And here it starts to go up. So maybe that's indicating the reversal in price change.

But it's also kind of difficult too because we're only looking at our time here, so we should probably increase the, you know, how much we're reviewing. So let's just go to like 10 minutes and see how that looks. Okay, I bet we can't do that. Yeah, it's not returning anything.

It's probably the interval. Oh, you see. Okay, so we need 15 minutes. Let's just go five, see what it's like. Okay, so that's more interesting. So now we just see like this big movement up here. But then I bet if we look at long term, it's probably pretty stable.

So let's try and increase how much data we're actually looking at. So the interval. Okay, so what's our MNDT? Okay, so this is returning. Okay, so this is actually returning Friday. So I was wrong earlier when I said this is a Saturday's data. It's kind of stupid. But does this actually return us anything more than what we have here?

Okay, cool. So we're showing this full rather than compact. So output size is going to be full. So that should give us a lot more data. So before we were heading at 11 on the 29th. Let's see what we have now. Okay, that's way further back. So that's cool.

We probably, I don't know if we'll be able to even see that much though. So maybe it's worth maybe not doing a five minute interval as we're going. Like daily. Let's see what that looks like. Of course that changes this as well. So let's change it here. Hopefully that should work.

Okay, now. So let's check what we are returning. Okay, so how because we are doing the intraday. We can go 60 minutes. So let's change that to 60 minutes. Okay. Oh, no goes even further back. I wasn't expecting that. That's pretty good. That's a lot of data. So now I'm thinking, what do we want here?

Maybe daily? Do we want daily? Let's go weekly for now. Or maybe we can go daily. So let's have a look at these, how they are now. So here's obviously the volume. It's pushing up loads and everyone's buying into it. But you can see there's this massive increase here.

This is back in at the start of January. So start of January. And yet it was going up but not loads. You can just see it kind of pushing up, pushing up. And then here you have same again. It starts to go a little bit crazy. So here went over, went greater than zero.

And this is January 22nd. So here. This is before it all went crazy. So I think it kind of went down here and then back up. Let's see. It's pretty interesting. So, I mean, I think that's it for this video for now. It's probably, I mean, obviously there's a lot we could do like tidying up.

But just in getting a few indicators together and the actual stock data in Candlestick. So it's pretty easy. I mean, I've never done most of this before with Plotly and it's super easy to figure out. So that's pretty good. So I suppose that's it. So thank you for watching the video.

I'll leave all this code on GitHub as well. I'll add a link in the description if it helps. I mean, it's not too much anyway. But it might help you pull things together a bit quicker. So, anyway, thank you for watching. And I will see you again next time.