Streamlit - Building Financial Dashboards with Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] so hey everyone welcome back to another video today i'm going to be talking about streamlit which is the easiest way to build and share your data applications on the web so in this channel so far i've created a variety of web applications in order to visualize and display charts and share our data um on the web however this involves a lot of complexity uh a lot of times when i'm creating web applications i'm creating an api i'm returning json to the browser i'm creating html templates or react components and using a javascript framework and i'm fetching data from a server using javascript and then displaying it with html and there's a lot of back and forth between a network communication and a front-end and back-end code now if you're just a data scientist or researcher a lot of times you don't want to deal with all the complexity of running a web server and that's where frameworks like streamlit come into play and what streamlit and solutions like plotly dash allow you to do is just take your knowledge of python and use that in order to build a full web application and so i was checking out this stream streamlet framework i guess you would say or call it a library i was checking this out and i thought it was really intuitive and straightforward and i really liked it and thanks to python engineer you should check out his channel if you haven't already i've referenced him a couple of times but for people that want to know a lot about machine learning he covers these topics and he recently made this a stock prediction application which uses facebook profit and it's right here and that's where i learned a little bit about streamlit so he has a pretty good tutorial on that um and so shout out to python engineer i was supposed to collaborate with them but i like had a complete meltdown in december and never got it done uh so yeah check out this application uh if you can so let me give you a quick tour of the streamlit website and a couple things you can do with it and i'm going to show you what we're going to build in this tutorial so i have the website open here and you see it says it's very easy to share data apps and you can see right here you just write a little bit of python code and you can essentially just print stuff to a web application so instead of just doing like a python print statement that prints to the console you do like streamlit.write or streamlit.image and just give you these really simple functions where you can just pass it uh the location of a url and i'll just print the image on the page you can pass it a data frame it prints a nice table that's just that displays on the web and it might remind you a little bit of jupiter no notebooks in that way as well but there's also these cool widgets that you can put in on the side to like interact with your data so they come there's little widgets for sliders and drop downs and uh text input and all that so it makes it really easy to like build forms interact with those forms and have those interactions with the forms manipulate the data that you're in your python code and i'll show you how that works in a second and so all you got to do is install this with pip you just install streamlit and you're ready to go and if you go through the docs and the components there's all these cool widgets for charts and and bar graphs maps images all that kind of stuff and there's cool gallery of like people creating these sliders to manipulate images and do machine learning applications so really cool applications of this okay and so what we're going to build today is i went ahead and took this for a test drive and i'm going to use a sum of the data both the data that we populated in the postgres database tutorials so i have this little pattern recognition thing so this little screener right so we can just populate some patterns here and when i select this drop down you see it pulls up the charts that of stocks that match that pattern using our price database and then i have a variety of dashboards here so you can see how i can switch between dashboards so i have one that queries twitter so popular twitter traders and shows the ideas that they are posting and charts so this is a good way to get some simple ideas some ideas for stocks you might want to trade so you see there's like trader stewie there um see the light trading ian mcmillan a few people that post charts all the time it's a good way to just generate ideas you know you don't want to really copy people's trades but it's good to see what other people are talking about and then i also have this hooked up to wall street bets and so you can see the number of mentions and i have this cool slider where i can see the number of mentions and i can scroll back number of days right and if i do like seven days you see how this data changes and you can see the different mentions on social media i also have hooked this up to stock twits so if you type like nvda it'll pull up all the chatter and on stock twits and pull up a chart and then also i have a dashboard that's just you know pulling up charts so i want to show you how to build your own little chart widget and this just uses plotly graph objects to build a chart and just put it in here and then display a data frame so i want to show you how to use just all the different types of widgets and you can put them together in a way that's useful for you so i'll show you how very simple it is to just get a data frame and then output a chart like this an interactive chart output a pandas data frame to the web and have this dynamically load different dashboards depending on what options are selected on the left side here so it's really easy to just place all these different widgets and have those all interact query our database query social media and visualize that data on the web and so we're just going to build uh one web application that has you know five different dashboards with different options and have it a very interactive web application that displays a visual data in a variety of ways so let's go ahead and get started with the tutorial all right as usual i have an empty visual studio code editor open and i'm going to build this from scratch but i'm also going to post the source code on github.com hackingthemarkets and i'll link it down below if you want to just follow along that way and not type it all out but i recommend following along and building this out and you'll learn it a little bit better all right so i have a visual studio code editor open i'm going to go to open and i usually just create a new project so let's create a new folder and i'm just going to call it a streamlit dashboard so streamlit dashboard like that and just open that up okay and the first thing we do is uh install our dependencies so i usually just create a requirements.txt and this has all of the different dependencies that we're going to use so the packages we need are streamlit because that's the main topic of this video i'm going to use pandas for data frames and i'm going to use plotly for the charts and graph the graph objects and i'm going to use a psycopg2 and i'm just going to use that to connect to our postgres database and so if you don't have the database if you didn't follow along with the previous tutorials on postgres you won't be able to do the database part of this but you can substitute in a different data source if you want so if you just want to use csv files to get your price data you can do it that way and we've talked about that plenty of times in the past so if you don't want to use the database part you can also use requests because we're also going to do some dashboards that just make requests to twitter and other apis oh and i'm also going to use tweepy or twee pie uh however you say it and this is a python package for interacting with twitter so i'm also going to talk about uh twitter data so i'm adding that as well all right so i will just create a new file i'm just going to call it let's call it dashboard.pi and i'm going to try to do import streamlet okay and i'll run that to make sure i have streamlined yeah so i have my virtual environment set up already but uh you can install all the packages so i'm going to do pip3 install r requirements text and it'll just find all those packages and install them or you can install all them manually so you can do a pip install streamlit etc etc for all these packages okay and then make sure you get your packages installed and then you can import streamlit and what's cool about streamlit is you just once you install it you have this command called streamlit and so you just run streamlit run and then the name of my file is dashboard.pi and just like that it fires up a web browser and you have a dashboard application so already have a server i already have a web app but there's nothing on it i haven't done anything okay so what do we have uh so i'm going to import streamlit as st a lot of times people just want to shorten this and use st by convention and so let's see if i call some of these functions so uh we have these very simple uh commands to display text so you can display text by typing text we have dot markdown if we want to display markdown we have latex for these math functions latex you say latex latex there's streamlit.write and there's streamlight.title for a title and a header okay so i can do title and i'll say this is the title okay and i'll do st.header this is a header and i'll do st.right this is regular text okay and if i do that and then i go back to my dashboard i just say rerun and you see just like that my web application already has a header it has a title or it has a title a header and regular text and i think there's even a sub header so subheader sub header okay and i can say always rerun okay so you see we have different headings and under the hood this is just generating html and probably javascript right and so this is very handy right see how it's doing all this you have this nice uh minified javascript file and these uh javascript files and css files so it kind of looks as we build this out you'll see it looks pretty attractive by default and that's very nice one downside is that if you really want a very custom user experience and very fancy interactions you're not going to get away with just using these simple functions so you're going to get the most sophisticated user experience if you use pure javascript and react components but this will get you like 90 of the way there okay so this covers a lot of common use cases so that's just displaying a simple text and what's also kind of cool is you can has these magic this magic ability they call it magic commands where i can write markdown just like this so i can do header uh subheader like that and if i do that you see that it i had it always rerun and you see it actually read that markdown in line and if i just defined a dictionary in in place so if i do key value and then key two value two do that you see that it automatically pretty prints the contents of the dictionary just like that and i'm not even assigning it into a variable so it has this ability to like rerun this script so it's just like writing a script but it has the ability to like display data in line so if i just define a data structure there it just dumps it to the screen so it makes it really super quick okay and so if i have just some dictionary equals key value and i do uh st dot write some dictionary i do that and you see it does the same thing right so you can just use this st write command to just print any data structure and so if i say some list equals one two three three and i just do st.right sum list you'll see i have a list there so that's a list and that's a dictionary it's cool so out of the gate you see how you can just print stuff to the screen and that works pretty well okay and so the other thing you can do is create a sidebar so i can create create a sidebar so i'll do st.sidebar okay and i'll do st sidebar dot right right this to the sidebar okay so if you don't put sidebar it'll just write it to the main content page if you do dot sidebar and whatever command it'll run that in the sidebar so you see now i have a sidebar so that's pretty cool too so you can see i already have a web application with some main content where i can print to it really easily and i have a sidebar where i can place some widgets so you can see i already have a very simple web page and just i can print data to the screen i didn't need to worry about the server i didn't need to worry about the css it already you know looks pretty clean and very easy to use okay so i have this sidebar here and let's go ahead and put a widget right so we have all these different widgets so it says display text display data and so if you want to display a data frame and just one more final example here so we'll do this so i'll do df df equals pd.frame and this just generating a random data frame and then we'll put real data in here shortly and so if i do a streamlet.dataframe streamlet.dataframedf so i'll do st.dataframe df okay if i do that you'll see my dashboard it says pd is not defined so i'm going to import pandas so import pandas as pd okay and so all i'm doing here is generating this random data frame and i'm telling streamlet to display a data frame instead okay so numpy is np is not defined and so they're using numpy as well so i'll do i'll just use that as well so import numpy is np and i'll add that requirement as well so install numpy if you don't have it already if you're on this channel you already have numpy so that's it and you see that we generated a random data frame and you see it i don't i didn't need to write a for loop to like loop through all the columns and rows and display this in a nice way it's already there and that you know there's this thing to like expand it out so i can see my whole data frame so it's handling a lot of this display logic for me so what if you want to display an image well there's st.image and you see how these are very easy right image data frame and so image you can pass it the path to an image and open an image on your local file system or you can give it a url and it automatically knows what to do so if i have apple stock chart i type that in and i have this image here let me see if i can get a direct address see i have an image address right there on nasdaq.com if i plug that right in to st.image and i go back to my dashboard you see now the image is in there and see you see how we can we have all these building blocks already and now all i really have to do is write the code to display these widgets and then access my database and access twitter and all these data sources and just use all of these function calls to display my data however i want so in the demo you'll remember that we had this little drop down on the left side so if you look at the api reference you see they're interactive widgets and so we can you call button to create a button we call check box radio select box and so forth and so you see i have select box here and so to put a select box on the page i can do st.select box okay and then let's see the parameters let's let's just copy their example okay option equals st dot select box and we'll say uh which dashboard okay and then we just pass it this tuple here so we pass it we give it a tuple with all of the options so we had a number of dashboards we wanted to make so in this tuple we'll put the twitter dashboard and we'll put the wall street bets dashboard and i'll just use lowercase twitter dashboard wall street bets dashboard we have a chart dashboard and we have a pattern dashboard and then we have what else i could stock twits okay so we have a variety of different dashboard examples we have and yeah let's look at i have it always refreshing so that's with re there's an always rerun or you can just manually rerun and you'll see at the bottom and now there's a little drop down just like that we have a drop down right there on our web page we didn't have to write a select tag option tag anything like that okay so you see that's in the main content so to put it on the sidebar instead of doing sc.selectbox we do st.sidebar.selectbox and it refreshes and now we have a which dashboard right there okay so instead of uh write this to the dashboard write this to the sidebar here i'm just gonna say options okay so we have a heading and instead of just right i'll do a title let's see yeah so that looks pretty good these are options which dashboard right and so you can select these different options so how do you make the page do something depending on what's selected right we want a different dashboard display depending on which one i chose and so there's some magic here where i can just say if option equals equals some value so let's say twitter i can do st dot actually before i do that i'm just gonna do st dot header option okay and i'm going to comment some of this other stuff out for now okay so i'll comment that let's comment all that out okay so all that's here now is the sidebar and st.header option and look at that so i have pattern wall street bets stock twits chart and pattern and it's kind of magical right so you see how this document is automatically reacting to what i select here so every time i change it option has a different value and then this option i can just use it in the header right there so it kind of almost reminds you even though it's in python kind of reminds you of a react component where there's a state for this particular component and depending on that state you can do something else on the web page so i can change that and then this st.header call automatically displays the option i have selected so that's pretty cool and so now you can see how i could do if option equal equal twitter i could do one thing so i could do st.subheader twitter dashboard for dashboard logic and i can do if option equal equal chart st.subheader this is the chart dashboard okay so that's the chart dashboard that's the twitter dashboard logic okay so you see how i'm switching between those and having this conditional logic that can so we have the sidebar component and that controls what happens on the right side so yeah that's that's really cool how that works like this this saves like a ton of time the ability to change that and just have a simple python conditional that writes content to the appropriate part of the page it makes it look nice without any additional logic so the first one i'll do here i'm going to do we can technically separate all these out and put each dashboard into a separate file i'm just going to do this all in one file and it'll be like a little over 100 lines of code and so the first one i'll do here is stock twits and so i'll do st.subheader stock twits let's see subheader is not defined so it's st.subheader okay so you see i have a subheader here and i don't need that subheader so let's look up the stocktwits api and i can go here and then we can look at the different api methods and then there's one for streams and so i can get the stream for a particular symbol and so the way this works is i can just do a web request to this apple.json here and since i have a request i can do r equals requests dot get okay and did i import i'll import requests and all this is is a web request to stock twits and if i send them the url in apple.json it'll return me all of the most recent mentions of apple stock and so r.json will have that so data equals r.json and then let's do st.write data okay so making if they select stock twits i'm going to request apple json from stock twits i'm going to get the response as json i'm going to write it to the web page so let's check it out so i have my dashboard and you can see yeah it's already i didn't need to do anything it automatically reloads okay so i have it always rerun and so i'm gonna go to stock twits okay you see i made the web requests we got a response got the symbol for apple and now we have all the messages for apple stock or at least the most recent ones right so we have this nice dictionary response already ready to go and so let's just loop through all these messages right so messages just a list so we'll have data and then we'll have quote messages and we'll loop through these okay so i'll do uh four for message and data messages let's just write out the message instead of all of the data let's do it that way okay so it's going to refresh and then you see now we're just looping through the messages so this is what a stock twits message look like and you see since we didn't print this to the con when we printed to the console in the past it's kind of hard to read and we got to like prettify it to read it but this i can just see everything going on so i can just write the body so i'll just do st right message body and then what else do we have created at will be nice so message created at okay and then we have the user so let's do the user and the username so we'll do uh st.right message user username so it's nest in the user and username and then we have the user's avatar so we can do st.right message user and then what is it avatar url so avatar url oh yeah and we don't want to do right we'll do image okay so it'll write the image so we have a web url for the image and just like that you see how easy that was we have a stock twits dashboard we have apple stock and so we have dimensions we see glizzy waters mentioning it at a certain time and we have that person's avatar and we have that image on the page we didn't write any image tags or anything okay so i'm going to reorder this a bit so i'm going to put the user on top then the username and then create it at and then the body okay so that's pretty good just like that i like it um and then so apple right we don't want to do apple stock every time right i don't want to hard code that says apple.json and so what i want to do is have another option here so when you select stock twits we want to display options that are specific to the stock twitch dashboard and so let's let them type in an input here and so what i'm going to do here is i will get rid of that pass and then on the sidebar so if the option is stock twits i'm going to do st.sidebar dot text input so we have a text input so i'm gonna look at the documentation yeah so we have text input so i'm gonna copy that okay so we have st.sidebar.text input and let's see what it expects we want a label so i'm going to call it uh symbol right we want the label for the text input to be symbol the default value so we'll put apple as the default value we don't need a max charge well we can do max jars of five and that's i think that's all we need okay so you can type a symbol in and then we'll make this string be dynamic so i'm going to make this into an f string and then this instead of apple here i will do a symbol so i'm going to assign this to a variable so i'll say symbol equals sb.start st.sidebar.txtinput and then whatever symbol they type in it's going to get assigned a symbol and then this web request will be dynamic depending on what they type into the text box okay so if i refresh this you see that the symbol is apple but i can see what people are saying about nvidia i can see what they're saying about microsoft twitter and so forth so we already have a dynamic way to look at stock twits and you can obviously this is not a very complex example you could go to stocktwits.com and type a lot of this in but you can do more and more complex things like add filters and query this api a number of ways so there's cool stuff you can do here like trending watch lists you can search on particular users you can look at forex information there's a lot of stuff in this api and what's cool about streamlightwrite and apis is that you can just make your own mashup of all this and the view to the world that's useful to you all in one place so there you go there's the stock twits dashboard so the next dashboard we'll create is the twitter dashboard and so i installed uh tweepi or tweepy and so i'm gonna import that right here i'm gonna get rid of all this stuff we we wrote earlier just to clean this up and so you see this is just the small amount of code for the stock twits dashboard and so let's fill out the twitter dashboard right so i'm going to select twitter okay twitter dashboard logic and let's just place what we need to access twitter so we have uh tweepon and what i'm going to do now is i'm going to create a config.pi and so this does have a prerequisite if you want to do this part you actually need to sign up at developer.twitter.com and submit an application so that you get this api key and so they'll give you like an api key and an api secret and i'm going to put that in my config.pi and so to do this part i'm going to do a twitter consumer key in my config.pi and i'm going to set that equal to value twitter.consumersecret and they'll give you all these values and you just plug them in and it takes a little bit for them to approve it so this isn't instant but it's still interesting so follow along if you don't have this already and you can build it later in the future should you wish to so i got twitter access token and twitter access token secret and i'm going to put this information in the config file like my personal one i'm not going to show that on the camera and the other thing i'll do is put some popular twitter traders in here so some usernames and these are the people we're going to scan for ideas so trader stewie is a super popular couple hundred thousand followers we got the chart life okay that's ian mcmillan he posts a lot of these bass breakout setups connect to usa plus a lot of charts and i got sunrise trader trader and uh tml trader okay and so i'm just defining a constant list of usernames and what we're going to do is request all the tweets of these particular users that we're interested in and find the ones that have cache tags in them and then show a chart for for each of their tweets and then they'll just give us a quick way to look at all the ideas that are getting posted on twitter so i'm gonna go to the documentation for a tweepy here tweepy.org i'm gonna go to read the docs and they have a little hello world type example and we'll just start with this and so i'm gonna copy this so we already imported uh tweepy and let's go ahead and initialize the api and so i'll just put this at the top here okay so i got tweepy.oauth handler and remember we just created that config file okay and then my config file has my config config.twitter consumer key and my config dot twitter consumer secret and then they have this access token so i'll just substitute in mine twitter access token and config dot twitter access token secret so now that we have this uh api instance here we can call uh methods of tweepy to get information so we have like api.home timeline there's one for a user's timeline you can get a user so there's all these different methods that you can call and so what i want to do is get a user's timeline so for each user that i listed each of those traders i just want to get their timeline and so i can just pass it their username and it should get me their tweets okay so i have api here and so what i want to do is under if option equals twitter i'll do api dot user timeline and let's just see if this works trader stewie so we'll hard code it in at first and i'll do tweets equals api user timeline trader stewie and i'll do st.right tweets okay let's see what that looks like okay and so you see now i already have a list of these status objects so we want to unpack that a little bit so if i do a for tweet in tweets st.right tweet it'll print each one individually and when it refreshes you can kind of see the structure of one of these tweet objects so i'm interested in the text right so i'll do a tweet dot text there you go so that's just the text and you can see some of these uh stocks so these cash tags here dpw and dollar ft ft and so let's only trader stewie often post a lot of pictures of food and you know beer and things that he's doing so if we're just interested in the stocks he's posting let's just parse the ones that have these dollar signs in them so what i'll do now is say if a dollar is in tweet.txt then we'll go forward so i'm only interested in tweets that have a dollar sign in them and then i'm going to split the text so i'm going to say words equals a tweet.text.split and i'm going to split on the spaces so that we have a list of all of the words and then i'm going to go through each of the words so i'll say for word in words if word dot starts with a dollar sign right if there's any words that start with the dollar sign and a word the rest of the word is alpha numeric so i'm going to say is alpha because they might just say dollar 799 that's just a price we want us things that start with a dollar sign and end in a few characters alpha numeric characters that way uh it's more likely to be a stock symbol okay and i'll do uh symbol equals word and then the symbol is the rest of the word so if the word is dollar dpw the symbol is from the first character to the end and we're gonna leave off the dollar sign so i'm gonna do word one colon and let's just write the symbol okay so these are just the symbols in his tweets and so what we'll go ahead and do is also you know write the tweet text in this case so i'll write tweet dot text and let's also show let's show the charts of these as well in line so that we can view them very easily and so what we can do is get the format for finvis which he often uses and so one way to easily get a finvis chart is you can put the symbol in a url like that and use their site to get this image of a chart so i'm going to put this format in here so the way to do this if you go to finviz you can look up a chart and i'm using the charts here and there's a special url url format that you can use to get an image of this and i've done this in previous videos so i'm going to take this format to get the raw image i'm going to do st.image and then instead of doing apple there i'm just going to substitute in the symbol so i'll make this an f string and then i'm going to put the symbol just like that and if i do that you'll see that now i have a trader stewie's tweets that mention stocks and just a list of stocks he mentions and along with the chart there so you can see him talking about neo bingo apps turbine i think digital turbine dpw and ftft maybe you want to look at him maybe not okay so that's a list and then if you want to like show his avatar i believe this also has a way to get their avatar so so we can do a user equals api.getuser trader stewie so we can do st.image and it's user dot profile image url and i already looked that up and if you don't know that you know there's no reason you're going to know that it's in the tweepy documentation and i took notes on this earlier so if you do get user this is a way to get the user and you can print that out and see all the information that you have available and you can see that i was able to get his uh yeah there's his avatar there and then i can also just print his username and so now instead of just doing trader stewie let's just loop through all of those all of the usernames we had in our config file so we'll do uh for user in config.twitter usernames and we'll do for username and twitter usernames okay and then we'll indent this out and we'll do user equals api get user and since we have username in this loop i'll get username and getusertimeline of username so we're making this dynamic now and then we're getting each of their profile images the tweets for each one of those users and then looping through all the tweets and displaying all the charts and if i do this now you'll see we have trader stewie there we have ian mcmillan there posting cryptocharts we have materials a lot of energy names showing up and who else do we got jets which is uh airline recovery play uh baidu looks like it's taking off and so yeah so we have all these traders timelines and yeah you see someone highlighted a twitter right there and so you kind of see and the intro the video if you recognize is a reference to one of the last videos we did on arkhanvest where we pointed out kathy and arkanvest buying twitter right here and you can see you know twitter's had a nice day since then so hope you got in on that um yeah and peloton cloudflare so people are talking about lots of stocks and yeah easy way to access twitter data and let me go ahead and write the username to other and not just the avatar so i'll do st.image i'll do st.right username actually i'll do header there you go trader stewie and i'll do subheader there you go so i have the username and then you see the chart life is here and so forth so now we have two things we have our stock twits dashboard that lets us see mentions for a particular stock right and then we have our twitter dashboard that lets us just show a list of users we're interested in in and charts of all the symbols they're talking about so next let's go ahead and access the wall street bets database that we had created previously and this should be very straightforward we already have the queries and so i'm going to go through this real quick just so you since you already know the concept of streamlit here so i'm going to do if option equals wall street bets and i'm going to say st subheader wall street bets and then i'll do if option equal equal pattern we'll do st.subheader pattern okay invalid syntax i forgot a colon okay wall street bets good and chart and pattern and let's let's fill in all these dashboards and so i'm going to do here for the wall street bets version is this is going to be database driven and we've already created a database connection in a previous tutorial so i'm going to do here is at the top initialize a connection to our database so i'm going to make sure we import import psycho pg2 i'm going to create a new database connection and i'm going to pass it the host database username and password i'm going to put these values in my config file i'm going to create a cursor okay so i already have these and these are the same as the previous tutorial i have a local database running at my localhost 127.001 and just a database user postgres and password password and i have that etf database we've already created so i'm establishing a database connection and a cursor and at that point i can just run sql so if i go to wall street bets here i can do cursor.execute select and let's just select some real quick so i'll do select star from mention uh and then i'll just do limit 10 and i'll do rows equals kircher dot fetch all okay and i'll do st.right rows okay so i'm fetching data from a database okay config has no value db host so let me save that okay and then i also need to import cycle pg to okay there we go okay and i want it to default on wall street bets and so since that's this version since i'm working on this code right now when i reload the page i might want it to default on a particular dashboard so what i can do here is as a parameter here which dashboard i put the options and then since this is the zeroth item in the tuple and this is the first item i can do a one there right and now when i refresh it you see how it starts on the second the first indexed one and now if i change it right and i reload the page it'll default to stock twits so i want it to default to wall street beds for now just so i can get this done so you see how i fest from the database right and i already have these mentions populated and this is just 10 records from our mention table and these are cache tags that people are talking about on wall street bets so what i'm going to do now is go ahead and copy some queries i've already created just to make this quick so so what i'm doing here is i'm using my database cursor and i'm running this query that's going to count the number of mentions it's going to fetch the symbol and then it's only going to find the mentions from the last 14 days and we're going to group by the stock id and we're only going to show stocks that were mentioned more than 10 times in the last 14 days and we're going to sort by number of mentions i'm going to write those to the screen i'm also going to just write out 100 mentions on the screen and i'm going to write out the date time symbol message the url and the username and write those to the screen and if i do that you'll see i have a list here and you can see gamestop people kept talking about that uh sndl sundial that was like a wheat stock amc tilray and so forth for the last 14 days and you can see uh one comparison you'll notice we talked about arc invest and we talked about mentions on wall street bets over time and what you'll notice is a lot of these stocks were just very quick uh pump and dump right so you saw when i talked about silver silver spiked 20 on monday and then immediately sold off the weed stocks one day they pop like all thirty percent like til array and sundial or a hundred percent they all immediately sold off a game stop you saw it happen uh and so forth so a lot of these meme stocks aren't very sustainable but also you know if you got in on early on these mentions then might have worked out for you whereas the kathy woodstocks they were a little more sustainable like twitter you know it's grew slowly over time and you know they're larger companies and have some fundamental reasons behind buying them so you can look through this list and maybe you'll find something of interest right and so i hard coded this for the last 14 days but what if we want this to be dynamic and so i mentioned you can add a slider widget and so yeah what if we want to add a slider on the sidebar here to control the number of days what i can do is go here and if the option is wall street bets i'll erase that and i'll just do num days number of days equals st.sidebar and there's a slider widget and i'll just say the label is number of days and i'll say 1 30 and 3 okay and so this gives it a minimum and a maximum and now if i look over here and i'm going to make this so you see how i don't have the rerun on anymore so i had to manually rerun it but you can also set it to auto rerun and so you see that 1 and 30 gives it a minimum maximum and the default here is three okay but this is not hooked up to my query so instead of interval 14 days what i'm going to do is put a placeholder holder here so i'll do percent s and i'm going to pass this tuple here num days and this comm at the end is very important so the number of days that you enter on the slide sidebar in the slider it's going to go get plugged into this query by this list here by this tuple right and so if i select 5 on the sidebar 5 is going to go in here because of this variable so if i do this you'll see i can slide this over so i can do last 12 days or i can go back to just one day two day right and it'll dynamically adjust so you can see palantir was mentioned more frequently uh recently okay and so you see how this slider is kind of cool it's like dynamically controlling a query and we could even like put this into a histogram as well or show the mentions over time across different days if we want to but i'm about to do a chart example so i'm not going to spend a lot of time on the visualization part i just wanted to show the counts and the reddit posts here so that's pretty cool we already have three dashboards we've used a select box we've used a slider we've used a text input to dynamically control three different dashboards with three different social media sources including wall street bets twitter and stock twits okay and so now let's go ahead and switch from social media and let's talk about charts and patterns and so let's go ahead and do our little pattern recognition that we did before so if you'll remember in the uh chart patterns video we created a few queries so in this time scale db aiohtp i've written just a laundry list of queries that we ran and so i have this one for bullish engulfing pattern and this one for this three bar breakout and so what we can do is copy these queries and run these queries against postgres the same way and i've already done this in advance just to make this quicker now since we've already just covered the core concepts so i'm going to copy this and explain what it does so i'm going to go to option equals pattern so option equals pattern and then i'll set that as the default one zero one two three four so let's work on the pattern dashboard okay and then under pattern here i'm going to put some code and you see like i have this gigantic query here and so basically all i do is have a query for each pattern so i wrote this one query to detect a bullish engulfing pattern review the previous video if you want to see how this works with the lag functions right and so i have a select box now that says which pattern and you can choose engulfing or three bar pattern if it's engulfing it runs this query and if it's a three bar it runs this query okay and then we just fetch the rows and use our pattern for displaying charts so finviz and then the symbols that these queries find we loop through and display charts that match those patterns so if i save that let's see if it works okay so i'm going to go to rerun okay and so now you see we have patterns and you see on the engulfing here i set this for i've already populated this again so i have this for february 18th right and so what i can do here and you can see for february 18th which is the previous day is right here this is february 19th it's friday night when i'm recording this uh so this is february 18th so you see the bullish engulfing pattern on docusign and so you know that's looking pretty good to continue even though docusigns ran quite a bit uh looks like it's continuing on you can see the bullish engulfing pattern on this raven ravn what is that a raven industries and so that one's been breaking out as well so agriculture products okay and then ups their bullish engulfing pattern doesn't look like it's gone anywhere so we'll see how that works out and then let's go to three bar pattern here and you see that ati stock here so if i look at ati stock allegheny technologies we can look at uh caterpillar looks great uh caterpillar so big name makes the tractors and such you can see it's it's just been on fire um and then emn hubspot yeah look at hubspot yeah so that's that's really been going crazy uh teledoc has been a good one and then this emnn and i can put a more dynamic chart to see a longer time frame we can see yeah this is about to go to all-time highs again so you know there's something going on here with eastman chemical company kodak which we took which came up in the uh previous one of the previous videos actually as an as an arc holding for some reason okay so yeah these might be worth looking at as a stocks that had a uh i think that i think our definition was that they sold off slightly on lower volume and then had a huge candle after that three bar selloff so you see all of them kind of have this little pattern here where there's a few days of decline and then a huge wake-up bar right so those might be worth looking at teledoc's been one of the best stocks for the last couple years um yeah so there you go there's a pattern detection right there that shows you a bunch of charts and so for our final dashboard let's go ahead and get a chart going and so i'm going to display this chart dashboard and so i'm going to do 0 1 two three let's select the chart one by default and so if the option is chart let's play some more code and i'm gonna do another paste just because i'm running out of time uh so i just wanna go through this so you don't have to watch me type at all so i'm going to do that and you see what i'm doing here let me rerun this and i need to import the plotly graph objects so i'm going to import plotly dot graph objects as geo okay let's see let's rerun let's see if this works there you go okay so that worked and so let's talk about what this is doing so i'm going to go back down to chart and see i'm putting a text input right so we displayed a text input and we can type in a symbol name so nbda or apple or microsoft or twitter right you can look at the chart and what that's doing here is whatever value i type in the text box is getting assigned a symbol and i'm using pandas read sql so pandas has a feature where you can execute an sql query so i'm just running an sql query to fetch data from my daily bars and this read sql qr function i covered in the full stack tutorial when i did the back testing so we back tested that opening range breakout and i used right read sql to get data uh from my sql database and then read it into a pandas data frame so it takes a query um a database connection and whatever parameters you need so you run the sql query and returns the results as a pandas data frame and in the back testing video i used this panas data frame in conjunction with backtrader to backtest a trading strategy here i'm just reading the data the price data into a data frame and then once i have the price data and a data frame it's very easy to use a plotly so just a the candlestick charting functionality that's part of plotly so if you go to plotly a candlestick chart okay they already have these graph objects to create a candlestick chart right so you can import and you do go figure and then you could do data equals go.candlestick and you just give it a date open hilo and close and it'll render render this candlestick chart for you and i'm not going through all this again because i've already made entire videos on candlestick charts on using pandas read sequel and someone was saying like you keep referring to these other videos i have to watch all the videos just to get uh to understand this project and yeah that's that's true since i've been making these videos over 100 videos over the course of a year i don't want to keep explaining the same concepts because some people have been following this channel for a year and you know they'll get bored if i just start from first principles over and over again i used to do that but now i'm just kind of assuming you have some knowledge of things i've already talked about so i'm using the candlestick chart here giving it the open hilo enclosed from my data frame that i read in from sql and then i'm setting this type equals category because that will exclude weekends since i have no price data on weekends and you can experiment with the layout if you want like a different height and so you do streamlit so st.plotly chart and you give it the figure and then you just write that to write the to the webpage so you see how i made that tall i don't want it that tall so i'll do 500 right and adjust it down 700 right so find the right size experiment with the layouts this is just a basic chart it's okay so you can pan pan and zoom so basic functionality for charting in here but you can make more and more sophisticated charts if you would like and you see how it's able to just dump out this data frame so i read from my database using sql query read all that data into a data frame and just dumped it on the screen and then i can type in the name of any symbol so palantir i type that in there and you see we have the chart of palantir dynamically rendered in our dashboard along with the price data that we can do whatever we want with so yeah look at that just like that little whirlwind tour there uh that's how you use streamlit and you see how i didn't even need to really dive into the documentation that much it seems very uh straightforward right we just do s t dot whatever type of widget or thing we want to render so we can write to the screen we can put an image on the screen we can put a select box a text input and we just store the results of whatever the user does like whatever the user selects on the slider or in the text box we store it in a variable and then we can just run conditionals on that variable and render different parts of this document or query different data depending on what the user does and we have a nice dashboard that looks pretty good and you know you might be saying what's the downsides to this the downsides i see is there's some complex user experiences that i wanted to do that it seemed a little bit more tricky like i wanted to show the avatars for all the users on the sidebar and have you click the image have clickable images where i click trader stewie and it just shows his tweets or have a fade effect and do all these effects or um have clickable grids and image galleries there's certain things like if there's not an out of the box component then it might be tricky to do and you'll have to drop down i think you can actually add react components to this thing um but it's more if if you really want to do exactly what you want and it's not built into the framework you're kind of limited and then also it's not really a full-on web application you'll notice when i change and click around this url doesn't change so if you want this really truly multi-page web application experience with logging in and out and and changing routes and all that then you might need something more sophisticated but if you just want to have some data frames some data sql database csv you want to display some quick chart widgets and provide users or other people that you collaborate with an easy user interface for controlling that data streamlit pretty good way to go very simple and i think i think we learned a lot and made some useful applications in just a short amount of time so uh thanks a lot for watching this video and stay tuned i got a lot more coming out soon take care
Info
Channel: Part Time Larry
Views: 58,781
Rating: 4.9492388 out of 5
Keywords: streamlit, python, dashboards, financial dashboard, dataviz, candlestick charts, wallstreetbets, reddit, social media, twitter, tweets, stocktwits
Id: 0ESc1bh3eIg
Channel Id: undefined
Length: 56min 10sec (3370 seconds)
Published: Sat Feb 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.