Financial AI Assistant in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on guys welcome back in today's video we're going to build an intelligent financial assistant in python so let us get right into it now before we get into actual coding i want to show you what we're going to end up with so i have this chatbot here this financial assistant which is already trained and i can do stuff like say hello it's going to say hey florian then i can say please plot a stock for me and those are not hard-coded inputs by the way so enter ticker symbol aapl for apple starting day is going to be i don't know 18th of march 2020 and then we're going to get this plot this candlestick chart here um then if i okay now it's a little bit lagging there you go if i now stop this i can again proceed with hello and i can do show my stocks your portfolio it's going to say what i have how much is it worth and it's thinking it says your portfolio is worth whatever so this is what we're going to build today and you can add as many functionalities as you want to all right so for this project we're going to need a bunch of external libraries we first need to install and for this we're going to open up a command line cmd in my case and we're going to install them using pip so if you're on linux you use pip or pip3 if you're on windows you use pip on mac i think it's similar so pip install and then we first start with neural intense this is going to be the fundamental module in today's video because that is the library that i have written myself to make it easy to create chatbots and ai assistants without having to go through the machine learning and neural network uh declaration on your own so if you're interested in what is happening behind the scenes you can just go to my github github.com9 and you can look at the code in neural intense we're going to use that library that makes it easier for us to create such chat bots so pip install neural intense like that then we're also going to need matplotlib for the visualizations we're going to need mpl finance for the candlestick charts we're going to need pandas for working with data frames and we're going to need pandas minus data reader to get the data from the yahoo finance api the other libraries we're going to need are part of the core python stack so first of all we're going to say from neural intense import generic assistant then we're going to say import matplotlib math.lab.pipeplot splt then we're going to import pandas pandas as pd and we're going to import pandas data here as wep and we're going to import mpl finance smpf and i think that's it for the external modules we then also need pickle so we're going to import pickle for serialization and we're going to import sys in order to exit the script when it's necessary so and of course one more thing we need to import date time as dt there you go so those are the imports now what our assistant is going to need is some sort of guideline that tells it how to react or what to say when we input a certain type of message so let's say i say something like hey hi hello then of course i want to chat bot or the assistant to answer with a greeting if i say something like please plot the stock or please show my portfolio i want the chatbot or the assistant to take that action however i don't want to hard code all the variations of how i could phrase that so if i say something like please plot a stock for me that should be equally good as saying plot a stock or anything similar to that so we don't want to have to input all the variations of that statement uh manually and we don't want to hard code that in order to make that happen we need to have a set of sample inputs and we need to have intents we need to have categories now i'm going to show you a json file now please don't despair because the json file is quite simple i'm going to show you to you um and tell you how it's structured i'm not going to type it all with you here because that would just waste time it looks large but don't don't despair because of that this is the json file but it's very very simple and you can you can just remove and add individual things so what we have here is this basic intense object and as you can see everything below that just repeats so it's it's not too complicated you have intense then you have the same set of things below it's always the same and how it is structured is we have intense and then this intense list here this intense object is actually a list of individual intents each each intent has a tag which is you could say the category for example greetings or plot the chart or add something to my portfolio remove something from my portfolio show my portfolio and so on and then we have the patterns the patterns are sample inputs so we said that we don't want to hard code it which means that if i provide enough samples like i want you to plot uh plot a stock price or please plot a chart plot a stock plot a stock price show me a stock price chart and so on if we have enough of those inputs it's going to recognize using machine learning which input which message from the user is similar to those patterns and if it recognizes that the pattern is similar it's going to see okay this is the plot chart intent my response is going to be this now we're not going to work too much with responses which is why i most of the time don't have anything important in here because we're going to use the tag of the intent in order to recognize what it wants wants us to do by the way we can remove the content set here you can ignore that this is not important this is from an old project however this is the structure of the intense file you can create your own intent file however you want i can show you here i have the categories greetings plot chart add portfolio remove portfolio show portfolio stock price um portfolio worth portfolio gains and buy you can add and remove whatever you want so maybe you just want a chat bot that is going to greet you and plot stock prices then you don't need anything more than that what you do is you provide patterns which are sample inputs and if you want responses you can also leave that empty the important thing is that you have tag and patterns and of course we can make this a little bit more beautiful by just saying reform it code here but that looks a little bit larger so i didn't do that this is how this looks as a json file intents tag patterns responses tag pattern responses and so on this is what we're going to need we need a json file that is structured like that you can have five intents you can have a thousand intents you can have two intents you can have one intent which is kind of useless but that is your decision how you structure your intent file depending on what you want from your financial assistant you need to put that information in here this is no functionality this is just recognizing what you want in the patterns you have to enter a bunch of different ways to phrase what you actually want and once you have that you can start with the coding so i'm going to first show you how the assistant basically works and then we're going to implement the functions of the individual intents so in order to create an assistant you just say assistant or whatever you want to call it equals generic assistant and this generic assistant now takes parameters first of all it needs an intense file or an intense object in our case we just have the intense.json file that i just showed you it's not too complicated whatever you want to put into into it and then you can if you want also specify additional things now if you don't do if you don't specify additional things it's going to take the responses from the json file but what you can do with that assistant is because i'm or let me first show you how you actually request something from that assistant if you have that assistant what you do is you train it so you say assistant dot train model this is all happening in the background this is what neural intense does for you automatically it trains the model based on the intense file so it's going to recognize patterns and intents once this is done what you can do is of course you can save them all so you can say assistant.save model and then you can load the model as well but that's just something on the side once the model is trained and you have it trained you can just go ahead and say assistant.request and you can pass a message like hello i want to see my portfolio or whatever you want to do here now once you call this request function here you're either going to get a response if you don't have any mappings or you can map a function to the intent how do you do that you specify a dictionary which you can call mappings for example and mappings is just going to be a dictionary that maps an intent type to a function so what i can do here is we have the intent greetings which is the tag that's important you always need to specify the tag so greetings in this case we have greetings and if the neural network if the assistant recognizes that the message that i now entered belongs to greetings then call this function here so i can define a function up here my function and it can do something i code it too much and go i guess def my function we're in python so we use colons and then we pass here so this is just a basic function if i want this function to be called whenever greetings is recognized then i pass my function here but not by calling it by just referring to it and then i have to take these mappings here and pass them to the generic assistant by saying intent methods equals mappings like that and now every time i request and pass a message like hello for example it's going to recognize okay hello belongs to greetings which means that i have to do greetings all greetings is mapped to my function so it's going to call my function this is the basic functionality of the neural intense module all right so everything else that we need to do now is just coding the individual functionalities for the different intents so in my case i have stuff like plotting charts adding to portfolio removing from it showing it and so on and you can do whatever you want here you can also just have a chat bot that you want to you know that you want to web script some news about a company for you you have to find the code yourself and just input it there i'm going to show you those different functionalities here but on my channel i have different tutorials like predicting stock prices like a new summarizer like all the different projects you can just take them and uh put them in here you just need one function one intent and that's it you can scale this module up however you want so i'm going to delete all this down here because we don't need it yet first of all we're going to start with a function so the first one or actually before we start with the functions we need a portfolio so we're going to say a portfolio it's just going to be a basic dictionary which is going to have as a key the company for example apple or the ticker symbol and then as a value how many shares i have from that company for example this is just a an imaginary portfolio it's not my my portfolio so let's say we have 20 apple shares let's say we have five tesla shares and let's say we have uh i don't know 10 goldman sachs shares like that this is the portfolio and now what i want to do is i want to save that portfolio into a uh into a file so i want to serialize it into a pickle file and in order to do that i'm just going to say with open and i'm going to choose a file which i'm going to call portfolio dot pkl open that in writing bytes sf and we're going to say f dot or actually how do we do that we say pickle dot dump and we dump the portfolio into the file there you go so this is how you do it and now we can run this immediately because later on we are just going to load it and save it back into that this is just the initialization um i think it's still loading all the modules let's let's wait a little bit there you go and now we have this portfolio.pkl file so i can delete this line here and i can delete uh i mean i don't have to delete it i can actually use this as an opening so i can say open the portfolio file sf and then i'm going to say portfolio equals pickle load f like that and then i can go ahead and say print portfolio i do that now i i already have it in a file so i don't have to have it just as an object during runtime i have this portfolio saved into a file so i can load it change it and save it back into the file which is what the assistant is going to do so once we have that this is how we load the portfolio when we start the script uh then we're going to have a basic function which is just going to be safe portfolio so safe it's not going to be a command it's just going to happen as a byproduct of changing something so safe portfolio is going to be with open portfolio pkl in writing bytes sf pickle dot dump the portfolio into the file so if we change something we're going to save it like that so let's see the first thing we're going to do is we're going to add something to the portfolio so whenever we trigger the add something or add add stock or add something to the portfolio intent we're going to call this add portfolio function here and we're not just going to execute it we're going to ask for some further input but everything that happens inside of that function is going to be hard coded so uh it's going to ask for what we want to do and so on but we're going to have this hard coded so the first thing we're going to do is we're going to ask for the ticker symbol we're going to say okay which stock do you want to add and we're going to pass that and then we're going to ask for the amount and we're going to see which amount or what amount or let's see how many shares do you want to add maybe i shouldn't be using title case here why am i doing this what stock do you want to add all right and now the important thing is that we need to take care if the stock is already in the portfolio we don't want to overwrite the value one increase it so we're going to say if the ticker is part of the portfolio dot key so if we already have a stock like that in the portfolio we're just going to increase it so we're going to save portfolio ticker is going to be plus equal amount otherwise if it's not in there we're going to create a new field by saying portfolio ticker is exactly the amount like that um yeah and in the end of course we need to save the portfolio then we're also going to implement the remove portfolio function here and this is going to be quite similar we're going to ask again for the ticker we're going to say ticker equals input which stock do you want to remove and we're going to ask for the ticket symbol here then the amount or actually we don't want to remove it which stock do we want to sell it's probably better because we don't have to remove all of this all of the individual shares and we're going to say amount equals how many shares do you want to sell that's it and then we're going to say if the ticker is actually in the portfolio keys portfolio dot keys then we're going to allow it to be removed so we're going to say uh or actually we need to first check for the amount so we're going to say if the ticker is part of the portfolio if we already have that stock we're going to check if we have um the amount that this user is trying to sell so we're going to say if the amount is less or equal to portfolio ticker then we're going to allow it so then we're going to say portfolio ticker minus equals amount and we're going to save the portfolio otherwise we're going to print you don't have enough shares and if this security is not even on the portfolio we're going to say you don't own any shares off and then ticker and last but not least simple function show portfolio is going to be dev show portfolio and we're going to print your portfolio for ticker in portfolio we're going or actually portfolio dot keys we're going to say print f string you own portfolio of that particular ticket symbol shares off the actual ticker what's the problem here oh like that so we get the actual amount from the portfolio we say you own that amount shares off that particular company uh and that's actually it so now let's go ahead and create another function which is going to calculate the actual worth of the portfolio because we have an intent that is called portfolio worth and this is asking how much is my portfolio worth so if the investor is asking that the assistant should say okay i'm calculating the value and this is the number so we're going to say def portfolio worth and we're going to start with the sum being 0 and then we're just going to iterate over the tickers so we're going to say four ticker in portfolio keys and then we're going to load the data from the uh yahoo finance api we're going to get the last value and we're going to add that to the sum so i'm going to say data equals web.datareader of that particular ticker from the yahoo finance api and the price is going to be data close dot i lock negative one which is the last value and then we're going to say sum plus equals price in the end we're going to say print f string and we're going to say your portfolio is worth some us dollars the next function we're going to implement is the portfolio gains function this is the next intent and it's actually asking how is my portfolio performing so compare it to a date in the past now you can come up with a sophisticated algorithm that also takes into account uh that you sold and bought in between but we're just going to compare for the sake of this video the stocks that i own right now the value that they have right now compared to the value that they would have or that they had in the past the exact stocks that we own right now so i'm going to say portfolio gains and we're going to say string or actually starting date is going to be input we're going to say enter a date for comparison we're going to use the date format of pandas which is year month day date this is just so the user knows the format and then we're going to start with a sum now being zero and the sum then being zero and we're going to try because sometimes on some days there's not going to be any trading and in such a case we're going to get an index error uh so if we choose a day that uh there was no trading on we're just going to say okay that day there was no trading on it so we cannot look up the price there but if there was a day like that or actually let's go ahead and just make the make the exception handling first accept index error pass for now and in here we're now going to say okay try for every ticker in the portfolio keys we're going to say the data equals web dot data reader of the ticker from the yahoo finance api and we're going to get all the data and we're going to say the price now is just the same thing so data close i lock negative one and the price then is going to be a query a pandas query so price then is going to be data dot location and here we're going to say the location where the data dot index is going to be the same as the starting date so the index is the date and if we found that column we're going to get the close value where the closing values end from the values zero there you go so that's the price then and then we're just going to go ahead and say sum now plus equals price now sum then just equals price then there you go and of course we need to print it in the end so when we're done we're just going to say print your portfolio or actually let's do it with a gain so we're going to say relative gains are going to be and we need to do an f string the relative gains are of course what is the portfolio right now divided by what it was worth times 100 so we're going to just say sum now or actually we need to put that in parentheses here some now minus some then so we're all only interested in the actual profit some now minus some then divided by some then and the result that we have here is going to be taken times 100 relative gains are going to be that number percent and the absolute gains are just going to be some minus something in us dollars there you go and the error handling is of course also important we're just going to say there was no trading on this date there you go now looking at the intense file we now have two more intents that we have to cover first of all the buy and second of all the plot chart and also if we want to you can also do the greetings but that's not necessary because we have responses here we're going to do the plot chart next so we're going to have this function down here which is going to be def plot chart and this is just going to be a very basic candlestick chart so we're going to say ticker equals input choose a ticker symbol whatever and we also need a starting date so starting date equals or actually let's say starting string because we need to convert it to a date uh starting string equals input choose a starting date this time that the format is going to be different because here we have uh not just pandas we have a different format here so we have the actual data reader format that we need to pass here or we need to first convert it into a daytime object that's what i'm saying and the format here is actually day day month month year year year year like that might be a bit confusing you can change that if you want to and then we're going to say plt style dot use you can choose a style that you want to use i'm going to use dark background because that's the neural nine style and then we're going to define the start date so start is going to be dt date time and we're going to use a function called strp time i don't know if that means strip time or i don't know whatever that is it's definitely converting a string to a date so we need to pass a date string which is going to be our starting string and then we need to pass a format and the format is going to just be uh percent d percent m and percent uppercase y that's important and the end is going to be simple the end is just dt datetime dot now so this is the range that we're going to plot we first need to get the data we're going to say data equals web data reader ticker uh from the yahoo finance api start end there you go so that's the data and we now need to do some uh colors first because i don't like the basic color schemes of mpl finance so we're going to do colors equals mpf dot make market callers i'm going to say okay if the stock is going up that has to be green i don't accept anything except for green when a stock is going up which is 0 0 ff00 um when it's going down i don't accept anything but red so down is going to be ff000 and the rest is going to be just inherited so wicked ah not wicked what the uh wick equals inherit and what was the other thing edge is also inherent i don't even know what that means to be honest but inherit basically means take it from the parent theme so i don't really care about it one thing that's important is volume has to be in so we want to see the volume and in of course has to be a string and those are the colors we now have to make a style out of them so mpf style and of course you can just skip that part if you're not interested in styling but mpf style is going to be mpf dot make mpf style and we're going to take the base mpf style so the base uh base mpf style is going to be the style called night clouds and i'm going to add the market colors are going to be the colors that we just defined there you go and finally we're going to say mpf dot plot we're going to plot the data type of the plot is going to be a candlestick chart and the style is going to be the mpf style and the volume is going to be true that is it and last but not least we're going to define the by function this is going to be very simple def buy is going to be print good buy or whatever you want to say and then cis exit 0. all right so that's all we need i'm 100 sure that it won't work first try but still we're going to uh start creating the assistant now we're going to say assistant equals generic assistant as i showed you in the beginning the intense json file is the intense json file and the mappings oh yeah we need to define the mappings first the mappings is going to be just a basic dictionary which maps intense to functions so if we have the intent what do we have here if we have plot chart it is going to be plot chart surprise as i said or i think i said you don't need to choose the same name but it makes things easier in the end if we have of course we don't use equals we use colon if we have the intent what is it called add portfolio we're going to have the function add portfolio we're not calling the functions we're passing them if we have remove portfolio we're going to have the function remove portfolio if we have a show portfolio we're going to have the function show portfolio if we have what do we have here plot chart portfolio gains portfolio worth let's do portfolio worth if we have that we're going to call portfolio work if we have portfolio gains we're going to have portfolio gains and if we have a plot chart we're going to have plot oh we actually have that already so did we have everything here do we have everything seems like it we don't need save portfolio we only need buy in the end as well buy is going to be buy then we're passing here as intent methods we're going to pass this mappings dictionary so that the assistant knows okay if i get those intents as a result then i have to call these functions so once we have that of course you can also choose a name i didn't mention that but if you want you can choose a name so you can call this um do i have to pass the keyword name though i think we don't have to pass the keyword name we can actually remove the intense method here because mappings is anyway the second parameter and then we can just say financial assistant model or call it whatever you want and now we're going to say assistant dot train model assistant dot save model and when this is done we can say while true message is going to be the user input and the answer from the assistant is going to be assistant.request message and that's actually it so i'm not sure how long it's going to train i'm going to run this and if it takes too long i'm going to just cut it out from the video or do a time lapse or anything like that but it shouldn't take too long it would be a surprise to me if we didn't make any mistakes at all but i mean it could happen after all it is my module and i know how to use it so let's see it should be done any moment there you go let's do a basic hello hello sir there you go so show my portfolio oh it actually works wow show me the price of a stock apple uh oh actually this was not a functionality that that i implemented right did i have something like that oh stock price actually i didn't implement that intent doesn't matter you can do it if you want to so now it recognized actually uh that i want to plot a stock price which is also fine so let's go ahead and say 17th of march 2020 there you go this is the candlestick chart we can of course also zoom into it there you go so then i can say okay add a stock to my portfolio which stock do you want to add let's say i want to add facebook how many shares eight okay then i want to also add some more apple stocks add a stock i want to add apple 10 shares oh we have a problem here what is the problem here how many shares do you want to add we got the problem unsupported operand types for plus equals int and string oh that is important yes okay that's important we need to go to the ad portfolio thing and we need to say integer of amount because amount what we input here is a string so if we if we assign it that's fine but if we actually or actually that's not fine because it's not going to be a string it's not going to be an integer it's going to be a string so we always need to take care of that if we remove something that should be the case as well so we need to make sure this is an integer um yeah there you go so that should work but all in all it actually worked quite good quite well you can also see that we now have this model here this assistant model that we can load if we want to so actually let's do that we don't have to train it again we can go down here and instead of training it i hope this works we're going to say assistant.load model if it doesn't work you're going to have to do it in a different way but i think it should work never tested this feature actually but it should work let's just wait for the module to load and oh maybe it's already loaded and it doesn't tell me because it's not training so let's try to say hello no it's not working it actually works wow okay that's a surprise uh it's an up and down here okay it works so let's do it again add something to my portfolio i'm going to add apple and i'm going to add 20 shares here so show my portfolio now we can now see i have eight shares of facebook now if i'm not mistaken since we added eight shares of facebook and it was a string if i now add something it should give me another exception but not because the program does not work but because the data is already a string so if i say add a stock and i want to add more facebook and i want to add let's say nine yeah it's going to crash okay but that's not because the script doesn't work it's because it didn't work now if i if i delete the portfolio do it again it's going to work so this is how you build a simple financial assistant in python so that's it for today's video hope you enjoyed it if you learned something if so let me know by hitting a like button leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you very much for watching see you next video and bye [Music] you
Info
Channel: NeuralNine
Views: 10,115
Rating: 4.9659576 out of 5
Keywords: financial, finance, stocks, stock, crypto, invest, ai assistant, ai, machine learning, python, artificial intelligence, finance ai, finance assistant
Id: Y47kjQvffPo
Channel Id: undefined
Length: 36min 54sec (2214 seconds)
Published: Sat May 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.