How to Build a Crypto Trading Bot with Python on Binance Testnet

46.83k views2266 WordsCopy TextShare
Algovibes
Ready to start trading cryptocurrency risk-free with Python? In this video, I’ll show you how to set...
Video Transcript:
want to build your own crypto trading bot or maybe just test your strategies in real time without losing money in this video I'm going to show you something powerful how to set up binance test net with python where you can simulate real trades risk-free and start building your own trading Bots no complicated setup no Advanced coding just a simple stepbystep process to get you started and here's the best part you can start trading in minutes all right let's get started you go on this site I will link it in the video description then you need to be logged into your GitHub account if you don't have a GitHub account just set one up it literally takes less than one minute if you are not logged in or don't have one you will have something like login to get up here after you have logged into to GitHub this will pop up and you just click on the first one generate a hmac key give it a name so let's just call it test generate it and then you will see your API key and your API secret which as the warning is indicating will never be shown again after that so store it somewhere I directly store it in our notebook here so I'm just giving it a name stored as a string API key and same story for the API secret key here so API undor secret is that one so we got our API key and our API secret so let's also import some necessary libraries we only need from binance client client and also time this one important information here you need the pyth binance library in order for this to work so be sure you have installed that beforehand time Library we just need to utilize some handy functions like uh pausing a endless y Loop for instance but you will see in the course of the video where I will utilize it so we have everything we need to instantiate client which is the variable which we are using to access all kinds of utilities from the python binance library in order for that to work we need to instantiate client provide our API key and our API secet and then very important you provide test net equals true to indicate that you're working on the test net and not in your real binance account here so with that we have in entiated the client and are able to operate in the test net so first things first what everyone wants to know how much back do I have so how much is my balance so just use client. getet account and then you're getting an overview of what they are giving you in terms of fake assets here so for instance you're getting one eth one Bitcoin seven Litecoin isn't life good and you even get 10K usdt so be invited to play around with that check out what you're getting here uh but in nutshell you're just getting a bunch of fake Bitcoin money both in terms of coin money and in terms of stable coin money here now let's set up a simple trading bot and let's just use the most straightforward logic so we just want to buy so place a market by order when our current price let's just take Bitcoin is below a certain threshold and we want to sell if the price is above a certain threshold all right so this is the logic of the trading bot so first of all let's define some variables first the symbol we want to trade which is Bitcoin in relation to usdt then we need a Buy price threshold which is just let's just take 60k and then same for the sell price threshold let's just take 68k here and then we want to all also Define a trade quantity that is how many Bitcoin we want to buy and I just want to buy 0. 001 Bitcoin so these are my variables I'm predefining so again just the symbol um interested to trade then a buying and a selling threshold and then the amount I want to buy and sell next up utility functions for the trading board first most obvious and most important function is a function which is pulling the current bitcoin price in order for this trading Bo to work so let's define a function get current price for the given symbol in our case Bitcoin but you can play around with all other symbols you're interested in then we just Define ticker use client get symbol ticker and provide our symbol then this function should should just return the price of that ticker and I'm also doing a float conversion here as it's coming as a string type from the API so I'm just providing ticker price here and this function super simple is just pulling the most recent bitcoin price so if I'm calling that you see I'm getting 603k roughly so if I'm executing that again in some seconds this price will change as this is always the most recent price here so just let's just test it now it's 631 18863 if I'm calling that again as you see this is changing this is an important function but the next important one is still outstanding which is a function organizing my orders and we are starting with our buying orders so I'm just calling that place buy order also takes a symbol and a quantity is it needs to know how many of those coins we want to buy and then just Define the order here use client order and then as I said we using a market buy order there are many other order types such as EG limit orders we are keeping it simple we're just placing a market by order provide the symbol AS symbol and the quantity as the provided quantity and then also we want to have a feedback something like buy order done and then just print out uh the order and the execution how it went through and some information for what price and so on so we can actually test that I mean it's fake money anyways so let's just place it buy order that you're getting an understanding of how this is how this working so if we just buy Bitcoin and actually uh buy 0.
001 so place byy order can also just provide trade quantity as we have defined that above here so if we place that we are getting bu order done then symbol BTC usct and then you see statues fill you get the price where this order was executed for then the quantity uh and also you're getting the the usdt equivalent so you bought for 63 USD and if you pull your balance now you should have 0. 001 more Bitcoin in your balance so let's go above here so you see we had uh one bit Bitcoin here and if you pull your account now you will see that you have 0 point Sorry 1. 001 Bitcoin here and a bit less usct namely exactly the quantity I just pulled over so just that you see how this is uh connecting here next up selling order just copy paste all that kind of stuff so place the sell order same arents here just some slight renamings so client order market sell and then sell order done obviously so that's already it for defining a selling order and now let's just sell whatever we just bought so Bitcoin and then again 0.
Copyright © 2025. Made with ♥ in London by YTScribe.com