How to build a Discord Bot to set and track Multiple Crypto Price Targets (part 2) — with Python

what's up everyone my name is andrew phong welcome 
back to the part two of the discord boss series   where we track crypto prices for 24 7 and also 
set price alerts with python so just a quick   recap of the last video we have created a crypto 
bot inside replit and put it inside our discord   server which is called crypto alert server and we 
have also created some client events for example   when the bot is online it will return us with a 
notification saying that you have logged in as a   client and also if the message sent to the general 
channel is starting with yah then it will return   this with yeet as a response that we have just 
configured inside the client this client event   and we also defined two major functions that 
we'll use within our program and our bot which   is getting the crypto prices by the crypto 
name and also checking if the cryptocurrencies   is supported inside this bot so we'll be 
using these two functions get crypto prizes   and is crypto supported into doing mainly three 
things namely to send crypto prizes directly   to list all the available coins supported by this 
bot and also to check whether a coin is being   supported and last but not least setting multiple 
price alerts for the cryptocurrencies that you're   currently monitoring so without further ado 
let's get started okay so the first task we have   is to send crypto prices directly meaning that 
whenever we type for example dogecoin in the chat   it will directly and automatically fetch the data 
of the current price of dogecoin and send it back   to us as a message remember that in the last 
video we have stored everything in the crypto   prizes names and the corresponding values inside a 
database the first thing you should do is to check   the message.content and we have to convert it into 
lowercase because in our database we all use the   crypto eye design all in lowercase and 
then if it's in the database dot keys then we can await message dot channel dot send so we can send a f string the current price of the user 
input which is message.content is colon so it's get we call the function 
we define which is get crypto prices and then message dot content dot lower to make 
sure they match and that will be in us dollar so that will be the first task 
and the second task is to list   all the available coins supported by 
this bot so so if the message dot content starts with the command which is dollar sign list then we will give the users a list so we 
first create a list which is crypto supported   list so we can do some list comprehension so let's 
put a bracket key for key in database dot keys so   we basically do a for loop for the database dot 
keys and append it into crypto supported list and then we can send the 
message back to the channel dot send and that will be crypto supported list   so that will be the second task and for the third 
task we have to check whether a coin is supported   so that will be very easy because we have the 
function is crypto supported so we can type   if message dot content dot starts with and that 
will be a dollar sign support and with a space   so um we need a space because uh 
whenever the users type support   and then bitcoin there will there will be a space 
between the support and the cryptocurrency name   so what we have to do is to 
split the string into two parts   and only get the second part which is 
the cryptocurrencies so let's do it now we first crit define a variable 
which is crypto to be checked equals message dot content dot split so 
what do we split by we split by the support comma one and then we get the this is the first 
item and this is the second item so it's one   and we also don't forget to convert it to 
lowercase because database uses lowercase   cryptocurrencies and then we await the message 
dot channel dot send as usual and we use   this as crypto supported function that we 
defined in the first video and we put in the   input which is crypto to retract and that would 
return us with a boolean response true or false   okay so that take cares of the first three tasks okay so let's assume that we have the following 
three price targets which is 350 355 and under   360 for a cryptocurrency so let's assume 
the following sequence so if it's 349   and then it increases to 351 353 357 
361 and then it dropped back down to 349   and then it's soared to 360 76 okay so some of you 
guys may think that is rather a simple pro a vals   problem so if the price is larger or smaller than 
a price target then send the notifications or send   the price alerts but that may be a a messy thought 
because if we have all the if else statements   for all the price targets then let's see what 
will happen so let's see the notification so   if the first if the first one 349 it doesn't hit 
any of the it doesn't come even close to the price   alert so it won't be sending any notification and 
for this one um let's talk about this one later so   351 it passes the first price target so it will 
send the price likely for 350.

So 353 is larger   than 350 then it sends 350 again and 357 is sent 
both 350 and also 355. about 361 it sends both 350   55 and 360. this is for increase and for decrease 
if it drops back down to 349 it will send all the   alerts as well so if it goes back up it will also 
send all the prize targets so you can see there   are a lot of notifications that we actually don't 
want what we actually want is the first time that   it passes a certain price so if this will be what 
we want if it is from 349 and it passes to 351   we will send the price of that for this one 
350 and for the second time if it's if it still   passes 350 and not drop back down to 349 then we 
won't be sending this and we won't be sending this   and this and for 357 we will send this one 
because this is the first time it passes 355   and for this one we won't be sending 
and for 360 we will send this   and for 349 will send all of this because it is 
used to be passed the all the price i get and it's   now fallen below all the price targets so we'll 
send all and this two so let's see how we can   design an algorithm to tackle this problem we will 
first initiate a variable called the hit price   target so we first if it's for the increase 
we take the minimum of the price target so 349   350 apologies so 349 doesn't come 
close to this range so it's zero   and if it passes to 350 51 it will then store 
the closest uh price target the maximum price   target which is 350.

