A Bitmex Python Tutorial

Mostafa Ayoub
Coinmonks
3 min readApr 22, 2020

--

BitMEX is a cryptocurrency trading platform that offers comprehensive API and supporting tools in addition to leverage trading, with varying leverage sizes depending on the product. Bitmex also provides a platform where it is possible to simulate trading called BitMEX Testnet without risking funds before doing any real investing.

In this tutorial, we will learn how to utilize Bitmex’s public API in order to fetch market data, fetch position data, set sell/buy orders, and finally update the leverage.

First of all, make sure that you already have signed in Bitmex and Bitmex Testnet. Then you can generate your API and Test API key for Bitmex.

Installation

Import the Library and Get Client Instance

In this tutorial we will proceed with Bitmex Testnet’s API to get the client instance.

Execute a Market Order

To execute an order we first have to fill some parameters. The first order would long 10 XBTUSD contracts at the current market price and the second would short 10 XBTUSD contracts at the current market price.

Update Leverage

Since Bitmex offers leverage trading, we will be able to update the leverage of our positions using:

Close All Positions

All open positions of a certain symbol can be closed using:

Check Order History

To fetch our latest order history, we first set reverse=true to get newest-first.

To get the specific data from the orders we can clean the order using:

Check Open Positions

Check open positions of a certain symbol then select specific data using:

Get Past Minute Data

Fetch the past minute’s data of a certain symbol and select specific data using:

You can also get the last couple of minutes if needed using:

Also, Read

Get Best Software Deals Directly In Your Inbox

--

--