Crypto Trading Bot in Python For Coinbase

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 a simple coinbase crypto trading bot in python so let us get right into it all right so we're going to build a crypto trading bot in python using the coinbase pro api and in order to do that we need to have a coinbase pro account now if you're using kraken or any other platform any other exchange any other crypto exchange let me know in the comment section down below if you want to see a video for your platform for example kraken or any other platform uh if the api is not too messy and if it's well documented at least to some degree i might do a video on that especially if this video performs well so let me know in the comments section today we're going to focus on coinbase pro and i'm not going to show you how to set up a basic coinbase pro account that's not too complicated and it's not really related to programming but if you have a coinbase pro account already i'm going to show you how you can set up the api how we can connect to it and how we can automate trading in python now however before we get into the actual coding i need to as always mention that this video is not financial advice it is not investing advice it is not crypto advice i'm not a financial professional i'm just a programming teacher on youtube a machine learning teacher a data science teacher i'm not a financial advisor professional or anything so don't listen to me when it comes to trading strategies or what to buy what to sell i'm not giving that advice i'm just showing you how you can use programming to automate your investments so having said that let's go into uh the coinbase pro interface here and we're going to click on our account and go to api um in this back end here in the api we can create new apis as you can see up here new api key in this case i already have a key the important thing is that this key that you see here is the public key this is not anything that is uh sensitive data so you can see that and it's not problematic but you're also going to be shown two keys that are only going to be visible once so you want to write them down or you want to store them in a file you want to encrypt them anything like that and those are the passphrase and the secret key the api secret key and those are going to be only showed once and you should never share them with anyone i'm not going to show them in this video but if you want to create a new api you just click up here you say uh you choose the portfolio in this case my default portfolio a name and you set the permissions this is very important you want to say okay i can only view stuff so i can only uh query stuff or you can actually do trades you can transfer money or cryptocurrencies and so on as you can see here we have a passphrase we can change that you should not show that to anyone and once you create that api key you're also going to be given a secret key and a public key the public key is not problematic here the secret key is so you don't want to share that with anyone this is how you set it up in coinbase once you have that you need to have these three things you need to have the public key the secret key and the passphrase and then we can go into python all right so let's get into the python coding and for this we're going to start by opening up the command line we're going to need a library called cb pro which stands for coinbase pro and this is the python library that we're going to use in order to connect to the api and use it so pip install cb pro like that i'm not going to do that because i already have it and once you have installed this you can say import cb pro import coinbase pro so now we can choose to use either a public client or an authenticated client the difference is a public client doesn't need authentication we don't need a secret key we don't need a public key we don't need a passphrase but of course we cannot do any account specific actions we cannot sell we cannot buy we cannot get some specific information for our account we can just look up some basic stuff like what currencies are out there what's the time in the market uh what are the stats for this particular currency and so on uh and we're going to start with that we're just going to say public client equals and then we're going to say cb pro dot public client and then we can print some basic information for example we can say result equals public client dot get products uh do we have a get products they go get products basically what can we buy and what can we sell and we can save for row in result print row and i can run this and in this case i'm only interested in the id so i'm going to say id there you go and you can see those are the things that we can buy for example omg with great great britain pounds or etc with euro or ethereum with bitcoin and so on those are the products that we can buy if we only want to have the currencies we can just say get currencies not get products then we're going to see them individually listed as you can see we have ethereum we have us dollars we don't have ethereum to us dollars we have them specifically here as currencies we can also get the time so get time and just print result and you can see that we got a timestamp here so those are just some basic informations that we can get from the coinbase pro api we can also get some information about a specific product for example public client get product order book for bitcoin and us dollar and then we can see the asks and the bits uh basically the different orders i didn't print that so print result there you go you can see bits asks and we can do all sorts of things for example get product ticker get product ticker it's also a function here and you can see the price you can see the size you can see uh the trade id and so on um and of course we can also see the 24 hour stats which are quite interesting so get 24 get product 24 hour stats and we can see okay open high low volume last we don't really have a close necessarily because that's the crypto market it's 24 7. um and what we can also do is we can get the trades as a generator now for those of you who don't know what a generator is i recommend you to watch my advanced python tutorial series but we can use a function for example if we're interested in the ethereum traits we can just say public client dot get product traits of ethereum to us dollar and we can then say print next ethereum trades and we can do this a bunch of time so we can see all the trades as you can see here from newest to oldest and uh this is just a generator and with every next we see the next oldest um or actually the next youngest or the next oldest trait um so this is one thing we can do as well and yeah that's just a public client if we're interested in basic information prices and stats and so on you can use that but if you want to take action you want to use the authenticated client all right so now let's get into the interesting part we're going to get into buying and selling because this is what we're here for we're not here for querying information we can do that in another video and we have done that in other videos so we're going to look at the actual actions buy and sell and for this we're going to have to create an authenticated client and for this we need credentials now in my case here i have stored these things in a file called passphrase it's not only the passphrase it's the public key the private key and the passphrase so what we're going to do here is in my case you can also do it clear text if you want to it's not really secure so i would not recommend it but you can do it i'm just going to say data equals open passphrase in reading mode dot read dot split lines and then i'm going to say uh now i just need to look up the order in which i have those things in there so i think it's actually the public first so we're going to say public equals data zero and then i think i have the passphrase second and the secret last like that and if we want to create an authenticated client all we need to do is we need to say some name so off client for example equals cb pro dot authenticated client and here we now need to pass these things now the order is not the same as i have it in my file so don't be confused here the order is first of all i want to have the public thing so we want to have public then we want to have the secret public and then we want to have the passphrase this is the order of things so we can see if we get um authenticated and i'm going to show you how you can see that so if i run this we're going to see if it works or not authenticated client now if i don't pass the secret but hello world we're probably going to get a different result here so uh or actually we don't get a different result we're going to get a different result later on so maybe we're not going to see it yet uh but let's do something very simple let's just say uh off client.getaccounts and if it works we should be authenticated there you go we have we have an authenticated account i think because if i change this to hello we should see that it says there you go invalid string doesn't work uh but let's say it's not invalid let's say we use secret here but we use a different passphrase for example hello now i run this it says invalid passphrase so since it doesn't say that if we do it correctly it means that we are authenticated and we can work with that okay so now that we know that uh what can we do we can actually go ahead and execute some buys and sells now i'm not going to do this i'm going to attempt it and it's going to say that i don't have enough funds because i'm going to try to sell something that i don't own and i'm going to try to buy something that i don't have enough money for uh just for demonstration purposes so we're not going to see any transactions made here uh but what we can do is we can just say offclient.buy this is the function we're going to use and here we can specify an order for example we can say okay i'm looking to buy for a price of 10 whatever 10 is in my case it's going to be euros because i'm in europe and i cannot pay with us dollars i have to pay with euros on coinbase uh for 10 euros i want to buy 0.1 um ethereum so i say price is 10.0 the size is going to be 0.1 and the order type is going to be a limit now you should know what a limit in a market order is a limit is basically i'm specified a specifying price and size and a market order is basically just buy for the current price if you have enough funds um and what am i going to buy i'm going to buy product id um ethereum euro so in this case i'm asking for 0.1 ethereum for 10 euros which is not realistic i'm not gonna get that um because that's too cheap i can also go more extreme and say i want 2.1 ethereum for 10 euros i'm not going to get that and if you want to see what happens when we execute that we can just print what it returns because it's going to be an error message that says we cannot buy this so if i run this we see insufficient funds so you can see that if the funds were not insufficient i would be able to buy them this is how you can buy stuff with the coinbase pro api same thing goes for uh a market order so i can just go ahead and say i'm going to print this as well off client.buy and here of course we don't specify a price because a market order basically says you say how much you want and we buy it for the market price in this case i'm going to say okay i want to have 10 ethereum uh order type is market and the product is the same so it's basically just ethereum for euro and yeah you can see you will see that this also says insufficient funds because we cannot do that now let's try to sell something let's say off client dot sell and we can sell for example let's say i want to have a price of uh like that price of 20 000 uh euros for a size of 10 [Music] bitcoin so i want to sell bitcoin i want to sell 10 bitcoins for 20 000 i think that's uh even too cheap so let's add some zeros here um and this is going to be a limit order so order type is going to be a limit and a product id is going to be btc euro so i'm going to sell bitcoins for euros here but i don't own any bitcoins i only own a theorem in this account so i'm not going to be able to do that again i'm pr i'm i'm specifically doing stuff that i cannot do because i don't want to make any transactions here but if i have the funds i would be able to do that in this case it says insufficient funds i don't have the bitcoins that i'm trying to sell and the same thing goes with the market um sell so basically just off client sell and then you say size i want to sell 10 bitcoins or the type is going to be market for the current market price product id bitcoin euro so if i would have 10 bitcoins i would be able to sell it for the market price it says no no no i mean i'm not printing that so it would say insufficient funds as well so this is how you do a basic buy and sell and we can also do an alternative way i'm going to show you how you can do that alternatively if you don't want to specify that the order type is this or that you can just say off client dot and now you can go ahead and say place limit order so you can just say in general this is a limit order uh and instead of specifying the order type because it's already specified here you can specify the size so are you buying or are you selling and we can circuit product id is bitcoin euro and i'm on the side that i want to buy so i'm on the buying side and i want to buy for a price of i don't know 10 euros i want to buy two bitcoins which i'm not going to be able to do but that is what i'm trying to do here and we're going to print the result as well here so this is just an alternative way we're actually doing the same thing but this time we're not specifying the order we're specifying the site we can do the same thing with cell here uh and we can do the same thing with off client dot place market order and again product id side and size this is not too complicated we can also cancel orders we can just say auth client dot cancel all basically canceling all orders for a specific product in this case bitcoin euro for example uh now i think it's going to give us an error that we don't have any orders or it's just going to not return anything because yeah it's just going to return an empty list because we didn't have any orders to cancel if we would have canceled any orders it would probably say okay cancel them those are the orders cancelled and we can also get if we want that we can also get um off client dot get orders and we can see as a generator again what orders we have with next next next and we can look into those now this is all just the theory and now let's look at a practical example this is going to be a trivial example why because i already have videos on algorithmic trading on machine learning price prediction on technical analysis so if you're interested in actual strategies look into those videos and you might find something that interests you in this video i'm just going to show you i just want to show you how you actually do the transactions with coinbase pro and what strategy you use is up to you so you can implement a strategy the point is uh if you have a strategy for example one of my videos algorithmic trading strategy or technical analysis in python you might have a decision that now you want to sell but how do you actually sell this is that video how do you actually sell and how do you actually buy if you don't want to do it manually so you can combine the content into one full trading bot here so here we're just going to do a very trivial example we're going to say import time and we're going to just do something pretty stupid it's just going to be something like a stop order or anything like that we're just going to say okay sell under a certain price or buy after a certain price and so on um it would be more reasonable to do something like moving averages when they cross your cell when they cross again you buy and so on that's this would probably make more sense but again this is just for demonstrational purposes we're going to say a certain sell price if a currency reaches the price of 30 000 for example we are going to sell it and we're going to sell i don't know 0.3 units of that and the buy price if the currency falls below 25 000 we're going to buy 0.2 units just something that i made up right now and then we could run our bot by saying while true price equals and we use the float function on off client dot get product ticker by the way the auth client can do everything that the public client can do and more so it's not like you have to use the public client for getting data you can also use the auth client uh so product id equals btc [Music] btc euro and we're interested in the price so that is the price that we currently have and now we can say okay if that price is less than the buy price then we're going to say print buying btc and we can say off client dot buy and we can say the size is the buy size or the buy amount the order type is market and the product id is btc euro there you go and we can do the same thing for the selling so we can say okay elif if the price goes above the sell price so the idea here in this case in this imaginary strategy is that when we fall below the buy price we buy when we go above the sell price we sell this is just some something that i made up right now we can say selling btc and we say off client cell size is going to be sell amount order type is going to be market and the product id is what we are selling btc euro and otherwise we're just doing nothing so print actually we need an else print nothing and time sleep 10. so we don't want to do it all the time we don't want to do it all uh every 10 seconds maybe we want to do it every two minutes maybe you want to do it five times a day that's up to you but this is how you can do it you basically just leave the bot running and uh it's going to tell you nothing nothing nothing if you want you can also print the price so i have the messages here i didn't want to type all of them so i'm just going to copy them here from the prepared content basically here i have buying btc euro because price off blah blah blah fell below buying price limit off by price this is just a separator for thousands and we can do the same thing with the sell price so i'm going to copy that and i'm going to paste that here and we can also have this not doing anything because price is whatever it is just some more sophisticated messages here so if i run this now you can see okay not doing anything price is 28 694.47 us dollars in this case uh not us dollars euros in this case um now the price changed but it's still not anything significant we can also try to manipulate this into uh let's say 28 700 i'm not sure if we're going to reach that and if it falls below 28 uh 690 we're going to buy in this imaginary scenario as you can see here rose above selling price of that because that is the price right now so it's selling of course we're not selling because we don't have it maybe you would want to have uh some error message for couldn't sell or anything but you can keep this running all the time you can maybe change this to just for demonstration purposes here we're going to change this to two seconds you can see selling and selling and selling maybe it's going to fall below then it's going to nothing now it rose uh quite a lot so this is just something you can do in reality you would use a more sophisticated strategy maybe you would use the machine learning neural network from the video predicting crypto prices maybe you would use moving averages that are crossing and then you would say okay now sell now buy in this case we just did something that doesn't make any sense um but this is how you build a trading bot a crypto trading bot in python you choose a strategy and you implement all the strategy as in all the videos that i have on this channel but the final thing that you need to do is you need to take action so you need to then say buy or sell when a certain trigger happens so that's it for today's video hope you enjoyed i hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below let me know if you want to see a video like this for kraken or any other exchange and besides that don't forget to subscribe and hit the notification bell to not miss a single future video for free other than that thank you much for watching see you next video and bye [Music] you
Info
Channel: NeuralNine
Views: 21,395
Rating: 4.9509954 out of 5
Keywords: trading bot, coinbase, crypto trading bot, crypto bot, coinbase bot, python coinbase, coinbase pro, coinbase python bot, python trading bot, python crypto bot
Id: BN7ZAWRLJP0
Channel Id: undefined
Length: 25min 8sec (1508 seconds)
Published: Wed Jul 07 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.