How you trade in the DEX with only python

in #bitshares8 years ago

After months of work figuring out how the wire format of Graphene/BitShares works, I know feel confident enough to release the code for it as a major release of the python-graphenelibs.

With the most recent release 0.4, you can now:

  • Place buy, and sell orders
  • Borrow Smartcoins
  • Adjust your collateral ratio
  • Get all sorts of data from the exchange

and all of this works without the cli-wallet. All that is needed is a public API node such as the one of OpenLedger.info.

Let's take a look how things work from the user perspective:

1. Installation

We first install some requirements:

sudo apt-get install libffi-dev libssl-dev python-dev

then we install the python-graphenelibs either

  • from github via

    python3 setup.py install --user
    
  • or via pip

    pip install --upgrade "graphenelib>=0.4"
    

    (add --user if you are not root)

2. Configuration

Now let's create a configuration file config.py and put the following variables:

  • API node:

    witness_url = "wss://bitshares.openledger.info/ws"
    
  • Markets that we are interested in general (for returnTicker()).

    watch_markets = ["USD:BTS", "GOLD:BTS"]
    market_separator = ":"
    

    This limitation is required because there are way more markets that BitShares can deal with than you are interested in (for sure).

  • Trading account and active private key.

    account = "xeroc"
    wif = "5xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    

    The private key provided here will be used to sign out going transaction.

3. Implementation

Not that we have the configuration done, let's do some actions. First, let's read some data from the blockchain.
We create a new file called main.py and put the following content:

from grapheneexchange.exchange import GrapheneExchange
from pprint import pprint
import config

dex   = GrapheneExchange(Config, safe_mode=True)
pprint(dex.returnTradeHistory("USD:BTS"))
pprint(dex.returnTicker())
pprint(dex.return24Volume())
pprint(dex.returnOrderBook("USD:BTS"))
pprint(dex.returnBalances())
pprint(dex.returnOpenOrders("all"))

This will give us

  • the trade history for USD:BTS,
  • the ticker for all watched markets,
  • the 24h volume of the watched markets,
  • the order book for USD:BTS,
  • the balance of our account as well as
  • our open orders

We can now transact with the DEX buy constructing and singning new transactions that document our intentions.
Well, all of that stuff is done in the background of the library such that you only need to do this:

Buy/Sell

pprint(dex.buy("USD:BTS", 0.001, 10))
pprint(dex.sell("USD:BTS", 0.001, 10))

to buy/sell the quote asset in the USD:BTS market, which is USD. So we sell/buy 10 USD at 0.001 BTS/USD.

Of course, we can cancel orders:

pprint(dex.cancel("1.7.1111"))

Call Positions

pprint(dex.borrow(10, "USD", 3.0))
pprint(dex.adjust_debt(10, "USD", 3.0))
pprint(dex.close_debt_position("USD"))

Here, we borrow 10 USD from the market such that we have a collateral ratio of 3.0,
then we borrow another 10 USD and make sure the collateral ratio stays at 3.0.
And finally, we close the debt position and release the full collateral.

Summary

Hope this helps people use the DEX in their trading bots. Have fun coding!

Documentation

Sort:  

I'm interested in coding openledger bot. Where can I find all of the functions if the library, like dex.returnOrderBook, etc? Thanks in advance!

Buddy its on the bottom in Documentation, here:
https://python-graphenelib.readthedocs.io/en/latest/

Hope this helps you make some great tools and bots ^_*