How to Buy and Sell Bitcoin with Python
Learn how you can buy and sell Bitcoins automatically using only your Python skills.
Does the crypto world interest you? Are you also a programming enthusiast, or a programmer with some experience? Here is a good place to come, to try out something interesting. In this article, we are going to discover how it is possible to buy and sell automatically Bitcoins using only your Python skills. After reading this article, with some attention, you will be able to automate your future purchases, if you want. Sounds good? Let’s dive in.
Attention: this article has only an informative and entertainment scope. Everything that is written here is not financial advice. Make your own research and configuration before starting investing in crypto and using code to move your crypto. Everything you do with your own money is your responsibility.
Setup and dependencies
In this article, I will use Python 3.10, but all the versions that are newer than 3.6 are enough. To run, we will use a specific module and a specific exchange. The module is CCXT, which will let us makes an order on the exchange, which will be Binance
If you don’t have Binance, download it from here:
You can choose whatever exchange you want. After, when we will set the environment, I will tell you to search for your exchange in the given document.
To start, you need to install CCXT. It is simple. You only need to go into a terminal and type:
>> pip install cctxThen, a bunch of lines should appear, and at the end, if there are no errors, everything should be set up.
Let’s code it out
The first thing we must do is to import in our script the module we just installed, and also set up the exchange that we want to put. As said before, we will use Binance, but if you prefer using Coinbase or something else, here is the code to the CCTX documentation, so you can find how to change it. It is pretty simple:
So, here is the code:
import cctxexchange = cctx.binance({ 'apiKey':'short string', 'secret':'secret string', 'enableRateLimit':True
})exchange.load_markets()
In the first line, we import the module. Then, when setting up the exchange variable, we store our Binance API key and secret key. Obviously, these data are personal, so you had better not share them with others.
Get Matteo Possamai’s stories in your inbox
Join Medium for free to get updates from this writer.
To get this information you need to search for Binance API in the app, and then get the required data. At this point, you can make orders and sell from your account, with some lines of code. Sounds great, doesn't it?
Then, we will add some functionalities and utilities that could be useful along the way.
def quantity_in_dollars(crypto, dollars):
#crypto example: 'BTC'
#dollars example: 100 return dollars / exchange.fetchTicket(crypto+'/USDT')['last']
This function returns how much value a coin is a given amount of dollars. For example, I want to buy 100 dollars of Bitcoin, I make the conversion of the last price, and now I know how many coins I can afford and I can buy.
def buy(crypto, quantity):
exchange.createMarketBuyOrder(crypto+'/USDT', quantity)
def sell(crypto, quantity):
exchange.createMarketSellOrder(crypto+'/USDT', quantity)
Now, with these two new functions, you can buy and sell the coin in your account, using the dollar that is in it. Be careful using them. If they give errors, in the majority of the case is because your account wallet has not had enough coins (both fiat and crypto) to perform the buy or the sell order.
So, now you have the functions that allow you to buy and sell as much Bitcoin as you want, also basing your count with dollars or the fiat you prefer. What’s next? Now you can make purchases when you want, and sell at the best moment.
If you are interested, you can also create a bot using these particular foundations. You need to decide on a simple trading strategy and implement it, trying to create an unlimited money creator.
Obviously, it won’t be really easy if you don’t have a lot of experience, but it will be really fun. As said at the beginning of the article, pay attention to your money, and make sure to not waste them. Remember that everything you do is your responsibility, this article has only a demonstrative and entertaining purpose.
So, I hope you find this article entertaining, and I hope you will make the correct choice. If you enjoyed it, please consider clapping and subscribing.
More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter and LinkedIn. Join our community Discord.