And if it's uh 353 it's still 
350 it because it doesn't pass the second one yet   and for 357 we will store 355 because 
it's the maximum price target that it hit   and 361 will be 360 and this will 
be zero and for 376 it will be 360.   and the second thing we should store is something 
called the price trend so it's called price trend it contains a stock price and also an end price   so the first one is only the stock price so it 
doesn't have the previous price so we omit this   and for the second one we will take in the hit 
price target bef for the price before 0 and 351 then we do the same we drag this 
number 350 to 353 and then 350 to 257 35 and 360 one and then 360 to 349 and then it's zero to 376.

And then based on this price trend list we're able 
to tell the price is increasing or decreasing so   for the increasing price we know that 
this the end price is larger than the   stock price if it's the same so if it's 
351 351 or for example if here 376 and 376   then this list will be the same and we 
don't and we don't do anything for that okay so this will be increasing increasing 
increasing increasing decreasing and increasing   okay we then set up another   list which is called notification so for the first 
one it doesn't have any notification that we need   because it's out of range of price targets so it 
is an empty list we then check this range this   price trend range and seek for the price target 
that are within this range so that will be 350 and exclusive of 350 we will then find out 
the price targets so this will be the same   as before so it is an empty list and for 350 to 
357 exclusive of 350 that will be hitting 355.

And for 355 to 361 that exclusive 
of 355 that will hit 360.   so for this one the price trend is decreasing 
in range so we flip the boundaries and   exclude 349. so you cross out 349 because for 
for the previous ones we cross out 0 150 350   255 that's why we don't have 355 350 and 350 here 
so if it's changing the sign the price trend is   decreasing then we change the to omits the m price 
instead okay so that will trigger 360 355 and 350 and it's if it's increasing then we will cross out   zero and that will hit all our price 
packets which is 350 355 and 360.   so that's essentially how the algorithm works 
so you may ask what's the difference between   this two notifications list so basically it they 
both hit all the price targets however this is   falling below all the three price targets 
and this is passing all three price targets   so how do we distinguish between the increased 
notification and decrease notification   so that's basically based on the list order so if 
it's ascending order then that will be increase and increase increase and this will be decreased because 
we start from 360 to 350 to check   to check the price target one by one and 
that's why we have a list that is descending   so this will be and checking by checking 
this list that is descending we can send a   decrease notification instead of an increase so 
this one the list is in ascending order because   it's checked it checked from uh 350 uh it actually 
checked from 1 to 376 and check the price target   if it's 350 350 355 360 they are within this 
list and this is ascending so this is increase so that's pretty much for this algorithm and 
hopefully it makes sense to all of you so i've   written the price alert scheduling algorithm 
inside programmers as you can tell we have the   price target and also the sequence that we have 
just mentioned inside the whiteboard tutorial   and there are mainly three functions that we'll 
be using which are the check price trend reverse   alert and also the normal alert so basically the 
code will be running inside a check price trend   so if the stock price is smaller than end price 
and then the price crypto price is increasing   and they will return us with the normal alert and 
the normal alert will do a check inside the price   target in ascending order so we'll be checking if 
350 355 and also 360 are in the within the list   of start price and end price if the sub price and 
end price are the same we do nothing and return a   empty list if else the order is in reverse 
direction then we will be using the reverse   alert function to to tackle the notifications 
so inside the reverse alert notifications   we will then uh invert the list of price targets 
so we'll be checking the price circuit in   descending order this step is essential because we 
have to tell the difference between the increase   uh notification and also the decreased 
notification so take this example uh we just   mentioned that like with the price dropping to 
349 it will send all the notifications for 360 36   55 and 350 and when they pass 376 it will 
also trigger all the price alerts but in   increasing manner so what we have to do is 
to tell the difference between these two   um the notification inside these two and that 
will be the fourth function which is checking   if the two lists are in the same order so if 
they're in the same order then we will ignore this   and if the if the boolean is true then 
we'll ignore this if the order is false   so in this case this will be false then we will 
actually return the list and send a different   notification so i won't go through the all the 
knitty gritty details inside the main loop but   i would just print out the output to let you see 
so the sequence 449 it won't set in a notification   when it first passes 350 it will send an increase 
in notification and the second time it passes   uh 350 it won't send any notifications and 
when the first time it passes 355 which   is our second price circuit it will send a 
new notification and also the same for 360   and when it drops back down to 349 it will send 
a decrease notification instead of an increased   notification for the price alert and when it 
go back up to 376 it will also trigger all the   increased notifications so that's pretty much it 
how it works and we will then translate this piece   of code into the cryptobot in the next video 
and in the next video i will also be sharing   with you how to we actually use uptown robot to 
keep our server the discord server being paid by   every five minutes to make sure that our bot 
is running 24 7 and we are always fetching   the crypto price data so that's pretty much it 
for this tutorial i hope that you guys learned   something about the discord commands the messages 
and most importantly the uh i would say the   scheduling price alerts algorithm which will only 
send price on location the first time it passes   or fallen below the price targets so all the 
source code will be in the description and all the   links and relevant documentations will also 
be in the description so that's it from me   uh stay tuned for part three of the discord port 
series and see you guys in the next video bye guys

You May Also Like