Binance Live/Historical Trading Profit and Loss w/ Python Binance API

Cem Pekturk
5 min readOct 31, 2020

Hi there, in the last couple of months, I trade some altcoins in Binance for fun (a very small amount of money). After I buy some and sell them then buy another one and sell them, so I did many transactions. I wanted to see my profit and loss both in historically and lively of these transactions, but Binance doesn’t provide this information. Note: just a couple of days ago, it started to provide total profit for each day, but still there is no information for each transaction or altcoin. Hence, I decided to find out the way to see my profit or loss. I created some Excel sheets to track it, but it was exhausting to write every transaction you make. Then, I found some portfolio tracker applications that shows your profit and loss. I didn’t look at many of them, but one of them was very similar to my excel system that I needed to write down every transaction that I made. It was fancier though like charts etc. Indeed, there was a subscription option. I probably could connect with my Binance account and could automate the process if I paid for it. However, I didn’t want to do it because I was trading a very small amount of money.

Anyway, if you trade crypto money at Binance, you probably suffer from this situation. Finally, I decided to use Binance API to automate the process and solved all of my problems.

I want to share the Python code and how to retrieve historical and live profit and loss for each transaction.

I created 4 different functions in the binance_class that needs 3 arguments, your API key and secret from your Binance account and all_tickers (this is all coin pairs at Binance, over 1100);

1) prepare_data: I created this function to see historical cryptocurrency prices like ‘BTCUSDT’. You can decide beginning date, end date, and interval that you want to retrieve.

prepare_data Fuction

2) balance: This function returns all the coins that are available at Binance with its current balances including locked and free. There are two outputs in this function. First is a list of the coins that you have and second is dataframe that you see below. The table below shows all coins, its quantity, current price, and total worth in BTC and USD.

3) order_history: This function is my main goal. It returns all of your trade history that you make with bitcoin. The dataframe includes the price that you paid in the transaction, quantity of transaction, total quantity, average cost, your profit/loss in percentage, BTC, and USD.

If you don’t input anything, the function will return all coins that you sell or buy with BTC. However, you can input the coins that you exchange with other currencies or coins such as ‘YFIIUSDT’ or ‘QSPUSDT’. It doesn’t return the transactions that make with other currencies or coins because there is a request limit in Binance API, so we cannot return all of them at the same time.

order_history Function

When you trade, you buy more to reduce the cost of the coin if the coin price decrease. Lets say, you buy 10 XYZ coin with 3.00$ and XYZ decreased to 2.50$. If you buy 10 more with 2.50$, your cost is going to be 2.75$. Thus, I created average_cost variable to calculate this for each coin.

I want to explain the USD profit loss variables. Calculating profit or loss in USD is tricky because we buy and sell with BTC (at least I do like this), but BTC/USD is changing constantly. So, I calculated USD profit loss in two different ways. profit_loss_USD = profit_loss_BTC * current bitcoin price (sell day). profit_loss_USD2 = USD earned (sell day) — USD_spend(buy day). In profit_loss_USD, we don’t care how much USD spend on the day we bought. We just care, how many BTC earned and multiply by sell day BTC price. However, in profit_loss_USD2, I calculated how much USD I spend for the coin on the day I bought and then deduct the USD that I earned on the day I sold the coin.

4) live_profit_loss: This function returns your current assets’ profits and losses. You can input the list of assets that is retrieved by the balance function or you can just input the list of coins that you want to learn its current profit and losses.

Two inputs are required for this function. First input is the output of order_history that provides the cost of the coin. Second input is a list of the assets that you have. You can input the list of the assets that you want to see, or you can input the list that you get from the balance function.

live_profit_loss Function

The table above shows the output of the live_profit_loss function. It shows the last date that you bought the coin and the current date. It also shows the order cost, current price, all quantity you have, current profit and loss in percentage, in USD, and current worth (total_USD) of all assets.

As you can see that I bought 42 SUB crypto in 2018 (the time bitcoin increased significantly) and then I have never looked. Now, it lost 97% of the worth. So, don’t do like that :). This market can be highly profitable sometimes, but it is also highly risky. You can lose 97% of your money, be careful!!!

Using the Functions

You can reach all code from the GitHub link. Thanks for reading :)

--

--