Crypto News

Coding a crypto algorithmic paper trading tracker for Binance

This one is quite a simple script but I have found it very useful in tracking and measuring paper trading performance for my cryptocurrency trades on Binance. This paper trading tracker is meant to make it easier for you to visualise your PNL for each coin bought by your crypto trading algorithm.

As you might know, Binance offers a spot Testnet in order for you to test out your crypto trading bot and ensure that it places trades as it should. However, the spot testnet is very limited and offers no structure in the way of measuring your bot’s performance. Binance is leaving that to the user. 

For the past few weeks I’ve been testing a reddit crypto trading bot that buys based on posts sentiment. I started off by running it with live funds in the first 2 weeks and eventually moved it over to paper trading. 

I needed a way to easily keep track of my trades, as even in live mode Binance doesn’t offer the type of flexibility, or view that I’m looking for. So I wrote a script that will keep track of my trades and plot the historical performance of each trade placed by the crypto trading bot, like so:

The script will also spit out your  daily PNL for each coin, as well as an overall PNL. I feel this gives me a better overview over my trading bot performance compared to what the Binance view offers. Luckily it’s quite a simple python script, so let’s jump into it.

The one prerequisite that I have to point out is that this tool expects a json file format in order to work, so your trade data must be stored (or converted) to json.

The first thing you want to do is to install your library requirements. We’re only  using 3 additional libraries so let’s just install them via pip. Open your cmd or Terminal and type in the following commands. Note that you’ll need to write pip3 if you’re doing this on a mac.

These libraries will enable us to use the python-binance wrapper, a plotting library and a helper library to make our plots and charts look sexier. Ok with that done, go ahead and fire up you python compiler and let’s write some code. We’re going to start by importing the libraries that we just installed, along with a couple of other libs that are pre-installed in Python.

Next we’ll define the Binance client in order to be able to pull the daily close prices since the day a trade has been placed. Since we’re using Binance’s open endpoints we don’t need to provide an API key or Secret.

Now let’s define some styles for our plot

With the preparation stuff out of the way, it’s time to load out trading data. Remember how I said we need our trading data to be stored in a specific json format? Here’s an example of the schema that the script expects:

A simple function taking one argument (data) will enable us to access this file in Python:

A second function taking two arguments is going to deal with fetching historical data for each coin in our order.json file

The next function iterates through the json file and uses the previous function to call the Binance API and return the relevant historical data for each coin in order.json. As the date and time of our trades are stored as unix timestamps, we need to convert these into a format the the Python-Binance wrapper can recorgnize using datetime.fromtimestamp and strftime.

We would also need to store the buy price of each trade, so we’re going to write a simple function that iterates through the order.json file and stores the buy price of each coin in a dict.

The next two function are used to calculate the PNL for each coin.  The first function returns the daily PNL for each coin in a list, while the second function returns the total PNL for all coins, as well as the latest PNL for each coin.

And that’s everything we need to get some simple performance tracking for our paper trading crypto bot strategies. All we need to do now is to call the functions in a main() function and execute the script. You can use a scatter or plot graph to track performance, but if you’re interested in plotting, do some reading on matplotlib – it’s a pretty great library.

All that’s left now is to execute the main function

That’s it. If all went well, you should be able to see some graphs, similar to the ones in the beginning of the article if you save the json example from above into an order.json file and drop it in the same directory as the script.

 

Did you enjoy this article? Please consider subscribing to the newsletter for more awesome content.

If you appreciate my content and would like to see more, you can support me by donating  here  or via the Brave Browser. This is much appreciated as it helps me to keep the content free and open source. Thank you!

The post Coding a crypto algorithmic paper trading tracker for Binance appeared first on Automated Cryptocurrency Trading.

Read more: https://www.cryptomaton.org/2021/07/24/coding-a-crypto-algorithmic-paper-trading-tracker-for-binance/

Text source: Automated Cryptocurrency Trading

Disclaimer: Financial information and news are not financial advice, read the disclaimer.
Buy & sell Crypto in minutes

Join BINANCE!

The world's largest crypto exchange

You're just steps away from receiving your reward.

The most complete Crypto News Center.

Search Stories:

Latest top stories